* device/include/pic16/adc.h,
[fw/sdcc] / device / lib / pic16 / libio / adc / adcopen.c
1
2 /*
3  * adcopen - initialize AD module
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 #include <pic18fregs.h>
30 #include <adc.h>
31
32
33 /* parameters are:
34  *   channel: one of ADC_CHN_*
35  *   fosc:    one of ADC_FOSC_*
36  *   pcfg:    one of ADC_CFG_*
37  *   config:  ADC_FRM_*  |  ADC_INT_*
38  */
39
40 void adc_open(unsigned char channel, unsigned char fosc, unsigned char pcfg, unsigned char config)
41 {
42   ADCON0 = 0;
43   ADCON1 = 0;
44
45   /* setup channel */
46 #if defined(__SDCC_ADC_STYLE2455)
47   ADCON0 |= (channel & 0x07) << 2;
48 #else /* all other devices */
49   ADCON0 |= (channel & 0x07) << 3;
50 #endif
51
52   /* setup fosc */
53 #if defined(__SDCC_ADC_STYLE2455)
54   ADCON2 |= (fosc & 0x03);
55 #else /* all other devices */
56   ADCON0 |= (fosc & 0x03) << 6;
57   ADCON1 |= (fosc & 0x04) << 4;
58 #endif
59
60   /* setup reference and pins */
61 #if defined(__SDCC_ADC_STYLE2455)
62   ADCON1 |= pcfg & 0x3f;
63 #else /* all other devices */
64   ADCON1 |= pcfg & 0x0f;
65 #endif
66
67 #if defined(__SDCC_ADC_STYLE2455)
68   ADCON2 |= (config & ADC_FRM_RJUST);
69 #else /* all other devices */
70   ADCON1 |= (config & ADC_FRM_RJUST);
71 #endif
72
73   if (config & ADC_INT_ON) {
74     PIR1bits.ADIF = 0;
75     PIE1bits.ADIE = 1;
76     INTCONbits.PEIE = 1;
77   }
78
79   /* enable the A/D module */
80   ADCON0bits.ADON = 1;
81 }
82