Large cummulative patch for pic16 port.
[fw/sdcc] / device / lib / pic16 / libio / adc / adcopen.c
1
2 #include <pic18fregs.h>
3
4 #include <adc.h>
5
6
7 /* parameters are:
8  *   channel: one of ADC_CHN_*
9  *   fosc:    one of ADC_FOSC_*
10  *   pcfg:    one of ADC_CFG_*
11  *   config:  ADC_FRM_*  |  ADC_INT_*
12  */
13
14 void adc_open(unsigned char channel, unsigned char fosc, unsigned char pcfg, unsigned char config)
15 {
16         ADCON0 = 0;
17         ADCON1 = 0;
18
19         /* setup channel */
20         ADCON0 |= (channel & 0x07) << 3;
21
22         /* setup fosc */
23         ADCON0 |= (fosc & 0x03) << 6;
24         ADCON1 |= (fosc & 0x04) << 4;
25         
26         /* setup reference and pins */
27         ADCON1 |= pcfg & 0x0f;
28         
29         ADCON0 |= (config & ADC_FRM_RJUST);
30         
31         if(config & ADC_INT_ON) {
32                 PIR1bits.ADIF = 0;
33                 PIE1bits.ADIE = 1;
34                 INTCONbits.PEIE = 1;
35         }
36         
37         /* enable the A/D module */
38         ADCON0bits.ADON = 1;
39 }