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.

dsPIC33f ADC reading voltage help

jemonlemon

Newbie
Joined
Jan 25, 2021
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
32
Hello,
I am an ECE student working with the PIC33F and I am having some difficult getting the ADC to work. I want to read the voltage from the AN1 pin and I made sure that the AN1 pin is getting a selected voltage between 0-3.3V.
I followed the instructions on the Mircochip manual, I feel as I missed some steps as I cannot get the ADC functioning.

Here is what I have for my initialization:
Code:
void ADC_Init()
{
    TRISAbits.TRISA1 = 1;
    AD1CON1bits.ADON = 0;
    AD1CON1bits.ADSIDL = 0;
    AD1CON1bits.AD12B = 1;

    // a) Select port pins as analog inputs
    AD1PCFGL = 0;

    // b) Select voltage reference source
    AD1CON2bits.VCFG = 0;

    // c) Select analog conversion clock
    AD1CON3bits.ADCS = 0;

    // d) Determine how many S/H channels is used
    AD1CON2bits.CHPS = 0;

    // e) Select the appropriate sample/conversion sequence
    AD1CON1bits.SSRC = 0b000;
    AD1CON3bits.SAMC = 0;

    // f) Select how conversion results are presented in the buffer
    AD1CON1bits.FORM = 0b11;

    // g) Turn on ADC module
    AD1CON1bits.ADON = 1;
}

Here is what is in my main.c:
Code:
int main() {
    ...
    // Retrieve voltage
    AD1CON1bits.SAMP = 1;
   _delay_us(10);
   AD1CON1bits.SAMP = 0;
   while(!AD1CON1bits.DONE);
   double ADCValue = ADC1BUF0;
   ...
}

While debugging, I find that when the voltage at AN1 is near 0, the ADCValue equals 32768.0. When the voltage is higher than 1, it gives 32848.0. When voltage is around 0.5, it gives 32816.0. If anyone has some insight, I greatly appreciate it.

Thank you in advance.
 
Last edited:

MichalP

Junior Member level 1
Joined
Jul 16, 2018
Messages
17
Helped
1
Reputation
2
Reaction score
1
Trophy points
3
Location
Krakow
Activity points
78
I think 0 is writing as 32768 , so you need to define this as zero and perform some calc and you will have real values couse 32816 seems to be half
 

ads-ee

Super Moderator
Staff member
Joined
Sep 10, 2013
Messages
7,805
Helped
1,809
Reputation
3,628
Reaction score
1,768
Trophy points
1,393
Location
USA
Activity points
58,918
According to the datasheet the dsPIC33f has a 10-bit or 12-bit ADC so the value 32768 has bit-15 set, which is outside the maximum value of the ADC output. I suspect you aren't properly reading the value and masking some other bits that are probably status of some sort. Only the lower 12-bit (or 10-bits) should be ADC conversion data.
 

jemonlemon

Newbie
Joined
Jan 25, 2021
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
32
I think 0 is writing as 32768 , so you need to define this as zero and perform some calc and you will have real values couse 32816 seems to be half

Ah I see that I may have to add calculations to get the proper value. Although one problem I mention in the next quote.

According to the datasheet the dsPIC33f has a 10-bit or 12-bit ADC so the value 32768 has bit-15 set, which is outside the maximum value of the ADC output. I suspect you aren't properly reading the value and masking some other bits that are probably status of some sort. Only the lower 12-bit (or 10-bits) should be ADC conversion data.

I totally missed that. I ended up masking the 13-15 bits. I do run into a new problem where I am getting is that the values I get from the ADC are inconsistent (when AN1 is getting 2V, the ADC either gives 128.0 or 96.0). Here are some values I get:

AN1 (Pin 20) input voltagedouble ADCValue (after masking for only bits 0-11)
0.023280.0
0.53164.0
1.0264.0 or 96.0 or 16.0 or 48.0
1.968128.0 or 96.0
 

LaTeX Commands Quick-Menu:

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top