76 lines
1.7 KiB
C
Executable File
76 lines
1.7 KiB
C
Executable File
#include <avr/io.h>
|
|
|
|
#include "main.h"
|
|
#include "lcdlibrary/lcd.h"
|
|
|
|
/** @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 << DDRC0);
|
|
DIDR0 |= (1 << ADC0D); // Disable digital input
|
|
|
|
// Pin Config for LM35 ADC
|
|
DDRC &= ~(1 << DDRC1);
|
|
DIDR0 |= (1 << ADC1D); // Disable digital input
|
|
}
|
|
|
|
/** @brief Initializes ADC with required settings
|
|
*/
|
|
void adc_init() {
|
|
/* ADC Settings
|
|
* Use Aref as Vref
|
|
* Initially set input as GND
|
|
* Data right-adjusted
|
|
* No Interrupts
|
|
*/
|
|
|
|
// Set MUX[3:0] to 0b1111 (GND reference)
|
|
ADMUX |= (1 << MUX3) | (1 << MUX2) | (1 << MUX1) | (1 << MUX0);
|
|
// Make sure data is right adjusted
|
|
ADMUX &= ~(1 << ADLAR);
|
|
|
|
// 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
|
|
ADMUX &= 0xF0 | 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;
|
|
}
|
|
|
|
|
|
int main() {
|
|
pin_init();
|
|
adc_init();
|
|
|
|
while (1) {
|
|
|
|
}
|
|
|
|
}
|