Author: sebastien.lelong
Date: Sun May 10 09:18:29 2009
New Revision: 978

Modified:
    trunk/include/peripheral/adc/adc_channels.jal
    trunk/tools/adc_channels.jal.tmpl
    trunk/tools/adc_channels_generator.py

Log:
add "adc_pin_ANx_direction = input" statements according to the number of  
selected ADC channels. This adc_channels.jal lib is looking like a monster,  
fortunately it is generated. Is it Frankenstein ?

Modified: trunk/include/peripheral/adc/adc_channels.jal
==============================================================================
--- trunk/include/peripheral/adc/adc_channels.jal       (original)
+++ trunk/include/peripheral/adc/adc_channels.jal       Sun May 10 09:18:29 2009
@@ -32,8 +32,10 @@

  if target_chip == PIC_18F6585 | target_chip == PIC_18F6680 | target_chip  
== PIC_18F8585 | target_chip == PIC_18F8680 | target_chip == PIC_16F870 |  
target_chip == PIC_16F871 | target_chip == PIC_16F73 | target_chip ==  
PIC_16F74 | target_chip == PIC_16F76 | target_chip == PIC_16F77 |  
target_chip == PIC_16F737 | target_chip == PIC_16F747 | target_chip ==  
PIC_16F767 | target_chip == PIC_16F777 | target_chip == PIC_16F72 |  
target_chip == PIC_16F873A | target_chip == PIC_16F874A | target_chip ==  
PIC_16F876A | target_chip == PIC_16F877A | target_chip == PIC_16F716 |  
target_chip == PIC_18F2439 | target_chip == PIC_18F2539 | target_chip ==  
PIC_18F4439 | target_chip == PIC_18F4539 | target_chip == PIC_16F873 |  
target_chip == PIC_16F874 | target_chip == PIC_16F876 | target_chip ==  
PIC_16F877 | target_chip == PIC_16F872 | target_chip == PIC_16F818 |  
target_chip == PIC_16F819 then

+   -- Datasheet: 30491C
     if target_chip == PIC_18F6585 | target_chip == PIC_18F6680 |  
target_chip == PIC_18F8585 | target_chip == PIC_18F8680 then

+         -- These are some constants similar to what can be found in MCC18  
adc.h header file
           const bit*4 ADC_PCFG_9ANA_0REF = 0b_0110
           const bit*4 ADC_PCFG_8ANA_0REF = 0b_0111
           const bit*4 ADC_PCFG_16ANA_0REF = 0b_0000
@@ -51,16 +53,160 @@
           const bit*4 ADC_PCFG_6ANA_0REF = 0b_1001
           const bit*4 ADC_PCFG_7ANA_0REF = 0b_1000

+         -- This array stores PCFG config bits, 3 by 3, for each number of
+         -- ADC channels, from 1 to the max:
+         -- For each 3-tuple: (no vref, 1 vref, 2 vref)
           const byte ADC_PCFG_MAP [48] = {0b_1111, 0b_0, 0b_0, 0b_1110,  
0b_0, 0b_0, 0b_1101, 0b_0, 0b_0, 0b_1100, 0b_0, 0b_0, 0b_1011, 0b_0, 0b_0,  
0b_1010, 0b_0, 0b_0, 0b_1001, 0b_0, 0b_0, 0b_1000, 0b_0, 0b_0, 0b_0111,  
0b_0, 0b_0, 0b_0110, 0b_0, 0b_0, 0b_0101, 0b_0, 0b_0, 0b_0100, 0b_0, 0b_0,  
0b_0011, 0b_0, 0b_0, 0b_0010, 0b_0, 0b_0, 0b_0001, 0b_0, 0b_0, 0b_0000,  
0b_0, 0b_0}
           var bit*4 no_vref = 0
           var bit*4 one_vref = 0
           var bit*4 two_vref = 0

+         if (ADC_NCHANNEL + ADC_NVREF) == 0 then
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 1 then
+            adc_pin_AN0_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 2 then
+            adc_pin_AN1_direction = input
+            adc_pin_AN0_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 3 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN0_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 4 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN3_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 5 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN3_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 6 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN5_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN3_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 7 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN5_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN6_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 8 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN5_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN7_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN6_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 9 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN5_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN8_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN7_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN6_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 10 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN5_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN8_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN9_direction = input
+            adc_pin_AN7_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN6_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 11 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN5_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN8_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN9_direction = input
+            adc_pin_AN7_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN10_direction = input
+            adc_pin_AN6_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 12 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN5_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN8_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN9_direction = input
+            adc_pin_AN7_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN10_direction = input
+            adc_pin_AN11_direction = input
+            adc_pin_AN6_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 13 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN5_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN8_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN9_direction = input
+            adc_pin_AN12_direction = input
+            adc_pin_AN7_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN10_direction = input
+            adc_pin_AN11_direction = input
+            adc_pin_AN6_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 14 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN5_direction = input
+            adc_pin_AN13_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN8_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN9_direction = input
+            adc_pin_AN12_direction = input
+            adc_pin_AN7_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN10_direction = input
+            adc_pin_AN11_direction = input
+            adc_pin_AN6_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 16 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN5_direction = input
+            adc_pin_AN13_direction = input
+            adc_pin_AN14_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN8_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN9_direction = input
+            adc_pin_AN12_direction = input
+            adc_pin_AN7_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN10_direction = input
+            adc_pin_AN15_direction = input
+            adc_pin_AN11_direction = input
+            adc_pin_AN6_direction = input
+         end if
+
     end if


+   -- Datasheet: 30569B
     if target_chip == PIC_16F870 | target_chip == PIC_16F871 then

+         -- These are some constants similar to what can be found in MCC18  
adc.h header file
           const bit*4 ADC_PCFG_8ANA_0REF = 0b_0000
           const bit*4 ADC_PCFG_7ANA_1REF = 0b_0001
           const bit*4 ADC_PCFG_4ANA_1REF = 0b_0011
@@ -77,16 +223,57 @@
           const bit*4 ADC_PCFG_6ANA_0REF = 0b_1001
           const bit*4 ADC_PCFG_6ANA_2REF = 0b_1000

-         const byte ADC_PCFG_MAP [27] = {0b_0110, 0b_0, 0b_0, 0b_1110,  
0b_0, 0b_1111, 0b_0, 0b_0101, 0b_1101, 0b_0100, 0b_0, 0b_1100, 0b_0,  
0b_0011, 0b_1011, 0b_0010, 0b_1010, 0b_0, 0b_1001, 0b_0, 0b_1000, 0b_0,  
0b_0001, 0b_0, 0b_0000, 0b_0, 0b_0}
+         -- This array stores PCFG config bits, 3 by 3, for each number of
+         -- ADC channels, from 1 to the max:
+         -- For each 3-tuple: (no vref, 1 vref, 2 vref)
+         const byte ADC_PCFG_MAP [21] = {0b_0110, 0b_0, 0b_0, 0b_1110,  
0b_0, 0b_0, 0b_0100, 0b_0101, 0b_1111, 0b_0, 0b_0, 0b_1101, 0b_0010,  
0b_0011, 0b_1100, 0b_1001, 0b_1010, 0b_1011, 0b_0000, 0b_0001, 0b_1000}
           var bit*4 no_vref = 0
           var bit*4 one_vref = 0
           var bit*4 two_vref = 0

+         if (ADC_NCHANNEL + ADC_NVREF) == 0 then
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 1 then
+            adc_pin_AN0_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 3 then
+            adc_pin_AN0_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 4 then
+            adc_pin_AN0_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 5 then
+            adc_pin_AN4_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 6 then
+            adc_pin_AN5_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN1_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 8 then
+            adc_pin_AN7_direction = input
+            adc_pin_AN5_direction = input
+            adc_pin_AN6_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+         end if
+
     end if


+   -- Datasheet: 30325B
     if target_chip == PIC_16F73 | target_chip == PIC_16F74 | target_chip ==  
PIC_16F76 | target_chip == PIC_16F77 then

+         -- These are some constants similar to what can be found in MCC18  
adc.h header file
           const bit*3 ADC_PCFG_5ANA_0REF = 0b_010
           const bit*3 ADC_PCFG_4ANA_1REF = 0b_011
           const bit*3 ADC_PCFG_7ANA_1REF = 0b_001
@@ -95,16 +282,43 @@
           const bit*3 ADC_PCFG_2ANA_1REF = 0b_101
           const bit*3 ADC_PCFG_0ANA_0REF = 0b_110

-         const byte ADC_PCFG_MAP [21] = {0b_110, 0b_0, 0b_0, 0b_0, 0b_101,  
0b_0, 0b_100, 0b_0, 0b_0, 0b_0, 0b_011, 0b_0, 0b_010, 0b_0, 0b_0, 0b_0,  
0b_001, 0b_0, 0b_000, 0b_0, 0b_0}
+         -- This array stores PCFG config bits, 3 by 3, for each number of
+         -- ADC channels, from 1 to the max:
+         -- For each 3-tuple: (no vref, 1 vref, 2 vref)
+         const byte ADC_PCFG_MAP [12] = {0b_110, 0b_0, 0b_0, 0b_100,  
0b_101, 0b_0, 0b_010, 0b_011, 0b_0, 0b_000, 0b_001, 0b_0}
           var bit*3 no_vref = 0
           var bit*3 one_vref = 0
           var bit*3 two_vref = 0

+         if (ADC_NCHANNEL + ADC_NVREF) == 0 then
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 3 then
+            adc_pin_AN0_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN3_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 5 then
+            adc_pin_AN4_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN3_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 8 then
+            adc_pin_AN7_direction = input
+            adc_pin_AN5_direction = input
+            adc_pin_AN6_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN1_direction = input
+         end if
+
     end if


+   -- Datasheet: 30498C
     if target_chip == PIC_16F737 | target_chip == PIC_16F747 | target_chip  
== PIC_16F767 | target_chip == PIC_16F777 then

+         -- These are some constants similar to what can be found in MCC18  
adc.h header file
           const bit*4 ADC_PCFG_9ANA_0REF = 0b_0110
           const bit*4 ADC_PCFG_8ANA_0REF = 0b_0111
           const bit*4 ADC_PCFG_14ANA_0REF = 0b_0000
@@ -122,16 +336,143 @@
           const bit*4 ADC_PCFG_6ANA_0REF = 0b_1001
           const bit*4 ADC_PCFG_7ANA_0REF = 0b_1000

+         -- This array stores PCFG config bits, 3 by 3, for each number of
+         -- ADC channels, from 1 to the max:
+         -- For each 3-tuple: (no vref, 1 vref, 2 vref)
           const byte ADC_PCFG_MAP [45] = {0b_1111, 0b_0, 0b_0, 0b_1110,  
0b_0, 0b_0, 0b_1101, 0b_0, 0b_0, 0b_1100, 0b_0, 0b_0, 0b_1011, 0b_0, 0b_0,  
0b_1010, 0b_0, 0b_0, 0b_1001, 0b_0, 0b_0, 0b_1000, 0b_0, 0b_0, 0b_0111,  
0b_0, 0b_0, 0b_0110, 0b_0, 0b_0, 0b_0101, 0b_0, 0b_0, 0b_0100, 0b_0, 0b_0,  
0b_0011, 0b_0, 0b_0, 0b_0010, 0b_0, 0b_0, 0b_0001, 0b_0, 0b_0}
           var bit*4 no_vref = 0
           var bit*4 one_vref = 0
           var bit*4 two_vref = 0

+         if (ADC_NCHANNEL + ADC_NVREF) == 0 then
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 1 then
+            adc_pin_AN0_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 2 then
+            adc_pin_AN1_direction = input
+            adc_pin_AN0_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 3 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN0_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 4 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN0_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 5 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN0_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 6 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN5_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 7 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN6_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN5_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 8 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN7_direction = input
+            adc_pin_AN6_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN5_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 9 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN7_direction = input
+            adc_pin_AN6_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN5_direction = input
+            adc_pin_AN8_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 10 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN7_direction = input
+            adc_pin_AN6_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN9_direction = input
+            adc_pin_AN5_direction = input
+            adc_pin_AN8_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 11 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN10_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN7_direction = input
+            adc_pin_AN6_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN9_direction = input
+            adc_pin_AN5_direction = input
+            adc_pin_AN8_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 12 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN10_direction = input
+            adc_pin_AN11_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN7_direction = input
+            adc_pin_AN6_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN9_direction = input
+            adc_pin_AN5_direction = input
+            adc_pin_AN8_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 13 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN10_direction = input
+            adc_pin_AN11_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN7_direction = input
+            adc_pin_AN6_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN12_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN9_direction = input
+            adc_pin_AN5_direction = input
+            adc_pin_AN8_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 14 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN10_direction = input
+            adc_pin_AN11_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN13_direction = input
+            adc_pin_AN7_direction = input
+            adc_pin_AN6_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN12_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN9_direction = input
+            adc_pin_AN5_direction = input
+            adc_pin_AN8_direction = input
+         end if
+
     end if


+   -- Datasheet: 39597C
     if target_chip == PIC_16F72 then

+         -- These are some constants similar to what can be found in MCC18  
adc.h header file
           const bit*3 ADC_PCFG_4ANA_1REF = 0b_010
           const bit*3 ADC_PCFG_4ANA_1REF = 0b_011
           const bit*3 ADC_PCFG_4ANA_1REF = 0b_001
@@ -140,16 +481,34 @@
           const bit*3 ADC_PCFG_3ANA_0REF = 0b_100
           const bit*3 ADC_PCFG_2ANA_1REF = 0b_101

-         const byte ADC_PCFG_MAP [15] = {0b_111, 0b_0, 0b_0, 0b_0, 0b_101,  
0b_0, 0b_100, 0b_0, 0b_0, 0b_0, 0b_001, 0b_0, 0b_000, 0b_0, 0b_0}
+         -- This array stores PCFG config bits, 3 by 3, for each number of
+         -- ADC channels, from 1 to the max:
+         -- For each 3-tuple: (no vref, 1 vref, 2 vref)
+         const byte ADC_PCFG_MAP [9] = {0b_111, 0b_0, 0b_0, 0b_100,  
0b_101, 0b_0, 0b_000, 0b_001, 0b_0}
           var bit*3 no_vref = 0
           var bit*3 one_vref = 0
           var bit*3 two_vref = 0

+         if (ADC_NCHANNEL + ADC_NVREF) == 0 then
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 3 then
+            adc_pin_AN0_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN3_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 5 then
+            adc_pin_AN4_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN1_direction = input
+         end if
+
     end if


+   -- Datasheet: 39582B
     if target_chip == PIC_16F873A | target_chip == PIC_16F874A |  
target_chip == PIC_16F876A | target_chip == PIC_16F877A then

+         -- These are some constants similar to what can be found in MCC18  
adc.h header file
           const bit*4 ADC_PCFG_8ANA_0REF = 0b_0000
           const bit*4 ADC_PCFG_7ANA_1REF = 0b_0001
           const bit*4 ADC_PCFG_4ANA_1REF = 0b_0011
@@ -166,32 +525,90 @@
           const bit*4 ADC_PCFG_6ANA_0REF = 0b_1001
           const bit*4 ADC_PCFG_6ANA_2REF = 0b_1000

-         const byte ADC_PCFG_MAP [27] = {0b_0110, 0b_0, 0b_0, 0b_1110,  
0b_0, 0b_1111, 0b_0, 0b_0, 0b_1101, 0b_0100, 0b_0101, 0b_1100, 0b_0,  
0b_0011, 0b_1011, 0b_0010, 0b_1010, 0b_0, 0b_1001, 0b_0, 0b_1000, 0b_0,  
0b_0001, 0b_0, 0b_0000, 0b_0, 0b_0}
+         -- This array stores PCFG config bits, 3 by 3, for each number of
+         -- ADC channels, from 1 to the max:
+         -- For each 3-tuple: (no vref, 1 vref, 2 vref)
+         const byte ADC_PCFG_MAP [21] = {0b_0110, 0b_0, 0b_0, 0b_1110,  
0b_0, 0b_0, 0b_0100, 0b_0, 0b_1111, 0b_0, 0b_0101, 0b_1101, 0b_0010,  
0b_0011, 0b_1100, 0b_1001, 0b_1010, 0b_1011, 0b_0000, 0b_0001, 0b_1000}
           var bit*4 no_vref = 0
           var bit*4 one_vref = 0
           var bit*4 two_vref = 0

+         if (ADC_NCHANNEL + ADC_NVREF) == 0 then
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 1 then
+            adc_pin_AN0_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 3 then
+            adc_pin_AN0_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 4 then
+            adc_pin_AN1_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 5 then
+            adc_pin_AN4_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 6 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN5_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 8 then
+            adc_pin_AN4_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN7_direction = input
+            adc_pin_AN6_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN5_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+         end if
+
     end if


+   -- Datasheet: 41206B
     if target_chip == PIC_16F716 then

+         -- These are some constants similar to what can be found in MCC18  
adc.h header file
           const bit*3 ADC_PCFG_3ANA_1REF = 0b_001
           const bit*3 ADC_PCFG_4ANA_0REF = 0b_000
           const bit*3 ADC_PCFG_3ANA_0REF = 0b_100
           const bit*3 ADC_PCFG_2ANA_1REF = 0b_101
           const bit*3 ADC_PCFG_0ANA_0REF = 0b_110

-         const byte ADC_PCFG_MAP [12] = {0b_110, 0b_0, 0b_0, 0b_0, 0b_101,  
0b_0, 0b_100, 0b_001, 0b_0, 0b_000, 0b_0, 0b_0}
+         -- This array stores PCFG config bits, 3 by 3, for each number of
+         -- ADC channels, from 1 to the max:
+         -- For each 3-tuple: (no vref, 1 vref, 2 vref)
+         const byte ADC_PCFG_MAP [9] = {0b_110, 0b_0, 0b_0, 0b_100,  
0b_101, 0b_0, 0b_000, 0b_001, 0b_0}
           var bit*3 no_vref = 0
           var bit*3 one_vref = 0
           var bit*3 two_vref = 0

+         if (ADC_NCHANNEL + ADC_NVREF) == 0 then
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 3 then
+            adc_pin_AN0_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN3_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 4 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN1_direction = input
+         end if
+
     end if


+   -- Datasheet: 30292C
     if target_chip == PIC_16F873 | target_chip == PIC_16F874 | target_chip  
== PIC_16F876 | target_chip == PIC_16F877 then

+         -- These are some constants similar to what can be found in MCC18  
adc.h header file
           const bit*4 ADC_PCFG_8ANA_0REF = 0b_0000
           const bit*4 ADC_PCFG_7ANA_1REF = 0b_0001
           const bit*4 ADC_PCFG_4ANA_1REF = 0b_0011
@@ -208,16 +625,57 @@
           const bit*4 ADC_PCFG_6ANA_0REF = 0b_1001
           const bit*4 ADC_PCFG_6ANA_2REF = 0b_1000

-         const byte ADC_PCFG_MAP [27] = {0b_0110, 0b_0, 0b_0, 0b_1110,  
0b_0, 0b_1111, 0b_0, 0b_0101, 0b_1101, 0b_0100, 0b_0, 0b_1100, 0b_0,  
0b_0011, 0b_1011, 0b_0010, 0b_1010, 0b_0, 0b_1001, 0b_0, 0b_1000, 0b_0,  
0b_0001, 0b_0, 0b_0000, 0b_0, 0b_0}
+         -- This array stores PCFG config bits, 3 by 3, for each number of
+         -- ADC channels, from 1 to the max:
+         -- For each 3-tuple: (no vref, 1 vref, 2 vref)
+         const byte ADC_PCFG_MAP [21] = {0b_0110, 0b_0, 0b_0, 0b_1110,  
0b_0, 0b_0, 0b_0100, 0b_0101, 0b_1111, 0b_0, 0b_0, 0b_1101, 0b_0010,  
0b_0011, 0b_1100, 0b_1001, 0b_1010, 0b_1011, 0b_0000, 0b_0001, 0b_1000}
           var bit*4 no_vref = 0
           var bit*4 one_vref = 0
           var bit*4 two_vref = 0

+         if (ADC_NCHANNEL + ADC_NVREF) == 0 then
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 1 then
+            adc_pin_AN0_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 3 then
+            adc_pin_AN0_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 4 then
+            adc_pin_AN0_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 5 then
+            adc_pin_AN4_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 6 then
+            adc_pin_AN5_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN1_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 8 then
+            adc_pin_AN7_direction = input
+            adc_pin_AN5_direction = input
+            adc_pin_AN6_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+         end if
+
     end if


+   -- Datasheet: 30485A
     if target_chip == PIC_18F2439 | target_chip == PIC_18F2539 |  
target_chip == PIC_18F4439 | target_chip == PIC_18F4539 then

+         -- These are some constants similar to what can be found in MCC18  
adc.h header file
           const bit*4 ADC_PCFG_8ANA_0REF = 0b_0000
           const bit*4 ADC_PCFG_7ANA_1REF = 0b_0001
           const bit*4 ADC_PCFG_4ANA_1REF = 0b_0011
@@ -233,16 +691,55 @@
           const bit*4 ADC_PCFG_6ANA_0REF = 0b_1001
           const bit*4 ADC_PCFG_6ANA_2REF = 0b_1000

-         const byte ADC_PCFG_MAP [27] = {0b_0110, 0b_0, 0b_0, 0b_0, 0b_0,  
0b_1110, 0b_0, 0b_0101, 0b_1101, 0b_0100, 0b_0, 0b_1100, 0b_0, 0b_0011,  
0b_1011, 0b_0010, 0b_1010, 0b_0, 0b_1001, 0b_0, 0b_1000, 0b_0, 0b_0001,  
0b_0, 0b_0000, 0b_0, 0b_0}
+         -- This array stores PCFG config bits, 3 by 3, for each number of
+         -- ADC channels, from 1 to the max:
+         -- For each 3-tuple: (no vref, 1 vref, 2 vref)
+         const byte ADC_PCFG_MAP [18] = {0b_0110, 0b_0, 0b_0, 0b_0100,  
0b_0101, 0b_1110, 0b_0, 0b_0, 0b_1101, 0b_0010, 0b_0011, 0b_1100, 0b_1001,  
0b_1010, 0b_1011, 0b_0000, 0b_0001, 0b_1000}
           var bit*4 no_vref = 0
           var bit*4 one_vref = 0
           var bit*4 two_vref = 0

+         if (ADC_NCHANNEL + ADC_NVREF) == 0 then
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 3 then
+            adc_pin_AN0_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 4 then
+            adc_pin_AN1_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 5 then
+            adc_pin_AN4_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 6 then
+            adc_pin_AN2_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN3_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN5_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 8 then
+            adc_pin_AN4_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN7_direction = input
+            adc_pin_AN6_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN5_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+         end if
+
     end if


+   -- Datasheet: 30221C
     if target_chip == PIC_16F872 then

+         -- These are some constants similar to what can be found in MCC18  
adc.h header file
           const bit*4 ADC_PCFG_5ANA_0REF = 0b_0000
           const bit*4 ADC_PCFG_4ANA_1REF = 0b_0001
           const bit*4 ADC_PCFG_4ANA_1REF = 0b_0011
@@ -259,16 +756,41 @@
           const bit*4 ADC_PCFG_5ANA_0REF = 0b_1001
           const bit*4 ADC_PCFG_3ANA_2REF = 0b_1000

-         const byte ADC_PCFG_MAP [18] = {0b_0110, 0b_0, 0b_0, 0b_1110,  
0b_0, 0b_1111, 0b_0, 0b_0101, 0b_1101, 0b_0100, 0b_0, 0b_1000, 0b_0,  
0b_1010, 0b_0, 0b_1001, 0b_0, 0b_0}
+         -- This array stores PCFG config bits, 3 by 3, for each number of
+         -- ADC channels, from 1 to the max:
+         -- For each 3-tuple: (no vref, 1 vref, 2 vref)
+         const byte ADC_PCFG_MAP [15] = {0b_0110, 0b_0, 0b_0, 0b_1110,  
0b_0, 0b_0, 0b_0100, 0b_0101, 0b_1111, 0b_0, 0b_0, 0b_1101, 0b_1001,  
0b_1010, 0b_1000}
           var bit*4 no_vref = 0
           var bit*4 one_vref = 0
           var bit*4 two_vref = 0

+         if (ADC_NCHANNEL + ADC_NVREF) == 0 then
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 1 then
+            adc_pin_AN0_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 3 then
+            adc_pin_AN0_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 4 then
+            adc_pin_AN0_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 5 then
+            adc_pin_AN4_direction = input
+            adc_pin_AN0_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+         end if
+
     end if


+   -- Datasheet: 39598E
     if target_chip == PIC_16F818 | target_chip == PIC_16F819 then

+         -- These are some constants similar to what can be found in MCC18  
adc.h header file
           const bit*4 ADC_PCFG_5ANA_0REF = 0b_0000
           const bit*4 ADC_PCFG_4ANA_1REF = 0b_0001
           const bit*4 ADC_PCFG_4ANA_1REF = 0b_0011
@@ -285,11 +807,34 @@
           const bit*4 ADC_PCFG_5ANA_0REF = 0b_1001
           const bit*4 ADC_PCFG_3ANA_2REF = 0b_1000

-         const byte ADC_PCFG_MAP [18] = {0b_0110, 0b_0, 0b_0, 0b_1110,  
0b_0, 0b_1111, 0b_0, 0b_0101, 0b_1101, 0b_0100, 0b_0, 0b_1000, 0b_0,  
0b_1010, 0b_0, 0b_1001, 0b_0, 0b_0}
+         -- This array stores PCFG config bits, 3 by 3, for each number of
+         -- ADC channels, from 1 to the max:
+         -- For each 3-tuple: (no vref, 1 vref, 2 vref)
+         const byte ADC_PCFG_MAP [15] = {0b_0110, 0b_0, 0b_0, 0b_1110,  
0b_0, 0b_0, 0b_0100, 0b_0101, 0b_1111, 0b_0, 0b_0, 0b_1101, 0b_1001,  
0b_1010, 0b_1000}
           var bit*4 no_vref = 0
           var bit*4 one_vref = 0
           var bit*4 two_vref = 0

+         if (ADC_NCHANNEL + ADC_NVREF) == 0 then
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 1 then
+            adc_pin_AN0_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 3 then
+            adc_pin_AN0_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 4 then
+            adc_pin_AN0_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+         elsif (ADC_NCHANNEL + ADC_NVREF) == 5 then
+            adc_pin_AN0_direction = input
+            adc_pin_AN1_direction = input
+            adc_pin_AN4_direction = input
+            adc_pin_AN2_direction = input
+            adc_pin_AN3_direction = input
+         end if
+
     end if


@@ -323,54 +868,67 @@
        -- At least 1 channel
        if ADC_NCHANNEL > 0 then
           ANCON0_PCFG0 = false
+         adc_pin_AN0_direction = input
        end if
        -- At least 2 channels
        if ADC_NCHANNEL > 1 then
-          ANCON0_PCFG1 = false
+         ANCON0_PCFG1 = false
+         adc_pin_AN1_direction = input
        end if
        -- At least 3 channels
        if ADC_NCHANNEL > 2 then
            ANCON0_PCFG2 = false
+         adc_pin_AN2_direction = input
        end if
        -- At least 4 channels
        if ADC_NCHANNEL > 3 then
-          ANCON0_PCFG3 = false
+         ANCON0_PCFG3 = false
+         adc_pin_AN3_direction = input
        end if
        -- At least 5 channels
        if ADC_NCHANNEL > 4 then
-          ANCON0_PCFG4 = false
+         ANCON0_PCFG4 = false
+         adc_pin_AN4_direction = input
        end if
        -- At least 6 channels
        if ADC_NCHANNEL > 5 then
-          ANCON0_PCFG5 = false
+         ANCON0_PCFG5 = false
+         adc_pin_AN5_direction = input
        end if
        -- At least 7 channels
        if ADC_NCHANNEL > 6 then
-          ANCON0_PCFG6 = false
+         ANCON0_PCFG6 = false
+         adc_pin_AN6_direction = input
        end if
        -- At least 8 channels
        if ADC_NCHANNEL > 7 then
-          ANCON0_PCFG7 = false
+         ANCON0_PCFG7 = false
+         adc_pin_AN7_direction = input
        end if
        -- At least 9 channels
        if ADC_NCHANNEL > 8 then
-          ANCON1_PCFG8 = false
+         ANCON1_PCFG8 = false
+         adc_pin_AN8_direction = input
        end if
        -- At least 10 channels
        if ADC_NCHANNEL > 9 then
-          ANCON1_PCFG9 = false
+         ANCON1_PCFG9 = false
+         adc_pin_AN9_direction = input
        end if
        -- At least 11 channels
        if ADC_NCHANNEL > 10 then
-          ANCON1_PCFG10 = false
+         ANCON1_PCFG10 = false
+         adc_pin_AN10_direction = input
        end if
        -- At least 12 channels
        if ADC_NCHANNEL > 11 then
-          ANCON1_PCFG11 = false
+         ANCON1_PCFG11 = false
+         adc_pin_AN11_direction = input
        end if
        -- At least 13 channels
        if ADC_NCHANNEL > 12 then
-          ANCON1_PCFG12 = false
+         ANCON1_PCFG12 = false
+         adc_pin_AN12_direction = input
        end if


@@ -390,18 +948,23 @@
           -- ANCON0_PCFG2, ANCON0_PCFG3 and ANCON0_PCFG4 are always defined
           if an_pin_num == 0 then
              ANCON0_PCFG0 = false
+            adc_pin_AN0_direction = input
           end if
           if an_pin_num == 1 then
              ANCON0_PCFG1 = false
+            adc_pin_AN1_direction = input
           end if
           if an_pin_num == 2 then
              ANCON0_PCFG2 = false
+            adc_pin_AN2_direction = input
           end if
           if an_pin_num == 3 then
              ANCON0_PCFG3 = false
+            adc_pin_AN3_direction = input
           end if
           if an_pin_num == 4 then
              ANCON0_PCFG4 = false
+            adc_pin_AN4_direction = input
           end if

           -- Sometimes, there are gaps in PCFG bits
@@ -414,12 +977,15 @@
           if defined(ANCON0_PCFG5) == true & defined(ANCON0_PCFG6) == true  
& defined(ANCON0_PCFG7) == true then
              if an_pin_num == 5 then
                 ANCON0_PCFG5 = false
+               adc_pin_AN5_direction = input
              end if
              if an_pin_num == 6 then
                 ANCON0_PCFG6 = false
+               adc_pin_AN6_direction = input
              end if
              if an_pin_num == 7 then
                 ANCON0_PCFG7 = false
+               adc_pin_AN7_direction = input
              end if
           end if

@@ -427,18 +993,23 @@
           if defined(ANCON1_PCFG8) == true & defined(ANCON1_PCFG9) == true  
& defined(ANCON1_PCFG10) == true & defined(ANCON1_PCFG11) == true &  
defined(ANCON1_PCFG12) == true then
              if an_pin_num == 8 then
                 ANCON1_PCFG8 = false
+               adc_pin_AN8_direction = input
              end if
              if an_pin_num == 9 then
                 ANCON1_PCFG9 = false
+               adc_pin_AN9_direction = input
              end if
              if an_pin_num == 10 then
                 ANCON1_PCFG10 = false
+               adc_pin_AN10_direction = input
              end if
              if an_pin_num == 11 then
                 ANCON1_PCFG11 = false
+               adc_pin_AN11_direction = input
              end if
              if an_pin_num == 12 then
                 ANCON1_PCFG12 = false
+               adc_pin_AN12_direction = input
              end if
           end if
        end procedure
@@ -452,33 +1023,43 @@
        --     - set_analog_pin(12) won't set this pin as digital, but the  
equivalent
        --       one named AN12, if any
        --     - set_analog_pin(4) will do the same for AN4
+      --
+      -- Note: the pin direction is not modified
        procedure set_digital_pin(byte in an_pin_num) is
           if an_pin_num == 0 then
              ANCON0_PCFG0 = true
+            adc_pin_AN0_direction = input
           end if
           if an_pin_num == 1 then
              ANCON0_PCFG1 = true
+            adc_pin_AN1_direction = input
           end if
           if an_pin_num == 2 then
              ANCON0_PCFG2 = true
+            adc_pin_AN2_direction = input
           end if
           if an_pin_num == 3 then
              ANCON0_PCFG3 = true
+            adc_pin_AN3_direction = input
           end if
           if an_pin_num == 4 then
              ANCON0_PCFG4 = true
+            adc_pin_AN4_direction = input
           end if

           -- PCFG5, PCFG6 and PCFG7 are always found together
           if defined(ANCON0_PCFG5) == true & defined(ANCON0_PCFG6) == true  
& defined(ANCON0_PCFG7) == true then
              if an_pin_num == 5 then
                 ANCON0_PCFG5 = true
+               adc_pin_AN5_direction = input
              end if
              if an_pin_num == 6 then
                 ANCON0_PCFG6 = true
+               adc_pin_AN6_direction = input
              end if
              if an_pin_num == 7 then
                 ANCON0_PCFG7 = true
+               adc_pin_AN7_direction = input
              end if
           end if

@@ -486,18 +1067,23 @@
           if defined(ANCON1_PCFG8) == true & defined(ANCON1_PCFG9) == true  
& defined(ANCON1_PCFG10) == true & defined(ANCON1_PCFG11) == true &  
defined(ANCON1_PCFG12) == true then
              if an_pin_num == 8 then
                 ANCON1_PCFG8 = true
+               adc_pin_AN8_direction = input
              end if
              if an_pin_num == 9 then
                 ANCON1_PCFG9 = true
+               adc_pin_AN9_direction = input
              end if
              if an_pin_num == 10 then
                 ANCON1_PCFG10 = true
+               adc_pin_AN10_direction = input
              end if
              if an_pin_num == 11 then
                 ANCON1_PCFG11 = true
+               adc_pin_AN11_direction = input
              end if
              if an_pin_num == 12 then
                 ANCON1_PCFG12 = true
+               adc_pin_AN12_direction = input
              end if
           end if
        end procedure

Modified: trunk/tools/adc_channels.jal.tmpl
==============================================================================
--- trunk/tools/adc_channels.jal.tmpl   (original)
+++ trunk/tools/adc_channels.jal.tmpl   Sun May 10 09:18:29 2009
@@ -35,19 +35,22 @@


  #def analyze_config(bits,dpins,mapping)
-       #set num_a = len([d for d in dpins.values() if d == 'A'])
-       #set num_d = len([d for d in dpins.values() if d == 'D'])
-       #set num_vref = len([d for d in dpins.values() if d.startswith('VREF')])
+       #set analog = [d for d in dpins.items() if d[1] == 'A']
+       #set vrefs = [d for d in dpins.items() if d[1].startswith('VREF')]
+       #set num_a = len(analog)
+       #set num_vref = len(vrefs)
        ## There can be "anything" bits...
        #silent bits = bits.replace("x","0")
        ## Mapping for 0, 1 or 2 VREF
-       #silent mapping.setdefault(num_a,{0 : '0', 1 : '0', 2 : '0'})[num_vref] 
=  
bits
+       #silent mapping.setdefault(num_a + num_vref,{0 : '0', 1 : '0',  
2 : '0', 'analog_pins' : []})[num_vref] = bits
+       #silent mapping[num_a + num_vref]['analog_pins'] = analog + vrefs
        #return "const bit*%d ADC_PCFG_%sANA_%sREF = 0b_%s" %  
(len(bits),num_a,num_vref,bits)
  #end def

  #def pcfg_conf(dsref)
        #set confs = $adc_pcfg[$dsref]
        #set mapping = {}
+         -- These are some constants similar to what can be found in MCC18  
adc.h header file
        #for bits,dpins in confs.items()
                #set r = $analyze_config(bits,dpins,mapping)
           $r
@@ -59,10 +62,33 @@
        #set lenbits = str(len(bits))
        #set map = "const byte ADC_PCFG_MAP [" + lenlmap + "] = {0b_" + ",  
0b_".join(lmap) + "}"

+         -- This array stores PCFG config bits, 3 by 3, for each number of
+         -- ADC channels, from 1 to the max:
+         -- For each 3-tuple: (no vref, 1 vref, 2 vref)
           $map
           var bit*$lenbits no_vref = 0
           var bit*$lenbits one_vref = 0
           var bit*$lenbits two_vref = 0
+
+       ## Generate procedure to access combinations
+       ## All PICs in this section are in the same DS (assert)
+       ## Get a PIC in this DS to access ANx pins mapping
+       #set dummy_pic = $ds_pic[dsref][-1]
+    #for i,(numch,dconf) in enumerate(smap)
+               #if i == 0
+         if (ADC_NCHANNEL + ADC_NVREF) == $numch then
+               #else
+         elsif (ADC_NCHANNEL + ADC_NVREF) == $numch then
+               #end if
+               #for strpin in dconf['analog_pins']:
+                       #set an_pin = strpin[0].split("/")[0]
+                       #if not an_pin.startswith("AN")
+                               #silent an_pin = $adc_pins[dummy_pic][an_pin]
+                       #end if
+            adc_pin_${an_pin}_direction = input
+               #end for
+       #end for
+         end if
  #end def


@@ -71,7 +97,10 @@
        #silent [bydsref.setdefault($pic_ds[p],[]).append(p) for p in  
$pcfg_combination_pics]
        #for dsref, pics in bydsref.items()
                #set condition = " | ".join(map(lambda x: "target_chip == 
PIC_%s" %  
x.upper(),pics))
+               #set ds_refs = list(set([$pic_ds[p] for p in pics]))
+               #assert len(ds_refs) == 1

+   -- Datasheet: $ds_refs[0]
     if $condition then

  $pcfg_conf($dsref)
@@ -122,54 +151,67 @@
        -- At least 1 channel
        if ADC_NCHANNEL > 0 then
           ANCON0_PCFG0 = false
+         adc_pin_AN0_direction = input
        end if
        -- At least 2 channels
        if ADC_NCHANNEL > 1 then
-          ANCON0_PCFG1 = false
+         ANCON0_PCFG1 = false
+         adc_pin_AN1_direction = input
        end if
        -- At least 3 channels
        if ADC_NCHANNEL > 2 then
            ANCON0_PCFG2 = false
+         adc_pin_AN2_direction = input
        end if
        -- At least 4 channels
        if ADC_NCHANNEL > 3 then
-          ANCON0_PCFG3 = false
+         ANCON0_PCFG3 = false
+         adc_pin_AN3_direction = input
        end if
        -- At least 5 channels
        if ADC_NCHANNEL > 4 then
-          ANCON0_PCFG4 = false
+         ANCON0_PCFG4 = false
+         adc_pin_AN4_direction = input
        end if
        -- At least 6 channels
        if ADC_NCHANNEL > 5 then
-          ANCON0_PCFG5 = false
+         ANCON0_PCFG5 = false
+         adc_pin_AN5_direction = input
        end if
        -- At least 7 channels
        if ADC_NCHANNEL > 6 then
-          ANCON0_PCFG6 = false
+         ANCON0_PCFG6 = false
+         adc_pin_AN6_direction = input
        end if
        -- At least 8 channels
        if ADC_NCHANNEL > 7 then
-          ANCON0_PCFG7 = false
+         ANCON0_PCFG7 = false
+         adc_pin_AN7_direction = input
        end if
        -- At least 9 channels
        if ADC_NCHANNEL > 8 then
-          ANCON1_PCFG8 = false
+         ANCON1_PCFG8 = false
+         adc_pin_AN8_direction = input
        end if
        -- At least 10 channels
        if ADC_NCHANNEL > 9 then
-          ANCON1_PCFG9 = false
+         ANCON1_PCFG9 = false
+         adc_pin_AN9_direction = input
        end if
        -- At least 11 channels
        if ADC_NCHANNEL > 10 then
-          ANCON1_PCFG10 = false
+         ANCON1_PCFG10 = false
+         adc_pin_AN10_direction = input
        end if
        -- At least 12 channels
        if ADC_NCHANNEL > 11 then
-          ANCON1_PCFG11 = false
+         ANCON1_PCFG11 = false
+         adc_pin_AN11_direction = input
        end if
        -- At least 13 channels
        if ADC_NCHANNEL > 12 then
-          ANCON1_PCFG12 = false
+         ANCON1_PCFG12 = false
+         adc_pin_AN12_direction = input
        end if


@@ -189,18 +231,23 @@
           -- ANCON0_PCFG2, ANCON0_PCFG3 and ANCON0_PCFG4 are always defined
           if an_pin_num == 0 then
              ANCON0_PCFG0 = false
+            adc_pin_AN0_direction = input
           end if
           if an_pin_num == 1 then
              ANCON0_PCFG1 = false
+            adc_pin_AN1_direction = input
           end if
           if an_pin_num == 2 then
              ANCON0_PCFG2 = false
+            adc_pin_AN2_direction = input
           end if
           if an_pin_num == 3 then
              ANCON0_PCFG3 = false
+            adc_pin_AN3_direction = input
           end if
           if an_pin_num == 4 then
              ANCON0_PCFG4 = false
+            adc_pin_AN4_direction = input
           end if

           -- Sometimes, there are gaps in PCFG bits
@@ -213,12 +260,15 @@
           if defined(ANCON0_PCFG5) == true & defined(ANCON0_PCFG6) == true  
& defined(ANCON0_PCFG7) == true then
              if an_pin_num == 5 then
                 ANCON0_PCFG5 = false
+               adc_pin_AN5_direction = input
              end if
              if an_pin_num == 6 then
                 ANCON0_PCFG6 = false
+               adc_pin_AN6_direction = input
              end if
              if an_pin_num == 7 then
                 ANCON0_PCFG7 = false
+               adc_pin_AN7_direction = input
              end if
           end if

@@ -226,18 +276,23 @@
           if defined(ANCON1_PCFG8) == true & defined(ANCON1_PCFG9) == true  
& defined(ANCON1_PCFG10) == true & defined(ANCON1_PCFG11) == true &  
defined(ANCON1_PCFG12) == true then
              if an_pin_num == 8 then
                 ANCON1_PCFG8 = false
+               adc_pin_AN8_direction = input
              end if
              if an_pin_num == 9 then
                 ANCON1_PCFG9 = false
+               adc_pin_AN9_direction = input
              end if
              if an_pin_num == 10 then
                 ANCON1_PCFG10 = false
+               adc_pin_AN10_direction = input
              end if
              if an_pin_num == 11 then
                 ANCON1_PCFG11 = false
+               adc_pin_AN11_direction = input
              end if
              if an_pin_num == 12 then
                 ANCON1_PCFG12 = false
+               adc_pin_AN12_direction = input
              end if
           end if
        end procedure
@@ -251,33 +306,43 @@
        --     - set_analog_pin(12) won't set this pin as digital, but the  
equivalent
        --       one named AN12, if any
        --     - set_analog_pin(4) will do the same for AN4
+      --
+      -- Note: the pin direction is not modified
        procedure set_digital_pin(byte in an_pin_num) is
           if an_pin_num == 0 then
              ANCON0_PCFG0 = true
+            adc_pin_AN0_direction = input
           end if
           if an_pin_num == 1 then
              ANCON0_PCFG1 = true
+            adc_pin_AN1_direction = input
           end if
           if an_pin_num == 2 then
              ANCON0_PCFG2 = true
+            adc_pin_AN2_direction = input
           end if
           if an_pin_num == 3 then
              ANCON0_PCFG3 = true
+            adc_pin_AN3_direction = input
           end if
           if an_pin_num == 4 then
              ANCON0_PCFG4 = true
+            adc_pin_AN4_direction = input
           end if

           -- PCFG5, PCFG6 and PCFG7 are always found together
           if defined(ANCON0_PCFG5) == true & defined(ANCON0_PCFG6) == true  
& defined(ANCON0_PCFG7) == true then
              if an_pin_num == 5 then
                 ANCON0_PCFG5 = true
+               adc_pin_AN5_direction = input
              end if
              if an_pin_num == 6 then
                 ANCON0_PCFG6 = true
+               adc_pin_AN6_direction = input
              end if
              if an_pin_num == 7 then
                 ANCON0_PCFG7 = true
+               adc_pin_AN7_direction = input
              end if
           end if

@@ -285,18 +350,23 @@
           if defined(ANCON1_PCFG8) == true & defined(ANCON1_PCFG9) == true  
& defined(ANCON1_PCFG10) == true & defined(ANCON1_PCFG11) == true &  
defined(ANCON1_PCFG12) == true then
              if an_pin_num == 8 then
                 ANCON1_PCFG8 = true
+               adc_pin_AN8_direction = input
              end if
              if an_pin_num == 9 then
                 ANCON1_PCFG9 = true
+               adc_pin_AN9_direction = input
              end if
              if an_pin_num == 10 then
                 ANCON1_PCFG10 = true
+               adc_pin_AN10_direction = input
              end if
              if an_pin_num == 11 then
                 ANCON1_PCFG11 = true
+               adc_pin_AN11_direction = input
              end if
              if an_pin_num == 12 then
                 ANCON1_PCFG12 = true
+               adc_pin_AN12_direction = input
              end if
           end if
        end procedure

Modified: trunk/tools/adc_channels_generator.py
==============================================================================
--- trunk/tools/adc_channels_generator.py       (original)
+++ trunk/tools/adc_channels_generator.py       Sun May 10 09:18:29 2009
@@ -40,6 +40,7 @@
        tmpl.adc_pcfg = adc_pcfg
        tmpl.pic_ds = pic_ds
        tmpl.ds_pic = ds_pic
+       tmpl.adc_pins = adc_pins
        
        fout = file("adc_channels.jal","w")
        print >> fout,tmpl.main()

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"jallib" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/jallib?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to