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

A/D reading with 16F877

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



Joined: 07 Sep 2003
Posts: 40
Location: Adelaide, Australia

View user's profile Send private message

A/D reading with 16F877
PostPosted: Thu Aug 14, 2003 11:53 am     Reply with quote

Hi! I built a simple thermometer that reads temperature from 0-50 degree. But problem I got is that the readings I get jump some values. As an example, A/D reads from 0 to 009F, than jumps to 0100. It doesn't read from 00A0 to 00FF. Same from 0100 to 0180, than jumps to 0200; and again from 0200 to 02A0, than jumps to 0300 and finally reads ok to 03FF. Sorry but I just don't understand with it does that. Any ideas?

Thanks a lot.

Here's the code.

void main()
{

int iTemp;
long lTemp;
float fTemp;

setup_adc_ports(A_ANALOG);
setup_adc(ADC_CLOCK_DIV_2);
setup_spi(FALSE);
setup_psp(PSP_DISABLED);
setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_OFF);
lcd_init();

lcd_gotoxy(1,1);
printf(LCD_PUTC, "Bonjour, il fait: ");
lcd_gotoxy(1,2);
printf(LCD_PUTC, "Valeur HEX: ");
lcd_gotoxy(21,1);
printf(LCD_PUTC, "Valeur C: ");

while(1)
{
delay_ms(100);
set_adc_channel(0);
delay_ms(100);
lTemp = Read_ADC();

lcd_gotoxy(12,2);
printf(LCD_PUTC, "\%4LX", lTemp);

fTemp = (lTemp / 20.48);
iTemp = ceil(fTemp);

lcd_gotoxy(30,1);
printf(LCD_PUTC, "\%2u", iTemp);
}
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516965
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: A/D reading with 16F877
PostPosted: Thu Aug 14, 2003 12:16 pm     Reply with quote

:=void main()
:= {
:=
:= int iTemp;
:= long lTemp;
:= float fTemp;
:=
:= setup_adc_ports(A_ANALOG);

This next line immediately draws my attention. The data sheet
says to use the Fosc/2 clock only if your crystal frequency
is at 1.25 MHz or below. What freq are you using ?

:= setup_adc(ADC_CLOCK_DIV_2);

:= setup_spi(FALSE);
:= setup_psp(PSP_DISABLED);
:= setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
:= setup_timer_1(T1_DISABLED);
:= setup_timer_2(T2_DISABLED,0,1);
:= setup_ccp1(CCP_OFF);
:= setup_ccp2(CCP_OFF);
:= lcd_init();
:=
:= lcd_gotoxy(1,1);
:= printf(LCD_PUTC, "Bonjour, il fait: ");
:= lcd_gotoxy(1,2);
:= printf(LCD_PUTC, "Valeur HEX: ");
:= lcd_gotoxy(21,1);
:= printf(LCD_PUTC, "Valeur C: ");
:=
:= while(1)
:= {
:= delay_ms(100);
:= set_adc_channel(0);
:= delay_ms(100);
:= lTemp = Read_ADC();
:=
:= lcd_gotoxy(12,2);
:= printf(LCD_PUTC, "\%4LX", lTemp);
:=
:= fTemp = (lTemp / 20.48);
:= iTemp = ceil(fTemp);
:=
:= lcd_gotoxy(30,1);
:= printf(LCD_PUTC, "\%2u", iTemp);
:= }
:= }
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516966
pat



Joined: 07 Sep 2003
Posts: 40
Location: Adelaide, Australia

View user's profile Send private message

Re: A/D reading with 16F877
PostPosted: Thu Aug 14, 2003 12:31 pm     Reply with quote

Oups, actually I'm using a 20MHz crystal.

Pat
:=:=void main()
:=:= {
:=:=
:=:= int iTemp;
:=:= long lTemp;
:=:= float fTemp;
:=:=
:=:= setup_adc_ports(A_ANALOG);
:=
:=This next line immediately draws my attention. The data sheet
:=says to use the Fosc/2 clock only if your crystal frequency
:=is at 1.25 MHz or below. What freq are you using ?
:=
:=:= setup_adc(ADC_CLOCK_DIV_2);
:=
:=:= setup_spi(FALSE);
:=:= setup_psp(PSP_DISABLED);
:=:= setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
:=:= setup_timer_1(T1_DISABLED);
:=:= setup_timer_2(T2_DISABLED,0,1);
:=:= setup_ccp1(CCP_OFF);
:=:= setup_ccp2(CCP_OFF);
:=:= lcd_init();
:=:=
:=:= lcd_gotoxy(1,1);
:=:= printf(LCD_PUTC, "Bonjour, il fait: ");
:=:= lcd_gotoxy(1,2);
:=:= printf(LCD_PUTC, "Valeur HEX: ");
:=:= lcd_gotoxy(21,1);
:=:= printf(LCD_PUTC, "Valeur C: ");
:=:=
:=:= while(1)
:=:= {
:=:= delay_ms(100);
:=:= set_adc_channel(0);
:=:= delay_ms(100);
:=:= lTemp = Read_ADC();
:=:=
:=:= lcd_gotoxy(12,2);
:=:= printf(LCD_PUTC, "\%4LX", lTemp);
:=:=
:=:= fTemp = (lTemp / 20.48);
:=:= iTemp = ceil(fTemp);
:=:=
:=:= lcd_gotoxy(30,1);
:=:= printf(LCD_PUTC, "\%2u", iTemp);
:=:= }
:=:= }
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516967
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

Re: A/D reading with 16F877
PostPosted: Thu Aug 14, 2003 12:43 pm     Reply with quote

:=Oups, actually I'm using a 20MHz crystal.
-------------------------------------------------------

Change it to this:

setup_adc(ADC_CLOCK_DIV_32);
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516968
pat



Joined: 07 Sep 2003
Posts: 40
Location: Adelaide, Australia

View user's profile Send private message

Re: A/D reading with 16F877
PostPosted: Thu Aug 14, 2003 9:17 pm     Reply with quote

Yesss! You're the man!
Thanks again for that!

Pat

:=:=Oups, actually I'm using a 20MHz crystal.
:=-------------------------------------------------------
:=
:=Change it to this:
:=
:=setup_adc(ADC_CLOCK_DIV_32);
___________________________
This message was ported from CCS's old forum
Original Post ID: 144516987
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