View previous topic :: View next topic |
Author |
Message |
loginatnine
Joined: 07 Oct 2008 Posts: 8
|
Timer0 weird count |
Posted: Wed Oct 15, 2008 4:44 pm |
|
|
Hi
I'm using this code and from what I read, this code should increment the "time" variable each 1.024ms. But, from what I can see on my lcd, it seems to go 20 times slower than that, 0.05 on my lcd look like 1 second...I'm using a 4MHz crystal.
Code: |
#include<16F877A.H>
#device PASS_STRINGS = IN_RAM
#use delay(clock=4000000)
#include<stdio.h>
#fuses HS
void lcd_init(void);
void lcd_clear(void);
void lcd_print(char*);
float time=0;
char strtime[80];
void main(void)
{
lcd_init();
lcd_clear();
SET_TIMER0(0);
setup_timer_0(RTCC_INTERNAL|RTCC_8_BIT|RTCC_DIV_4);
while(1)
{
if(GET_TIMER0()==255)
{
time =time+0.001024;
sprintf(strtime,"%3.5f",time);
lcd_clear();
lcd_print(strtime);
}
}
}
|
Thanks! |
|
|
loginatnine
Joined: 07 Oct 2008 Posts: 8
|
|
Posted: Wed Oct 15, 2008 5:11 pm |
|
|
Ok I understand, thanks for your help. There is something else I'm wondering, I read that the timer0 is a 8bit timer so it counts up to 255 and then go back to 0. If this is true, how come this code write something on my lcd?
Code: | void main(void)
{
lcd_init();
lcd_clear();
SET_TIMER0(0);
setup_timer_0(RTCC_INTERNAL|RTCC_8_BIT|RTCC_DIV_256);
while(1)
{
if(GET_TIMER0()==277)
{
time =time+0.065536;
sprintf(strtime,"%3.5f",time);
lcd_clear();
lcd_print(strtime);
}
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Oct 15, 2008 5:14 pm |
|
|
The '277' value is truncated to an 8 bit value. It's 0x115 in hex notation.
When it's truncated to 8 bits, it becomes 0x15. Your code detects the
0x15 value in the if() statement. |
|
|
loginatnine
Joined: 07 Oct 2008 Posts: 8
|
|
Posted: Wed Oct 15, 2008 5:20 pm |
|
|
PCM programmer wrote: | The '277' value is truncated to an 8 bit value. It's 0x115 in hex notation.
When it's truncated to 8 bits, it becomes 0x15. Your code detects the
0x15 value in the if() statement. |
you rock, thanks a billion times for the very useful infos!! |
|
|
|