PDA

View Full Version : Changing PreScaler during interrupt



bcd
- 1st May 2008, 02:40
I have a little app I am writing where I need to be able to change the Pre-Scaler of the interrupt from the interrupt routine, but am unsure if it is possible. Can someone just check my logic here :

Setup Interrupt (I am using TMR0) with 1:1 prescaler and required Pre-load to give me my required interrupt time.

Enable interrupts

When interrupt fires
disable interrupt
set new prescaler value
do what I need to do during the interrupt
enable interrupts
return from Interrupt

I don't need to change the Pre-load value, just the Prescaler value.
I was looking to do this with Instant Interrupts, but am unsure if I am barking up the wrong tree and just wanted a bit of guidance.

With Thanks,
Bill.

skimask
- 1st May 2008, 03:25
Don't know which PIC you're using, but...
According to the 18F4620 datasheet, in the Timer 0 section, it says that anytime you write to TMR0, you clear the pre-scale COUNT but not the prescale value set in T0CON. And it also says that the prescaler can be changed 'ON THE FLY'.

bcd
- 1st May 2008, 06:57
I am using a F737 which shares the pre-scaler between the Watchdog and TMR0.

Re-reading the data sheet I can see where I was getting a bit confused <code>(From the datasheet)
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. When assigned
to WDT, a CLRWDT instruction will clear the prescaler
along with the Watchdog Timer.
The prescaler is not
readable or writable.
</code>

I was thinking Pre-Scaler meant the ratio rather than the count. Looks like it should work. Might be time to break out the breadboard and see what happens...

bill

skimask
- 1st May 2008, 13:38
I am using a F737 which shares the pre-scaler between the Watchdog and TMR0.....................
I was thinking Pre-Scaler meant the ratio rather than the count. Looks like it should work. Might be time to break out the breadboard and see what happens...
bill
Yep, same deal with the 18F4620, and probably most other PICs. I think you got the picture.

bcd
- 1st May 2008, 13:48
Its a bit messy - but it works ! Tomorrow we optimise !
This is a snippet that shows how to change the Prescaler value from within the interrupt routinue using Instant Interrupts.



DEFINE OSC 20

rs_led var portb.7
led var portc.4

loops var byte ' count round the loop to load prescaler


INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts


ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR0_INT, _doBAM, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM

OPTION_REG = %11010000 'Set TMR0 Prescaler to 2, leave RBPU alone

TMR0 = 158 ' preload with 158 to give 21uS interrupts
loops = 0

' interrupt routinue
doBAM:
toggle rs_led
if loops > 6 then
loops = 0
else
loops = loops+1
endif
option_reg.0 = loops.0
option_reg.1 = loops.1
option_reg.2 = loops.2
TMR0 = 158
@ INT_RETURN



bill.