From 30760767c8ab4c10f8f3f7a0072bd0f55324383b Mon Sep 17 00:00:00 2001 From: David Lenfesty Date: Tue, 22 Oct 2019 12:58:18 -0600 Subject: [PATCH] Done? --- lab_3/main.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++---- lab_3/main.h | 10 ++++++++ 2 files changed, 71 insertions(+), 4 deletions(-) diff --git a/lab_3/main.c b/lab_3/main.c index af7c537..80657db 100755 --- a/lab_3/main.c +++ b/lab_3/main.c @@ -1,7 +1,22 @@ -#include - #include "main.h" -#include "lcdlibrary/lcd.h" + + +const char* lm35_string = "LM35 Temp: "; +const struct { + x = sizeof(lm35_string); + y = 0; +} lm35_value_pos; + +#ifdef CALIBRATION +const char* diode_string = "Diode Value: "; +#else +const char* diode_string = "Diode Temp: "; +#endif + +const struct { + x = sizeof(diode_string); + y = 1; +} diode_value_pos; /** @brief Initialises general pins for use. * @@ -64,12 +79,54 @@ uint16_t adc_run_conversion(uint8_t adc_selection) { } +float lm35_convert(uint16_t adc_reading) { + return (float) (adc_reading * ADC_VREF / ADC_RESOLUTION) / LM35_SENSITIVITY; +} + int main() { + // Initialise peripherals pin_init(); adc_init(); + // Initialise display + // NOTE: LCD uses PB0-PB6 + lcd_init(LCD_DISP_ON); + + // Write "base" strings to LCD + lcd_puts(lm35_string); + lcd_gotoxy(0, diode_value_pos.y); // Switch to bottom + lcd_puts(diode_string); + + + // Note that special compiler/linker flags have to be added to enable + // printf-ing floats + // See: https://startingelectronics.org/articles/atmel-AVR-8-bit/print-float-atmel-studio-7/ while (1) { - + // Read LM35 value, and write to LCD + float lm35_temp = lm35_convert(adc_run_conversion(1)); + // Convert measured value to string + char[6] lm35_temp_str; + sprintf(lm35_temp_str, "%3.1f", lm35_temp); + + // Display temp on LCD + lcd_gotoxy(lm35_value_pos.x, lm35_value_pos.y); + lcd_puts((const char*) lm35_temp_str); + + + // Read diode value, and write to LCD + #ifdef CALIBRATION + uint16_t diode_value = adc_run_conversion(0); + char[6] diode_str; + sprintf(diode_str, "%d", diode_value); + #else + float diode_temp = diode_convert(adc_run_conversion(0)); + char[6] diode_str; + sprintf(diode_str, "%3.1f", diode_temp); + #endif + + // Display diode info on LCD + lcd_gotoxy(diode_value_pos.x, diode_value_pos.y); + lcd_puts((const char*) diode_str); } } diff --git a/lab_3/main.h b/lab_3/main.h index 5655cab..17ff68e 100644 --- a/lab_3/main.h +++ b/lab_3/main.h @@ -1,5 +1,15 @@ #ifndef MAIN_H_ #define MAIN_H_ +#include +#include "lcdlibrary/lcd.h" + +// Comment out to run in "normal" mode +#define CALIBRATION + +#define ADC_VREF 3.0 +#define ADC_RESOLUTION 1023 // (2^ 10 - 1, 10 bits resolution) + +#define LM35_SENSITIVITY 0.01 // V / degree C, see LM35 datasheet #endif \ No newline at end of file