Wrong Bits in ADC

General discussion on mikroC for dsPIC30/33 and PIC24.
Post Reply
Author
Message
andrea1984
Posts: 6
Joined: 02 Jul 2011 17:34

Wrong Bits in ADC

#1 Post by andrea1984 » 13 Jan 2012 19:54

Hi,
i'm using the 12 bit ADC in PIC24FV16KA301.
I power the PIC with 5V, and I set the ADC registers to work eith AN0 channel with 0-5V range input.
When I try to sample the AN0 channel with input 5V i get 1024, if 1V I get 200, and so (3V = 600).
It seems that is working as 10 bit ADC instead of 12 bit.
Even if one bit is for the sign (ADC on this PIC is very complex), there is one bit more for the positive range.

Here is the code:

Code: Select all

void main() 
{
    int count = 0;
    unsigned int adc_value;
    int i;
    
    ANSA = 0;
    ANSB = 0;
    OC1CON1 = 0;
    OC2CON1 = 0;
    SPI1STAT = 0;
    SPI2STAT = 0;
    I2C1CON = 0;
    I2C2CON = 0;
    RCFGCAL = 0;
    CTMUCON1 = 0;
    TRISB.RELE = 0;
    CM1CON = 0;
    
    TRISA.ADC = 1;
    TRISA.ADC_AMP = 1;
    
    //AD1CON1 = 0X8074;
    AD1CON1 = 0X8174;
    //AD1CON2 = 0X1800;
    AD1CON2 = 0X800;
    //AD1CON3 = 0X1F10;
    AD1CON3 = 0X9F3F;
    AD1CON5 = 0;
    AD1CHS  = 0;
    
    UART1_Init(2400);    // Initialize UART module at 9600 bps
    Delay_ms(1000);      // Wait for UART module to stabilize
    
    while (1)
    {
     UART1_Write_Text("ADC: ");
     adc_value = ADC1BUF0;
     
     for (i=11;i>=0;i--)
     {
      if ((adc_value & (1 << i)) != 0)
         UART1_Write_Text("1");
      else
          UART1_Write_Text("0");
       
     }
     
     //sprintf(txt, " = %d\r", adc_value);
     UART1_Write_Text("\r");
     Delay_ms(2000);
    }
}

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: Wrong Bits in ADC

#2 Post by janko.kaljevic » 16 Jan 2012 15:45

Hello,

Did you tried this with our library.
For example something like this:

Code: Select all

ADC1_Init_Advanced(_ADC_12bit, _ADC_INTERNAL_REF);  
adc_value = ADC1_Get_Sample(10);
Best regards.

andrea1984
Posts: 6
Joined: 02 Jul 2011 17:34

Re: Wrong Bits in ADC

#3 Post by andrea1984 » 16 Jan 2012 15:52

I found the iussue. There is an errata correge for this pic that enables ADC to work with 12 bit.

Post Reply

Return to “mikroC for dsPIC30/33 and PIC24 General”