View previous topic :: View next topic |
Author |
Message |
jim garvin Guest
|
Using the pic for a clock |
Posted: Wed Aug 27, 2003 1:34 pm |
|
|
I have been trying to make a clock out of a pic and I am
having some problems understanding how to use the
set_timerO() function.
With a 20Mhz xtal, if I do set_timer0(156) that
should give me a 20us overflow. So does that mean it will hit
my interupt function ever 20us? And if so do I do a put a counter
in that function to count from 50,000 to 0 for 1 second?
(1sec/20us = 50,000) Is there an easier way?
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517341 |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
Re: Using the pic for a clock |
Posted: Wed Aug 27, 2003 1:43 pm |
|
|
:=I have been trying to make a clock out of a pic and I am
:=having some problems understanding how to use the
:=set_timerO() function.
:= With a 20Mhz xtal, if I do set_timer0(156) that
:=should give me a 20us overflow. So does that mean it will hit
:=my interupt function ever 20us? And if so do I do a put a counter
:=in that function to count from 50,000 to 0 for 1 second?
:=(1sec/20us = 50,000) Is there an easier way?
Search the forum for RTC. Their examples using timer2 to keep time with zero long term drift or at least as good as the xtl.
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517342 |
|
|
jim garvin Guest
|
Re: Using the pic for a clock |
Posted: Wed Aug 27, 2003 1:52 pm |
|
|
<font face="Courier New" size=-1>I searched and did not see a good example.
Here is my code but it is off by a 30 seconds in 3 hours
#include <16f77.h>
#device *=16
#fuses HS,WDT,NOPROTECT
#use delay(clock=20000000) // 20Mhz crystal
#use rs232(baud=9600, xmit=PIN_E0, rcv=PIN_E1,PARITY=N,BITS=8,INVERT)
#define INTS_PER_SECOND 76 // (20000000/(4*256*256))
int seconds; // running seconds counter
int minutes; // running minutes counter
int hours; // running hours counter
int countdown; // interrupts left in a second
#int_rtcc
clock()
{
restart_wdt();
// 76 times per second.
if(--countdown==0)
{
++seconds;
countdown=INTS_PER_SECOND;
if(seconds >= 60)
{
seconds=0;
minutes++;
}//if second
if(minutes >= 60)
{
minutes=0;
hours++;
}//if minutes
if(hours > 12)
{
hours=1;
}//if hours
}// if a second passed
}//clock
//////////////////// MAIN //////////////////////////////////
//
main()
{
int last_second=0; // used to check if the seconds have change
char MinO; // leading 0 on Minutes display
char SecO; // leading 0 on Seconds display
countdown = INTS_PER_SECOND;
setup_wdt(WDT_288MS);
set_rtcc(50);
setup_counters( RTCC_INTERNAL, RTCC_DIV_256);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);
hours =4;
minutes =5;
seconds =0;
while(1)
{
if(seconds != last_second)
{
printf("\%d:\%02U:\%02U \n\r",hours,minutes,seconds);
last_second = seconds;
}//if second has changed
}// while(1)
}//main()</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517343 |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
Re: Using the pic for a clock |
Posted: Wed Aug 27, 2003 2:31 pm |
|
|
:=<font face="Courier New" size=-1>I searched and did not see a good example.
:=Here is my code but it is off by a 30 seconds in 3 hours
:=#include <16f77.h>
:=#device *=16
:=#fuses HS,WDT,NOPROTECT
:=#use delay(clock=20000000) // 20Mhz crystal
:=#use rs232(baud=9600, xmit=PIN_E0, rcv=PIN_E1,PARITY=N,BITS=8,INVERT)
:=
:=#define INTS_PER_SECOND 76 // (20000000/(4*256*256))
:=
:=int seconds; // running seconds counter
:=int minutes; // running minutes counter
:=int hours; // running hours counter
:=int countdown; // interrupts left in a second
:=
:=
:=#int_rtcc
:=clock()
:={
:= restart_wdt();
:= // 76 times per second.
:= if(--countdown==0)
:= {
:= ++seconds;
:= countdown=INTS_PER_SECOND;
:= if(seconds >= 60)
:= {
:= seconds=0;
:= minutes++;
:= }//if second
:=
:= if(minutes >= 60)
:= {
:= minutes=0;
:= hours++;
:= }//if minutes
:=
:= if(hours > 12)
:= {
:= hours=1;
:= }//if hours
:=
:= }// if a second passed
:=}//clock
:=
:=//////////////////// MAIN //////////////////////////////////
:=//
:=main()
:={
:= int last_second=0; // used to check if the seconds have change
:= char MinO; // leading 0 on Minutes display
:= char SecO; // leading 0 on Seconds display
:=
:= countdown = INTS_PER_SECOND;
:= setup_wdt(WDT_288MS);
:= set_rtcc(50);
:= setup_counters( RTCC_INTERNAL, RTCC_DIV_256);
:=
:= enable_interrupts(INT_RTCC);
:= enable_interrupts(GLOBAL);
:=
:= hours =4;
:= minutes =5;
:= seconds =0;
:=
:= while(1)
:= {
:= if(seconds != last_second)
:= {
:= printf("\%d:\%02U:\%02U \n\r",hours,minutes,seconds);
:= last_second = seconds;
:= }//if second has changed
:=
:= }// while(1)
:=
:=}//main()</font>
Looking at the math the 20000000/4*256*256 is 76.29394531
so there is .0038 \% error over the integer value 76. Now 3 hours is 3600*3 seconds so the accumulative error is around 42 secs. This assumes no time delay in and out of the interrupt or Xtal speed variation. You could adjust for the error every time it accumulates up to a second in your code thus adjusting for the 10 secs or so each hour you are off.
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517346 |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
Re: Using the pic for a clock |
Posted: Wed Aug 27, 2003 3:00 pm |
|
|
<a href="http://www.pic-c.com/forum/general/posts/6403.html" TARGET="_blank">http://www.pic-c.com/forum/general/posts/6403.html</a>
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517349 |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
Re: Using the pic for a clock |
Posted: Wed Aug 27, 2003 8:10 pm |
|
|
<a href="http://www.pic-c.com/forum/general/posts/144517014.html" TARGET="_blank">http://www.pic-c.com/forum/general/posts/144517014.html</a>
:=I have been trying to make a clock out of a pic and I am
:=having some problems understanding how to use the
:=set_timerO() function.
:= With a 20Mhz xtal, if I do set_timer0(156) that
:=should give me a 20us overflow. So does that mean it will hit
:=my interupt function ever 20us? And if so do I do a put a counter
:=in that function to count from 50,000 to 0 for 1 second?
:=(1sec/20us = 50,000) Is there an easier way?
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517367 |
|
|
jim garvin Guest
|
Re: Using the pic for a clock |
Posted: Thu Aug 28, 2003 5:55 am |
|
|
OK.. I saw the example and it worked. Could some one explane
how the how to manipulate the formula
20000000/4*256*256 = 76.29394531
How does the
setup_counters(RTCC_INTERNAL, RTCC_DIV_64); and
set_rtcc(0);
apply?
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517387 |
|
|
|