AVR A/D example.c
Zo stránky SensorWiki
Jednoduchý prevod na vyžiadanie:
#include <avr/io.h>
void adc_init(void); // A/D converter initialization
int main(void)
{
unsigned int buffer;
adc_init(); // Init A/D converter
while (1)
{
ADCSRA |= (1<<ADSC); // Start A/D conversion
while ( !(ADCSRA & (1<<ADIF))); // Wait until AD not finished
buffer = ADC; // Then, read result (ADCH<<8) | ADCL)
}
}
void adc_init(void) // A/D converter initialization
{
ADMUX = 0b01000100; // Ref: external, Channel 4 (ADC4) - NTC
ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); // =0b10000111, A/D ON, prescaler = 128
}