Hello. I have a problem with understanding how to implement accurately a real time timer using 68HC11 with C. Now I don't have any previous knowledge in assembly so I can't use most of the reference book out there.
The problem is to make a real time clock that counts accurately.
My lecturer provides me with 2 codes that is to be put together. 1 to count the offset period of 1 cycle of the timer and 2 to is a real time timer.
I need a direction to try making it work so help is appreciated.
Code to count the offset period between the previous period and the next one.
A real-time timer. But if it runs for a long time it will not be accurate anymore.
The problem is to make a real time clock that counts accurately.
My lecturer provides me with 2 codes that is to be put together. 1 to count the offset period of 1 cycle of the timer and 2 to is a real time timer.
I need a direction to try making it work so help is appreciated.
Rich (BB code):
// Some further examples of setting timer unit
// No. 2 - Measure Waveform period using TIC1
// I.T. - 25/10/04
// Sets up TOC2 to toggle mode and then reads TCNT and adds required
offset period
#include <iof1.h>
#include <stdio.h>
void main(void)
{
unsigned int data1,result;
float freq;
unsigned int *tcnt,*toc2,*tic1;
unsigned char *tflg1,*tctl2;
tctl2=(unsigned char*)0x21;
tflg1=(unsigned char*)0x23;
toc2=(unsigned char*)0x18;
tcnt=(unsigned char*)0x0e;
tic1=(unsigned char*)0x0e;
*tctl2=0x10; //set TIC1 to RISING EDGE CAPTURE mode
for(;;)
{
*tflg1=0x04; //Clear TIC1 Flag
while(((*tflg1)&0x04)==0); //Wait for TIC1 FLAG
data1=*tic1; //Read TIC1 register
*tflg1=0x04; //Clear TIC1 Flag
while(((&tflg1)&0x04)==0); //Wait for TIC1 FLAG
result=*tic1-data1; //subtract first time from second
freq = 2.0e6/(float)result;//calculate frequency assume 2Mhz CLk
printf("Interval was %u Frequency is %5.0f\n\r",result,freq);
}
}
Rich (BB code):
// Some further examples of setting timer unit
// No. 3 - Polling of Real time Clock
// I.T. - 25/10/04
#include <stdio.h>
/*Real time clock program - POLLING of timer*/
void main()
{
unsigned char *padr,*paddr,*tflg2,*pactl,*tmsk2;
int hours,mins,secs,ticks;
tflg2=(unsigned char*)0x25;
pactl=(unsigned char*)0x26;
*pactl = 0x3;
for(;;)
{
*tflg2 = 0x40; /*reset timer*/
while((*tflg2 & 0x40) == 0); /*wait for timeout*/
ticks++;
if (ticks>=30)
{
ticks=0;
secs++;
printf("%i:%i:%i\r",hours,mins,secs);
}
if (secs>=60)
{
secs=0;
mins++;
}
if (mins>=60)
{
mins=0;
hours++;
}
if (hours>=24)
{
hours=0;
}
}
}