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 /* 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
55 /* distinguish between 18f242-style and 18f2455-style ADC */
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) \
73 #define __SDCC_ADC_STYLE2455 1
76 #elif defined(pic18f242) || defined(pic18f252) || defined(pic18f442) || defined(pic18f452) \
77 || defined(pic18f248) || defined(pic18f258) || defined(pic18f448) || defined(pic18f458)
79 #define __SDCC_ADC_STYLE242 1
81 #else // unknown device
83 #error Device ADC style is unknown, please update your adc.h manually and/or inform the maintainer!
85 #endif // !large ADC device
88 /* channel selection */
89 #if defined(__SDCC_ADC_STYLE2455)
91 #define ADC_CHN_0 0x00
92 #define ADC_CHN_1 0x01
93 #define ADC_CHN_2 0x02
94 #define ADC_CHN_3 0x03
95 #define ADC_CHN_4 0x04
96 #define ADC_CHN_5 0x05
97 #define ADC_CHN_6 0x06
98 #define ADC_CHN_7 0x07
99 #define ADC_CHN_8 0x08
100 #define ADC_CHN_9 0x09
101 #define ADC_CHN_10 0x0a
102 #define ADC_CHN_11 0x0b
103 #define ADC_CHN_12 0x0c
105 #else /* all other devices */
107 #define ADC_CHN_1 0x00
108 #define ADC_CHN_2 0x01
109 #define ADC_CHN_3 0x03
110 #define ADC_CHN_4 0x04
111 #define ADC_CHN_5 0x05
112 #define ADC_CHN_6 0x06
113 #define ADC_CHN_7 0x07
118 /* reference and pin configuration */
119 #if defined(__SDCC_ADC_STYLE2455)
121 #define ADC_CFG_13A_0R 0x01
122 #define ADC_CFG_13A_1R 0x11
123 #define ADC_CFG_13A_2R 0x31
124 #define ADC_CFG_12A_0R 0x03
125 #define ADC_CFG_12A_1R 0x13
126 #define ADC_CFG_12A_2R 0x33
127 #define ADC_CFG_11A_0R 0x04
128 #define ADC_CFG_11A_1R 0x14
129 #define ADC_CFG_11A_2R 0x34
130 #define ADC_CFG_10A_0R 0x05
131 #define ADC_CFG_10A_1R 0x15
132 #define ADC_CFG_10A_2R 0x35
133 #define ADC_CFG_09A_0R 0x06
134 #define ADC_CFG_09A_1R 0x16
135 #define ADC_CFG_09A_2R 0x36
136 #define ADC_CFG_08A_0R 0x07
137 #define ADC_CFG_08A_1R 0x17
138 #define ADC_CFG_08A_2R 0x37
139 #define ADC_CFG_07A_0R 0x08
140 #define ADC_CFG_07A_1R 0x18
141 #define ADC_CFG_07A_2R 0x38
142 #define ADC_CFG_06A_0R 0x09
143 #define ADC_CFG_06A_1R 0x19
144 #define ADC_CFG_06A_2R 0x39
145 #define ADC_CFG_05A_0R 0x0a
146 #define ADC_CFG_05A_1R 0x1a
147 #define ADC_CFG_05A_2R 0x3a
148 #define ADC_CFG_04A_0R 0x0b
149 #define ADC_CFG_04A_1R 0x1b
150 #define ADC_CFG_04A_2R 0x3b
151 #define ADC_CFG_03A_0R 0x0c
152 #define ADC_CFG_03A_1R 0x1c
153 #define ADC_CFG_03A_2R 0x3c
154 #define ADC_CFG_02A_0R 0x0d
155 #define ADC_CFG_02A_1R 0x1d
156 #define ADC_CFG_02A_2R 0x3d
157 #define ADC_CFG_01A_0R 0x0e
158 #define ADC_CFG_01A_1R 0x1e
159 #define ADC_CFG_01A_2R 0x3e
160 #define ADC_CFG_00A_0R 0x0f
162 #else /* all other devices */
164 #define ADC_CFG_8A_0R 0x00
165 #define ADC_CFG_7A_1R 0x01
166 #define ADC_CFG_5A_0R 0x02
167 #define ADC_CFG_4A_1R 0x03
168 #define ADC_CFG_3A_0R 0x04
169 #define ADC_CFG_2A_1R 0x05
170 #define ADC_CFG_0A_0R 0x06
171 #define ADC_CFG_6A_2R 0x08
172 #define ADC_CFG_6A_0R 0x09
173 #define ADC_CFG_5A_1R 0x0a
174 #define ADC_CFG_4A_2R 0x0b
175 #define ADC_CFG_3A_2R 0x0c
176 #define ADC_CFG_2A_2R 0x0d
177 #define ADC_CFG_1A_0R 0x0e
178 #define ADC_CFG_1A_2R 0x0f
182 /* initialize AD module */
183 void adc_open(unsigned char channel, unsigned char fosc, unsigned char pcfg, unsigned char config);
185 /* shutdown AD module */
186 void adc_close(void);
188 /* begin a conversion */
191 /* return 1 if AD is performing a conversion, 0 if done */
192 char adc_busy(void) __naked;
194 /* get value of conversion */
195 int adc_read(void) __naked;
197 /* setup conversion channel */
198 void adc_setchannel(unsigned char channel) __naked;