-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadc.h
186 lines (161 loc) · 5.83 KB
/
adc.h
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
/*******************************************************************************
*
* TITLE adc.h
*
* 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.
*
*******************************************************************************/
#ifndef _adc_h
#define _adc_h
// Number of ADC channels to cycle through. This value must be a value
// between one and fourteen or sixteen (fifteen is not an option). Make sure
// each of these analog channels is defined as an input in user_routines.c/
// User_Initialization() if you're using the EDU-RC.
#define NUM_ADC_CHANNELS 1
// Pick the slowest ADC sample rate that still meets your performace criteria.
// These #defines are used below to set the value of ADC_SAMPLE_RATE and to
// set the timer 2 update rate in adc.c/Initialize_Timer_2().
#define ADC_SAMPLE_RATE_200HZ
// #define ADC_SAMPLE_RATE_400HZ
// #define ADC_SAMPLE_RATE_800HZ
// #define ADC_SAMPLE_RATE_1600HZ
// #define ADC_SAMPLE_RATE_3200HZ
// #define ADC_SAMPLE_RATE_6400HZ
// Number of ADC samples that will be averaged for each update. More will
// generally give you more resolution and less noise (see chart below), but
// your update rate will decrease proportionately.
//
// ADC Samples Effective
// Averaged Bits of Measurement Voltage
// Per Update Resolution Range Per Bit
// ___________ __________ ___________ _________
// 1 10 0-1023 4.88 mV
// 2 10 0-1023 4.88 mV
// 4 11 0-2047 2.44 mV
// 8 11 0-2047 2.44 mV
// 16 12 0-4095 1.22 mV
// 32 12 0-4095 1.22 mV
// 64 13 0-8191 610 uV
// 128 13 0-8191 610 uV
// 256 14 0-16383 305 uV
//
// #define ADC_SAMPLES_PER_UPDATE_1
// #define ADC_SAMPLES_PER_UPDATE_2
#define ADC_SAMPLES_PER_UPDATE_4
// #define ADC_SAMPLES_PER_UPDATE_8
// #define ADC_SAMPLES_PER_UPDATE_16
// #define ADC_SAMPLES_PER_UPDATE_32
// #define ADC_SAMPLES_PER_UPDATE_64
// #define ADC_SAMPLES_PER_UPDATE_128
// #define ADC_SAMPLES_PER_UPDATE_256
//
// If you modify stuff below this line, you'll break the software.
//
#ifdef ADC_SAMPLES_PER_UPDATE_1
#define ADC_SAMPLES_PER_UPDATE 1
#define ADC_RANGE 1024L
#define ADC_RESULT_DIVISOR 0 - 0 // 10-bit effective resolution
#endif
#ifdef ADC_SAMPLES_PER_UPDATE_2
#define ADC_SAMPLES_PER_UPDATE 2
#define ADC_RANGE 1024L
#define ADC_RESULT_DIVISOR 1 - 0 // 10-bit effective resolution
#endif
#ifdef ADC_SAMPLES_PER_UPDATE_4
#define ADC_SAMPLES_PER_UPDATE 4
#define ADC_RANGE 2048L
#define ADC_RESULT_DIVISOR 2 - 1 // 11-bit effective resolution
#endif
#ifdef ADC_SAMPLES_PER_UPDATE_8
#define ADC_SAMPLES_PER_UPDATE 8
#define ADC_RANGE 2048L
#define ADC_RESULT_DIVISOR 3 - 1 // 11-bit effective resolution
#endif
#ifdef ADC_SAMPLES_PER_UPDATE_16
#define ADC_SAMPLES_PER_UPDATE 16
#define ADC_RANGE 4096L
#define ADC_RESULT_DIVISOR 4 - 2 // 12-bit effective resolution
#endif
#ifdef ADC_SAMPLES_PER_UPDATE_32
#define ADC_SAMPLES_PER_UPDATE 32
#define ADC_RANGE 4096L
#define ADC_RESULT_DIVISOR 5 - 2 // 12-bit effective resolution
#endif
#ifdef ADC_SAMPLES_PER_UPDATE_64
#define ADC_SAMPLES_PER_UPDATE 64
#define ADC_RANGE 8192L
#define ADC_RESULT_DIVISOR 6 - 3 // 13-bit effective resolution
#endif
#ifdef ADC_SAMPLES_PER_UPDATE_128
#define ADC_SAMPLES_PER_UPDATE 128
#define ADC_RANGE 8192L
#define ADC_RESULT_DIVISOR 7 - 3 // 13-bit effective resolution
#endif
#ifdef ADC_SAMPLES_PER_UPDATE_256
#define ADC_SAMPLES_PER_UPDATE 256
#define ADC_RANGE 16384L
#define ADC_RESULT_DIVISOR 8 - 4 // 14-bit effective resolution
#endif
#ifdef ADC_SAMPLE_RATE_200HZ
#define ADC_SAMPLE_RATE 200
#endif
#ifdef ADC_SAMPLE_RATE_400HZ
#define ADC_SAMPLE_RATE 400
#endif
#ifdef ADC_SAMPLE_RATE_800HZ
#define ADC_SAMPLE_RATE 800
#endif
#ifdef ADC_SAMPLE_RATE_1600HZ
#define ADC_SAMPLE_RATE 1600
#endif
#ifdef ADC_SAMPLE_RATE_3200HZ
#define ADC_SAMPLE_RATE 3200
#endif
#ifdef ADC_SAMPLE_RATE_6400HZ
#define ADC_SAMPLE_RATE 6400
#endif
#define VREF_POS_MV 5000L // ADC Vref+ expressed in millivolts
#define VREF_NEG_MV 0L // ADC Vref- expressed in millivolts
// number of ADC sample sets generated per second
#define ADC_UPDATE_RATE ADC_SAMPLE_RATE/(ADC_SAMPLES_PER_UPDATE * NUM_ADC_CHANNELS)
// function prototypes
void Initialize_ADC(void);
void Disable_ADC(void);
void Initialize_Timer_2(unsigned int);
void Disable_Timer_2(void);
void Timer_2_Int_Handler(void);
void ADC_Int_Handler(void);
unsigned int Get_ADC_Result(unsigned char);
unsigned int Convert_ADC_to_mV(unsigned int);
unsigned char Get_ADC_Result_Count(void);
void Reset_ADC_Result_Count(void);
#endif