# Motorola 68HC11 Help with C language. Making an accurate hardware timer.

#### brightjoey

Joined Nov 12, 2009
14
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.

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
*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);
}
}
Code to count the offset period between the previous period and the next one.

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()
{
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;
}
}
}
A real-time timer. But if it runs for a long time it will not be accurate anymore.

#### MrChips

Joined Oct 2, 2009
28,128
The timer will only be as accurate as the XTAL. If you want to keep perfect time, you have to put trimmer caps on the XTAL and adjust the frequency using a calibrated frequency standard. The accuracy has nothing to do with the MCU code.

#### Yako

Joined Nov 24, 2011
245
You're in for a clock cycle counting exercise.

All of your instructions in the code: logic conditions, calls and what not -- the amount of time required to execute them needs to be taken into account and some how formulated and subtracted from the delay routine to arrive at an accurate 1 second tick.

Have fun with that one.

Last edited:

#### Yako

Joined Nov 24, 2011
245
You have to put trimmer caps on the XTAL and adjust the frequency using a calibrated frequency standard.
And that too.

#### Yako

Joined Nov 24, 2011
245
The accuracy has nothing to do with the MCU code.
What this guy is doing is he is making a clock using a software timer, whereas mine uses a hardware timer. This is not an easy task.

#### brightjoey

Joined Nov 12, 2009
14
I don't know if the XTAL has trimmer caps or not but I can't mess with the hardware as it is fixed by the board. My lecturer main purpose for us is to make it accurate in C program.

In code 1, If I use the difference of both tic1 and data1 that is "result" and add it back to data1. I could get back tic1 value. make it read into another address. and use that new value as the new clock cycle period. So the clock tick will forever use the constant clock cycle period.

Will that work?

#### MrChips

Joined Oct 2, 2009
28,128
You do not have to do any clock cycle counting. Your objective will be to use the XTAL oscillator and hence the CPU clock to drive the hardware timer (which is built in as part of the MCU).
You will set the hardware timer to give you periodic interrupts at a convenient period, for example, 100us. You just have to count interrupts. If this is done right, there is no loss of counts and your time will be as accurate as the XTAL.

#### MrChips

Joined Oct 2, 2009
28,128
You're in for a clock cycle counting exercise.

All of your instructions in the code: logic conditions, calls and what not -- the amount of time required to execute them needs to be taken into account and some how formulated and subtracted from the delay routine to arrive at an accurate 1 second tick.

Have fun with that one.
Yako, I do not know what are your qualifications. I am not into flaming anybody but you are beginning to irritate me. Half the time you do not know what you are talking about. The purpose of this forum is to give sound advice to people who can use it, not to increase your post counts.

#### brightjoey

Joined Nov 12, 2009
14
You do not have to do any clock cycle counting. Your objective will be to use the XTAL oscillator and hence the CPU clock to drive the hardware timer (which is built in as part of the MCU).
You will set the hardware timer to give you periodic interrupts at a convenient period, for example, 100us. You just have to count interrupts. If this is done right, there is no loss of counts and your time will be as accurate as the XTAL.
Okay I'm almost feeling you but I still don't really understand. I activate a Real time-Periodic Interrupt Flag, such as

*tmsk2=0x40 //Enable RTI Interrupt

@interrupt void timer (void)
{ Count how many interrupts here

*tflg2=0x40 // Reset RTI Flag

}

#### brightjoey

Joined Nov 12, 2009
14
Sorry double post

#### MrChips

Joined Oct 2, 2009
28,128
OK, let's begin. Assuming your XTAL is 8MHz, then E-CLOCK is 2MHz.
One clock period is 0.5us.

You are going to use a Timer Output Compare function.
In your Timer Output Compare interrupt handler, you will add 20,000 to the Output Compare register. This will give you an interrupt every 10ms.

If you wish, you can add 2000 to the Output Compare register. This will give you a 1ms time base instead of 10ms. Up to you.

As easy as pie.