1

I am trying to read the ADC value from the potentiometer in PIC24F Curiosity Development Board (PIC24FJ128GA204) then turn on the LED if the value more 1000 (I configured it as a 10-bit). However, The maximum value that place in the buffer is around 500. The following code shows the problem. Please advise.

#include <xc.h>
#define Pot_TriState _TRISC0
#define Pot_AnalogState _ANSC0

int main(void) {
    Pot_TriState = 1;
    Pot_AnalogState = 1;

    _TRISC5 = 0;
    _LATC5 = 0;
    ADC_Config();
    while (1) {
        if (ADC1BUF10 >= 1000) {
            _LATC5 = 0; //Never be executed 
        }
        if (ADC1BUF10 >= 300) {
            _LATC5 = 1;
        }
    }
    return 0;
}

void ADC_Config() {
    AD1CON1bits.ADON = 0; // ADC must be off when changing configuration
    //start conversion automatically after sampling and configure ADC to either 10 or 12 bits
    AD1CON1bits.SSRC = 7;
    AD1CON1bits.MODE12 = 0;
    AD1CON2bits.PVCFG = 0; //A/D Converter Positive Voltage Reference Configuration bits
    AD1CON2bits.NVCFG0 = 0; // A/D Converter Negative Voltage Reference Configuration bit
    AD1CHSbits.CH0SB = 0b01010; //01010 = AN10
    AD1CHSbits.CH0SA = 0b01010; //added
    AD1CON3bits.ADRC = 1; // 1 = RC clock --- ADC?s internal RC clock
    AD1CON3bits.SAMC = 0b11111; // set auto sampling time -- Auto-Sample Time Select bits11111 = 31 TAD
    AD1CON2bits.BUFREGEN = 1; //Conversion result is loaded into the buffer location determined by the converted channel
    AD1CON1bits.ASAM = 1;
    AD1CON1bits.ADON = 1;
}
  • I can't find definition of `ADC1BUF10 ` in above code . So I don't know what is the data type. – Sandy Dec 02 '19 at 10:01
  • 1
    As I also don't know how `ADCBUF10` is defined, this is just a guess: but, if it's a 10-bit ***signed*** integer, it's maximum value will be 511. – Adrian Mole Dec 02 '19 at 10:09
  • The result of the ADC conversion is found in a 16-bit AD1BUFx register. A/D result buffers are numbered in hexadecimal; ADC1BUF0 through ADC1BUFF represent Buffers 0 through 15. – Ibrahim Alnefisi Dec 02 '19 at 12:03
  • So if you have 16 buffers, numbered `0` to `F`, wouldn't `10` (as in `ADC1BUF10`) be invalid? – Some programmer dude Dec 02 '19 at 12:10
  • Will, it is exist, see PIC24FJ128GA204 datasheet if you do not mind. page (49 / 438). @Someprogrammerdude – Ibrahim Alnefisi Dec 02 '19 at 12:39
  • @Someprogrammerdude well, the point is there are 16 16-bits registers responsible to store the converted ADC value. They are act as global variables and they already defined. I configured the ADC to put its value in ADC1BUF10. – Ibrahim Alnefisi Dec 02 '19 at 16:30
  • Just to clarify your earlier comment, the buffers are *not* named `ADC1BUF0` to `ADC1BUFF` (with hexa-decimal numbering) but `ADC1BUF0` to `ADC1BUF15` (with decimal numbering)? Or are is it both (with aliases for e.g. `ADC1BUF10` and `ADC1BUFA`)? It's that inconsistency I would like clarified. Inconsistencies and ambiguity doesn't work well together with programming. – Some programmer dude Dec 02 '19 at 19:05
  • 1
    @Someprogrammerdude Yes, there is inconsistency with the naming. But this is what actually written in the datasheet. I tried to use A instead of 10 but it didn't work. – Ibrahim Alnefisi Dec 03 '19 at 07:40
  • Have you tried this: `AD1CON1bits.FORM = 0;`, in ADC_Config() ?? It usually pays to be explicit. – Michaël Roy Jan 11 '20 at 18:03

0 Answers0