View previous topic :: View next topic |
Author |
Message |
zork
Joined: 25 Jul 2012 Posts: 1
|
adc oscillating problem |
Posted: Wed Jul 25, 2012 11:51 am |
|
|
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
|
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Jul 25, 2012 4:11 pm |
|
|
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. |
|
|
|