* device/include/pic16/pic18f[24][3456]k20.h,
[fw/sdcc] / device / include / pic16 / adc.h
1
2 /*
3  * A/D conversion module library header
4  *
5  * written by Vangelis Rokas, 2004 <vrokas AT otenet.gr>
6  *
7  * Devices implemented:
8  *      PIC18F[24][45][28]
9  *      PIC18F2455-style
10  *
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Library General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  *
26  * $Id$
27  */
28
29 #ifndef __ADC_H__
30 #define __ADC_H__
31
32 /* link I/O libarary */
33 #pragma library io
34
35 /* interrupt on/off flag */
36 #define ADC_INT_OFF     0x00
37 #define ADC_INT_ON      0x01
38
39
40 /* output format */
41 #define ADC_FRM_RJUST   0x80
42 #define ADC_FRM_LJUST   0x00
43
44
45 /* reference voltage configuration (not for 18f242-style ADC) */
46 #define ADC_VCFG_VDD_VSS  0x00
47 #define ADC_VCFG_AN3_VSS  0x10
48 #define ADC_VCFG_VDD_AN2  0x20
49 #define ADC_VCFG_AN3_AN2  0x30
50
51 /* oscillator frequency */
52 #define ADC_FOSC_2      0x00
53 #define ADC_FOSC_4      0x04
54 #define ADC_FOSC_8      0x01
55 #define ADC_FOSC_16     0x05
56 #define ADC_FOSC_32     0x02
57 #define ADC_FOSC_64     0x06
58 #define ADC_FOSC_RC     0x07
59
60 /*
61  * acquisition time (65j50-style only)
62  * -- to be ORed with ADC_FOSC
63  */
64 #define ADC_ACQT_0      (0x00 << 3)
65 #define ADC_ACQT_2      (0x01 << 3)
66 #define ADC_ACQT_4      (0x02 << 3)
67 #define ADC_ACQT_6      (0x03 << 3)
68 #define ADC_ACQT_8      (0x04 << 3)
69 #define ADC_ACQT_12     (0x05 << 3)
70 #define ADC_ACQT_16     (0x06 << 3)
71 #define ADC_ACQT_20     (0x07 << 3)
72
73 /*
74  * calibration enable (65j50-style only)
75  * -- to be ORed with ADC_FOSC
76  */
77 #define ADC_CAL         0x40
78
79 /*
80  * Distinguishing between ADC-styles:
81  *
82  * ADCON0:
83  * bit  18f242  18f1220 18f2220 18f65j50
84  *  0   ADON    ADON    ADON    ADON
85  *  1   -       GO      GO      GO
86  *  2   GO      CHS0    CHS0    CHS0
87  *  3   CHS0    CHS1    CHS1    CHS1
88  *  4   CHS1    CHS2    CHS2    CHS2
89  *  5   CHS2    -       CHS3    CHS3
90  *  6   ADCS0   VCFG0   -       VCFG0
91  *  7   ADCS1   VCFG1   (ADCAL) VCFG1
92  *
93  * ADCON1:
94  *  bit 18f242  18f1220 18f2220 18f65j50
95  *   0  PCFG0   PCFG0   PCFG0   ADCS0
96  *   1  PCFG1   PCFG1   PCFG1   ADCS1
97  *   2  PCFG2   PCFG2   PCFG2   ADCS2
98  *   3  PCFG3   PCFG3   PCFG3   ACQT0
99  *   4  -       PCFG4   VCFG0   ACQT1
100  *   5  -       PCFG5   VCFG1   ACQT2
101  *   6  ADCS2   PCFG6   -       ADCAL
102  *   7  ADFM    -       -       ADFM
103  */
104
105 /* 18f242-style */
106 #if    defined(pic18f242) || defined(pic18f252) || defined(pic18f442) || defined(pic18f452) \
107     || defined(pic18f248) || defined(pic18f258) || defined(pic18f448) || defined(pic18f458)
108
109 #define __SDCC_ADC_STYLE242     1
110
111 /* 18f1220-style */
112 #elif  defined(pic18f1220) || defined(pic18f1320)
113
114 #define __SDCC_ADC_STYLE1220    1
115
116 /* 18f2220-style, ordered by device family */
117 #elif  defined(pic18f2220) || defined(pic18f2320) || defined(pic18f4220) || defined(pic18f4320) \
118     || defined(pic18f2221) || defined(pic18f2321) || defined(pic18f4221) || defined(pic18f4321) \
119     || defined(pic18f23k20) || defined(pic18f24k20) || defined(pic18f25k20) || defined(pic18f26k20) \
120     || defined(pic18f2410) || defined(pic18f2510) || defined(pic18f4410) || defined(pic18f4510) \
121     || defined(pic18f2420) || defined(pic18f2520) || defined(pic18f4420) || defined(pic18f4520) \
122     || defined(pic18f2423) || defined(pic18f2523) || defined(pic18f4423) || defined(pic18f4523) \
123     || defined(pic18f2450) || defined(pic18f4450) \
124     || defined(pic18f2455) || defined(pic18f2550) || defined(pic18f4455) || defined(pic18f4550) \
125     || defined(pic18f2480) || defined(pic18f2580) || defined(pic18f4480) || defined(pic18f4580) \
126     || defined(pic18f24j10) || defined(pic18f25j10) || defined(pic18f44j10) || defined(pic18f45j10) \
127     || defined(pic18f2515) || defined(pic18f2610) || defined(pic18f4515) || defined(pic18f4610) \
128     || defined(pic18f2525) || defined(pic18f2620) || defined(pic18f4525) || defined(pic18f4620) \
129     || defined(pic18f2585) || defined(pic18f2680) || defined(pic18f4585) || defined(pic18f4680) \
130     || defined(pic18f2682) || defined(pic18f2685) || defined(pic18f4682) || defined(pic18f4685) \
131     || defined(pic18f43k20) || defined(pic18f44k20) || defined(pic18f45k20) || defined(pic18f46k20) \
132     || defined(pic18f6520) || defined(pic18f6620) || defined(pic18f6720) \
133     || defined(pic18f6585) || defined(pic18f6680) || defined(pic18f8585) || defined(pic18f8680) \
134     || defined(pic18f66j60) || defined(pic18f66j65) || defined(pic18f67j60) \
135     || defined(pic18f8520) || defined(pic18f8620) || defined(pic18f8720) \
136     || defined(pic18f86j60) || defined(pic18f86j65) || defined(pic18f87j60) \
137     || defined(pic18f96j60) || defined(pic18f96j65) || defined(pic18f97j60) \
138
139 #define __SDCC_ADC_STYLE2220    1
140
141 #elif defined(pic18f65j50) || defined(pic18f66j50) || defined(pic18f66j55) || defined(pic18f67j50) \
142    || defined(pic18f85j50) || defined(pic18f86j50) || defined(pic18f86j55) || defined(pic18f87j50) \
143
144 #define __SDCC_ADC_STYLE65J50
145
146 #else /* unknown device */
147
148 #error Device ADC style is unknown, please update your adc.h manually and/or inform the maintainer!
149
150 #endif
151
152
153 /* channel selection (CHS field in ADCON0) */
154 #define ADC_CHN_0               0x00
155 #define ADC_CHN_1               0x01
156 #define ADC_CHN_2               0x02
157 #define ADC_CHN_3               0x03
158 #define ADC_CHN_4               0x04
159 #define ADC_CHN_5               0x05
160 #define ADC_CHN_6               0x06
161 #define ADC_CHN_7               0x07
162 #define ADC_CHN_8               0x08
163 #define ADC_CHN_9               0x09
164 #define ADC_CHN_10              0x0a
165 #define ADC_CHN_11              0x0b
166 #define ADC_CHN_12              0x0c
167 #define ADC_CHN_13              0x0d
168 #define ADC_CHN_14              0x0e
169 #define ADC_CHN_15              0x0f
170
171
172 /* Port configuration (PCFG (and VCFG) field(s) in ADCON1) */
173 #if defined(__SDCC_ADC_STYLE242)
174
175 #define ADC_CFG_8A_0R   0x00
176 #define ADC_CFG_7A_1R   0x01
177 #define ADC_CFG_5A_0R   0x02
178 #define ADC_CFG_4A_1R   0x03
179 #define ADC_CFG_3A_0R   0x04
180 #define ADC_CFG_2A_1R   0x05
181 #define ADC_CFG_0A_0R   0x06
182 #define ADC_CFG_6A_2R   0x08
183 #define ADC_CFG_6A_0R   0x09
184 #define ADC_CFG_5A_1R   0x0a
185 #define ADC_CFG_4A_2R   0x0b
186 #define ADC_CFG_3A_2R   0x0c
187 #define ADC_CFG_2A_2R   0x0d
188 #define ADC_CFG_1A_0R   0x0e
189 #define ADC_CFG_1A_2R   0x0f
190
191 #elif defined(__SDCC_ADC_STYLE1220)
192
193 /*
194  * These devices use a bitmask in ADCON1 to configure AN0..AN6
195  * as digital ports (bit set) or analog input (bit clear).
196  *
197  * These settings are selected based on their similarity with
198  * the 2220-style settings; 1220-style is more flexible, though.
199  *
200  * Reference voltages are configured via adc_open's config parameter
201  * using ADC_VCFG_*.
202  */
203
204 #define ADC_CFG_6A      0x00
205 #define ADC_CFG_5A      0x20
206 #define ADC_CFG_4A      0x30
207 #define ADC_CFG_3A      0x38
208 #define ADC_CFG_2A      0x3c
209 #define ADC_CFG_1A      0x3e
210 #define ADC_CFG_0A      0x3f
211
212 #elif defined(__SDCC_ADC_STYLE2220)
213
214 /*
215  * The reference voltage configuration should be factored out into
216  * the config argument (ADC_VCFG_*) to adc_open to facilitate a
217  * merger with the 1220-style ADC.
218  */
219
220 #define ADC_CFG_16A     0x00
221 /* 15 analog ports cannot be configured! */
222 #define ADC_CFG_14A     0x01
223 #define ADC_CFG_13A     0x02
224 #define ADC_CFG_12A     0x03
225 #define ADC_CFG_11A     0x04
226 #define ADC_CFG_10A     0x05
227 #define ADC_CFG_9A      0x06
228 #define ADC_CFG_8A      0x07
229 #define ADC_CFG_7A      0x08
230 #define ADC_CFG_6A      0x09
231 #define ADC_CFG_5A      0x0a
232 #define ADC_CFG_4A      0x0b
233 #define ADC_CFG_3A      0x0c
234 #define ADC_CFG_2A      0x0d
235 #define ADC_CFG_1A      0x0e
236 #define ADC_CFG_0A      0x0f
237
238 /*
239  * For compatibility only: Combined port and reference voltage selection.
240  * Consider using ADC_CFG_nA and a separate ADC_VCFG_* instead!
241  */
242
243 #define ADC_CFG_16A_0R  0x00
244 #define ADC_CFG_16A_1R  0x10
245 #define ADC_CFG_16A_2R  0x30
246
247 /* Can only select 14 or 16 analog ports ... */
248 #define ADC_CFG_15A_0R  0x00
249 #define ADC_CFG_15A_1R  0x10
250 #define ADC_CFG_15A_2R  0x30
251
252 #define ADC_CFG_14A_0R  0x01
253 #define ADC_CFG_14A_1R  0x11
254 #define ADC_CFG_14A_2R  0x31
255 #define ADC_CFG_13A_0R  0x02
256 #define ADC_CFG_13A_1R  0x12
257 #define ADC_CFG_13A_2R  0x32
258 #define ADC_CFG_12A_0R  0x03
259 #define ADC_CFG_12A_1R  0x13
260 #define ADC_CFG_12A_2R  0x33
261 #define ADC_CFG_11A_0R  0x04
262 #define ADC_CFG_11A_1R  0x14
263 #define ADC_CFG_11A_2R  0x34
264 #define ADC_CFG_10A_0R  0x05
265 #define ADC_CFG_10A_1R  0x15
266 #define ADC_CFG_10A_2R  0x35
267 #define ADC_CFG_09A_0R  0x06
268 #define ADC_CFG_09A_1R  0x16
269 #define ADC_CFG_09A_2R  0x36
270 #define ADC_CFG_08A_0R  0x07
271 #define ADC_CFG_08A_1R  0x17
272 #define ADC_CFG_08A_2R  0x37
273 #define ADC_CFG_07A_0R  0x08
274 #define ADC_CFG_07A_1R  0x18
275 #define ADC_CFG_07A_2R  0x38
276 #define ADC_CFG_06A_0R  0x09
277 #define ADC_CFG_06A_1R  0x19
278 #define ADC_CFG_06A_2R  0x39
279 #define ADC_CFG_05A_0R  0x0a
280 #define ADC_CFG_05A_1R  0x1a
281 #define ADC_CFG_05A_2R  0x3a
282 #define ADC_CFG_04A_0R  0x0b
283 #define ADC_CFG_04A_1R  0x1b
284 #define ADC_CFG_04A_2R  0x3b
285 #define ADC_CFG_03A_0R  0x0c
286 #define ADC_CFG_03A_1R  0x1c
287 #define ADC_CFG_03A_2R  0x3c
288 #define ADC_CFG_02A_0R  0x0d
289 #define ADC_CFG_02A_1R  0x1d
290 #define ADC_CFG_02A_2R  0x3d
291 #define ADC_CFG_01A_0R  0x0e
292 #define ADC_CFG_01A_1R  0x1e
293 #define ADC_CFG_01A_2R  0x3e
294 #define ADC_CFG_00A_0R  0x0f
295
296 #elif defined(__SDCC_ADC_STYLE65J50)
297
298 /*
299  * These devices use a bitmask in ANCON0/1 to configure
300  * AN7..0/AN15..8 as digital ports (bit set) or analog
301  * inputs (bit clear).
302  *
303  * These settings are selected based on their similarity with
304  * the 2220-style settings; 65j50-style is more flexible, though.
305  *
306  * Reference voltages are configured via adc_open's config parameter
307  * using ADC_VCFG_*.
308  */
309
310 #define ADC_CFG_16A     0x0000
311 #define ADC_CFG_15A     0x8000
312 #define ADC_CFG_14A     0xC000
313 #define ADC_CFG_13A     0xE000
314 #define ADC_CFG_12A     0xF000
315 #define ADC_CFG_11A     0xF800
316 #define ADC_CFG_10A     0xFC00
317 #define ADC_CFG_9A      0xFE00
318 #define ADC_CFG_8A      0xFF00
319 #define ADC_CFG_7A      0xFF80
320 #define ADC_CFG_6A      0xFFC0
321 #define ADC_CFG_5A      0xFFE0
322 #define ADC_CFG_4A      0xFFF0
323 #define ADC_CFG_3A      0xFFF8
324 #define ADC_CFG_2A      0xFFFC
325 #define ADC_CFG_1A      0xFFFE
326 #define ADC_CFG_0A      0xFFFF
327
328 #else   /* unhandled ADC style */
329
330 #error No supported ADC style selected.
331
332 #endif  /* ADC_STYLE */
333
334
335
336 /* initialize AD module */
337 #if defined(__SDCC_ADC_STYLE65J50)
338 void adc_open(unsigned char channel, unsigned char fosc, unsigned int pcfg, unsigned char config);
339 #else
340 void adc_open(unsigned char channel, unsigned char fosc, unsigned char pcfg, unsigned char config);
341 #endif
342
343 /* shutdown AD module */
344 void adc_close(void);
345
346 /* begin a conversion */
347 void adc_conv(void);
348
349 /* return 1 if AD is performing a conversion, 0 if done */
350 char adc_busy(void) __naked;
351
352 /* get value of conversion */
353 int adc_read(void) __naked;
354
355 /* setup conversion channel */
356 void adc_setchannel(unsigned char channel);
357
358 #endif
359