Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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

Status
Not open for further replies.

brightjoey

New Member
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. I posted this in allaboutcircuit.com but not luck 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:
// 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);
}
}
Code to count the offset period between the previous period and the next one.

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;
}
}
}
A real-time timer. But if it runs for a long time it will not be accurate anymore.
 
I remember the 6811 had a beautiful free-running timer that would generate interrupts on matched values. The timer never needed to be stopped, reset or interfered with - all that was necessary was to calculate the offset for the next match.

You don't seem to be using that wonderful timer in your code. Looks like you just have a simple do-while loop.
 
Last edited:
Yes. that would be the 16-bit timer counter (tcnt)

but how do I calculate the offset for the next match?
AND how do I include that offset so that a constant value of a clock cycle is always obtained?
maybe it's maths but I'n not quite sure where to go.
 
It's easier than you think - just add a number to the previous offset and load the new value into the Timer Output Compare registers. That's it.

The timer only counts up, so when it gets to the next value, it will trigger the next interrupt. The timer rolls over from FFFF to 0000, so just ignore the high carry. You DO have to make sure your other routines are done before the next interrupt (obviously) - but for what you're doing you have plenty of time. For instance: suppose you have a 2mhz system clock, and set the timer prescaler to 16. Now it will take FFFFF (1,048,575) cycles for the 16 bit timer to go from 0000 to FFFF. At 2mhz, that will take over a half-second:

[LATEX]\frac{1}{2mhz} × 16 × 2^{16} = .5242875 S[/LATEX]

and one tick of the timer will be:

[LATEX]\frac{1}{2mhz} × 16 = .000008 S[/LATEX]

So, if you set your number to 62,500 (F424) that will be .5 seconds exactly!

Briefly - Enable the Timer Output Compare 1 interrupt, put the address to your interrupt service routine at the interrupt vector for Timer Input Capture 1, write your timer interrupt service routine to add F424 to the old number and load it into TOC1 hi and low, reset the interrupt flag, update your seconds, minute, hour, day, month, year, whatever registers, and return.

When the 6811 timer gets a match, that wonderful 6811 interrupt system will send the cpu right into the timer service routine, and automatically save your registers on the stack. I miss my old pal the 6811, it was a great chip to work with.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top