* device/include/pic16/adc.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 /* oscillator frequency */
46 #define ADC_FOSC_2      0x00
47 #define ADC_FOSC_4      0x04
48 #define ADC_FOSC_8      0x01
49 #define ADC_FOSC_16     0x05
50 #define ADC_FOSC_32     0x02
51 #define ADC_FOSC_64     0x06
52 #define ADC_FOSC_RC     0x07
53
54
55 /* distinguish between 18f242-style and 18f2455-style ADC */
56
57 /* ordered by device family */
58 #if    defined(pic18f1220) || defined(pic18f1320) \
59     || defined(pic18f2220) || defined(pic18f2320) || defined(pic18f4220) || defined(pic18f4320) \
60     || defined(pic18f2221) || defined(pic18f2321) || defined(pic18f4221) || defined(pic18f4321) \
61     || defined(pic18f2410) || defined(pic18f2510) || defined(pic18f4410) || defined(pic18f4510) \
62     || defined(pic18f2420) || defined(pic18f2520) || defined(pic18f4420) || defined(pic18f4520) \
63     || defined(pic18f2423) || defined(pic18f2523) || defined(pic18f4423) || defined(pic18f4523) \
64     || defined(pic18f2450) || defined(pic18f4450) \
65     || defined(pic18f2455) || defined(pic18f2550) || defined(pic18f4455) || defined(pic18f4550) \
66     || defined(pic18f2480) || defined(pic18f2580) || defined(pic18f4480) || defined(pic18f4580) \
67     || defined(pic18f24j10) || defined(pic18f25j10) || defined(pic18f44j10) || defined(pic18f45j10) \
68     || defined(pic18f2515) || defined(pic18f2610) || defined(pic18f4515) || defined(pic18f4610) \
69     || defined(pic18f2525) || defined(pic18f2620) || defined(pic18f4525) || defined(pic18f4620) \
70     || defined(pic18f2585) || defined(pic18f2680) || defined(pic18f4585) || defined(pic18f4680) \
71     || defined(pic18f2682) || defined(pic18f2685) || defined(pic18f4682) || defined(pic18f4685) \
72     || defined(pic18f6520) || defined(pic18f6620) || defined(pic18f6720) \
73     || defined(pic18f8520) || defined(pic18f8620) || defined(pic18f8720) \
74     || defined(pic18f6585) || defined(pic18f6680) || defined(pic18f8585) || defined(pic18f8680) \
75
76 #define __SDCC_ADC_STYLE2455    1
77
78 // 97j60 family
79 #elif  defined(pic18f66j60) || defined(pic18f66j65) || defined(pic18f67j60) \
80     || defined(pic18f86j60) || defined(pic18f86j65) || defined(pic18f87j60) \
81     || defined(pic18f96j60) || defined(pic18f96j65) || defined(pic18f97j60) \
82
83 #define __SDCC_ADC_STYLE97J60   1
84
85 // small ADC device?
86 #elif  defined(pic18f242) || defined(pic18f252) || defined(pic18f442) || defined(pic18f452) \
87     || defined(pic18f248) || defined(pic18f258) || defined(pic18f448) || defined(pic18f458)
88
89 #define __SDCC_ADC_STYLE242     1
90
91 #else // unknown device
92
93 #error Device ADC style is unknown, please update your adc.h manually and/or inform the maintainer!
94
95 #endif  // !large ADC device
96
97
98 /* channel selection */
99 #if defined(__SDCC_ADC_STYLE2455) || defined(__SDCC_ADC_STYLE97J60)
100 #define ADC_CHN_0               0x00
101 #define ADC_CHN_1               0x01
102 #define ADC_CHN_2               0x02
103 #define ADC_CHN_3               0x03
104 #define ADC_CHN_4               0x04
105 #define ADC_CHN_5               0x05
106 #define ADC_CHN_6               0x06
107 #define ADC_CHN_7               0x07
108 #define ADC_CHN_8               0x08
109 #define ADC_CHN_9               0x09
110 #define ADC_CHN_10              0x0a
111 #define ADC_CHN_11              0x0b
112 #define ADC_CHN_12              0x0c
113 #if defined(__SDCC_ADC_STYLE97J60) // 97j60 family has 2 more ADC ports
114 #define ADC_CHN_13              0x0d
115 #define ADC_CHN_14              0x0e
116 #define ADC_CHN_15              0x0f
117 #endif
118
119 #else   /* all other devices */
120
121 #define ADC_CHN_1               0x00
122 #define ADC_CHN_2               0x01
123 #define ADC_CHN_3               0x03
124 #define ADC_CHN_4               0x04
125 #define ADC_CHN_5               0x05
126 #define ADC_CHN_6               0x06
127 #define ADC_CHN_7               0x07
128
129 #endif  // ADC_STYLE
130
131
132 /* reference and pin configuration */
133 #if defined(__SDCC_ADC_STYLE2455) || defined(__SDCC_ADC_STYLE97J60)
134
135 // 97j60 family has 2 more possible ADC configs
136 #if defined(__SDCC_ADC_STYLE97J60) // 97j60 family has 2 more ADC ports
137 #define ADC_CFG_16A_0R  0x00
138 #define ADC_CFG_16A_1R  0x10
139 #define ADC_CFG_16A_2R  0x30
140 #define ADC_CFG_15A_0R  0x00    // can switch only from 14 analog ports to 16 enabled analog ports
141 #define ADC_CFG_15A_1R  0x10
142 #define ADC_CFG_15A_2R  0x30
143 #define ADC_CFG_14A_0R  0x01
144 #define ADC_CFG_14A_1R  0x11
145 #define ADC_CFG_14A_2R  0x31
146 #define ADC_CFG_13A_0R  0x02
147 #define ADC_CFG_13A_1R  0x12
148 #define ADC_CFG_13A_2R  0x32
149 #else
150 #define ADC_CFG_13A_0R  0x01
151 #define ADC_CFG_13A_1R  0x11
152 #define ADC_CFG_13A_2R  0x31
153 #endif
154 #define ADC_CFG_12A_0R  0x03
155 #define ADC_CFG_12A_1R  0x13
156 #define ADC_CFG_12A_2R  0x33
157 #define ADC_CFG_11A_0R  0x04
158 #define ADC_CFG_11A_1R  0x14
159 #define ADC_CFG_11A_2R  0x34
160 #define ADC_CFG_10A_0R  0x05
161 #define ADC_CFG_10A_1R  0x15
162 #define ADC_CFG_10A_2R  0x35
163 #define ADC_CFG_09A_0R  0x06
164 #define ADC_CFG_09A_1R  0x16
165 #define ADC_CFG_09A_2R  0x36
166 #define ADC_CFG_08A_0R  0x07
167 #define ADC_CFG_08A_1R  0x17
168 #define ADC_CFG_08A_2R  0x37
169 #define ADC_CFG_07A_0R  0x08
170 #define ADC_CFG_07A_1R  0x18
171 #define ADC_CFG_07A_2R  0x38
172 #define ADC_CFG_06A_0R  0x09
173 #define ADC_CFG_06A_1R  0x19
174 #define ADC_CFG_06A_2R  0x39
175 #define ADC_CFG_05A_0R  0x0a
176 #define ADC_CFG_05A_1R  0x1a
177 #define ADC_CFG_05A_2R  0x3a
178 #define ADC_CFG_04A_0R  0x0b
179 #define ADC_CFG_04A_1R  0x1b
180 #define ADC_CFG_04A_2R  0x3b
181 #define ADC_CFG_03A_0R  0x0c
182 #define ADC_CFG_03A_1R  0x1c
183 #define ADC_CFG_03A_2R  0x3c
184 #define ADC_CFG_02A_0R  0x0d
185 #define ADC_CFG_02A_1R  0x1d
186 #define ADC_CFG_02A_2R  0x3d
187 #define ADC_CFG_01A_0R  0x0e
188 #define ADC_CFG_01A_1R  0x1e
189 #define ADC_CFG_01A_2R  0x3e
190 #define ADC_CFG_00A_0R  0x0f
191
192 #else   /* all other devices */
193
194 #define ADC_CFG_8A_0R   0x00
195 #define ADC_CFG_7A_1R   0x01
196 #define ADC_CFG_5A_0R   0x02
197 #define ADC_CFG_4A_1R   0x03
198 #define ADC_CFG_3A_0R   0x04
199 #define ADC_CFG_2A_1R   0x05
200 #define ADC_CFG_0A_0R   0x06
201 #define ADC_CFG_6A_2R   0x08
202 #define ADC_CFG_6A_0R   0x09
203 #define ADC_CFG_5A_1R   0x0a
204 #define ADC_CFG_4A_2R   0x0b
205 #define ADC_CFG_3A_2R   0x0c
206 #define ADC_CFG_2A_2R   0x0d
207 #define ADC_CFG_1A_0R   0x0e
208 #define ADC_CFG_1A_2R   0x0f
209
210 #endif // ADC_STYLE
211
212 /* initialize AD module */
213 void adc_open(unsigned char channel, unsigned char fosc, unsigned char pcfg, unsigned char config);
214
215 /* shutdown AD module */
216 void adc_close(void);
217
218 /* begin a conversion */
219 void adc_conv(void);
220
221 /* return 1 if AD is performing a conversion, 0 if done */
222 char adc_busy(void) __naked;
223
224 /* get value of conversion */
225 int adc_read(void) __naked;
226
227 /* setup conversion channel */
228 void adc_setchannel(unsigned char channel) __naked;
229
230 #endif
231