forked from prusa3d/MM-control-01
-
Notifications
You must be signed in to change notification settings - Fork 1
/
adc.c
87 lines (76 loc) · 1.38 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//adc.c
#include "adc.h"
#include <avr/io.h>
uint8_t adc_sta;
uint8_t adc_cnt;
uint16_t adc_val[ADC_CHAN_CNT];
uint16_t adc_sim_msk;
#ifdef ADC_READY
extern void ADC_READY(void);
#endif //ADC_READY
void adc_init(void)
{
adc_sim_msk = 0x00;
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
ADMUX |= (1 << REFS0);
ADCSRA |= (1 << ADEN);
// ADCSRA |= (1 << ADIF) | (1 << ADSC);
DIDR0 = (ADC_CHAN_MSK & 0xff);
DIDR2 = (ADC_CHAN_MSK >> 8);
adc_res();
}
void adc_res(void)
{
adc_sta = 0;
adc_cnt = 0;
uint8_t i; for (i = 0; i < ADC_CHAN_CNT; i++)
if ((adc_sim_msk & (1 << i)) == 0)
adc_val[i] = 0;
}
void adc_mux(uint8_t ch)
{
ch &= 0x0f;
if (ch & 0x08) ADCSRB |= (1 << MUX5);
else ADCSRB &= ~(1 << MUX5);
ADMUX = (ADMUX & ~(0x07)) | (ch & 0x07);
}
uint8_t adc_chan(uint8_t index)
{
uint8_t chan = 0;
uint16_t mask = 1;
while (mask)
{
if ((mask & ADC_CHAN_MSK) && (index-- == 0)) break;
mask <<= 1;
chan++;
}
return chan;
}
void adc_cyc(void)
{
if (adc_sta & 0x80)
{
uint8_t index = adc_sta & 0x0f;
if ((adc_sim_msk & (1 << index)) == 0)
adc_val[index] += ADC;
if (index++ >= ADC_CHAN_CNT)
{
index = 0;
adc_cnt++;
if (adc_cnt >= ADC_OVRSAMPL)
{
#ifdef ADC_READY
ADC_READY();
#endif //ADC_READY
adc_res();
}
}
adc_mux(adc_chan(index));
adc_sta = index;
}
else
{
ADCSRA |= (1 << ADSC); //start conversion
adc_sta |= 0x80;
}
}