How to Read TMR0 Value?

Status
Not open for further replies.

Suraj143

Active Member
Hi all
Datasheet says TMR0 rolls over from FFh to 00h to generate T0IF flag.

I want to read the TMR0 register. I want to read d’16’ is available in TMR0.

Code:
	movf	TMR0,W
	xorlw	d'16'
	btfss	STATUS,Z
	goto	Exit
	movwf	Result

Is the above code correct or do I have to subtract from 256?

Because datasheet says TMR0 counts from FFh to 00h.
 
I'm not sure what you're asking. Timer0 counts up (increments) by one every instruction cycle (without prescaler). It starts at from $00 and counts up to $ff. The next time it increments it rolls over to $00, triggering the interrupt, and keeps counting.

So you're polling the timer looking for it to hit 16 ($10)? I'm no expert, but I'd think that you're unlikely to "be there at the right time" very often. Wouldn't it make more sense to preload the timer with 241 ($f1) and 16 cycles later (I added two for the two-cycle inhibit after writing to TMR0) you get an interrupt. (I might be off by one...) That way you're sure not to miss it.
 
Last edited:

Ahhhhh that solved the problem.I thought TMR0 counts from FFh to 00h.Its counts from 00h to FFh & T0IF wii generate when it roll over from FFh to 00h.

That means my above code is correct?
 
Ahhhhh that solved the problem.I thought TMR0 counts from FFh to 00h.Its counts from 00h to FFh & T0IF wii generate when it roll over from FFh to 00h.
I thought that was what you meant. Ya, it increments - NOT decrements.

That means my above code is correct?
Not sure what you're even trying to do with that code.
 
If you just want TMR0 to rollover on a 16 count then preload the timer with 255-16 instead of checking for 16. What's your prescaler set for?
 
Hi all
Datasheet says TMR0 rolls over from FFh to 00h to generate T0IF flag.

Because datasheet says TMR0 counts from FFh to 00h.

TMR0 counts up from 00h to FFh, when it rolls over from FFh back to 0 the interupt flag is set. Is that what you are asking ?
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…