sysprog-basic/include/adc.c

19 lines
501 B
C

#include "adc.h"
#include <stdint.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <util/delay.h>
void adc_init() {
DDRC=0x0; // does not exist but is default?
ADCSRA = (1<<ADEN)|(1<<ADPS1)|(1<<ADPS2);
ADMUX = (1<<REFS0);
}
uint16_t adc_read(uint8_t channel) {
ADMUX &= 0xf0; // max channel?
ADMUX |= (channel & 0x0f); // select channel
ADCSRA |= (1<<ADSC); // interrupt enable
while((ADCSRA&(1<<ADIF))==0); // wait for something
return ADCL + (ADCH << 8); // read
}