You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
855 B
C
34 lines
855 B
C
#include <avr/io.h>
|
|
#include <avr/interrupt.h>
|
|
#include <stdint.h>
|
|
|
|
volatile float ADC_reading[5] = {0,0,0,0,0};
|
|
|
|
void initADC(void)
|
|
{
|
|
ADMUX = 1 << REFS0 | 0 << REFS1; //Select external Vref
|
|
ADCSRA = _BV(ADEN) | _BV(ADIE); // enable adc, enable interrupt
|
|
ADCSRA |= 1 << ADPS2 | 1 << ADPS1 | 1 << ADPS0; // set clock-prescaler to 128
|
|
ADCSRA |= 1 << ADSC; // start conversion
|
|
}
|
|
|
|
void adc_set_channel(uint8_t ch){
|
|
ADMUX = (ADMUX & 0xE0) | (ch & 0x1F);
|
|
}
|
|
|
|
ISR(ADC_vect)
|
|
{
|
|
static uint8_t ch = 0;
|
|
//Reading 10bit conversion result
|
|
uint16_t tmp = ADCL; //copy the first LSB bits
|
|
tmp |= ADCH << 8; //copy remaing byte
|
|
|
|
ADC_reading[ch] = (((float)tmp * 0.3));
|
|
//ADC_reading[ch] = (((float)tmp * 0.03)/0.092)-2.3;
|
|
|
|
ch = (ch + 1)%5;
|
|
adc_set_channel(ch);
|
|
|
|
ADCSRA |= (1 << ADSC); //Start next conversion
|
|
}
|