PDA

View Full Version : why trm0 preset don't work in this position?



lutherblisset
- 2nd February 2016, 14:53
where i have to point the tmr0 preset to properly work?

mainloop:

if INTCON.2=1 THEN
toggle portb.2
endif
INTCON.2=0
TMR0 = 128 'here don't work

goto mainloop

HenrikOlsson
- 2nd February 2016, 15:29
Which PIC?

lutherblisset
- 2nd February 2016, 16:09
it's the 16f628a

HenrikOlsson
- 2nd February 2016, 16:55
How do you know presetting TMR0 doesn't work?
It looks to me like you're doing it (and clearing the interrupt flag) all the time (ie outside of the IF/ENDIF block).....
I guess what you actually want to do is:

mainloop:

if INTCON.2=1 THEN
INTCON.2=0
toggle portb.2
TMR0 = 128 'yes work
endif

goto mainloop

/Henrik.

lutherblisset
- 2nd February 2016, 17:19
Yes now it work, with the frequency counter, measuring the frequency now it's 483 hz, nearly correct for the 1:8 prescaler and 4mhz quartz. The toggle don't half the frequency? For example without tmr0 presetting i measure 244,14 hz that is just 488,28/2 cause the toggle, with the presetting at your point now is 483,05 hz.......

HenrikOlsson
- 2nd February 2016, 17:45
The toggle does effectively halve the frequency but neither time or the timer stops during the actual time it takes for the instructions to execute.

Clearing the interrupt flag takes instruction(s), toggle the output takes multiple instructions, reloading the timer takes instruction(s). Each instruction takes time. By the time you're actually presetting the timer to 128 it's already continued counting and you're "pulling it back" ever so slightly every time, so the frequency drops. No secrets, nothing special, just the way it works. Think about it.

Here are two different versions you can try to see which one is the most accurate for your needs, then you can always tweak the value.


mainloop:

if INTCON.2=1 THEN
TMR0 = 128 'yes work
INTCON.2=0
toggle portb.2
endif

goto mainloop




mainloop:

if INTCON.2=1 THEN
TMR0 = TMR0 + 128 'yes work
INTCON.2=0
toggle portb.2
endif

goto mainloop

/Henrik.

lutherblisset
- 2nd February 2016, 17:52
thanks, now the frequency is 484 hz