120 lines
2.8 KiB
C
Executable File
120 lines
2.8 KiB
C
Executable File
#include "main.h"
|
|
|
|
|
|
// arrays to hold strings for LCD
|
|
static char lm35_string[17];
|
|
static char diode_string[17];
|
|
|
|
/** @brief Initialises general pins for use.
|
|
*
|
|
* @note Does not initialise LCD pins. That is handled by lcdlibrary
|
|
*/
|
|
void pin_init() {
|
|
/* Pin Mappings:
|
|
* PC0 -> ADC0 input for diode measurement
|
|
* PC1 -> ADC1 input for LM35
|
|
*/
|
|
|
|
// Pin Config for Diode ADC
|
|
DDRC &= ~(1 << DDC0);
|
|
DIDR0 |= (1 << ADC0D); // Disable digital input
|
|
|
|
// Pin Config for LM35 ADC
|
|
DDRC &= ~(1 << DDC1);
|
|
DIDR0 |= (1 << ADC1D); // Disable digital input
|
|
}
|
|
|
|
/** @brief Initializes ADC with required settings
|
|
*/
|
|
void adc_init() {
|
|
/* ADC Settings
|
|
* Use AVcc as Vref
|
|
* Initially set input as GND
|
|
* Data right-adjusted
|
|
* No Interrupts
|
|
*/
|
|
// Set Vref
|
|
ADMUX |= (1 << REFS1) | (1 << REFS0);
|
|
// Set the clock prescaler to 128 (slower ADC means more accurate measurements)
|
|
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
|
|
|
|
// Enable ADC
|
|
ADCSRA |= (1 << ADEN);
|
|
}
|
|
|
|
/** @brief Blocking function to read an ADC conversion from a selected ADC input.
|
|
*
|
|
* @note Blocks until conversion finishes, so speed of this function is dependant
|
|
* on ADC prescaler.
|
|
*/
|
|
uint16_t adc_run_conversion(uint8_t adc_selection) {
|
|
// Select ADC input, using
|
|
ADMUX = (ADMUX & 0xF0) | (0x0F & adc_selection);
|
|
|
|
// Start conversion
|
|
ADCSRA |= (1 << ADSC);
|
|
|
|
// Wait until conversion is complete
|
|
while (!(ADCSRA & (1 << ADIF)));
|
|
|
|
// Read out conversion value
|
|
// may not be correct
|
|
return ADC;
|
|
}
|
|
|
|
|
|
/** @brief Converts an ADC value from a measurement on an LM35 into a temperature.
|
|
*/
|
|
int8_t lm35_convert(uint16_t adc_reading) {
|
|
return (float) (adc_reading * ADC_VREF / ADC_RESOLUTION) / LM35_SENSITIVITY;
|
|
}
|
|
|
|
/** @brief Converts and ADC value from a measurement on a diode into a temperature.
|
|
* TODO
|
|
*/
|
|
int8_t diode_convert(uint16_t adc_reading) {
|
|
// Do some funky math here
|
|
|
|
return 0;
|
|
}
|
|
|
|
int main() {
|
|
// Initialise display
|
|
// NOTE: LCD uses PB0-PB6
|
|
lcd_init(LCD_DISP_ON);
|
|
|
|
// Initialise peripherals
|
|
pin_init();
|
|
adc_init();
|
|
|
|
|
|
|
|
while (1) {
|
|
// Reset position
|
|
lcd_gotoxy(0, 0);
|
|
|
|
// Read LM35 value, and write to LCD
|
|
uint16_t lm35_value = adc_run_conversion(1);
|
|
int8_t lm35_temp = lm35_convert(lm35_value);
|
|
// Convert measured value to string
|
|
sprintf(lm35_string, "LM35 : %4d %3dC", lm35_value, lm35_temp);
|
|
|
|
// Display temp on LCD
|
|
lcd_puts((const char*) lm35_string);
|
|
|
|
lcd_gotoxy(0, 1);
|
|
|
|
|
|
// Read diode value, and write to
|
|
uint16_t diode_value = adc_run_conversion(0);
|
|
int8_t diode_temp = diode_convert(diode_value);
|
|
sprintf(diode_string, "Diode: %4d %3dC", diode_value, diode_temp);
|
|
|
|
// Display diode info on LCD
|
|
lcd_puts((const char*) diode_string);
|
|
|
|
_delay_ms(500);
|
|
}
|
|
|
|
}
|