Multi adc Problems

Discussion in 'Programmer's Corner' started by house91320, Feb 23, 2012.

  1. house91320

    Thread Starter New Member

    Feb 8, 2012
    5
    0
    So Iv'e got this code that sample 4 adcs, and when it was with one adc it worked fine, but once I added the other three it stopped working, anyone have any ideas.

    Code: Select all
    Code ( (Unknown Language)):
    1.  
    2. #define ADC_CS1 PORTA.F7   // output bit
    3. #define ADC_CS2 PORTE.F4
    4. #define ADC_CS3 PORTG.F9
    5. #define ADC_CS4 PORTA.F7
    6. #define ADC_SDO PORTB.F3
    7.  
    8. sbit ADC_SCK at LATG6_bit;
    9.  
    10. char buffer1[20];
    11. char buffer2[20];
    12. char buffer3[20];
    13. char buffer4[20];
    14.  
    15. long d1;
    16. long d2;
    17. long d3;
    18. long d4;
    19.  
    20.  
    21.  
    22.  
    23.  
    24. unsigned long x1_1,x2_1,x3_1,x4_1,x5_1;
    25. unsigned long x1_2,x2_2,x3_2,x4_2,x5_2;
    26. unsigned long x1_3,x2_3,x3_3,x4_3,x5_3;
    27. unsigned long x1_4,x2_4,x3_4,x4_4,x5_4;
    28.  
    29. /* read 32-bit data from LTC2400 */
    30.  
    31. unsigned long read_ADC1(void)
    32. {
    33.    char k1;
    34.    long n1;
    35.    n1= 0;
    36.    ADC_CS1 = 0;
    37.    Delay_us(10);
    38.    for(k1=0; k1<32; k1++)
    39.    {
    40.          n1<<= 1;
    41.      ADC_SCK = 1;
    42.      n1 |= ADC_SDO;
    43.          ADC_SCK = 0;
    44.     }
    45.         ADC_CS1 = 1;
    46.          n1&=0x01fffffff; // maskout sign bit
    47.          n1>>=4;   // get 24-bit conversion result
    48.      return n1;
    49. }
    50.  
    51. unsigned long filter_ADC1(void)
    52. {
    53.     x5_1 = x4_1;
    54.     x4_1 = x3_1;
    55.     x3_1 = x2_1;
    56.     x2_1 = x1_1;
    57.     x1_1 = read_ADC1();
    58.     return ((((x1_1+x2_1+x3_1+x4_1+x5_1)/5)*148)/100); // x 149 E-9 convert to 2.479V
    59. }
    60.  
    61. unsigned long read_ADC2(void)
    62. {
    63.    char k2;
    64.    long n2;
    65.    n2= 0;
    66.    ADC_CS2 = 0;
    67.    Delay_us(10);
    68.    for(k2=0; k2<32; k2++)
    69.    {
    70.          n2<<= 1;
    71.      ADC_SCK = 1;
    72.      n2 |= ADC_SDO;
    73.          ADC_SCK = 0;
    74.     }
    75.         ADC_CS2 = 1;
    76.          n2&=0x01fffffff; // maskout sign bit
    77.          n2>>=4;   // get 24-bit conversion result
    78.      return n2;
    79. }
    80.  
    81. unsigned long filter_ADC2(void)
    82. {
    83.     x5_2 = x4_2;
    84.     x4_2 = x3_2;
    85.     x3_2 = x2_2;
    86.     x2_2 = x1_2;
    87.     x1_2 = read_ADC2();
    88.     return ((((x1_2+x2_2+x3_2+x4_2+x5_2)/5)*148)/100); // x 149 E-9 convert to 2.479V
    89. }
    90.  
    91. unsigned long read_ADC3(void)
    92. {
    93.    char k3;
    94.    long n3;
    95.    n3= 0;
    96.    ADC_CS1 = 0;
    97.    Delay_us(10);
    98.    for(k3=0; k3<32; k3++)
    99.    {
    100.          n3<<= 1;
    101.      ADC_SCK = 1;
    102.      n3 |= ADC_SDO;
    103.          ADC_SCK = 0;
    104.     }
    105.         ADC_CS3 = 1;
    106.          n3&=0x01fffffff; // maskout sign bit
    107.          n3>>=4;   // get 24-bit conversion result
    108.      return n3;
    109. }
    110.  
    111. unsigned long filter_ADC3(void)
    112. {
    113.     x5_3 = x4_3;
    114.     x4_3 = x3_3;
    115.     x3_3 = x2_3;
    116.     x2_3 = x1_3;
    117.     x1_3 = read_ADC3();
    118.     return ((((x1_3+x2_3+x3_3+x4_3+x5_3)/5)*148)/100); // x 149 E-9 convert to 2.479V
    119. }
    120.  
    121. unsigned long read_ADC4(void)
    122. {
    123.    char k4;
    124.    long n4;
    125.    n4= 0;
    126.    ADC_CS4 = 0;
    127.    Delay_us(10);
    128.    for(k4=0; k4<32; k4++)
    129.    {
    130.          n4<<= 1;
    131.      ADC_SCK = 1;
    132.      n4 |= ADC_SDO;
    133.          ADC_SCK = 0;
    134.     }
    135.         ADC_CS4 = 1;
    136.          n4&=0x01fffffff; // maskout sign bit
    137.          n4>>=4;   // get 24-bit conversion result
    138.      return n4;
    139. }
    140.  
    141. unsigned long filter_ADC4(void)
    142. {
    143.     x5_4 = x4_4;
    144.     x4_4 = x3_4;
    145.     x3_4 = x2_4;
    146.     x2_4 = x1_4;
    147.     x1_4 = read_ADC4();
    148.     return ((((x1_4+x2_4+x3_4+x4_4+x5_4)/5)*148)/100); // x 149 E-9 convert to 2.479V
    149. }
    150.  
    151.  
    152.  
    153. void main() {
    154. UART1_Init(9600);
    155. TRISA7_bit = 0;
    156. TRISG6_bit = 0;
    157. TRISG7_bit = 1;
    158. TRISE4_bit = 0;
    159. TRISG9_bit = 0;
    160. TRISB3_bit = 0;
    161.  
    162. CHECON = 0x32;
    163. AD1PCFG = 0xFFFF;
    164.  
    165.   while(1){
    166.  
    167.  
    168.   d1 = read_ADC1();
    169.   sprintf(buffer1,"%08ld V1_",d1); //0xe4    x0.1%cV
    170.   UART_Write_Text(buffer1);
    171.  
    172.   Delay_ms(1000);
    173.  
    174.   d2 = read_ADC2();
    175.   sprintf(buffer2,"%08ld V2_",d2); //0xe4    x0.1%cV
    176.   UART_Write_text(buffer2);
    177.  
    178.   Delay_ms(1000);
    179.  
    180.   d3 = read_ADC3();
    181.   sprintf(buffer3,"%08ld A1_",d3); //0xe4    x0.1%cV
    182.   UART_Write_text(buffer3);
    183.  
    184.   Delay_ms(1000);
    185.  
    186.   d4 = read_ADC4();
    187.   sprintf(buffer4,"%08ld A2_\r\n",d4); //0xe4    x0.1%cV
    188.   UART_Write_text(buffer4);
    189.  
    190.   Delay_ms(1000);
    191.  
    192.  
    193.   }
    194. }
    195.  
     
    Last edited by a moderator: Feb 23, 2012
Loading...