assembly 使用PIC18F458的Proteus中的堆栈溢出

rwqw0loc  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(252)

我正在使用PIC18F4580模块,我想模拟我在Proteus中的MPLAB上编写的LED,但我一直得到以下信息:

[PIC18 STACK] PC=0x000E. Stack overflow is forcing device reset. [U1]

我在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

Proteus示意图:schematic
这是我第一次使用MPLAB和Proteus。
编辑:我的代码现在打开LED在任何时候,直到按下按钮,它关闭它,但我没有看到80高到低调用LED1,也,LED2似乎没有打开任何。
更新的代码:

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

更新示意图:enter image description here

jdzmm42g

jdzmm42g1#

stacko溢出问题似乎是由下面这行代码引起的:

Call LoopPB1

您递归调用LoopPB1 80次,但是PIC 18设备没有合适的硬件来执行递归操作。它们有32个深的硬件调用堆栈,因此当LoopPB1被调用第33次时,硬件堆栈将溢出。每当您执行递归或嵌套调用时,返回地址会被硬件自动推到堆栈上。2因此你只能在你的应用程序中做32个嵌套的或递归的调用。
您必须将该行更改为如下所示,以避免递归调用和堆栈溢出:

BRA LoopPB1

BRA指令是循环的正确指令,并且不会导致硬件堆栈溢出,因为它不必存储返回地址。
请注意,以下代码段在程序流中也是不可访问的:

BTFSS PORTD, 4
   BCF PORTC,1
   BRA PB1
   RETURN

作为上述问题以外的旁注;您的代码似乎没有执行家庭作业中要求的功能。

相关问题