PDA

View Full Version : How to start/stop TMR0?



kevj
- 29th August 2007, 04:17
I'm using a 16F688, trying to figure out this timer thing and I'm missing something...

How do I start and stop it?

I see TMR1 has an enable bit which apparently serves to start/stop Timer1, but can't find any such bit for Timer0.

Also, there is a paragraph in the data sheet that says the prescaler is not readable or writable.

Datasheet: 5.1.3 pg 46:
"The prescaler is not readable or writable. When assigned to the Timer0 module, all instructions writing to the TMR0 register will clear the prescaler."

Eh?!?! This seems to tell me I can't change the prescaler, and even if I could, as soon as I write a value to TMR0 (like to pre-load it with a value so it times out sooner) then this prescaler will be cleared. This makes no sense.


I'm just trying to do the super simple task of:

-setting a prescaler of 1:8
-load the TMR0 with 5
-clear the overflow flag
-start the timer
-watch for the flag to occur at overflow


There are also times I'm going to want to stop the timer, read the value, then perhaps re-start it. This would seem to be super simple with a start/stop flag.

I checked to see if there were any notes in the section about the WDT - maybe starting / stopping the WDT also controls Timer0 as they seem closely linked, but still nothing that was obvious.

Stumped. Please help. :(

Darrel Taylor
- 29th August 2007, 14:35
Timer0 is the Energizer Bunny of the 14-bit PIC world.
It keeps going and going .... and there is no on/off bit.

The prescaler value and assignment is in the OPTION_REG register.
When it says the prescaler will be cleared, it means the value that the prescaler has counted so far. Not the division ration used by it.

Once you set the prescaler to 1:8, it'll stay there till you change it.

There are also times I'm going to want to stop the timer, read the value, then perhaps re-start it.Not with Timer0.
It is possible to stop it by switching the clock source to T0CKI (PORTA.4), but that means you can't use that pin for anything else.
<br>

Bruce
- 29th August 2007, 14:59
Timer0 doesn't have an enable/disable control bit like timer1 does, but you should be able to
start/stop timer0 by toggling TOCS in the OPTION_REG.

If there are no pulse inputs on the timer0 clock input, it should stop incrementing when you
set TOCS, and restart in timer mode when you clear TOCS.

kevj
- 30th August 2007, 04:55
Ahhhh...... that explans a lot.

Thanks for the tip on how to trick it into sort of stopping for a read.

I don't feel quite so dumb now.