CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Incorrect ADC values for PIC16F1939

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
POPE19



Joined: 27 Jun 2017
Posts: 71

View user's profile Send private message

Incorrect ADC values for PIC16F1939
PostPosted: Thu Sep 07, 2017 7:43 am     Reply with quote

Hello,

I am using pic16f1939 for adxl335. The adc values read by pic is incorrect with 3.3 v reference.

It should read:
x - 1.611v (499)
y - 1.578v (489)
z - 1.985v (615)

However on serial monitor it is displayed as:
x = 126 y = 126 z = 126

Code:

#include <16F1939.h>
#fuses HS ,NOVCAP, MCLR, NOWDT, PUT, NOIESO, BROWNOUT, NOFCMEN,PLL_SW
#use delay(crystal=20MHz, clock=20MHz)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#define INTS_PER_SECOND     76      // (20000000/(4*256*256))

int int_count=INTS_PER_SECOND;
long seconds=0;
long x,y,z;


//============================================================================//

#INT_TIMER0          //This function is called
void clock_isr()
{                       //every time the RTCC (timer0)
                        //overflows (255->0)
                        //For this program this is apx
                        //31 times per second.

   if(--int_count==0)
   {
      seconds++;
      int_count=INTS_PER_SECOND;
   }
}
//============================================================================//
void adc_cal()
{
    SET_ADC_CHANNEL (0); // select AN0 channel
    delay_us(30);       // delay for sampling time Tad.
    x = read_adc();     // read channel
    printf("X = %LU \t ",x);
               
    SET_ADC_CHANNEL (5); // select AN5 channel
    delay_us(30);
    y = read_adc();
    printf("Y = %LU \t ",x);
         
    SET_ADC_CHANNEL (12); // select AN12 channel
    delay_us(30);
    z= read_adc();
    printf("Z = %LU \n \r ",x);
       
}
//============================================================================//
void main()
{
   set_tris_a (0xC9); //set RA-0,3,6,7 as inputs
   set_tris_b (0xC9); //set RB-0,3,6,7 as inputs
   
   set_rtcc(0);
   setup_timer_0(T0_INTERNAL | T0_DIV_256 | T0_8_BIT);
       
   setup_vref (VREF_OFF | VREF_ADC_OFF|VREF_COMP_DAC_OFF|TEMPERATURE_INDICATOR_DISABLED);
   setup_adc (ADC_CLOCK_DIV_32);
   //select A0,E0,B0 as analog input & (Vref+ = 3.3v) as voltage reference
   setup_adc_ports(sAN0 | sAN5 | sAN12,VSS_VREF);
   setup_comparator (NC_NC_NC_NC);

   enable_interrupts (INT_TIMER0);
   enable_interrupts(GLOBAL);
         
   while (TRUE) 
   {
      if(seconds>1)
      {
          adc_cal();
          seconds =0;
      }
   }
   
}
newguy



Joined: 24 Jun 2004
Posts: 1907

View user's profile Send private message

Re: INCORRECT ADC VALUES FOR PIC16F1939
PostPosted: Thu Sep 07, 2017 7:47 am     Reply with quote

Code:
setup_adc_ports(sAN0 | sAN5 | sAN12,VSS_VREF);


This line is telling the processor that YOU are supplying an external positive voltage reference for the A/D into the PIC's VREF+ pin. Is that what you are doing? If you instead want to set up the A/D to use the PIC's positive supply as the positive reference, then this line needs to change. Open the PIC16F1939.h file and look for the A/D section for the proper constants to use in this function.
alan



Joined: 12 Nov 2012
Posts: 357
Location: South Africa

View user's profile Send private message

PostPosted: Thu Sep 07, 2017 7:58 am     Reply with quote

Is this correct?
Code:
y = read_adc();
    printf("Y = %LU \t ",x);


Shouldn't x be y. Same hold for z.[/b]
POPE19



Joined: 27 Jun 2017
Posts: 71

View user's profile Send private message

PostPosted: Thu Sep 07, 2017 8:13 am     Reply with quote

Yes i am supplying the positive voltage 3.3volts on pin 5 (RA3). i don't want to use pic internal voltage reference.

Yes i changed the printf commands but still it displays wrong values:

x = 126 y=126 z = 156

Code:

void adc_cal()
{
    SET_ADC_CHANNEL (0); // select AN0 channel
    delay_us(30);       // delay for sampling time Tad.
    x = read_adc();     // read channel
    printf("X = %LU \t ",x);
               
    SET_ADC_CHANNEL (5); // select AN5 channel
    delay_us(30);
    y = read_adc();
    printf("Y = %LU \t ",y);
         
    SET_ADC_CHANNEL (12); // select AN12 channel
    delay_us(30);
    z= read_adc();
    printf("Z = %LU \n \r ",z);
       
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19505

View user's profile Send private message

PostPosted: Thu Sep 07, 2017 8:21 am     Reply with quote

Now it makes sense...... Smile

#device ADC-=10

Without this it is defaulting to returning the 8bit ADC value.
POPE19



Joined: 27 Jun 2017
Posts: 71

View user's profile Send private message

PostPosted: Thu Sep 07, 2017 9:08 am     Reply with quote

It works !! Thank you.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group