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