PIC mikroc.

Dumken

Member
Pls can someone help me with a working code on a digital clock explaining how TMR0 works. Mikroc precisely.
 
Weather its MickroC or any other tool.. Timer 0 can be explained in plain English..

Timer 0 runs... from start up to power down... It just runs..

To make a clock you need to set the pre-scale as high as you can.
With a 1Mhz crystal the timer will take a maximum of 262ms to timeout...

A clock cycle is FOSC / 4... So every 4 micro seconds..
There are 256 counts, as there are 256 bit conditions in a byte..
That means the timer will spill every 1.024 milli seconds..

If you load timer 0 with 6 every time it spills over, It will count to 1 milli second.

Here you have the basis of your clock.. The only little thing is the time it takes to load the timer. Loading it may take a couple of clock cycles..

C:
if(T0IF)
   {
   TMR0 = 6;
   milli++;
   T0IF = 0;
   }
if(milli>1000)
   {
   milli =0 ;
   secs ++;
   }

Best to do it in an interrupt though
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…