PDA

View Full Version : '628 tmr1 interrupts



jpeakall
- 26th February 2004, 17:30
Hi,

I posted this to the piclist, but thought (hoped!) I might get a faster reply here. I have been learning how to use the interrupts on a '628. I have it working, sort of. I want to have the interrupt occur when tmr1 overflows. The code I am using is:

@ DEVICE HS_OSC,MCLR_OFF,LVP_OFF,WDT_OFF,PROTECT_OFF
DEFINE OSC 20 ' We're using a 20MHz crystal

ATN con 254 'prefix value for LCD addresses
CLR con 1 'LCD clear command
line2 con 192 'Address for 1st char of second line

rawCount var word
tmra var rawcount.lowbyte
tmrb var rawcount.highbyte
i var byte

pause 1000
SEROUT2 portb.0,16468,[atn,CLR] 'Clears LCD
pause 10
serout2 portb.0,16468,[ "Initializing.."]
pause 1000
SEROUT2 portb.0,16468,[atn,CLR] 'Clears LCD

intcon = %01000000
pie1 = 1
pir1 =1
on interrupt goto int
enable


tmr1l =0
tmr1h =0
T1CON = %00000101
pauseus 400


loop:
input portb.3

goto loop

int:
disable
T1CON = %00000000
tmra = tmr1l
tmrb = tmr1h
serout2 portb.0,16468,["TIMER: ",dec rawcount]
pause 500
SEROUT2 portb.0,16468,[atn,CLR]
pause 1000
tmr1l =0
tmr1h =0
T1CON = %00000101
pauseus 400
enable
resume


With this code, I alwats get a result of 2007. The 2000 is from the 400us pause. The 7 would be about what I expect the loop to take. Why is it immedailty jumping out of the loop insted of waiting for timr1 to roll over?

Thanks,

Jonathan

www.madlabs.info

Darrel Taylor
- 27th February 2004, 09:06
By setting PIR1 = 1 you are setting the TMR1IF interrupt flag (PIR1.0). So it doesn't wait for the timer to overflow, it just goes straight to the interrupt routine.

Then once in the interrupt code, it gets caught in an endless loop because the TMR1IF never gets reset. The 2000 count is actually from the 400us pause in the interrupt routine, +7 for the resume and repeat interrupt call.

So, change PIR1 = 1 to PIR1 = 0 Then add the same line "PIR1=0" to the end of the interrupt routine, to reset the interrupt flag before resume.

Or, to be more precise, PIR1.0 = 0

HTH