In an ADC PICkle

Discussion in 'Embedded Systems and Microcontrollers' started by kutalinelucas, May 9, 2013.

  1. kutalinelucas

    Thread Starter Active Member

    Nov 20, 2007
    98
    0
    Hello...

    Was wondering if I could get a little direction...I'm trying to read the voltage on two ADC pins of a PIC18F4550, but not concurrently. The readings are taken on demand within a switch statement, and if the ADCON0 register (which chooses the channel) is set in a function outside the switch statement, I have no problems in taking readings by setting the ADCON0.GoDone bit within the switch statement.

    I'm just trying to find a way to set-up the adc register, and then select which adc channel to sample within the switch statement...the code below is just a small excerpt

    Code ( (Unknown Language)):
    1.  
    2. void main(void)
    3. {
    4.     InitialiseSystem();
    5.         adc_init();
    6.  
    7.     while(1)
    8.     {  
    9.         USBDeviceTasks();
    10.         ProcessIO();        
    11.     }
    12. }
    13.  
    14. void adc_init(void)
    15. {
    16.     ADCON1 = 0b00001101;//VSS,VDD ref. AN0 and AN1 analog only
    17.     ADCON0 = 0x00;//clear ADCON0 to select channel 0 (AN0)
    18.     ADCON2 = 0b00001000;//ADCON2 setup: Left justified, Tacq=2Tad, Tad=2*Tosc (or Fosc/2)
    19.     ADCON0bits.ADON = 0x01;//Enable A/D module
    20. }
    21.  
    22. void selectchannelan1(void)
    23. {
    24.     ADCON0 = 0x00;
    25. }
    26.                
    27. void selectchannelan2(void)
    28. {
    29.     ADCON0 = 0x04;
    30. }      
    31.  
    32. void ProcessIO(void)
    33. {  
    34.     if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return;
    35.    
    36.         if(!HIDRxHandleBusy(USBOutHandle))                                 
    37.             {  
    38.                 switch(ReceivedDataBuffer[0])
    39.             {
    40.  
    41.     case 0x04:                     
    42.        
    43.                 selectchannelan1();
    44.                     DelayUs(10);
    45.                         ADCON0bits.GO_DONE = 1;//Start A/D Conversion
    46.        
    47.                     while(ADCON0bits.GO_DONE != 0);
    48.                             ADC_VALUE_ARID = ADRESH;
    49.  
    50.             ToSendDataBuffer[0] = 0x04;
    51.                 ToSendDataBuffer[1] = ADC_VALUE_ARID;
    52.  
    53.                
    54.                 if(!HIDTxHandleBusy(USBInHandle))
    55.                 {
    56.                     USBInHandle = HIDTxPacket(HID_EP,(BYTE*)&ToSendDataBuffer[0],64);
    57.                 }
    58.                
    59.             DelayMs(5);
    60.            
    61. break;
    thanks for looking
     
  2. kutalinelucas

    Thread Starter Active Member

    Nov 20, 2007
    98
    0
    it's ok...you just need to disable the adc before selecting the channel...like this

    Code ( (Unknown Language)):
    1. void selectchannelan1(void)
    2. {
    3.     ADCON0bits.ADON = 0x00;
    4.     DelayUs(5);
    5.     ADCON0 = 0x00;
    6.     DelayUs(5);
    7.     ADCON0bits.ADON = 0x01;
    8. }
    9.                
    10. void selectchannelan2(void)
    11. {
    12.     ADCON0bits.ADON = 0x00;
    13.     DelayUs(5);
    14.     ADCON0 = 0x04;
    15.     DelayUs(5);
    16.     ADCON0bits.ADON = 0x01;
    17. }                  
     
Loading...