Reputation: 3
I'm using a PIC18F4580 module and I want to simulate an LED I wrote on MPLAB in Proteus but I keep getting the following:
[PIC18 STACK] PC=0x000E. Stack overflow is forcing device reset. [U1]
My code on MPLAB:
;Complete the schematic below showing how to connect on a PIC:
;(a) a pushbutton PB to RD4, so that the RD4=0 (LOW) when the pushbutton is pressed,
;and 1 (HIGH) when the pushbutton is released,
;(b) a LED1 to RC1, so that the LED1 is ON when RC1=0 (LOW) and OFF when RC1=1
;(HIGH),
;(c) a LED2 to RC2, so that the LED2 is ON when RC2=1 (HIGH) and OFF when RC2=0
;(LOW).
;Assume that bit RD4 is an input and represents the condition of a door alarm.
; If it goes LOW, it
;means that the door is open
;Monitor the pushbutton continuously. Whenever it goes LOW,
; send a HIGH-to-LOW pulse
;eighty times to the LED1, then turn off LED1 and turn on LED2. Otherwise, turn on this LED1
;and turn off LED2.
PORTD EQU 0xF83
PORTC EQU 0xF82
TRISC EQU 0xF94
TRISD EQU 0xF95
Counter EQU 0x20
;LED 1 > RC1 ON: 0 OFF: 1
;LED 2 > RC2 ON: 1 OFF: 0
BSF TRISD, 4
BCF TRISC, 1
BCF TRISC, 2
LoopPB1:
MOVLW D'80'
MOVWF Counter
Call PB1
DECFSZ Counter
Call LoopPB1
BSF PORTC, 1
BSF PORTC, 2
return
PB1:
BCF PORTC, 1 ;S
BCF PORTC, 1
return
BTFSS PORTD, 4
BCF PORTC,1
BRA PB1
RETURN
END
Schematic on Proteus: schematic
This is my first time working with MPLAB and Proteus.
EDIT: My code now turns on the LED at all times until push button is pressed it turns it off, But I don't see 80 high-to-low calls to LED1, also, LED2 seems to not turn on whatsoever.
Updated Code:
ORG 0
;Complete the schematic below showing how to connect on a PIC:
;(a) a pushbutton PB to RD4, so that the RD4=0 (LOW) when the pushbutton is pressed,
;and 1 (HIGH) when the pushbutton is released,
;(b) a LED1 to RC1, so that the LED1 is ON when RC1=0 (LOW) and OFF when RC1=1
;(HIGH),
;(c) a LED2 to RC2, so that the LED2 is ON when RC2=1 (HIGH) and OFF when RC2=0
;(LOW).
;Assume that bit RD4 is an input and represents the condition of a door alarm.
; If it goes LOW, it
;means that the door is open
;Monitor the pushbutton continuously. Whenever it goes LOW,
; send a HIGH-to-LOW pulse
;eighty times to the LED1, then turn off LED1 and turn on LED2. Otherwise, turn on this LED1
;and turn off LED2.
PORTD EQU 0xF83
PORTC EQU 0xF82
TRISC EQU 0xF94
TRISD EQU 0xF95
Counter EQU 0x20
;LED 1 > RC1 ON: 0 OFF: 1
;LED 2 > RC2 ON: 1 OFF: 0
BSF TRISD, 4
BCF TRISC, 1
BCF TRISC, 2
;Monitor the pushbutton continuously. Whenever it goes LOW,
; send a HIGH-to-LOW pulse
;eighty times to the LED1, then turn off LED1 and turn on LED2. Otherwise, turn on this LED1
;and turn off LED2.
MOVLW D'80'
MOVWF Counter
LoopPB1:
MOVLW D'80'
MOVWF Counter
Loopn:
BSF PORTC, 1 ;S
BCF PORTC, 1
DECFSZ Counter, F
BRA Loopn
BSF PORTC, 2
BSF PORTC, 1
BRA Check
Check:
BTFSS PORTD, 4
BRA Check
BRA LoopPB1
END
Updated schematic: enter image description here
Upvotes: 0
Views: 153
Reputation: 2520
The stacko verflow problem seems to be caused by this line:
Call LoopPB1
You call LoopPB1
80 times recursively, but PIC18 devices do not have proper hardware for recursion operations. They have 32 deep hardware call stack, hence by the time the LoopPB1
is called in 33rd time the hardware stack will just overflow. Whenever you do recursive or nested calls, the return address will be pushed onto the stack automatically by the hardware. There fore you can do only 32 nested or recursive calls in your application.
You must change that line as following to avoid recursive calls and stack overflow:
BRA LoopPB1
The BRA
instruction is the right one for looping and will not cause a hardware stack overflow since it does not have to store the return address.
Note that also the following piece of code is unreacheable in the program flow:
BTFSS PORTD, 4
BCF PORTC,1
BRA PB1
RETURN
As a side note other than the problems mentioned above; your code does not seem to be doing the functionality that requested in the homework.
Upvotes: 0