* device/include/pic16/pic18f25j10.h,
[fw/sdcc] / device / lib / pic16 / libio / adc / adcopen.c
index ab92131dd61f5e438af0933d04041589358df2be..846c8f0b1786dd56bd4802f6b796033a10ec2c31 100644 (file)
@@ -4,11 +4,6 @@
  *
  * written by Vangelis Rokas, 2004 <vrokas AT otenet.gr>
  *
- * Devices implemented:
- *     PIC18F[24][45][28]
- *     PIC18F2455-style
- *
- *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public License
  * as published by the Free Software Foundation; either version 2
@@ -22,8 +17,6 @@
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
- *
- * $Id$
  */
 
 #include <pic18fregs.h>
 /* parameters are:
  *   channel: one of ADC_CHN_*
  *   fosc:    one of ADC_FOSC_*
- *   pcfg:    one of ADC_CFG_*
- *   config:  ADC_FRM_*  |  ADC_INT_*
+ *   pcfg:    one of ADC_CFG_* (a bitmask with set bits denoting digital ports for 242-style)
+ *   config:  ADC_FRM_* | ADC_INT_* | ADC_VCFG_*
  */
 
 void adc_open(unsigned char channel, unsigned char fosc, unsigned char pcfg, unsigned char config)
 {
+  /* disable ADC */
   ADCON0 = 0;
-  ADCON1 = 0;
-
-  /* setup channel */
-#if defined(__SDCC_ADC_STYLE2455) || defined(__SDCC_ADC_STYLE97J60)
-  ADCON0 |= (channel & 0x07) << 2;
-#else /* all other devices */
-  ADCON0 |= (channel & 0x07) << 3;
-#endif
 
-  /* setup fosc */
-#if defined(__SDCC_ADC_STYLE2455) || defined(__SDCC_ADC_STYLE97J60)
-  ADCON2 |= (fosc & 0x03);
-#else /* all other devices */
-  ADCON0 |= (fosc & 0x03) << 6;
-  ADCON1 |= (fosc & 0x04) << 4;
-#endif
-
-  /* setup reference and pins */
-#if defined(__SDCC_ADC_STYLE2455) || defined(__SDCC_ADC_STYLE97J60)
-  ADCON1 |= pcfg & 0x3f;
-#else /* all other devices */
-  ADCON1 |= pcfg & 0x0f;
-#endif
-
-#if defined(__SDCC_ADC_STYLE2455) || defined(__SDCC_ADC_STYLE97J60)
-  ADCON2 |= (config & ADC_FRM_RJUST);
-#else /* all other devices */
-  ADCON1 |= (config & ADC_FRM_RJUST);
+#if defined(__SDCC_ADC_STYLE242)
+  ADCON0 = ((channel & 0x07) << 3) | ((fosc & 0x03) << 6);
+  ADCON1 = (pcfg & 0x0f) | (config & ADC_FRM_RJUST);
+  if (fosc & 0x04) {
+    ADCON1bits.ADCS2 = 1;
+  }
+#elif defined (__SDCC_ADC_STYLE1220)
+  ADCON0 = ((channel & 0x07) | (config & ADC_VCFG_AN3_AN2)) << 2;
+  ADCON1 = (pcfg & 0x7f);
+  ADCON2 = (ADCON2 & 0x38) | (fosc & 0x07) | (config & ADC_FRM_RJUST);
+#elif defined(__SDCC_ADC_STYLE2220)
+  ADCON0 = (channel & 0x0f) << 2;
+  /* XXX: Should be (pcfg & 0x0f) as VCFG comes from config,
+   * but we retain compatibility for now ... */
+  ADCON1 = (pcfg & 0x3f) | (config & ADC_VCFG_AN3_AN2);
+  ADCON2 = (ADCON2 & 0x38) | (fosc & 0x07) | (config & ADC_FRM_RJUST);
+#else /* unsupported ADC style */
+#error Unsupported ADC style.
 #endif
 
   if (config & ADC_INT_ON) {