Stack Overflow Pushing Return Address of Interrupt PIC 16F628A

Viewed 41

can someone help me? I'm new to pic programming, what I'm trying to do is make bit 3 of port b turn on as long as bit 0 and 7 are 1, main is bit 0. if bit 0 is not 1 output, bit 3, is 0. The bit 3 is turning on, but when I change the bit 0 the bit 3 doesn't change to off (0). I'm using INTCON REGISTER.

Here's my code:

#include "p16f628a.inc"

; CONFIG
 __config 0x3F18
; __CONFIG _FOSC_INTOSCIO & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _BOREN_OFF & _LVP_OFF & _CPD_OFF & _CP_OFF

 LIST P=16F628A ;DECLARAR EL PIC
 RADIX HEX ;DECLARAR QUE SE TRABAJARA EN HEXADECIMAL
 


 STATUS EQU 0x03
PORTA EQU 0X05
PORTB EQU 0X06
CMCON EQU 0X1F
 
 
ORG 0X00 ;DECLARAR DONDE INICIARA
GOTO CONF
ORG 0X04
GOTO INTERRUPCIONES
 ORG 0X08  ;BRINCAR REGISTROS DE MEMORIA


 CONF
 BSF CMCON, 0
 BSF CMCON, 1
 BSF CMCON, 2  ; ANALOG A DIGITAL
 

 BSF STATUS, 5
MOVLW 0XFF
MOVWF PORTA
MOVLW 0x81
MOVWF PORTB
BCF STATUS,5

BSF INTCON,3
BSF INTCON,4
BSF INTCON,7

MAIN
BCF PORTB,3
GOTO MAIN

INTERRUPCIONES
BCF INTCON,GIE
BTFSC INTCON,INTF
GOTO TEMPERATURA
TEMP
BSF INTCON,GIE
BCF INTCON,INTF
RETFIE


TEMPERATURA
BTFSC PORTB,7
BSF PORTB,3
BSF INTCON,GIE
GOTO TEMP
 END
1 Answers

If I understand correct; you want to set bit 3 of PORTB to 1 whenever the bit 0 or bit 7 goes high, but bit 0 is superior I mean it has higher priority over the bit 7. In the light of this information we can make the truth table as following:

RB0 (INT0) RB7 RB3 (output)
0 0 0
0 1 (ignore) 0
1 0 (ignore) 1
1 1 1

But my question here is: What is the role of bit 7 if it doesn't have any affect on the output since the bit 0 override it in any case? Please let me know that.

So according to the table the output will set to 1 only if the higher priority interrupt input (RB0) is 1. For the rest the output remains 0. Correct me if I miss something. Let's implement this logic into your existing code. By the way I've also noted that you've set the RB Port change interrupt but didn't service it. I will only write the related part with some optimizations.

INTERRUPCIONES
;     BCF       INTCON,GIE    ; No need to do this since the hardware does this automatically
     BTFSS     INTCON,INTF
     GOTO      DONE           ; Not INTF interrupt simply skip
     BCF       INTCON,INTF    ; Better clear this flag as soon as you start to service it
     BSF       PORTB,0        ; High priority input goes 1 hence no more questions to setting the output as 1

     ; Check for PORTB on change interrupt
     BTFSS     INTCON,RBIF
     GOTO      DONE           ; Not RBIF interrupt simply skip
     BCF       INTCON,RBIF    ; First of all clear the interrupt flag to avoid unintended recursive interrupts
     BTFSS     PORTB,0        ; Check if the higher priority input is 1
     GOTO      DONE           ; Higher priority input is not 1 so we ignore this change
     BCF       INTCON,RBIF    ; Better clear this flag as soon as you start to service it
     CALL      TEMPERATURA    ; Calling as a subroutine is supported up to 8 levels by this micro
DONE
;     BSF       INTCON,GIE    ; Don't do it, because the RETFIE instruction does it for you
     RETFIE

; If the program reaches here, the high priority input is 1.
; Now we need to check if the low priority input is 1 as well.
TEMPERATURA
     BTFSC     PORTB,7
     BSF       PORTB,3
;     BSF       INTCON,GIE    ; Not needed since we return to the ISR routine and RETFIE will do it for us
     RETURN

And finally let's simplify the above ode by eliminating unnecessary code lines:

INTERRUPCIONES
     BTFSS     INTCON,INTF
     GOTO      DONE           ; Not INTF interrupt simply skip
     BCF       INTCON,INTF    ; Better clear this flag as soon as you start to service it
     BSF       PORTB,0        ; High priority input goes 1 hence no more questions to setting the output as 1

     ; Check for PORTB on change interrupt
     BTFSS     INTCON,RBIF
     GOTO      DONE           ; Not RBIF interrupt simply skip
     BCF       INTCON,RBIF    ; First of all clear the interrupt flag to avoid unintended recursive interrupts
     BTFSS     PORTB,0        ; Check if the higher priority input is 1
     GOTO      DONE           ; Higher priority input is not 1 so we ignore this change
     BCF       INTCON,RBIF    ; Better clear this flag as soon as you start to service it
     CALL      TEMPERATURA    ; Calling as a subroutine is supported up to 8 levels by this micro
DONE
     RETFIE

; If the program reaches here, the high priority input is 1.
; Now we need to check if the low priority input is 1 as well.
TEMPERATURA
     BTFSC     PORTB,7
     BSF       PORTB,3
     RETURN
Related