• AVR Freaks

Hot!Getting the ADC to work (dsPIC33F)

Author
jhcho
New Member
  • Total Posts : 6
  • Reward points : 0
  • Joined: 2021/01/15 14:00:32
  • Location: 0
  • Status: offline
2021/01/25 14:33:10 (permalink)
0

Getting the ADC to work (dsPIC33F)

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:
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:
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.
#1

11 Replies Related Threads

    du00000001
    Just Some Member
    • Total Posts : 4114
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Getting the ADC to work (dsPIC33F) 2021/01/25 22:38:23 (permalink)
    0
    I'm not absolutely sure and do not have the time to try to reproduce the issue, but
        double ADCValue = ADC1BUF0;

    might not yield what you expect.
     
    If you insist in using floating point arithmetics, try
        double ADCValue = (double)ADC1BUF0;

    instead.

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #2
    jhcho
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2021/01/15 14:00:32
    • Location: 0
    • Status: offline
    Re: Getting the ADC to work (dsPIC33F) 2021/01/26 11:56:21 (permalink)
    0
    Thanks for your input. Unfortunately casting it still gives the same output.
    #3
    jhcho
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2021/01/15 14:00:32
    • Location: 0
    • Status: offline
    Re: Getting the ADC to work (dsPIC33F) 2021/01/26 15:07:04 (permalink)
    0
    I ended up masking the 13-15 bits to get more proper values. However, 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:
     
    Voltage into AN1 pin and ADC output
    0.0232 = 80.0
    0.531 = 64.0
    1.02 = 16.0 or 48.0 or 64.0 or 96.0
    1.98 = 128.0 or 96.0
     
    The values do change when I change the input voltage into AN1, but when getting above 1.02V, the ADC output is inconsistent.
    #4
    ric
    Super Member
    • Total Posts : 29861
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Getting the ADC to work (dsPIC33F) 2021/01/26 22:17:50 (permalink)
    5 (2)
    Why not sidestep the whole floating point issue to start with, and just output the raw hex value you are reading from ADC1BUF0 ?

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #5
    Howard Long
    Super Member
    • Total Posts : 887
    • Reward points : 0
    • Joined: 2005/04/04 08:50:32
    • Status: offline
    Re: Getting the ADC to work (dsPIC33F) 2021/01/27 04:17:51 (permalink)
    0
    Please let us know the exact device you're using.
    #6
    oe8fnk
    Old Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2014/12/16 07:34:34
    • Location: 0
    • Status: offline
    Re: Getting the ADC to work (dsPIC33F) 2021/01/27 05:15:54 (permalink)
    0
    "dspic33f" refers to abt. 80 microcontrollers with 2 different ADCs (the GS are different from the rest) so this is a guess: 
    "AD1CON3bits.ADCS = 0;  "
    is very unusual. Please do the math and set it to an intended value
    and give full 22 digits of device#
     
     
     
    #7
    jhcho
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2021/01/15 14:00:32
    • Location: 0
    • Status: offline
    Re: Getting the ADC to work (dsPIC33F) 2021/01/27 11:57:24 (permalink)
    0
    Sorry I should have mentioned this. I am using the dsPIC33FJ64MC804.
     
    Also, the raw values I get from ADC1BUF0 are the binary version of the numbers above with the 15th bit ticked.
    #8
    ric
    Super Member
    • Total Posts : 29861
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Getting the ADC to work (dsPIC33F) 2021/01/27 12:14:49 (permalink)
    5 (1)
    jhcho
    I ended up masking the 13-15 bits to get more proper values.

    Do you mean you are forcing the bottom three bits to zero?
    They would normally be called "Bits 2 to 0", not "13-15".
    From the values you are reporting, it would appear you are actually masking the lowest four bits, so all your results are multiples of 16.
    This will hide a bit of residual noise, but any noise that straddles a multiple of 16 is now amplified to be a whole 16 count.
    e.g. 31 -> 16, 33 -> 32
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #9
    jhcho
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2021/01/15 14:00:32
    • Location: 0
    • Status: offline
    Re: Getting the ADC to work (dsPIC33F) 2021/01/27 13:12:08 (permalink)
    0
    ric
    jhcho
    I ended up masking the 13-15 bits to get more proper values.

    Do you mean you are forcing the bottom three bits to zero?
    They would normally be called "Bits 2 to 0", not "13-15".
    From the values you are reporting, it would appear you are actually masking the lowest four bits, so all your results are multiples of 16.
    This will hide a bit of residual noise, but any noise that straddles a multiple of 16 is now amplified to be a whole 16 count.
    e.g. 31 -> 16, 33 -> 32



    Sorry, I meant that I am masking bits 12-15 as I'm using a 12-bit ADC. I had: double ADCValue = ADC1BUF0 & 0b0000111111111111. I did more testing and came to the conclusion I didn't need this mask or it was used wrong.
     
    AN1 Voltage - ADCValue with mask - ADC1BUF0 raw value > upper 8 bits to decimal
    0.000 - 0.0000 - 1000000000000000 > 128
    0.541 - 2032.0 - 1010011111110000 > 167
    0.919 - 4080.0 - 1011111111110000 > 191
    1.384 - 2944.0 - 1110101110000000 > 235
    1.403 - 3584.0 - 1110111000000000 > 238
    1.929 - 1792.0 - 0001011100000000 > 23
    post edited by jhcho - 2021/01/27 13:16:57
    #10
    oe8fnk
    Old Member
    • Total Posts : 8
    • Reward points : 0
    • Joined: 2014/12/16 07:34:34
    • Location: 0
    • Status: offline
    Re: Getting the ADC to work (dsPIC33F) 2021/01/27 14:55:53 (permalink)
    5 (1)
    " AD1CON1bits.FORM = 0b11; "  means   "0b11 = Signed fractional".
    This is the "signed fractional" data format.  Try integer format first.
     See also dspic33f family reference manual 70621.
    post edited by oe8fnk - 2021/01/27 15:07:45
    #11
    jhcho
    New Member
    • Total Posts : 6
    • Reward points : 0
    • Joined: 2021/01/15 14:00:32
    • Location: 0
    • Status: offline
    Re: Getting the ADC to work (dsPIC33F) 2021/01/27 15:07:57 (permalink)
    0
    oe8fnk
    " AD1CON1bits.FORM = 0b11; "  means   "0b11 = Signed fractional".
    This is the "signed fractional" data format.  Try integer format first.
     


    Yes I realized that awhile ago. Signed fractional is in sddddddddddd0000 format so I went off that. The values I'm getting are now linear. The only problem I have is that when the voltage gets near 1.9V, it turns negative. For now, I am using unsigned fractional and am using a formula to obtain values close to the actual AN1 voltage.
     
    I'm trying integer format now. Thank you.
     
     
    #12
    Jump to:
    © 2021 APG vNext Commercial Version 4.5