PIC16F84A开关拨动

时间:2018-06-29 11:23:57

标签: assembly toggle microcontroller pic

你好,我正在尝试制作一个电路,在我的微控制器中,我试图通过按下按钮来打开或关闭LED。我正在尝试使用RB0中的按钮切换LED引脚D2。这是我的电路

enter image description here

这是我的代码:

    STATE EQU 0DH

    ;set up
    BSF STATUS, RP0 
    CLRF TRISA
    MOVLW 01H
    MOVWF TRISB
    MOVLW 01H
    MOVWF STATE ;state = 1
    MOVLW 0H
    BCF STATUS, RP0

    ;program start
    START MOVWF PORTA

    BTFSS PORTB, 0
    GOTO START
    GOTO TOGGLE

    TOGGLE DECFSZ STATE, 1
           GOTO OFF
           GOTO ON

    ON    MOVLW 2H
          MOVWF STATE ;state = 2
          MOVLW 1H
          GOTO START

    OFF   MOVLW 0H
          GOTO START

问题是当我打开LED时,它会正常打开,但是几秒钟后它会关闭,即使我确实没有按一下按钮也需要帮助。另外有时候按钮不起作用,非常混乱,如此混乱

1 个答案:

答案 0 :(得分:0)

这是您目前正在使用的电路:

PIC16F84A button input 2 LEDs for output

此代码应在按下按钮时切换LED D2:

;   
; Project: 16F84A_button+LEDs_v2
; File: main.asm
; Target: PIC16F84A
; IDE: MPLAB v8.92
; Assembler: MPASM v5.51
; Assembler mode: Absolute
;   
; Additional files:
;   P16F84A.INC
;                           PIC16F84A
;                   +----------:_:----------+
;             <>  1 : RA2               RA1 : 18 <> LED_GREEN D1
;             <>  2 : RA3               RA0 : 17 <> LED_GREEN D2
;             <>  3 : RA4/T0CKI        OSC1 : 16 <- 4MHz crystal
;    ICSP_VPP ->  4 : MCLR             OSC2 : 15 -> 4MHz crystal
;         GND ->  5 : GND               VDD : 14 <- 5v0
;      BUTTON <>  6 : RB0/INT       PGD/RB7 : 13 <> ICSP_PGD
;             <>  7 : RB1           PGC/RB6 : 12 <> ICSP_PGC
;             <>  8 : RB2               RB5 : 11 <> 
;             <>  9 : RB3               RB4 : 10 <> 
;                   +-----------------------:
;                            DIP-18 
;
; Description:
;    Toggle LED pin D2 when the button on RB0 is pressed.
;   
#include "p16F84A.inc"
;
    list    r=dec, n=0, c=255   ; make default radix decimal, no page breaks and maximum line length in list file
    errorlevel -302             ; suppress register not in bank zero warning
;
; Setup the configuration word
;
     __CONFIG _FOSC_HS & _WDTE_OFF & _PWRTE_OFF & _CP_OFF
;
; Constants
;
#define DEBOUNCE_COUNT      H'FF'
#define BUTTON_RB0_MASK     B'00000001'
#define BUTTON_RB0_PRESSED  B'00000000'
#define BUTTON_RB0_RELEASED B'00000001'
#define LED_D2_MASK         B'00000001'
;
; Memory
;
  CBLOCK H'0D'
    DEBOUNCE_COUNTER
    BUTTON_SAMPLE
    BUTTON_CHANGED
    BUTTON_STABLE
  ENDC
;
; Code
;
    ;set up
    BANKSEL TRISA
    CLRF    TRISA               ; Make all of PORTA outputs
    MOVLW   H'01'
    MOVWF   TRISB               ; Make RB0 an input, RB1 to RB7 outputs
    BANKSEL PORTA
    CLRF    PORTA               ; Make all PORTA outputs zero

    ;setup button debouncer
    BANKSEL DEBOUNCE_COUNTER
    CLRF    BUTTON_SAMPLE
    CLRF    BUTTON_CHANGED
    CLRF    BUTTON_STABLE
    MOVLW   0

    ;program start
RESTART_DEBOUNCER:
    XORWF   BUTTON_SAMPLE,F     ; Update sample to current state
    MOVLW   DEBOUNCE_COUNT
    MOVWF   DEBOUNCE_COUNTER

START:
    MOVF    PORTB,W             ; Read current button state
    ANDLW   BUTTON_RB0_MASK     ; Make all non-button bits zero
    XORWF   BUTTON_SAMPLE,W     ; Compare against last sample
    BNZ     RESTART_DEBOUNCER   ; Branch is button is still changing
    MOVF    DEBOUNCE_COUNTER,F  ; Check button debounce counter for zero
    BZ      BUTTONS_DEBOUNCED   ; Branch when button has been stable long enough
    DECF    DEBOUNCE_COUNTER,F  ; Update debounce count
    GOTO    START               ; Loop scanning for button changes

BUTTONS_DEBOUNCED:
    MOVF    BUTTON_SAMPLE,W     ; Read new stable button state
    XORWF   BUTTON_STABLE,W     ; Compare against last stable button state
    MOVWF   BUTTON_CHANGED      ; Save Buttons that change since last stable button state
    BZ      START               ; Branch when no button changed state
    XORWF   BUTTON_STABLE,F     ; Update stable button state

    MOVF    BUTTON_CHANGED,W
    ANDLW   BUTTON_RB0_MASK
    BZ      START               ; Branch because RB0 did not change
    MOVF    BUTTON_STABLE,W
    ANDLW   BUTTON_RB0_MASK
    XORLW   BUTTON_RB0_PRESSED
    BZ      TOGGLE              ; Branch when RB0 changed to pressed
    GOTO    START

TOGGLE:
    MOVLW   LED_D2_MASK
    XORWF   PORTA,F
    GOTO    START

    END