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 support@ccsinfo.com

adc oscillating problem

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



Joined: 25 Jul 2012
Posts: 1

View user's profile Send private message

adc oscillating problem
PostPosted: Wed Jul 25, 2012 11:51 am     Reply with quote

Hello friends, could anyone tell me if this is right?
Code:

#include <16F876A.h>
#device adc=8
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES XT                       //Crystal osc <= 4mhz for PCM/PCH , 3mhz to 10 mhz for PCD
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected
#use delay(clock=4000000)

#include <lcd_flex.C>

int16  value_adc0 = 0;
char value_adc_0[6];

void  read_adc0()                   
   {
      set_adc_channel(0);
      delay_ms(10);
      value_adc0 = read_adc() ;       
      value_adc0=((value_adc0*0.03937007874015748031496062992126)*100); //(5/254)*100

    sprintf(value_adc_0,"%05lu",value_adc0);
   }
   
   void print_adc0()
   {
      lcd_putc(value_adc_0[2]);
      lcd_putc(value_adc_0[3]);
   }

void main()
{
   setup_adc_ports(AN0_AN1_AN3);
   setup_adc( ADC_CLOCK_DIV_8 );
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_8);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   enable_interrupts(INT_RTCC);
   enable_interrupts(GLOBAL);

   lcd_init();

while(true)
{
lcd_putc('\f');
read_adc0();
print_adc0();
lcd_gotoxy(1,1);
delay_ms(200);
}
}

I always have problem with this code, the ADC value is always fluctuating, what's wrong? I need help, please.

Thank you
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jul 25, 2012 3:09 pm     Reply with quote

Try a more simple test program for the ADC:
http://www.ccsinfo.com/forum/viewtopic.php?t=42090

Here are some threads with reasons for unstable ADC readings:
http://www.ccsinfo.com/forum/viewtopic.php?t=29875
http://www.ccsinfo.com/forum/viewtopic.php?t=43997
http://www.ccsinfo.com/forum/viewtopic.php?t=25540
http://www.ccsinfo.com/forum/viewtopic.php?t=44708
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Wed Jul 25, 2012 4:11 pm     Reply with quote

Quote:
value_adc0=((value_adc0*0.03937007874015748031496062992126)*100); //(5/254)*100


Where did that number come from? 5/254 is about 0.02, nowhere near 0.039. Floating point math in the PIC is only good to a little better than 6 digits so anything beyond 0.03937008 is rather pointless.

Is there a reason you force value_adc0 * 0.03937008 THEN * 100, instead of letting the compiler precalculate 0.03937008 * 100 so the PIC only has to actually do value_adc0 * 3.937008? It consumes more CPU cycles with no benefit I can see.
_________________
The search for better is endless. Instead simply find very good and get the job done.
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