struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / device / lib / pic16 / libio / adc / adcopen.c
blob88c038539ae57bdab0001626c04fb5aa9ac70b78
1 /*-------------------------------------------------------------------------
2 adcopen - initialize AD module
4 Copyright (C) 2004, Vangelis Rokas <vrokas AT otenet.gr>
6 This library is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this library; see the file COPYING. If not, write to the
18 Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
19 MA 02110-1301, USA.
21 As a special exception, if you link this library with other files,
22 some of which are compiled with SDCC, to produce an executable,
23 this library does not by itself cause the resulting executable to
24 be covered by the GNU General Public License. This exception does
25 not however invalidate any other reasons why the executable file
26 might be covered by the GNU General Public License.
27 -------------------------------------------------------------------------*/
29 #include <pic18fregs.h>
30 #include <adc.h>
34 * parameters are:
35 * channel: one of ADC_CHN_*
36 * fosc: one of ADC_FOSC_* | ADC_ACQT_* | ADC_CAL
37 * pcfg: one of ADC_CFG_* (a bitmask with set bits denoting digital ports for many styles)
38 * config: ADC_FRM_* | ADC_INT_* | ADC_VCFG_* | ADC_NVCFG_* | ADC_PVCFG_*
41 void
42 adc_open(unsigned char channel, unsigned char fosc, sdcc_pcfg_t pcfg, unsigned char config)
44 /* disable ADC */
45 #if (__SDCC_ADC_STYLE == 1865501)
46 WDTCONbits.ADSHR = 0; /* access ADCON0/1 */
47 #endif
48 ADCON0 = 0;
50 #if (__SDCC_ADC_STYLE == 1802420)
51 ADCON0 = ((channel & 0x07) << 3) | ((fosc & 0x03) << 6);
52 ADCON1 = (pcfg & 0x0f) | (config & ADC_FRM_RJUST);
53 if (fosc & 0x04) {
54 ADCON1bits.ADCS2 = 1;
56 #elif (__SDCC_ADC_STYLE == 1812200)
57 ADCON0 = ((channel & 0x07) | (config & ADC_VCFG_AN3_AN2)) << 2;
58 ADCON1 = (pcfg & 0x7f);
59 ADCON2 = (ADCON2 & 0x38) | (fosc & 0x07) | (config & ADC_FRM_RJUST);
60 #elif (__SDCC_ADC_STYLE == 1812300)
61 ADCON0 = ((channel & 0x03) << 2);
62 ADCON1 = (pcfg & 0x0f) | (config & ADC_VCFG_VREF);
63 ADCON2 = (fosc & 0x7f) | (config & ADC_FRM_RJUST);
64 #elif (__SDCC_ADC_STYLE == 1813502)
65 ANSEL = pcfg;
66 ANSELH = (pcfg >> 8);
67 ADCON0 = ((channel & 0x0f) << 2);
68 ADCON1 = (config & 0x0f);
69 ADCON2 = (config & ADC_FRM_RJUST) | (fosc & 0x3f);
70 #elif (__SDCC_ADC_STYLE == 1823222)
71 /* use ANSELA, ANSELB, ANSELC, ANSELD, ANSELE registers and
72 TRISA, TRISB, TRISC, TRISD, TRISE registers to set
73 corresponding port to analog mode */
74 /* 46k22 supports up to 28 ADC ports */
75 ADCON0 = ((channel & 0x1f) << 2);
76 /* TRIGSEL — — — PVCFG<1:0> NVCFG<1:0> */
77 ADCON1 = (fosc & ADC_TRIGGER) | (config & 0x0f);
78 /* ADFM — ACQT<2:0> ADCS<2:0> */
79 ADCON2 = (config & ADC_FRM_RJUST) | (fosc & 0x3f);
80 (void)pcfg; /* quieten compiler warning */
81 #elif (__SDCC_ADC_STYLE == 1822200)
82 ADCON0 = (channel & 0x0f) << 2;
83 /* XXX: Should be (pcfg & 0x0f) as VCFG comes from config,
84 * but we retain compatibility for now ... */
85 ADCON1 = (pcfg & 0x3f) | (config & ADC_VCFG_AN3_AN2);
86 ADCON2 = (config & ADC_FRM_RJUST) | (fosc & 0x3f);
87 #elif (__SDCC_ADC_STYLE == 1824501)
88 ANCON0 = pcfg;
89 ANCON1 = (pcfg >> 8);
90 ADCON0 = ((channel & 0x0f) << 2) | ((config & ADC_VCFG_AN3_AN2) << 2);
91 ADCON1 = (config & ADC_FRM_RJUST) | (fosc & 0x7f);
92 #elif (__SDCC_ADC_STYLE == 1865501)
93 WDTCONbits.ADSHR = 1; /* access ANCON0/1 */
94 ANCON0 = pcfg;
95 ANCON1 = (pcfg >> 8);
96 WDTCONbits.ADSHR = 0; /* access ADCON0/1 */
97 ADCON0 = ((channel & 0x0f) << 2) | ((config & ADC_VCFG_AN3_AN2) << 2);
98 ADCON1 = (config & ADC_FRM_RJUST) | (fosc & 0x7f);
99 #else /* unsupported ADC style */
100 #error Unsupported ADC style.
101 #endif
103 if (config & ADC_INT_ON) {
104 PIR1bits.ADIF = 0;
105 PIE1bits.ADIE = 1;
106 INTCONbits.PEIE = 1;
109 /* enable the A/D module */
110 ADCON0bits.ADON = 1;