PIC18F26K22: Can't get the ADC to work using C18 Compiler

Discussion in 'Embedded Systems and Microcontrollers' started by madisonmccarthy, Jun 24, 2016.

  1. madisonmccarthy

    Thread Starter New Member

    Jun 22, 2016
    2
    0
    Hello everyone,

    I'm having a great deal of difficulties correctly configuring the ADC with this microcontroller. The PIC18 is embedded into a prototype board which limits my ANx pin usage to AN14, AN12, and a few others. I have tried supplying varying voltages to pin AN12 (the one I chose to use) and my circuit resistance is roughly 4.6K Ohms.

    After scanning many forums as well as thoroughly reading the document sheet, I have been left with the code attached below

    The output that I receive is a random numbers in the range of 0x10 - 0x20 with or without a voltage supplied. At one point, I was reading all zeros instead of random numbers.

    I feel as though I am either missing a key step in this configuration, or have a problem hidden within the code I did not include. The board is also configured with a radio, however I did check the configurations and none of them overlapped with my configurations.

    I would greatly appreciate any advice as to where to look for an issue, or what I may be forgetting to do!


    Code (Text):
    1.  
    2. #pragma config FOSC = INTIO67, PLLCFG = ON, PRICLKEN = ON, FCMEN = OFF
    3. #pragma config PBADEN = ON, IESO = OFF, PWRTEN = ON, MCLRE = EXTMCLR
    4. #pragma config BOREN = OFF, HFOFST = OFF, T3CMX = PORTC0, STVREN = ON, WRTC = ON, WRTD = OFF
    5. #pragma config WDTEN = SWON, WDTPS = 8192, LVP = OFF, DEBUG = OFF
    6. ....
    7. OSCCON = 0x70;                    //Configure oscillator    
    8. OSCCON2 = 0x84;                    //set to Fcy = 64 MHz, Fosc = 16 MHz
    9. OSCTUNE = 0x40;   //??
    10.  
    11. PORTBbits.AN12 = 1; //Enable AN12??
    12. TRISBbits.RB0 = 1; // Enable RB0 as input
    13. ANSELBbits.ANSB0 = 1; // Enable pin as analog
    14.    
    15.  ADCON0bits.CHS = 0b01100; //Setting the Channel as AN12
    16.  ADCON1bits.NVCFG = 0; // Using Vdd
    17.  ADCON1bits.PVCFG = 0; // Using Vss
    18.  ADCON2bits.ADCS = 0b101; // Setting clock speed
    19.  ADCON2bits.ACQT = 0b101; // Setting aquisition time
    20.  ADCON2bits.ADFM = 1; // Right Justify
    21.  ADCON0bits.ADON = 1;    /* A/D Enabled */
    22. ....
    23. ADCON0bits.GO=1; // Start Conversion
    24.                        
    25. while (ADCON0bits.GO==1); //Polling until done
    26.  
    27. value = ADRESL; // Store the value into uint8 Data Type
    28. ....
     
  2. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    2,907
    2,165
    I've got a old C18 25k22 project that configs analog inputs. Maybe this will give you a hint at what's wrong, I haven't looked at the code for a while so it might be buggy.
    Code (Text):
    1.  
    2. #ifdef P25K22
    3. OSCCON = 0x70; // internal osc 16mhz, CONFIG OPTION 4XPLL for 64MHZ
    4. OSCTUNE = 0xC0; // 4x pll
    5. TRISC = 0b11111100; // [0..1] outputs for DIAG leds [2..7] for analog
    6. LATC = 0x00; // all LEDS on
    7. TRISAbits.TRISA6 = 0; // CPU clock out
    8. TRISBbits.TRISB1 = 1; // SSP2 pins clk in SLAVE
    9. TRISBbits.TRISB2 = 1; // SDI
    10. TRISBbits.TRISB3 = 0; // SDO
    11. TRISBbits.TRISB0 = 1; // SS2
    12. /* ADC channels setup */
    13. TRISAbits.TRISA0 = HIGH; // an0
    14. TRISAbits.TRISA1 = HIGH; // an1
    15. TRISAbits.TRISA2 = HIGH; // an2
    16. TRISAbits.TRISA3 = HIGH; // an3
    17. TRISAbits.TRISA5 = HIGH; // an4
    18. TRISBbits.TRISB4 = HIGH; // an11
    19. TRISBbits.TRISB0 = HIGH; // an12 SS2, don't use for analog
    20. TRISBbits.TRISB5 = HIGH; // an13
    21. TRISCbits.TRISC2 = HIGH; // an14
    22. TRISCbits.TRISC3 = HIGH; // an15
    23. TRISCbits.TRISC4 = HIGH; // an16
    24. TRISCbits.TRISC5 = HIGH; // an17
    25. TRISCbits.TRISC6 = HIGH; // an17
    26. TRISCbits.TRISC7 = HIGH; // an18
    27. TRISBbits.TRISB4 = 1; // QEI encoder inputs
    28. TRISBbits.TRISB5 = 1;
    29. TRISBbits.TRISB6 = 1;
    30. TRISBbits.TRISB7 = 1;
    31. ANSELA = 0b00101111; // analog bit enables
    32. ANSELB = 0b00110000; // analog bit enables
    33. ANSELC = 0b11111100; // analog bit enables
    34. VREFCON0 = 0b11100000; // ADC voltage ref 2.048 volts
    35. OpenADC(ADC_FOSC_64 & ADC_RIGHT_JUST & ADC_12_TAD, ADC_CH0 & ADC_INT_ON, ADC_REF_FVR_BUF & ADC_REF_VDD_VSS); // open ADC channel
    36. #endif
    37.  
    https://github.com/nsaspook/daq_gert/blob/master/RPi_PIC/SlaveO.c
     
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    First thing is line 11: PortB is the digital output data latch. Changing it has no effect when the port is an input or in analog mode. That is minor.

    Major is how you read the data: the conversion results in a 10 bit number sperad over two registers, see figure 17-2 of the data sheet. To use just the 8 bit part you need to left justify the result and read from ADRESH. Reading from a right justified result from the low register yields garbage as the two most significant bits are lost.
     
  4. madisonmccarthy

    Thread Starter New Member

    Jun 22, 2016
    2
    0
    Hi ErnieM,

    Thanks for the advice with the digital data latch, I will remove that line since it is useless. In regards to the 10 bit number spread over two registers, I am aware that I am using only the lower 8 bits of information. My application (when correctly running) will always result in the two bits stored in ADRESH always being 1 due to my voltage range, which means I no longer care about their values.

    Thank you for your code nsaspook, I will comb through it today and see if I can fix my errors!
     
Loading...