首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Proteus w/ PIC18F458中的堆栈溢出

Proteus w/ PIC18F458中的堆栈溢出
EN

Stack Overflow用户
提问于 2022-10-15 02:29:42
回答 1查看 41关注 0票数 0

我使用的是一个PIC18F4580模块,我想模拟我用Proteus在MPLAB上写的一个发光二极管,但是我一直得到以下信息:

代码语言:javascript
复制
[PIC18 STACK] PC=0x000E. Stack overflow is forcing device reset. [U1]

我的MPLAB代码:

代码语言:javascript
复制
;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

变形杆菌原理图:原理图

这是我第一次与MPLAB和Proteus合作。

编辑:我的代码现在一直打开发光二极管,直到按下按钮才能关闭它,但是我没有看到对LED1的80次高到低的调用,而且,LED2似乎没有打开任何东西。

经修订的守则:

代码语言:javascript
复制
    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

更新示意图:在这里输入图像描述

EN

回答 1

Stack Overflow用户

发布于 2022-10-15 08:43:55

stacko verflow问题似乎是由这一行引起的:

代码语言:javascript
复制
Call LoopPB1

您可以递归地调用LoopPB1 80次,但是PIC18设备没有适合递归操作的硬件。它们有32个深硬件调用堆栈,因此,当第33次调用LoopPB1时,硬件堆栈只会溢出。无论何时执行递归调用或嵌套调用,硬件都会自动将返回地址推送到堆栈上。因此,您只能在应用程序中执行32个嵌套或递归调用。

必须按以下方式更改该行,以避免递归调用和堆栈溢出:

代码语言:javascript
复制
BRA LoopPB1

BRA指令是循环的正确指令,不会导致硬件堆栈溢出,因为它不需要存储返回地址。

注意,下面的代码在程序流中也是不可实现的:

代码语言:javascript
复制
BTFSS PORTD, 4
   BCF PORTC,1
   BRA PB1
   RETURN

作为附带说明,除了上面提到的问题之外,您的代码似乎没有执行作业中所要求的功能。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74076282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档