PDA

View Full Version : Can't load 16 bit value in Timer 0



charliecoultas
16th August 2014, 15:54
Could somebody offer me some help please: I am using TMR0 on a 18F26K22 and I need 16 bit mode. I have read the datasheet a zillion times and can't understand why it doesn't work. The datasheet implies that if you "load" TMR0H first (put a value in it), it will not be actually actioned until TMR0L is loaded. They will both then somehow be loaded at the same time. I know why the two are loaded together, but I just can't get it to work.

TMR0H = $10
TMR0L = $00

Whatever I put in the top line, it doesn't get loaded.

Thanks
Charlie

grahamg
16th August 2014, 16:05
Charlie TMR0 is an 8 bit timer not 1 bit. It has a prescaler that is 8 bit and this will be cleared automatically when you load TMR0 low byte.

joesaliba
16th August 2014, 16:22
Charlie,

the T0CON.6 should be set to 0.


T0CON.6 = 0

From the datasheet page 159: -


Bit 6 T08BIT: Timer0 8-bit/16-bit Control bit
1 = Timer0 is configured as an 8-bit timer/counter
0 = Timer0 is configured as a 16-bit timer/counter

I have set this to 16 bit and it work great.

AlbertoFS
16th August 2014, 16:25
Hi Charlie,
The 18F26K22 can be configured for 16 bits Timer. (T0CON.6 = 0)
Read the PIC18F2x/4xK22 datasheet page 159

I had the same problem with Timer1 with the PIC18F25K22. I solved the problem with this code.
But I don't know if Timer0 has the same problem.

First, you must configure the T0CON and clear the bit7. (don't start the Timer0)
T0CON = 011000 ' only an example
TMR0H = ValueH
Nop
TMR0L = ValueL
Nop
T0CON.7 = 1 ' Start Timer0

I hope this code would be usefull.
The Binary value is truncated.
Alberto

charliecoultas
16th August 2014, 16:40
That's done the trick! I hadn't stopped the timer and was trying to reload it on the fly.
Very useful Alberto. Thanks guys.
Charlie

barak
18th August 2014, 23:10
Make your life easy, alias the timer register as a word = TMR0L & TMR0H. Example:

Dim Timer0REG As TMR0L.Word

Timer0REG = 0x1000

charliecoultas
25th August 2014, 09:57
Thanks Barak, that's a very useful tip. Sorry I didn't reply earlier, I've been away on holiday for a week.
Charlie

hadv215
25th August 2014, 13:31
dim wTemp as word

wTemp = <the value>


TMR0H = wTemp.HighByte
TMR0L = wTemp.LowByte

Harm