3 * A/D conversion module library header
5 * written by Vangelis Rokas, 2004 <vrokas AT otenet.gr>
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.
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.
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.
32 /* link I/O libarary */
35 /* interrupt on/off flag */
36 #define ADC_INT_OFF 0x00
37 #define ADC_INT_ON 0x01
41 #define ADC_FRM_RJUST 0x80
42 #define ADC_FRM_LJUST 0x00
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
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
62 * Distinguish between 18f242-style, 18f1220-style, and 18f2220-style ADC:
65 * bit 18f242 18f1220 18f2220
73 * 7 ADCS1 VCFG1 (ADCAL)
76 * bit 18f242 18f1220 18f2220
88 #if defined(pic18f242) || defined(pic18f252) || defined(pic18f442) || defined(pic18f452) \
89 || defined(pic18f248) || defined(pic18f258) || defined(pic18f448) || defined(pic18f458)
91 #define __SDCC_ADC_STYLE242 1
94 #elif defined(pic18f1220) || defined(pic18f1320)
96 #define __SDCC_ADC_STYLE1220 1
98 /* 18f2220-style, ordered by device family */
99 #elif defined(pic18f2220) || defined(pic18f2320) || defined(pic18f4220) || defined(pic18f4320) \
100 || defined(pic18f2221) || defined(pic18f2321) || defined(pic18f4221) || defined(pic18f4321) \
101 || defined(pic18f2410) || defined(pic18f2510) || defined(pic18f4410) || defined(pic18f4510) \
102 || defined(pic18f2420) || defined(pic18f2520) || defined(pic18f4420) || defined(pic18f4520) \
103 || defined(pic18f2423) || defined(pic18f2523) || defined(pic18f4423) || defined(pic18f4523) \
104 || defined(pic18f2450) || defined(pic18f4450) \
105 || defined(pic18f2455) || defined(pic18f2550) || defined(pic18f4455) || defined(pic18f4550) \
106 || defined(pic18f2480) || defined(pic18f2580) || defined(pic18f4480) || defined(pic18f4580) \
107 || defined(pic18f24j10) || defined(pic18f25j10) || defined(pic18f44j10) || defined(pic18f45j10) \
108 || defined(pic18f2515) || defined(pic18f2610) || defined(pic18f4515) || defined(pic18f4610) \
109 || defined(pic18f2525) || defined(pic18f2620) || defined(pic18f4525) || defined(pic18f4620) \
110 || defined(pic18f2585) || defined(pic18f2680) || defined(pic18f4585) || defined(pic18f4680) \
111 || defined(pic18f2682) || defined(pic18f2685) || defined(pic18f4682) || defined(pic18f4685) \
112 || defined(pic18f6520) || defined(pic18f6620) || defined(pic18f6720) \
113 || defined(pic18f6585) || defined(pic18f6680) || defined(pic18f8585) || defined(pic18f8680) \
114 || defined(pic18f66j60) || defined(pic18f66j65) || defined(pic18f67j60) \
115 || defined(pic18f8520) || defined(pic18f8620) || defined(pic18f8720) \
116 || defined(pic18f86j60) || defined(pic18f86j65) || defined(pic18f87j60) \
117 || defined(pic18f96j60) || defined(pic18f96j65) || defined(pic18f97j60) \
119 #define __SDCC_ADC_STYLE2220 1
121 #else /* unknown device */
123 #error Device ADC style is unknown, please update your adc.h manually and/or inform the maintainer!
128 /* channel selection (CHS field in ADCON0) */
129 #define ADC_CHN_0 0x00
130 #define ADC_CHN_1 0x01
131 #define ADC_CHN_2 0x02
132 #define ADC_CHN_3 0x03
133 #define ADC_CHN_4 0x04
134 #define ADC_CHN_5 0x05
135 #define ADC_CHN_6 0x06
136 #define ADC_CHN_7 0x07
137 #define ADC_CHN_8 0x08
138 #define ADC_CHN_9 0x09
139 #define ADC_CHN_10 0x0a
140 #define ADC_CHN_11 0x0b
141 #define ADC_CHN_12 0x0c
142 #define ADC_CHN_13 0x0d
143 #define ADC_CHN_14 0x0e
144 #define ADC_CHN_15 0x0f
147 /* Port configuration (PCFG (and VCFG) field(s) in ADCON1) */
148 #if defined(__SDCC_ADC_STYLE242)
150 #define ADC_CFG_8A_0R 0x00
151 #define ADC_CFG_7A_1R 0x01
152 #define ADC_CFG_5A_0R 0x02
153 #define ADC_CFG_4A_1R 0x03
154 #define ADC_CFG_3A_0R 0x04
155 #define ADC_CFG_2A_1R 0x05
156 #define ADC_CFG_0A_0R 0x06
157 #define ADC_CFG_6A_2R 0x08
158 #define ADC_CFG_6A_0R 0x09
159 #define ADC_CFG_5A_1R 0x0a
160 #define ADC_CFG_4A_2R 0x0b
161 #define ADC_CFG_3A_2R 0x0c
162 #define ADC_CFG_2A_2R 0x0d
163 #define ADC_CFG_1A_0R 0x0e
164 #define ADC_CFG_1A_2R 0x0f
166 #elif defined(__SDCC_ADC_STYLE1220)
169 * These devices use a bitmask in ADCON1 to configure AN0..AN6
170 * as digital ports (bit set) or analog input (bit clear).
172 * These settings are selected based on their similarity with
173 * the 2220-style settings; 1220-style is more flexible, though.
175 * Reference voltages are configured via adc_open's config parameter
179 #define ADC_CFG_6A 0x00
180 #define ADC_CFG_5A 0x20
181 #define ADC_CFG_4A 0x30
182 #define ADC_CFG_3A 0x38
183 #define ADC_CFG_2A 0x3c
184 #define ADC_CFG_1A 0x3e
185 #define ADC_CFG_0A 0x3f
187 #elif defined(__SDCC_ADC_STYLE2220)
190 * The reference voltage configuration should be factored out into
191 * the config argument (ADC_VCFG_*) to adc_open to facilitate a
192 * merger with the 1220-style ADC.
195 #define ADC_CFG_16A 0x00
196 /* 15 analog ports cannot be configured! */
197 #define ADC_CFG_14A 0x01
198 #define ADC_CFG_13A 0x02
199 #define ADC_CFG_12A 0x03
200 #define ADC_CFG_11A 0x04
201 #define ADC_CFG_10A 0x05
202 #define ADC_CFG_9A 0x06
203 #define ADC_CFG_8A 0x07
204 #define ADC_CFG_7A 0x08
205 #define ADC_CFG_6A 0x09
206 #define ADC_CFG_5A 0x0a
207 #define ADC_CFG_4A 0x0b
208 #define ADC_CFG_3A 0x0c
209 #define ADC_CFG_2A 0x0d
210 #define ADC_CFG_1A 0x0e
211 #define ADC_CFG_0A 0x0f
214 * For compatibility only: Combined port and reference voltage selection.
215 * Consider using ADC_CFG_nA and a separate ADC_VCFG_* instead!
218 #define ADC_CFG_16A_0R 0x00
219 #define ADC_CFG_16A_1R 0x10
220 #define ADC_CFG_16A_2R 0x30
222 /* Can only select 14 or 16 analog ports ... */
223 #define ADC_CFG_15A_0R 0x00
224 #define ADC_CFG_15A_1R 0x10
225 #define ADC_CFG_15A_2R 0x30
227 #define ADC_CFG_14A_0R 0x01
228 #define ADC_CFG_14A_1R 0x11
229 #define ADC_CFG_14A_2R 0x31
230 #define ADC_CFG_13A_0R 0x02
231 #define ADC_CFG_13A_1R 0x12
232 #define ADC_CFG_13A_2R 0x32
233 #define ADC_CFG_12A_0R 0x03
234 #define ADC_CFG_12A_1R 0x13
235 #define ADC_CFG_12A_2R 0x33
236 #define ADC_CFG_11A_0R 0x04
237 #define ADC_CFG_11A_1R 0x14
238 #define ADC_CFG_11A_2R 0x34
239 #define ADC_CFG_10A_0R 0x05
240 #define ADC_CFG_10A_1R 0x15
241 #define ADC_CFG_10A_2R 0x35
242 #define ADC_CFG_09A_0R 0x06
243 #define ADC_CFG_09A_1R 0x16
244 #define ADC_CFG_09A_2R 0x36
245 #define ADC_CFG_08A_0R 0x07
246 #define ADC_CFG_08A_1R 0x17
247 #define ADC_CFG_08A_2R 0x37
248 #define ADC_CFG_07A_0R 0x08
249 #define ADC_CFG_07A_1R 0x18
250 #define ADC_CFG_07A_2R 0x38
251 #define ADC_CFG_06A_0R 0x09
252 #define ADC_CFG_06A_1R 0x19
253 #define ADC_CFG_06A_2R 0x39
254 #define ADC_CFG_05A_0R 0x0a
255 #define ADC_CFG_05A_1R 0x1a
256 #define ADC_CFG_05A_2R 0x3a
257 #define ADC_CFG_04A_0R 0x0b
258 #define ADC_CFG_04A_1R 0x1b
259 #define ADC_CFG_04A_2R 0x3b
260 #define ADC_CFG_03A_0R 0x0c
261 #define ADC_CFG_03A_1R 0x1c
262 #define ADC_CFG_03A_2R 0x3c
263 #define ADC_CFG_02A_0R 0x0d
264 #define ADC_CFG_02A_1R 0x1d
265 #define ADC_CFG_02A_2R 0x3d
266 #define ADC_CFG_01A_0R 0x0e
267 #define ADC_CFG_01A_1R 0x1e
268 #define ADC_CFG_01A_2R 0x3e
269 #define ADC_CFG_00A_0R 0x0f
271 #else /* unhandled ADC style */
273 #error No supported ADC style selected.
275 #endif /* ADC_STYLE */
279 /* initialize AD module */
280 void adc_open(unsigned char channel, unsigned char fosc, unsigned char pcfg, unsigned char config);
282 /* shutdown AD module */
283 void adc_close(void);
285 /* begin a conversion */
288 /* return 1 if AD is performing a conversion, 0 if done */
289 char adc_busy(void) __naked;
291 /* get value of conversion */
292 int adc_read(void) __naked;
294 /* setup conversion channel */
295 void adc_setchannel(unsigned char channel);