-
Notifications
You must be signed in to change notification settings - Fork 6
/
ADC.c
226 lines (170 loc) · 6.12 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* ************************************************************************
*
* ADC functions
*
* (c) 2012-2023 by Markus Reschke
* based on code from Markus Frejek and Karl-Heinz Kübbeler
*
* ************************************************************************ */
/*
* local constants
*/
/* source management */
#define ADC_C
/*
* include header files
*/
/* local includes */
#include "config.h" /* global configuration */
#include "common.h" /* common header file */
#include "variables.h" /* global variables */
#include "functions.h" /* external functions */
/* ************************************************************************
* ADC
* ************************************************************************ */
/*
* read ADC channel and return voltage in mV
* - use Vcc as reference by default
* - switch to bandgap reference for low voltages (< 1.0V) to improve
* ADC resolution
* - with a 125kHz ADC clock a single conversion needs about 0.1ms
* with 25 samples we end up with about 2.6ms
*
* requires:
* - Channel: ADC MUX input channel
* - ATmega328: register bits corresponding with MUX0-3
* - ATmega324/644/1284: register bits corresponding with MUX0-4
* - ATmega640/1280/2560: register bits corresponding with MUX0-4
* (todo: add MUX5 to support also ADC8-15)
*/
uint16_t ReadU(uint8_t Channel)
{
uint16_t U; /* return value (mV) */
uint8_t Counter; /* loop counter */
uint8_t Ref; /* voltage reference register bits */
uint32_t Value; /* ADC value */
/* AREF pin is connected to external buffer cap (1nF) */
#if 0
/* manage channels ADC8-15 (ATmega640/1280/2560) */
if (Channel & 0b00100000) /* bit 6 set: ADC8-15 */
{
ADCSRB |= (1 << MUX5); /* set MUX5 */
}
else /* bit 6 not set: ADC0-7 */
{
ADCSRB &= ~(1 << MUX5); /* clear MUX5 */
}
#endif
/* prepare bitfield for register: start with AVcc as voltage reference */
Channel &= ADC_CHAN_MASK; /* filter reg bits for MUX channel */
Channel |= ADC_REF_VCC; /* add bits for voltage reference: AVcc */
sample:
ADMUX = Channel; /* set input channel and U reference */
/*
* change of voltage reference
* - voltage needs some time to stabilize at buffer cap
* - run a dummy conversion after change (recommended by datasheet)
* - It seems that we have to run a dummy conversion also after the
* ADC hasn't run for a while. So let's do one anyway.
*/
Ref = Channel & ADC_REF_MASK; /* get register bits for voltage reference */
if (Ref != Cfg.Ref) /* reference source has changed */
{
/* wait some time for voltage stabilization */
#ifndef ADC_LARGE_BUFFER_CAP
/* buffer cap: 1nF or none at all */
wait100us(); /* 100µs */
#else
/* buffer cap: 100nF */
wait10ms(); /* 10ms */
#endif
#if 0
/* dummy conversion */
ADCSRA |= (1 << ADSC); /* start conversion */
while (ADCSRA & (1 << ADSC)); /* wait until conversion is done */
#endif
Cfg.Ref = Ref; /* update reference source */
}
/* perform dummy conversion anyway */
ADCSRA |= (1 << ADSC); /* start conversion */
while (ADCSRA & (1 << ADSC)); /* wait until conversion is done */
/*
* sample ADC readings
*/
Value = 0UL; /* reset sampling variable */
Counter = 0; /* reset counter */
while (Counter < Cfg.Samples) /* take samples */
{
ADCSRA |= (1 << ADSC); /* start conversion */
while (ADCSRA & (1 << ADSC)); /* wait until conversion is done */
Value += ADCW; /* add ADC reading */
/* auto-switch voltage reference for low readings */
if (Counter == 4) /* 5 samples */
{
if ((uint16_t)Value < 1024) /* < 1V (5V / 5 samples) */
{
if (Ref != ADC_REF_BANDGAP) /* bandgap ref not selected */
{
if (Cfg.AutoScale == 1) /* autoscaling enabled */
{
Channel &= ~ADC_REF_MASK; /* clear reference bits */
Channel |= ADC_REF_BANDGAP; /* select bandgap reference */
goto sample; /* re-run sampling */
}
}
}
}
Counter++; /* another sample done */
}
/*
* convert ADC reading to voltage
* - single sample: U = ADC reading * U_ref / 1024
*/
/* get voltage of reference used */
if (Ref == ADC_REF_BANDGAP) /* bandgap reference */
{
U = Cfg.Bandgap; /* voltage of bandgap reference */
}
else /* Vcc as reference */
{
U = Cfg.Vcc; /* voltage of Vcc */
}
/* convert to voltage; */
Value *= U; /* ADC readings * U_ref */
// Value += 511 * Cfg.Samples; /* automagic rounding */
Value /= 1024; /* / 1024 for 10bit ADC */
/* de-sample to get average voltage */
Value /= Cfg.Samples;
U = (uint16_t)Value;
// todo: do we need a sanity check for U <= Vcc?
return U;
}
/* ************************************************************************
* convenience functions
* ************************************************************************ */
/*
* wait 5ms and then read ADC
* - same as ReadU()
*/
uint16_t ReadU_5ms(uint8_t Channel)
{
wait5ms(); /* wait 5ms */
return (ReadU(Channel));
}
/*
* wait 20ms and then read ADC
* - same as ReadU()
*/
uint16_t ReadU_20ms(uint8_t Channel)
{
wait20ms(); /* wait 20ms */
return (ReadU(Channel));
}
/* ************************************************************************
* clean-up of local constants
* ************************************************************************ */
/* source management */
#undef ADC_C
/* ************************************************************************
* EOF
* ************************************************************************ */