-
Notifications
You must be signed in to change notification settings - Fork 0
/
adc.c
46 lines (37 loc) · 748 Bytes
/
adc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "adc.h"
#include <avr/io.h>
#include <avr/interrupt.h>
static int cur_mux=0;
static uint16_t adc[MUX_MAX];
static int initialized=0;
ISR(ADC_vect) {
adc[cur_mux]=ADC;
if(cur_mux<MUX_MAX) {
cur_mux++;
}
else {
cur_mux=0;
}
ADMUX = (ADMUX & 0xf0) | cur_mux;
ADCSRA |= 1<<ADSC;
}
void adcInit() {
if(initialized)
return;
ADMUX = (1 << REFS0);
ADCSRA = (1 << ADEN) | (1 << ADSC) | (1 << ADIE) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
sei();
initialized=1;
}
uint16_t adcGet(int index) {
if(index>MUX_MAX)
return 0;
return adc[index];
}
uint16_t adcConvert(int index) {
if(index>MUX_MAX)
return 0;
if(index==ADC_BAT)
return ((uint32_t)adc[index])*ADC_BAT_MUL/ADC_BAT_DIV;
return adc[index];
}