This commit is contained in:
David Lenfesty 2019-10-22 12:58:18 -06:00
parent fe908f3317
commit 30760767c8
2 changed files with 71 additions and 4 deletions

View File

@ -1,7 +1,22 @@
#include <avr/io.h>
#include "main.h" #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. /** @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() { int main() {
// Initialise peripherals
pin_init(); pin_init();
adc_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) { 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);
} }
} }

View File

@ -1,5 +1,15 @@
#ifndef MAIN_H_ #ifndef MAIN_H_
#define MAIN_H_ #define MAIN_H_
#include <avr/io.h>
#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 #endif