-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadc.c
517 lines (441 loc) · 12.6 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/*******************************************************************************
*
* TITLE adc.c
*
* VERSION: 0.4 (Beta)
*
* DATE: 10-Jan-2006
*
* AUTHOR: R. Kevin Watson
*
* COMMENTS: You are free to use this source code for any non-commercial
* use. Please do not make copies of this source code, modified
* or un-modified, publicly available on the internet or
* elsewhere without permission. Thanks.
*
* Copyright ©2005-2006 R. Kevin Watson. All rights are reserved.
*
********************************************************************************
*
* CHANGE LOG:
*
* DATE REV DESCRIPTION
* ----------- --- ----------------------------------------------------------
* 10-Jul-2005 0.1 RKW - Original code.
* 17-Jul-2005 0.2 RKW - Added x128 and x256 oversampling options.
* 13-Dec-2005 0.3 RKW - Altered code to use the "special event trigger"
* functionality of the CCP2 hardware to initiate ADC
* conversions. This was formally done using timer 2.
* 10-Jan-2006 0.4 RKW - Ported to PIC18F8722, which required going back
* to using timer 2 to initiate an analog to digital
* conversion due to a bug in the PIC18F8722 design.
* Modified #pragma interruptlow statement to include
* .tmpdata section.
*
*******************************************************************************/
#include "adc.h"
#include "ifi_aliases.h"
#include "ifi_default.h"
unsigned int adc_sample_rate;
unsigned int adc_samples_per_update;
unsigned char num_adc_channels;
unsigned char adc_result_divisor;
volatile unsigned long accum[NUM_ADC_CHANNELS]; // sample accumulator
volatile unsigned int adc_result[NUM_ADC_CHANNELS]; // ADC recults
volatile unsigned int samples; // current number of samples accumulated
volatile unsigned char channel; // current ADC channel
volatile unsigned char adc_update_count = 0; // ADC update flag
/*******************************************************************************
*
* FUNCTION: Initialize_ADC()
*
* PURPOSE: Initializes the Analog to Digital Conversion (ADC) hardware.
*
* CALLED FROM: user_routines.c/User_Initialization()
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
* COMMENTS: Place "#include "adc.h" in the includes section
* of user_routines.c then call Initialize_ADC() in
* user_routines.c/User_Initialization().
*
*******************************************************************************/
void Initialize_ADC(void)
{
unsigned char i;
// Initialize ADC related global variables using default values from adc.h.
// Another way to do this would be to pass the values in, read them from
// values stored in EEPROM or perhaps set them via a menu-based terminal
// interface.
adc_sample_rate = ADC_SAMPLE_RATE;
num_adc_channels = NUM_ADC_CHANNELS;
adc_result_divisor = ADC_RESULT_DIVISOR;
adc_samples_per_update = ADC_SAMPLES_PER_UPDATE;
// reset the sample accumulator(s) to zero
for(i=0; i < num_adc_channels; i++)
{
accum[i] = 0L;
}
// start a new sample set
samples = 0;
// start at ADC channel zero
channel = 0;
// enable the ADC hardware and select ADC channel 0
ADCON0 = 0b00000001;
#ifdef _FRC_BOARD
// If this is being built for the FRC-RC, enable all sixteen analog
// inputs. This shouldn't be changed because the FRC-RC has sixteen
// dedicated analog inputs. The VCFG0 and VCFG1 bits are also set to
// select Vdd and Vss as the reference voltages
ADCON1 = 0;
#else
// If this is being built for the EDU-RC, enable NUM_ADC_CHANNELS
// analog inputs (NUM_ADC_CHANNELS is documented in adc.h). This must
// match the default value used in user_routines.c/User_Initialization()/
// Set_Number_of_Analog_Channels(). The VCFG0 and VCFG1 bits are also
// set to select Vdd and Vss as the reference voltages
if(num_adc_channels >= 1 && num_adc_channels <= 14)
{
ADCON1 = 15 - num_adc_channels;
}
else
{
ADCON1 = 0; // sixteen channnels or out of range
}
#endif
// select Fosc/64 as the ADC Conversion Clock and right justify data
// in the conversion result register
ADCON2 = 0b10000110;
// ADC interrupt is low priority
IPR1bits.ADIP = 0;
// to prevent a spurious interrupt, make sure the interrupt flag is reset
PIR1bits.ADIF = 0;
// enable the ADC interrupt
PIE1bits.ADIE = 1;
// initialize and start timer 2, which is responsible for starting
// analog to digital conversions
Initialize_Timer_2(adc_sample_rate);
}
/*******************************************************************************
*
* FUNCTION: Disable_ADC()
*
* PURPOSE: Disables the Analog to Digital Conversion (ADC) hardware.
*
* CALLED FROM:
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
* COMMENTS:
*
*******************************************************************************/
void Disable_ADC(void)
{
unsigned char i;
// disable timer 2
Disable_Timer_2();
// disable the ADC interrupt
PIE1bits.ADIE = 0;
// disable the ADC hardware and select ADC channel 0
ADCON0 = 0b00000000;
}
/*******************************************************************************
*
* FUNCTION: Initialize_Timer_2()
*
* PURPOSE: Initializes the timer 2 hardware, which is responsible for
* periodically starting analog to digital conversions.
*
* CALLED FROM: adc.c/Initialize_ADC()
*
* PARAMETERS: Unsigned integer containing the sample rate expressed in Hz
*
* RETURNS: Nothing
*
* COMMENTS: The only supported sample rates are 200Hz, 400Hz, 800Hz,
* 1600Hz, 3200Hz and 6400Hz.
*
*******************************************************************************/
void Initialize_Timer_2(unsigned int adc_sample_rate)
{
switch(adc_sample_rate)
{
case 200:
// use these parameters for a 200Hz ADC sample rate
// use a 1:16 prescaler and 1:14 postscaler
T2CON = 0b01101010;
// Count to 221 before rolling over and generating
// an interrupt (223.21 - 2 is ideal)
PR2 = 221;
break;
case 400:
// use these parameters for a 400Hz ADC sample rate
// use a 1:16 prescaler and 1:11 postscaler
T2CON = 0b01010010;
// Count to 140 before rolling over and generating
// an interrupt (142.05 - 2 is ideal)
PR2 = 140;
break;
case 800:
// use these parameters for a 800Hz ADC sample rate
// use a 1:16 prescaler and 1:11 postscaler
T2CON = 0b01010010;
// Count to 69 before rolling over and generating
// an interrupt (71.02 - 2 is ideal)
PR2 = 69;
break;
case 1600:
// use these parameters for a 1600Hz ADC sample rate
// use a 1:4 prescaler and 1:11 postscaler
T2CON = 0b01010001;
// Count to 140 before rolling over and generating
// an interrupt (142.05 - 2 is ideal)
PR2 = 140;
break;
case 3200:
// use these parameters for a 3200Hz ADC sample rate
// use a 1:1 prescaler and 1:11 postscaler
T2CON = 0b01010001;
// Count to 69 before rolling over and generating
// an interrupt (71.02 - 2 is ideal)
PR2 = 69;
break;
case 6400:
// use these parameters for a 6400Hz ADC sample rate
// use a 1:4 prescaler and 1:11 postscaler
T2CON = 0b01010000;
// Count to 140 before rolling over and generating
// an interrupt (142.05 - 2 is ideal)
PR2 = 140;
break;
default:
// if a non-supported rate is specified,
// default to a 200Hz ADC sample rate
// use a 1:16 prescaler and 1:14 postscaler
T2CON = 0b01101010;
// Count to 221 before rolling over and generating
// an interrupt (223.21 - 2 is ideal)
PR2 = 221;
break;
}
// make sure the timer 2 register starts at zero
TMR2 = 0x00;
// timer 2 interrupt is low priority
IPR1bits.TMR2IP = 0;
// to prevent a spurious interrupt, make sure the interrupt flag is reset
PIR1bits.TMR2IF = 0;
// enable the timer 2 interrupt
PIE1bits.TMR2IE = 1;
// enable timer 2
T2CONbits.TMR2ON = 1;
}
/*******************************************************************************
*
* FUNCTION: Disable_Timer_2()
*
* PURPOSE: Disables the timer 2 hardware.
*
* CALLED FROM:
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
* COMMENTS:
*
*******************************************************************************/
void Disable_Timer_2(void)
{
// disable the timer 2 interrupt
PIE1bits.TMR2IE = 0;
// disable timer 2
T2CONbits.TMR2ON = 0;
}
/*******************************************************************************
*
* FUNCTION: Get_ADC_Result()
*
* PURPOSE: Given the ADC channel number, returns the last ADC result
* expressed in "data number" units.
*
* CALLED FROM:
*
* PARAMETERS: ADC channel number
*
* RETURNS: Nothing
*
* COMMENTS:
*
*******************************************************************************/
unsigned int Get_ADC_Result(unsigned char channel)
{
unsigned int temp_adc_result;
if(channel <= num_adc_channels)
{
// disable the ADC interrupt
PIE1bits.ADIE = 0;
// one is subtracted because analog input 1 maps to adc_result[0],
// input 2 maps to adc_result[1],..., input 16 maps to adc_result[15]
temp_adc_result = adc_result[channel - 1];
// enable the ADC interrupt
PIE1bits.ADIE = 1;
}
else
{
// bad channel number; return zero
temp_adc_result = 0;
}
return(temp_adc_result);
}
/*******************************************************************************
*
* FUNCTION: Convert_ADC_to_mV()
*
* PURPOSE: Converts the raw output of the ADC to millivolts.
*
* CALLED FROM:
*
* PARAMETERS: ADC output value to convert
*
* RETURNS: Millivolts
*
* COMMENTS:
*
*******************************************************************************/
unsigned int Convert_ADC_to_mV(unsigned int adc)
{
return((unsigned int)(((long)adc * (VREF_POS_MV - VREF_NEG_MV)) / ADC_RANGE));
}
/*******************************************************************************
*
* FUNCTION: Get_ADC_Result_Count()
*
* PURPOSE:
*
* CALLED FROM:
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
* COMMENTS:
*
*******************************************************************************/
unsigned char Get_ADC_Result_Count()
{
unsigned char temp_adc_update_count;
// disable the ADC interrupt
PIE1bits.ADIE = 0;
temp_adc_update_count = adc_update_count;
// enable the ADC interrupt
PIE1bits.ADIE = 1;
return(temp_adc_update_count);
}
/*******************************************************************************
*
* FUNCTION: Reset_ADC_Result_Count()
*
* PURPOSE: Resets the ADC update counter to zero
*
* CALLED FROM:
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
* COMMENTS:
*
*******************************************************************************/
void Reset_ADC_Result_Count()
{
// disable the ADC interrupt
PIE1bits.ADIE = 0;
adc_update_count = 0;
// enable the ADC interrupt
PIE1bits.ADIE = 1;
}
/*******************************************************************************
*
* FUNCTION: Timer_2_Int_Handler()
*
* PURPOSE: Timer 2 interrupt service routine
*
* CALLED FROM: user_routines_fast.c/InterruptHandlerLow()
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
* COMMENTS:
*
*******************************************************************************/
void Timer_2_Int_Handler(void)
{
// start a new analog to digital conversion
ADCON0bits.GO = 1;
}
/*******************************************************************************
*
* FUNCTION: ADC_Int_Handler()
*
* PURPOSE: ADC interrupt service routine
*
* CALLED FROM: user_routines_fast.c/InterruptHandlerLow()
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
* COMMENTS:
*
*******************************************************************************/
void ADC_Int_Handler(void)
{
unsigned int adc;
unsigned char adcon0_temp;
int i;
// get conversion results
adc = ADRESH;
adc <<= 8;
adc += ADRESL;
// add the ADC data to the appropriate accumulator
accum[channel] += (long)adc;
// increment the ADC channel index
channel++;
// do we need to wrap around to zero?
if(channel >= num_adc_channels)
{
channel = 0;
samples++;
}
// Select next ADC channel. This also starts the process whereby the ADC
// sample and hold capacitor is allowed to start charging, which must be
// completed before the next analog to digital conversion can be started.
adcon0_temp = channel;
adcon0_temp <<= 2;
adcon0_temp |= 0b00000001;
ADCON0 = adcon0_temp;
// check to see if we've got a full sample set
if(samples >= adc_samples_per_update)
{
// update the ADC result array
for(i=0; i < num_adc_channels; i++)
{
adc_result[i] = (long)(accum[i] >> adc_result_divisor);
}
// reset the sample accumulator(s) to zero
for(i=0; i < num_adc_channels; i++)
{
accum[i] = 0L;
}
// signal that a fresh sample set is available
adc_update_count++;
// start a fresh sample set
samples = 0;
}
}