Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] PIC32 MX795F512L - ADC problem

Status
Not open for further replies.

newtonsrl

Junior Member level 3
Joined
Jan 28, 2016
Messages
29
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
222
Hi,
I made a custom board based on Microchip MEB (DM320005) and PIC32 Ethernet Starter Kit that use the MCU in subject.
My board has a touch screen that works fine, connected to AN11 AN12 AN13 and AN14.
The internal ADC10 of the pic convert the analog signal to X and Y position.
My board is powered by a Li-ion battery 3.7V and I need to know the battery voltage to have simple indication of the battery state, so I connected the battery to AN2 through a voltage divider.
I'm using the microchip touchscreen driver included in microchip framework V. 2013-06-15.
My problem is I cannot get the correct value of the adc for the AN2 channel. To do some tests I used the code below, took from the Microchip ADC reference:

Code:
void ReadBatteryAdc()
{
    //disable timer 4
    T4CONbits.TON = 0;
    
    CloseADC10();
    //mPORTBSetPinsAnalogIn(BIT_2);
    //ADC_POT_PCFG = RESISTIVETOUCH_ANALOG;
    
    TRISBbits.TRISB11 = 0;
    TRISBbits.TRISB12 = 0;
    TRISBbits.TRISB13 = 0;
    TRISBbits.TRISB14 = 0;
    TRISBbits.TRISB2 = 0;
    LATBbits.LATB11 = 0;
    LATBbits.LATB12 = 0;
    LATBbits.LATB13 = 0;
    LATBbits.LATB14 = 0;
    LATBbits.LATB2 = 1;
    AD1CHSSET = 0x0020000;

    AD1CON1 = 0;            // reset
    AD1CON2 = 0;            // AVdd, AVss, int every conversion, MUXA only
    AD1CON3 = 0x1FFF;       // 31 Tad auto-sample, Tad = 256*Tcy
    AD1CON1 = 0x80E0;       // Turn on A/D module, use auto-convert
    
    
    AD1CSSL = 0;            // No scanned inputs

    
    DelayMs(100);
    AD1CON1bits.SAMP = 1;
    DelayMs(100);
    while(AD1CON1bits.DONE == 0);
    
    vBitLevel = ADC1BUF0;


    //enable timer4
    TickTouchInit();
    TouchHardwareInit(NULL);
}

just for test, I set the pin RB2/AN2 as digital output, so when the pin is in high state I should read 0x3FF, and when is low I should read 0x00.
Changing the state of LATB2 the result of the ADC conversion doesn't change, and the result is always 0x00.
Changing the state of the LATB11 the conversion result change to 0x3FF when high and 0x00 when low.

I've already verified the PCB and there is no mistake or other problem, no crosstalk or other things.
Could someone help me to figure out please?
Thank you very much.
 

Are you running the MCU directly from the battery or through a LDO regulator?
The reason I ask is that you are setting AD1CON2 such that it will use AVdd as Vrefh and AVss as Vrefl. In other words you will always be measuring the battery voltage against a reference which is the battery voltage!
You may need to use a regulator to provide yourself with a lower reference value and then compare that against the battery voltage as seen through a voltage divider.
Susan
 

Hi Susan,
thanks for your reply.
I'm using my PIC32 with step-up switching and LDO regulator because in my board I need also 5V for analogic and 3.3V to supply the pic and other circuitry.

- - - Updated - - -

Now the code in my first post works correctly, the problem was the AD1CHSSET doesn't work for me. If I set AD1CHS after AD1CSSL instuction it works. This is just for test, because I set RB2 as digital output.
If i set it as analog input, and I have 0V to the input pin, my adc conversion result is always a value around 980(decimal).

Code:
void ReadBatteryAdc()
{
    //disable timer 4
    T4CONbits.TON = 0;

    CloseADC10();

    TRISBSET = 0x04;
    AD1PCFG = 0xFFFB;      //Set only RB2 as analog input
    

    AD1CON1 = 0;            // reset
    AD1CON2 = 0;            // AVdd, AVss, int every conversion, MUXA only
    AD1CON3 = 0x1FFF;       // 31 Tad auto-sample, Tad = 256*Tcy
    AD1CON1 = 0x80E0;       // Turn on A/D module, use auto-convert
    
    
    AD1CSSL = 0;            // No scanned inputs
    AD1CHS = 0x0020000;
    
    DelayMs(100);
    AD1CON1bits.SAMP = 1;
    DelayMs(100);
    while(AD1CON1bits.DONE == 0);
    
    vBitLevel = ADC1BUF0;


    //enable timer4
    TickTouchInit();
    TouchHardwareInit(NULL);
}
 
Last edited:

Hi,

Are all ADC related pins at recommended voltage level?
Especially check that no ADC related input (used or not) is never left floating.

Klaus
 

In my code I tried to exclude all other pin than AN2.
AN2 is connected to a voltage divider (1k/1k), and the low side resistor is in parallel with a 100uF capacitor.
Which other pin I should check?
Thanks a lot.
 

Hi,

In my code I tried to exclude all other pin than AN2.
I didn't mean code. I meant hardware.

Which other pin I should check?
As said: ALL ADC related pins.

Klaus
 

On the same board there is the touch screen, that is working correctly.
This is a working project. The last part is the battery monitor, that's doesn't work due to adc problem.
Avdd is connected directly to vdd, like in the Microchip Multimedia Expansion Board (DM320005)
Avss same like above. Both works as reference for adc.
Power supply is 3.265V, and stable. With scope I see 2mV of ripple, I think it is not a problem.
I cannot figure out....
 

Problem solved!
Everything caused by a bad contact in a connector I have between the two board.
Thanks a lot to everybody helped me.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top