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.

another way is to use the TIMER as counter

Status
Not open for further replies.

andig

New Member
Hi,

For a different application I used the Timer as a counter and a 1 second time base (interrupt based) would dump the values and do a reset of the timer.

Regards

Sougata
 
andig said:
Hi,

For a different application I used the Timer as a counter and a 1 second time base (interrupt based) would dump the values and do a reset of the timer.

Regards

Sougata

It is generally inaccurate to manually reset the timer counter. Timers are usually configured to reset themselves and start from the beginning with 100% accurate timing. Manually resetting the counter may require taking into account the number of instrutions and it's sometimes difficult to say how long an interrupt takes to respond to it.
 
Assuming we are talking about PICs here. Instead of resetting the timer, add the timer interval value to the timer. this compensates for timer overun due to ISR proccessing time or interrupt latency. So say your interval is 0x80 for an 8 bit timer. If the interrupt is delayed by 1 timer tick, the value of the timer will be 0xFF. When you add 0x80 to 0xFF you get 0x7F which is correct.

Note that to get exteme accuracy, you should poll the timer untill it changes and then add the interval value. This is especially true when you use the prescalar since writing to the timer clears it and thus tosses any partial accumulation. Also, you loose 2 Fosc/4 clocks when writing the timer (resync). You might want to keep track and compensate.

letting the timer reset itself is fine but some processors (PIC16s, for example) do this only at terminal count (i.e. 0) and start from maximum count (timer0 would become ff, for example).

Phil
 
philba said:
letting the timer reset itself is fine but some processors (PIC16s, for example) do this only at terminal count (i.e. 0) and start from maximum count (timer0 would become ff, for example).

It depends which timer you use, TMR0 is basically a legacy timer, to maintain compatibility with the really old PIC's - far better to use one of the others, TMR2 is my personal favourite, and you can preset where it resets and generates an interrupt.
 
Or you can find a PIC that has period registers with its timers so you can generate a periodic interrupt which has 0 processing overhead.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top