1
votes

I am trying to generate a rectangular pulse using INT1 interrupt. I am using timer0 interrupt to set a pulse length. i am using software polling method in this program. when I press PB1, then it should generate a rectangular pulse at PORTD. But the problem is, when I press PB1, then INT1IF interrupt flag never set.

here is my code

#include <p18f452.inc>


config OSC = HS
config BOR = OFF, WDT = OFF, LVP = OFF


org 0x0000
  goto start


#define count d'1234'


DAC_A:
movwf PORTD ;send WREG value in PORTD
bcf PORTA,5 ; clear RA5 for DAC output
bcf PORTA,4 ; make a low puls at DAC WR pin
bsf PORTA,4 ; set back to high
return


init:
;------------------
;I/O config
;------------------
movlw b'11001111' 
movwf TRISA     ;4 and 5 bit of PORTA as output
movlw b'00000000'
movwf TRISD     ;all the bits of PORTD as output
;------------------
;TMR0 config
;------------------
clrf  T0CON
bsf   T0CON,1   ;TMR0, pre-scaler 8
bsf   T0CON,7   ;TMR0, start 
;------------------
;INT0 interrupt config
;------------------
bsf   INTCON3,3 ;INT1, Enable
bcf   RCON,7    ;Interrupt priority disable
bsf   INTCON,7  ;global enterrupt enable
bsf   INTCON,6  ;peripheral enterrupt enable
;------------------
;other config
;------------------

return


;------------------------------------------------------------  

start:     
call init        ; initialisation code 
again:

bcf     INTCON3,0   ;clear INT1IF
poll_PB1:     
btfss   INTCON3,0   ;check INT1IF, if set skip
bra poll_PB1        
bcf     INTCON3,0   ;clear INT1IF

movlw   h'FF'
call    DAC_A

movlw   high(-count)         
movwf   TMR0H       ;load TMR0H
movlw   low(-count)     
movwf   TMR0L           ;load TMR0L
bcf     INTCON,2        ;clear TMR0IF
poll_TMR0:     
btfss   INTCON,2        ;check timeout
bra poll_TMR0     


movlw   h'00'
call    DAC_A


movlw   high(-count)         
movwf   TMR0H       ;load TMR0H
movlw   low(-count)     
movwf   TMR0L           ;load TMR0L
bcf     INTCON,2        ;clear TMR0IF
poll_TMR00:     
btfss   INTCON,2        ;check timeout
bra poll_TMR00

bra again       ;loop again

END
2

2 Answers

1
votes

If you don't use an interrupt service routine, you can turn off global interrupts. The bits should get set when the peripheral interrupt is set. But then again, why use an interrupt at all if you're polling anyway, just poll the input pin.

0
votes

You actually have the INT1 interrupt enabled - but don't have an interrupt handler. When the interrupt occurs, execution will jump to the address where the interrupt handler is supposed to be (in the middle of your DAC_A subroutine); that's not likely to do anything you want...

On a PIC at least, there's no need to actually have interrupts enabled in order for the individual interrupt flags to be set when their condition occurs. (Note that this is NOT true for all MCUs.)

But since you're just polling the interrupt flag in a tight loop, there's probably no reason to even do that - it's be simpler to just poll the input pin directly. The only reason you'd need to use the interrupt flag is if the input pulse is so short (possibly sub-microsecond, depending on clock speed) that the polling loop might miss it. You use the word "press" a few times, if this input is actually a pushbutton then there's no possibility of the pulse being too short to catch without an interrupt.

Two other potential issues I see with your code:

  • You're not setting up the ADCONx registers, so every IO pin that has analog input capability will be in analog mode by default. This will affect RA5 at the very least.

  • In DAC_A, you are doing successive bit operations on a single PORTx register, which has some issues (look up "PIC RMW problem" for details). Use the LATx register instead for setting output bits.