AVR A/D example.c: Rozdiel medzi revíziami
Zo stránky SensorWiki
Bez shrnutí editace |
Bez shrnutí editace |
||
Riadok 1: | Riadok 1: | ||
Jednoduchý prevod na vyžiadanie: | |||
<source lang="c"> | <source lang="c"> | ||
#include <avr/io.h> | #include <avr/io.h> | ||
void adc_init(void); | void adc_init(void); // A/D converter initialization | ||
int main(void) | int main(void) | ||
Riadok 8: | Riadok 10: | ||
unsigned int buffer; | unsigned int buffer; | ||
adc_init(); | adc_init(); // Init A/D converter | ||
while (1) | while (1) | ||
{ | { | ||
ADCSRA |= (1<<ADSC); | ADCSRA |= (1<<ADSC); // Start A/D conversion | ||
while ( !(ADCSRA & (1<<ADIF)) | while ( !(ADCSRA & (1<<ADIF))); // Wait until AD not finished | ||
buffer = (ADCH<<8) | ADCL | buffer = ADC; // Then, read result (ADCH<<8) | ADCL) | ||
} | } | ||
Riadok 20: | Riadok 22: | ||
void adc_init(void) | void adc_init(void) // A/D converter initialization | ||
{ | { | ||
ADMUX = | ADMUX = 0b01000100; // Ref: external, Channel 4 (ADC4) - NTC | ||
ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1); | ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); // =0b10000111, A/D ON, prescaler = 128 | ||
} | } | ||
</source> | </source> |
Verzia z 22:07, 2. december 2008
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
}