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 CCS Technical Support

digital clock using 18F4550

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



Joined: 07 Jun 2009
Posts: 8

View user's profile Send private message Yahoo Messenger

digital clock using 18F4550
PostPosted: Sun Jul 12, 2009 3:57 am     Reply with quote

hie....

I tried to count the second but its not working.
I just take a look on some examples that is given in the CCS compiler but my program does not count the second properly. It becomes slow.

Anybody can give me some tips about this?
Code:

#define INTS_PER_SECOND 76
.......
#int_rtcc                         
void clock_isr() {             
                                   
    if(--int_count==0)
    { 
      ++seconds;
      int_count=INTS_PER_SECOND;
     
         TIME[S_]++;
         if(TIME[S_] >= 60)
         {
            TIME[S_]=0;
            TIME[M_]++;
         }
         if(TIME[M_] >= 60)
         {
            TIME[M_]=0;
            TIME[H_]++;
         }
         if(TIME[H_] >= 12)
         {
            TIME[H_]=0;
         }
    }
}

void main()
{
..........
int_count=INTS_PER_SECOND;
   set_timer0(0);
   setup_counters( RTCC_INTERNAL, RTCC_DIV_256 | RTCC_8_BIT);
   enable_interrupts(INT_RTCC);
   enable_interrupts(GLOBAL);
......
}
akhter900



Joined: 07 Jun 2009
Posts: 8

View user's profile Send private message Yahoo Messenger

PostPosted: Sun Jul 12, 2009 5:07 am     Reply with quote

How can I count the exact value for ...
INTS_PER_SECOND
???
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun Jul 12, 2009 7:26 am     Reply with quote

You didn't mention which CCS example program you used as a starting point but searching the directory I found only ex_stwt.c to have INTS_PER_SECOND defined as 76.
Now, if you look into the program you will see the following line:
Code:
#define INTS_PER_SECOND 76     // (20000000/(4*256*256))
Where:
- 20000000 is the clock speed of the processor
- 4 is the number of clocks to execute a single instruction. Always 4 for the PIC10 to PIC18 processors.
- The last two numbers 256 come from the line:
Code:
setup_counters( RTCC_INTERNAL, RTCC_DIV_256 | RTCC_8_BIT);
- An 8 bit counter counts up to 256 before restarting at 0.
- A post divider of 256 decreases the number of interrupts by 256.

Assuming you have an 8MHz clock your calculation for the number of interrupts would become:
8000000 / (4 * 256 * 256) = 30.51
Set the number of interrupts to 30 or 31. You will get an error of 1.6% or 23 minutes/day. For most applications this is no problem.
For higher accuracy you could change the post divider to 128, this will give you:
8000000 / (4 * 256 * 128) = 61.04
Setting the number of interrupts per second to 61 reduces the error to 0.04% = 56seconds/day.
Depending on your actual clock rate you can play a bit with the numbers to get a more exact clock but I recommend to keep the number of interrupts below 200 or the interrupt overhead will become too high.

Another approach for a really accurate clock can be found in: http://www.ccsinfo.com/forum/viewtopic.php?t=26177
akhter900



Joined: 07 Jun 2009
Posts: 8

View user's profile Send private message Yahoo Messenger

PostPosted: Wed Jul 15, 2009 11:17 pm     Reply with quote

Thanks a lot for the details.

I use the line.... and its working well.
Code:

#define INTS_PER_SECOND 183

Thanks
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