【问题标题】:Hold button on/off using assembly code using PIC16F887使用 PIC16F887 使用汇编代码按住按钮开/关
【发布时间】:2016-04-18 12:55:48
【问题描述】:

我正在使用程序集、MPLAP 和 PROTEUS 编写代码,通过按钮打开或关闭 LED。这里的问题是我的代码在按钮处于向上位置时打开 LED 并在按钮处于向下位置时将其关闭。

我想要的是:单击按钮,LED 会亮起并保持亮起。再次单击按钮,LED 熄灭并保持熄灭。永远重复这一点。

这是我的代码:

; Exam 7 - Bonus 2


INCLUDE "P16F887.INC"
Status  EQU 0x20    ; Bien dung de
BSF     Status,0;


;Init----------------
BANKSEL TRISD   ; Lua chon bank 1
BCF     TRISD,0 ; PortD,0 la Output
CLRF    TRISD
BSF     TRISC,0 ; PortC,0 la Input

BANKSEL PORTD   ; Lua chon bank 0

;Main--------------
check db 0
Start


mainloop


btfsc PORTC,0
GOTO OFF

ON
movlw 0
movwf PORTD
GOTO mainloop



OFF
movlw 1
movwf PORTD
GOTO mainloop



;.............................

END

【问题讨论】:

  • 您需要更改代码,以便在输入位从 1 变为 0 时打开或关闭 LED。
  • @RossRidge 你能写你的代码吗??
  • 这似乎是你的工作。我没有得到这个任务。
  • 我不知道要写些什么。我的老师刚刚教了我一些关于操作的东西,比如 add,sub.... 他给了我这个作业,

标签: assembly pic led


【解决方案1】:

对于这样的事情,您还需要对开关进行去抖动,在本例中,这是通过一个简单的延迟计时器完成的。请务必在配置位中禁用看门狗定时器或自行添加 CLRWDT 指令。

    INCLUDE "p16f887.inc"

DelayCnt    equ 0x20
DelayCnt2   equ 0x21

    org 0

    ;Init----------------
    BANKSEL TRISD   ; Lua chon bank 1
    BCF     TRISD,0 ; PortD,0 la Output
    CLRF    TRISD
    BSF     TRISC,0 ; PortC,0 la Input

    BANKSEL PORTD   ; Lua chon bank 0

    ;Main--------------

mainloop

    BTFSS PORTC, 0
    GOTO $-1        ;Wait for button to be released

    BTFSC PORTC, 0  
    GOTO $-1        ;Wait for button to be pressed again

    CALL DELAY      ;Wait for button debounce

    BTFSC PORTC, 0
    GOTO mainloop   ;Back to begin if button bounced

    BTFSS PORTD, 0  ;If output is low...
    GOTO SETHIGH    ;Set it.
    BCF PORTD, 0    ;Otherwise clear it.
    GOTO mainloop

SETHIGH

    BSF PORTD, 0
    GOTO mainloop

DELAY           ;Delay function for button debounce
                ;Delays approx 25ms @ 1Mhz instruction clock 
                ;(Fosc = 4Mhz)

    MOVLW 0x20      
    MOVWF DelayCnt2 ;Load 0x20 into DelayCnt2

DELAY_INNER    

    MOVLW 0xFF      
    MOVWF DelayCnt      ;Load 0xFF into DelayCnt
    DECFSZ DelayCnt, F  
    GOTO $-1              ;Decrease DelayCnt until 0

    DECFSZ DelayCnt2, F 
    GOTO DELAY_INNER      ;Run inner loop until DelayCnt2 is 0

    RETURN

    ;.............................

    END

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多