adc.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*-------------------------------------------------------------------------
  2. adc.c - A/D conversion module library header
  3. Copyright (C) 2004, Vangelis Rokas <vrokas AT otenet.gr>
  4. This library is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this library; see the file COPYING. If not, write to the
  14. Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
  15. MA 02110-1301, USA.
  16. As a special exception, if you link this library with other files,
  17. some of which are compiled with SDCC, to produce an executable,
  18. this library does not by itself cause the resulting executable to
  19. be covered by the GNU General Public License. This exception does
  20. not however invalidate any other reasons why the executable file
  21. might be covered by the GNU General Public License.
  22. -------------------------------------------------------------------------*/
  23. /*
  24. * Devices implemented:
  25. * PIC18F[24][45][28]
  26. * PIC18F2455-style
  27. */
  28. #ifndef __ADC_H__
  29. #define __ADC_H__
  30. /* link I/O libarary */
  31. #pragma library io
  32. /*
  33. * adc_open's `channel' argument:
  34. *
  35. * one of ADC_CHN_*
  36. */
  37. /* channel selection (CHS field in ADCON0) */
  38. #define ADC_CHN_0 0x00
  39. #define ADC_CHN_1 0x01
  40. #define ADC_CHN_2 0x02
  41. #define ADC_CHN_3 0x03
  42. #define ADC_CHN_4 0x04
  43. #define ADC_CHN_5 0x05
  44. #define ADC_CHN_6 0x06
  45. #define ADC_CHN_7 0x07
  46. #define ADC_CHN_8 0x08
  47. #define ADC_CHN_9 0x09
  48. #define ADC_CHN_10 0x0a
  49. #define ADC_CHN_11 0x0b
  50. #define ADC_CHN_12 0x0c
  51. #define ADC_CHN_13 0x0d
  52. #define ADC_CHN_14 0x0e
  53. #define ADC_CHN_DAC 0x0e /* 13k50-style */
  54. #define ADC_CHN_15 0x0f
  55. #define ADC_CHN_FVR 0x0f /* 13k50-style */
  56. /* more channels: 23k22-style */
  57. #define ADC_CHN_16 0x10
  58. #define ADC_CHN_17 0x11
  59. #define ADC_CHN_18 0x12
  60. #define ADC_CHN_19 0x13
  61. #define ADC_CHN_20 0x14
  62. #define ADC_CHN_21 0x15
  63. #define ADC_CHN_22 0x16
  64. #define ADC_CHN_23 0x17
  65. #define ADC_CHN_24 0x18
  66. #define ADC_CHN_25 0x19
  67. #define ADC_CHN_26 0x1a
  68. #define ADC_CHN_27 0x1b
  69. #define ADC_CHN_K_CTMU 0x1d
  70. #define ADC_CHN_K_DAC 0x1e
  71. #define ADC_CHN_K_FVR 0x1f
  72. /*
  73. * adc_open's `fosc' argument:
  74. *
  75. * ADC_FOSC_* | ADC_ACQT_* | ADC_CAL | ADC_TRIGSEL_*
  76. *
  77. * 7 6 5 4 3 2 1 0
  78. * +-----+-----+-----+-----+-----+-----+-----+-----+
  79. * | TRG | CAL | ACQT | FOSC/ADCS |
  80. * +-----+-----+-----+-----+-----+-----+-----+-----+
  81. */
  82. /* oscillator frequency (ADCS field) */
  83. #define ADC_FOSC_2 0x00
  84. #define ADC_FOSC_4 0x04
  85. #define ADC_FOSC_8 0x01
  86. #define ADC_FOSC_16 0x05
  87. #define ADC_FOSC_32 0x02
  88. #define ADC_FOSC_64 0x06
  89. #define ADC_FOSC_RC 0x07
  90. /* acquisition time (13k50/2220/24j50/65j50-styles only) */
  91. #define ADC_ACQT_0 (0x00 << 3)
  92. #define ADC_ACQT_2 (0x01 << 3)
  93. #define ADC_ACQT_4 (0x02 << 3)
  94. #define ADC_ACQT_6 (0x03 << 3)
  95. #define ADC_ACQT_8 (0x04 << 3)
  96. #define ADC_ACQT_12 (0x05 << 3)
  97. #define ADC_ACQT_16 (0x06 << 3)
  98. #define ADC_ACQT_20 (0x07 << 3)
  99. /* calibration enable (24j50/65j50-style only) */
  100. #define ADC_CAL 0x40
  101. /* trigger selection (23k22-style only) */
  102. #define ADC_TRIGGER 0x80
  103. /*
  104. * adc_open's `pcfg' argment:
  105. *
  106. * ADC_CFG_* (see below, style-specific)
  107. */
  108. /*
  109. * adc_open's `config' argument:
  110. *
  111. * ADC_FRM_* | ADC_INT_* | ADC_VCFG_* | ADC_NVCFG_* | ADC_PVCFG_*
  112. *
  113. * 7 6 5 4 3 2 1 0
  114. * +-----+-----+-----+-----+-----+-----+-----+-----+
  115. * | FRM | INT | VCFG | PVCFG | NVCFG |
  116. * +-----+-----+-----+-----+-----+-----+-----+-----+
  117. */
  118. /* output format */
  119. #define ADC_FRM_LJUST 0x00
  120. #define ADC_FRM_RJUST 0x80
  121. /* interrupt on/off flag */
  122. #define ADC_INT_OFF 0x00
  123. #define ADC_INT_ON 0x40
  124. /* reference voltage configuration (not for 18f242-style ADC) */
  125. #define ADC_VCFG_VDD_VSS 0x00
  126. #define ADC_VCFG_AN3_VSS 0x10
  127. #define ADC_VCFG_VDD_AN2 0x20
  128. #define ADC_VCFG_AN3_AN2 0x30
  129. /* reference voltage configuration (13k50-style) */
  130. #define ADC_NVCFG_VSS 0x00
  131. #define ADC_NVCFG_AN5 0x01
  132. #define ADC_PVCFG_VDD (0x00 << 2)
  133. #define ADC_PVCFG_AN4 (0x01 << 2)
  134. #define ADC_PVCFG_FVR (0x02 << 2)
  135. /* reference voltage configuration (23k22-style) */
  136. #define ADC_NVCFG_AN2 0x01
  137. #define ADC_PVCFG_AN3 (0x01 << 2)
  138. #define ADC_TRIGSEL_CCP5 (0x00 << 7)
  139. #define ADC_TRIGSEL_CTMU (0x01 << 7)
  140. /*
  141. * Distinguishing between ADC-styles:
  142. * - 18f24j50-style devices have separate ANCON0/ANCON1
  143. * registers for A/D port pin configuration, whereas
  144. * 18f65j50-style devices multiplex ANCONx and ADCONx
  145. *
  146. * ADCON0:
  147. * bit 18f242 18f1220 18f1230 18f13k50 18f2220 18f24j50 18f65j50 18f23k22
  148. * 0 ADON ADON ADON ADON ADON ADON ADON ADON
  149. * 1 - GO GO GO GO GO GO GO
  150. * 2 GO CHS0 CHS0 CHS0 CHS0 CHS0 CHS0 CHS0
  151. * 3 CHS0 CHS1 CHS1 CHS1 CHS1 CHS1 CHS1 CHS1
  152. * 4 CHS1 CHS2 - CHS2 CHS2 CHS2 CHS2 CHS2
  153. * 5 CHS2 - - CHS3 CHS3 CHS3 CHS3 CHS3
  154. * 6 ADCS0 VCFG0 - - - VCFG0 VCFG0 CHS4
  155. * 7 ADCS1 VCFG1 SEVTEN - (ADCAL) VCFG1 VCFG1 -
  156. *
  157. * ADCON1:
  158. * bit 18f242 18f1220 18f1230 18f13k50 18f2220 18f24j50 18f65j50 18f23k22
  159. * 0 PCFG0 PCFG0 PCFG0 NVCFG0 PCFG0 ADCS0 ADCS0 NVCFG0
  160. * 1 PCFG1 PCFG1 PCFG1 NVCFG1 PCFG1 ADCS1 ADCS1 NVCFG1
  161. * 2 PCFG2 PCFG2 PCFG2 PVCFG0 PCFG2 ADCS2 ADCS2 PVCFG0
  162. * 3 PCFG3 PCFG3 PCFG3 PVCFG1 PCFG3 ACQT0 ACQT0 PVCFG1
  163. * 4 - PCFG4 VCFG0 - VCFG0 ACQT1 ACQT1 -
  164. * 5 - PCFG5 - VCFG1 ACQT2 ACQT2 -
  165. * 6 ADCS2 PCFG6 - - ADCAL ADCAL -
  166. * 7 ADFM - - - ADFM ADFM TRIGSEL
  167. *
  168. * ADCON2:
  169. * bit 18f242 18f1220 18f1230 18f13k50 18f2220 18f24j50 18f65j50 18f23k22
  170. * 0 ADCS0 ADCS0 ADCS0 ADCS0
  171. * 1 ADCS1 ADCS1 ADCS1 ADCS1
  172. * 2 ADCS2 ADCS2 ADCS2 ADCS2
  173. * 3 ACQT0 ACQT0 ADQT0 ACQT0
  174. * 4 ACQT1 ACQT1 ADQT1 ACQT1
  175. * 5 ACQT2 ACQT2 ADQT2 ACQT2
  176. * 6 - - - -
  177. * 7 ADFM ADFM ADFM ADFM
  178. */
  179. #include "pic18fam.h"
  180. /* Port configuration (PCFG (and VCFG) field(s) in ADCON1) */
  181. #if (__SDCC_ADC_STYLE == 0)
  182. #warning The target device is not supported by the SDCC PIC16 ADC library.
  183. #elif (__SDCC_ADC_STYLE == 1802420)
  184. #define ADC_CFG_8A_0R 0x00
  185. #define ADC_CFG_7A_1R 0x01
  186. #define ADC_CFG_5A_0R 0x02
  187. #define ADC_CFG_4A_1R 0x03
  188. #define ADC_CFG_3A_0R 0x04
  189. #define ADC_CFG_2A_1R 0x05
  190. #define ADC_CFG_0A_0R 0x06
  191. #define ADC_CFG_6A_2R 0x08
  192. #define ADC_CFG_6A_0R 0x09
  193. #define ADC_CFG_5A_1R 0x0a
  194. #define ADC_CFG_4A_2R 0x0b
  195. #define ADC_CFG_3A_2R 0x0c
  196. #define ADC_CFG_2A_2R 0x0d
  197. #define ADC_CFG_1A_0R 0x0e
  198. #define ADC_CFG_1A_2R 0x0f
  199. #elif (__SDCC_ADC_STYLE == 1812200)
  200. /*
  201. * These devices use a bitmask in ADCON1 to configure AN0..AN6
  202. * as digital ports (bit set) or analog input (bit clear).
  203. *
  204. * These settings are selected based on their similarity with
  205. * the 2220-style settings; 1220-style is more flexible, though.
  206. *
  207. * Reference voltages are configured via adc_open's config parameter
  208. * using ADC_VCFG_*.
  209. */
  210. #define ADC_CFG_6A 0x00
  211. #define ADC_CFG_5A 0x20
  212. #define ADC_CFG_4A 0x30
  213. #define ADC_CFG_3A 0x38
  214. #define ADC_CFG_2A 0x3c
  215. #define ADC_CFG_1A 0x3e
  216. #define ADC_CFG_0A 0x3f
  217. #elif (__SDCC_ADC_STYLE == 1812300)
  218. /*
  219. * These devices use a bitmask in ADCON1 to configure AN0..AN3
  220. * as digital ports (bit set) or analog input (bit clear).
  221. *
  222. * These settings are selected based on their similarity with
  223. * the 2220-style settings; 1230-style is more flexible, though.
  224. *
  225. * Reference voltages are configured via adc_open's config parameter
  226. * using ADC_VCFG_*.
  227. */
  228. #define ADC_CFG_4A 0x00
  229. #define ADC_CFG_3A 0x08
  230. #define ADC_CFG_2A 0x0c
  231. #define ADC_CFG_1A 0x0e
  232. #define ADC_CFG_0A 0x0f
  233. #define ADC_VCFG_AVDD 0x00 /* AVdd */
  234. #define ADC_VCFG_VREF 0x10 /* Vref+ */
  235. #elif (__SDCC_ADC_STYLE == 1813502)
  236. /*
  237. * These devices use a bitmask in ANSEL/H to configure
  238. * AN7..0/AN15..8 as digital ports (bit clear) or analog
  239. * inputs (bit set).
  240. *
  241. * These settings are selected based on their similarity with
  242. * the 2220-style settings; 13k50-style is more flexible, though.
  243. *
  244. * Reference voltages are configured via adc_open's config parameter
  245. * using ADC_PVCFG_* and ADC_NVCFG_*.
  246. */
  247. #define ADC_CFG_16A 0xFFFF
  248. #define ADC_CFG_15A 0x7FFF
  249. #define ADC_CFG_14A 0x3FFF
  250. #define ADC_CFG_13A 0x1FFF
  251. #define ADC_CFG_12A 0x0FFF
  252. #define ADC_CFG_11A 0x07FF
  253. #define ADC_CFG_10A 0x03FF
  254. #define ADC_CFG_9A 0x01FF
  255. #define ADC_CFG_8A 0x00FF
  256. #define ADC_CFG_7A 0x007F
  257. #define ADC_CFG_6A 0x003F
  258. #define ADC_CFG_5A 0x001F
  259. #define ADC_CFG_4A 0x000F
  260. #define ADC_CFG_3A 0x0007
  261. #define ADC_CFG_2A 0x0003
  262. #define ADC_CFG_1A 0x0001
  263. #define ADC_CFG_0A 0x0000
  264. #elif (__SDCC_ADC_STYLE == 1822200)
  265. /*
  266. * The reference voltage configuration should be factored out into
  267. * the config argument (ADC_VCFG_*) to adc_open to facilitate a
  268. * merger with the 1220-style ADC.
  269. */
  270. #define ADC_CFG_16A 0x00
  271. /* 15 analog ports cannot be configured! */
  272. #define ADC_CFG_14A 0x01
  273. #define ADC_CFG_13A 0x02
  274. #define ADC_CFG_12A 0x03
  275. #define ADC_CFG_11A 0x04
  276. #define ADC_CFG_10A 0x05
  277. #define ADC_CFG_9A 0x06
  278. #define ADC_CFG_8A 0x07
  279. #define ADC_CFG_7A 0x08
  280. #define ADC_CFG_6A 0x09
  281. #define ADC_CFG_5A 0x0a
  282. #define ADC_CFG_4A 0x0b
  283. #define ADC_CFG_3A 0x0c
  284. #define ADC_CFG_2A 0x0d
  285. #define ADC_CFG_1A 0x0e
  286. #define ADC_CFG_0A 0x0f
  287. /*
  288. * For compatibility only: Combined port and reference voltage selection.
  289. * Consider using ADC_CFG_nA and a separate ADC_VCFG_* instead!
  290. */
  291. #define ADC_CFG_16A_0R 0x00
  292. #define ADC_CFG_16A_1R 0x10
  293. #define ADC_CFG_16A_2R 0x30
  294. /* Can only select 14 or 16 analog ports ... */
  295. #define ADC_CFG_15A_0R 0x00
  296. #define ADC_CFG_15A_1R 0x10
  297. #define ADC_CFG_15A_2R 0x30
  298. #define ADC_CFG_14A_0R 0x01
  299. #define ADC_CFG_14A_1R 0x11
  300. #define ADC_CFG_14A_2R 0x31
  301. #define ADC_CFG_13A_0R 0x02
  302. #define ADC_CFG_13A_1R 0x12
  303. #define ADC_CFG_13A_2R 0x32
  304. #define ADC_CFG_12A_0R 0x03
  305. #define ADC_CFG_12A_1R 0x13
  306. #define ADC_CFG_12A_2R 0x33
  307. #define ADC_CFG_11A_0R 0x04
  308. #define ADC_CFG_11A_1R 0x14
  309. #define ADC_CFG_11A_2R 0x34
  310. #define ADC_CFG_10A_0R 0x05
  311. #define ADC_CFG_10A_1R 0x15
  312. #define ADC_CFG_10A_2R 0x35
  313. #define ADC_CFG_09A_0R 0x06
  314. #define ADC_CFG_09A_1R 0x16
  315. #define ADC_CFG_09A_2R 0x36
  316. #define ADC_CFG_08A_0R 0x07
  317. #define ADC_CFG_08A_1R 0x17
  318. #define ADC_CFG_08A_2R 0x37
  319. #define ADC_CFG_07A_0R 0x08
  320. #define ADC_CFG_07A_1R 0x18
  321. #define ADC_CFG_07A_2R 0x38
  322. #define ADC_CFG_06A_0R 0x09
  323. #define ADC_CFG_06A_1R 0x19
  324. #define ADC_CFG_06A_2R 0x39
  325. #define ADC_CFG_05A_0R 0x0a
  326. #define ADC_CFG_05A_1R 0x1a
  327. #define ADC_CFG_05A_2R 0x3a
  328. #define ADC_CFG_04A_0R 0x0b
  329. #define ADC_CFG_04A_1R 0x1b
  330. #define ADC_CFG_04A_2R 0x3b
  331. #define ADC_CFG_03A_0R 0x0c
  332. #define ADC_CFG_03A_1R 0x1c
  333. #define ADC_CFG_03A_2R 0x3c
  334. #define ADC_CFG_02A_0R 0x0d
  335. #define ADC_CFG_02A_1R 0x1d
  336. #define ADC_CFG_02A_2R 0x3d
  337. #define ADC_CFG_01A_0R 0x0e
  338. #define ADC_CFG_01A_1R 0x1e
  339. #define ADC_CFG_01A_2R 0x3e
  340. #define ADC_CFG_00A_0R 0x0f
  341. #elif (__SDCC_ADC_STYLE == 1823222)
  342. /* use ANSELA, ANSELB, ANSELC, ANSELD, ANSELE registers and
  343. * TRISA, TRISB, TRISC, TRISD, TRISE registers to set
  344. * corresponding port to analog mode
  345. * Note: 46k22 supports up to 28 ADC ports */
  346. #elif (__SDCC_ADC_STYLE == 1824501) || (__SDCC_ADC_STYLE == 1865501)
  347. /*
  348. * These devices use a bitmask in ANCON0/1 to configure
  349. * AN7..0/AN15..8 as digital ports (bit set) or analog
  350. * inputs (bit clear).
  351. *
  352. * These settings are selected based on their similarity with
  353. * the 2220-style settings; 24j50/65j50-style is more flexible, though.
  354. *
  355. * Reference voltages are configured via adc_open's config parameter
  356. * using ADC_VCFG_*.
  357. */
  358. #define ADC_CFG_16A 0x0000
  359. #define ADC_CFG_15A 0x8000
  360. #define ADC_CFG_14A 0xC000
  361. #define ADC_CFG_13A 0xE000
  362. #define ADC_CFG_12A 0xF000
  363. #define ADC_CFG_11A 0xF800
  364. #define ADC_CFG_10A 0xFC00
  365. #define ADC_CFG_9A 0xFE00
  366. #define ADC_CFG_8A 0xFF00
  367. #define ADC_CFG_7A 0xFF80
  368. #define ADC_CFG_6A 0xFFC0
  369. #define ADC_CFG_5A 0xFFE0
  370. #define ADC_CFG_4A 0xFFF0
  371. #define ADC_CFG_3A 0xFFF8
  372. #define ADC_CFG_2A 0xFFFC
  373. #define ADC_CFG_1A 0xFFFE
  374. #define ADC_CFG_0A 0xFFFF
  375. #else /* unhandled ADC style */
  376. #error No supported ADC style selected.
  377. #endif /* __SDCC_ADC_STYLE */
  378. #if (__SDCC_ADC_STYLE == 1813502) \
  379. || (__SDCC_ADC_STYLE == 1824501) \
  380. || (__SDCC_ADC_STYLE == 1865501)
  381. typedef unsigned int sdcc_pcfg_t;
  382. #else /* other styles */
  383. typedef unsigned char sdcc_pcfg_t;
  384. #endif
  385. /* initialize AD module */
  386. void adc_open (unsigned char channel, unsigned char fosc, sdcc_pcfg_t pcfg, unsigned char config);
  387. /* shutdown AD module */
  388. void adc_close (void);
  389. /* begin a conversion */
  390. void adc_conv (void);
  391. /* return 1 if AD is performing a conversion, 0 if done */
  392. char adc_busy (void) __naked;
  393. /* get value of conversion */
  394. int adc_read (void) __naked;
  395. /* setup conversion channel */
  396. void adc_setchannel (unsigned char channel);
  397. #endif