Hi guys,
I'm debugging a code for a 16f877a PIC chip and I can't force the damn thing to interrupt when I want it to.
Before starting the interrupts and going to a wait-until-off-switch-pressed loop I do the following:
This sets the TMR1 and preloads it with 3035 so that the interrupt happens every 62500us (4Mhz XTAL used). Afterwards the program goes into a loop where it simply waits until the right button is switched. Of course during the time spent in the loop the interrupts should execute every 62500us doing the following:
You can see that there's a TEST ONLY section at the beginning of the INT routine which is there to see whether the INT happens or not. As you can guess it doesn't and the LED hooked up to PORTD, 7 never lights up.
Now I've tested the TMR1IF flag simply to see whether it overflows at all, and it does. With this code in the loop where it waits for the switch-off (after the INTs are turned on):
the LED lights up proving that the TMR1 counts up and actually overflows.
Has anyone got any ideas why the PIC can't see this flag or why it chooses to ignore it even though PIE1 bit 0 is set (TMR1 interrupt enable bit)? The PIC simply doesn't do the interrupts and everything else works fine, it doesn't freeze or get stuck.
I'd appreciate any feedback.
Regards,
dsc.
I'm debugging a code for a 16f877a PIC chip and I can't force the damn thing to interrupt when I want it to.
Before starting the interrupts and going to a wait-until-off-switch-pressed loop I do the following:
Code:
movlw b'00000100' ;set prescale 1:1
movwf T1CON
bcf T1CON, 0 ;turn off timer
movlw b'11011011' ;lower byte to TMR1L
movwf TMR1L
movlw b'00001011' ;high byte to TMR1H
movwf TMR1H ;those two bytes write 3035 into the timer
bcf PIR1, 0 ;reset int flag
bsf INTCON, 6 ;enable all unmasked ints
bsf INTCON, 7 ;turn on global interrupts
bsf T1CON, 0 ;turn on timer
bsf STATUS, RP0 ;select bank 1
bsf PIE1, 0 ;enable timer1 interrupts
bcf STATUS, RP0 ;select bank 0
This sets the TMR1 and preloads it with 3035 so that the interrupt happens every 62500us (4Mhz XTAL used). Afterwards the program goes into a loop where it simply waits until the right button is switched. Of course during the time spent in the loop the interrupts should execute every 62500us doing the following:
Code:
org 0x0004
goto Int_Serv ;interrupt service
Code:
Int_Serv
;====TEST ONLY====
bsf PORTD, 7 ;indication of the mode state for test purposes
;=================
;store regs for INT return
movwf w_temp ;copy w to temp register
swapf STATUS,W ;swap status to be saved into w
clrf STATUS ;bank 0, regardless of current bank, clears irp,rp1,rp0
movwf s_temp ;save status to bank zero s_temp register
movf PCLATH, W ;only required if using pages 1, 2 and/or 3
movwf p_temp ;save PCLATH into w
clrf PCLATH ;page zero, regardless of current page
;always overwrite timer value as it has to run 8 x 62500us
bcf T1CON, 0 ;turn off timer
movlw b'11011011' ;lower byte to TMR1L
movwf TMR1L
movlw b'00001011' ;high byte to TMR1H
movwf TMR1H ;those two bytes write 3035 into the timer
bsf T1CON, 0 ;turn on timer
(INT routine code)
bcf PIR1, 0 ;reset int flag
;restore saved reg values
movf p_temp, W ;restore PCLATH
movwf PCLATH ;move w into PCLATH
swapf s_temp,W ;swap s_temp register into w
;(sets bank to original state)
movwf STATUS ;move w into STATUS register
swapf w_temp,F ;swap w_temp
swapf w_temp,W ;swap w_temp into w
retfie
You can see that there's a TEST ONLY section at the beginning of the INT routine which is there to see whether the INT happens or not. As you can guess it doesn't and the LED hooked up to PORTD, 7 never lights up.
Now I've tested the TMR1IF flag simply to see whether it overflows at all, and it does. With this code in the loop where it waits for the switch-off (after the INTs are turned on):
Code:
btfsc PIR1, TMR1IF
bsf PORTD, 7
the LED lights up proving that the TMR1 counts up and actually overflows.
Has anyone got any ideas why the PIC can't see this flag or why it chooses to ignore it even though PIE1 bit 0 is set (TMR1 interrupt enable bit)? The PIC simply doesn't do the interrupts and everything else works fine, it doesn't freeze or get stuck.
I'd appreciate any feedback.
Regards,
dsc.