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.

Writing to TMR0

Status
Not open for further replies.

eng1

New Member
Hi. I know that TMR0 stops for two cycles if it's written to. I usually work around this by writing an adjusted value, as suggested in the datasheet.
I would like to know your opinion on the prescaler operation, when it is assigned to TMR0 and TMR0 is written to.
When assigned to the Timer0 module, all instructions writing to the TMR0 register (e.g., CLRF 1, MOVWF 1, BSF 1, x....etc.) will clear the prescaler.
(from the datasheet of the PIC16F628A)
 
My opinion is that the data sheet is correct.

I have never heard of the timer stopping for 2 cycles. Where have you read that?

I recently posted to this thread where the poster wanted a 1 second delay using timer0. I allowed 2 cycles for the latency of the IRQ, is it this latency that you are referring to?

Mike.
 
TMR0 is badly flawed, it's only kept for backwards compatibility with old PIC's - far better to use one of the more modern timers, such as TMR2 (my personal favourite!).
 
Pommie said:
I have never heard of the timer stopping for 2 cycles. Where have you read that?

I've read that in the datasheets of PIC16F84A and PIC16F628A, for example.
In Timer mode, the TMR0 register value will increment every instruction cycle (without prescaler). If the TMR0 register is written to, the increment is inhibited for the following two cycles. The user can work around this by writing an adjusted value to the TMR0 register.
As I understand it, this is a general rule for TMR0, even if you don't place the instruction in the ISR (where you have to consider the interrupt latency too).

Back to my question about the prescaler, supposing that I've read TMR0 and written back without changes, I wouldn't expect the prescaler to be cleared.
 
eng1 said:
I've read that in the datasheets of PIC16F84A and PIC16F628A, for example.

As I understand it, this is a general rule for TMR0, even if you don't place the instruction in the ISR (where you have to consider the interrupt latency too).

Back to my question about the prescaler, supposing that I've read TMR0 and written back without changes, I wouldn't expect the prescaler to be cleared.

I hadn't noticed that in the data sheet. Just shows that you never fully understand these things.

As to writing back the same value to TMR0, I would expect any write to clear the prescaler. Why do you need to know?

Mike.
 
I'm not having practical problems, but I was reading some documents and wanted to understad that correctly. It would be a hard-to-find bug, I think.
Thank you for your replies.

Nigel, TMR2 is my favourite :)
 
Last edited:
For the record, I favour timer 1 as it is 16 bit and can use the special event trigger as a 16 bit period. So, if you want a 100Hz interrupt with a 20MHz clock then you simply load ccp1 with 50,000. Try getting a 100Hz interrupt out of timer 2.

Mike.
 
what i haven't really understood with timer0 is when we write to it and have the prescaler set to some ratio. i noticed from the datasheet of pic16f54 that the timer0 increments with every instruction cycle if not written too, if it is written to and the prescalar is off the timer0 does not increment for the next two instruction cycles.
when the prescalar was set to 1:2, teh timer0 did not increment for the next 3 instruction cycles... so for a prescalar of 1:4, the timer0 will not increment for the next 4 instruction cycles?

thanks
 
No the 2 instruction delay is the hardware time to load the data from the internal registers, it always takes 8 xtal clocks (ie 2 PIC instructions). After those 2 instructions have occured, TMR0 will start running again and the prescaler counter will always start from 0 again. If you have to write to TMR0 when it has a prescaler you can just assume the average prescaler value when it resets, which is fine for baudrates etc but I wouldn't use for a real time clock.

Writing to the TMR0 includes ANY operation that writes, including changing one bit; BSF and BCF.

If you need to generate accurate 1second periods there is a good method here;
Zero-error 1 second timing algorithm

that uses TMR0 as a free-running timer so there is never any issue caused by writing to it or having any prescaler value. :)

I use TMR0 a lot, but usually as a free running timer to use for delays as the other timers can then be available for other tasks where you need to write to a timer.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top