我使用汇编,MPLAP和PROTEUS来编写代码,通过按钮打开或关闭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
答案 0 :(得分:0)
对于类似的东西,你还需要去掉开关,在这个例子中,这是通过一个简单的延迟计时器来完成的。务必在配置位中禁用看门狗定时器或自行添加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