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

Timer0 weird count

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



Joined: 07 Oct 2008
Posts: 8

View user's profile Send private message

Timer0 weird count
PostPosted: Wed Oct 15, 2008 4:44 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Oct 15, 2008 5:11 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Oct 15, 2008 5:14 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Oct 15, 2008 5:20 pm     Reply with quote

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!!
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