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.

Polling TMR0 Code Problem

Status
Not open for further replies.

Suraj143

Active Member
I need to poll the TMR0 in 16F54 to generate 32768uS.

Is this below code will work very accurate?

Note: process routine cycles vary on every TMR0 overflows.

Code:
;PS=1:128,TMR0=256
;XT=4Mhz

T0_Loop	movf	TMR0,W		;read TMR0
	btfss	STATUS,Z	;is TMR0 overflowed?
	goto	T0_Loop		;no,then loop
				;yes,overflowed

Process	----			;
	----			;

	goto	T0_Loop
 
I'm pretty sure Microchip has an app note on this very thing. I can't remember if the old 16F54 has a TMR0.IF (which would make it easy)
Use something modern if you can like a 16F627 or 16F88
 
No no I need to do this without T0IF bit.Thats why its very hard for me.If T0IF is there its easy.

I need to do this without T0IF bit.
 
Last edited:
No no I need to do this without T0IF bit.Thats why its very hard for me.If T0IF is there its easy.

I need to do this without T0IF bit.
I may be wrong, but it strikes me that your loop is very likely to be in the wrong place when that timer hits zero, and will miss it completely fairly often.
 
I may be wrong, but it strikes me that your loop is very likely to be in the wrong place when that timer hits zero, and will miss it completely fairly often.

futz you got it.Thats what I was thinking.The loop will be in the wrong place.After TMR0 overflows.

That is the place I need to fine tune.
 
futz you got it.Thats what I was thinking.The loop will be in the wrong place.After TMR0 overflows.

That is the place I need to fine tune.
That's why they invented interrupts. :D

You only need to write code to catch it "near" zero. It won't be absolutely accurate, but better than totally missing the end sometimes/often. But the loop still has to be tiny and fast or else you just make your problem worse.
 
As long as "Process" doesn't take too long, and if the prescaler is set to 1:128, then the code should have plenty of time to detect a roll over.
 
As long as "Process" doesn't take too long, and if the prescaler is set to 1:128, then the code should have plenty of time to detect a roll over.

Hi kchriste my process routine is very small.It takes below 20uS time.

But every TMR0 overflows this process routine cylcles may vary like 17uS,19uS etcc...but maximum it takes 20uS time.
 
That's why they invented interrupts. :D

You only need to write code to catch it "near" zero. It won't be absolutely accurate, but better than totally missing the end sometimes/often. But the loop still has to be tiny and fast or else you just make your problem worse.

Hi kchriste my process routine is very small.It takes below 20uS time.

But every TMR0 overflows this process routine cylcles may vary like 17uS,19uS etcc...but maximum it takes 20uS time.[/quote]

In this case you should be fine. With your prescaler setting of 128 the TMR0 register will contain a value of '0' for a full 128 cycles (128 usecs). That's more than enough time to sample it at a value of '0' and perform your Process. You just need to make sure you don't process that '0' value multiple times (grin)...

Mike
 
Last edited:
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top