1

到目前为止,我编写的代码只会不断闪烁,无论哪个开关是 HIGH 或 LOW。我似乎无法弄清楚如何使一个开关比其他开关更快,因为它会以相同的速率闪烁所有开关或保持稳定(不闪烁)。谁能帮帮我?

任务:

编写一个程序,扫描开关设置并根据这些开关设置调整 LED 和 LED 的闪烁率。

你的任务是编写一个程序来检测开关设置并在每个开关从低设置为高时减慢 LED 的闪烁。你的程序应该是这样的。注意:ICSA.asm 和switches.asm 包含您需要的部分。

  1. 扫描开关
    a. 如果开关 1 为高电平,则更改闪烁计时器并使其闪烁更慢
    b. 如果开关 2 为高电平,则更改闪烁计时器并使其闪烁更慢
    c。如果开关 3 为高电平,则更改闪烁计时器并使其闪烁速度变慢
    d。如果开关 4 为高电平,则更改闪烁计时器并使其闪烁更慢
    e。如果开关 5 为高电平,则更改闪烁计时器并使其闪烁速度变慢
    f。如果开关 6 为高电平,则更改闪烁计时器并使其闪烁速度变慢
    g。如果开关 7 为高电平,则更改闪烁计时器并使其闪烁速度变慢
    h。如果开关 8 为高,则更改闪烁计时器并使其闪烁速度变慢
  2. 闪烁 LED
  3. 转到扫描开关

我的代码:

;=============================================================================
;   Assembled using MPASM 7.4
;   Modified for 20Mhz at 9600 baud
;=============================================================================
;   Include Files:  p16f873A.inc    V1.00
;=============================================================================
;   The program 
;   1.  Turns RB7 on and off
;   2.  Transmits characters
;
;=============================================================================

list p=16f873A, st=OFF, x=OFF, n=0
errorlevel -302
#include <p16f873A.inc>
__CONFIG _BODEN_OFF & _CP_OFF & _PWRTE_ON & _WDT_OFF &  _HS_OSC & _DEBUG_OFF & _CPD_OFF & _LVP_ON
;
;-----------------------------------------------------------------------------
; RS232 Constants
RTS_OUTPUT  EQU 1   ;Port B Pin 1 output for flow control
CTS_INPUT   EQU 2   ;Port B Pin 2 input for flow control
BAUD_CONSTANT   EQU 0x81    ;Constant for baud generator for 9600 baud 20MHz


;-----------------------------------------------------------------------------
;Variables in bank0

    CBLOCK  0x20

    counter1:   1   ;delay counter
    counter2:   1
    counter3:   1

    ENDC

;=============================================================================
;Reset vector code

    ORG 0x0000 

ResetVector 
    pagesel Init_RS232      ;select page for Init_RS232 
    goto    Init_RS232      ;go to Init_RS232

;=============================================================================
;Start of code 
    ORG 0x0600      ;Use page 6

;Set up USART for asynchronous comms
;Routine is only called once and can be placed in-line saving a call and return
;This routine returns in bank0

Init_RS232
    banksel PORTB           ;change to PORTB bank
    bsf PORTB, RTS_OUTPUT   ;set RTS off before setting as output
    banksel TRISB           ;change to TRISB bank
    bcf TRISB, RTS_OUTPUT   ;enable RTS pin as output
    movlw   BAUD_CONSTANT   ;set baud rate 
    movwf   SPBRG
    bsf TXSTA, BRGH         ;baud rate high speed option
    bsf TXSTA, TXEN         ;enable transmission
    banksel RCSTA           ;change to RCSTA bank
    bsf RCSTA, CREN         ;enable reception
    bsf RCSTA, SPEN         ;enable serial port

Init_TRISA

Init_TRISB
    banksel TRISB       ;set i/o port data direction
    bcf TRISB, 0x07     ;output=0

Send
    banksel RCSTA
    bcf RCSTA, CREN         ;disable reception - clear reception
    bsf RCSTA, CREN         ;enable reception

    pagesel Blink
    call Blink

check

    pagesel Send
    goto Send

;----------------------------------------------------------------------------
Check_switches

;output 0 to decoder
    banksel PORTB
    bcf PORTB, 0x04     ;output=0=RB4=decoder input A (low order)
    bcf PORTB, 0x05     ;output=0=RB5=decoder input B
    bcf PORTB, 0x06     ;output=0=RB6=decoder input C (high order)      
    nop                 ;NOPs - 1 micro second delay to compensate for 4051 chips with slightly slower specifications
    nop
    nop 
    nop                 
    nop
    pagesel delay01
    banksel PORTC
    btfsc PORTC,0x05    ;RC5
    call delay01

;output 1 to decoder
    banksel PORTB
    bsf PORTB, 0x04     ;output=0=RB4=decoder input A (low order)
    bcf PORTB, 0x05     ;output=0=RB5=decoder input B
    bcf PORTB, 0x06     ;output=0=RB6=decoder input C (high order)      
    nop                 ;NOPs to compensate for chips with slightly slower specifications
    nop 
    nop
    nop 
    nop
    pagesel delay01
    banksel PORTC
    btfsc PORTC,0x05    ;RC5
    call delay01

;output 2 to decoder
    banksel PORTB
    bcf PORTB, 0x04     ;output=0=RB4=decoder input A (low order)
    bsf PORTB, 0x05     ;output=0=RB5=decoder input B
    bcf PORTB, 0x06     ;output=0=RB6=decoder input C (high order)      
    nop                 ;NOPs to compensate for chips with slightly slower specifications
    nop 
    nop 
    nop
    nop
    pagesel delay01
    banksel PORTC
    btfsc PORTC,0x05    ;RC5
    call delay01

;output 3 to decoder
    banksel PORTB
    bsf PORTB, 0x04     ;output=0=RB4=decoder input A (low order)
    bsf PORTB, 0x05     ;output=0=RB5=decoder input B
    bcf PORTB, 0x06     ;output=0=RB6=decoder input C (high order)      
    nop                 ;NOPs to compensate for chips with slightly slower specifications
    nop
    nop 
    nop 
    nop
    pagesel delay01
    banksel PORTC
    btfsc PORTC,0x05    ;RC5
    call delay01

;output 4 to decoder
    banksel PORTB
    bcf PORTB, 0x04     ;output=0=RB4=decoder input A (low order)
    bcf PORTB, 0x05     ;output=0=RB5=decoder input B
    bsf PORTB, 0x06     ;output=0=RB6=decoder input C (high order)      
    nop                 ;NOPs to compensate for chips with slightly slower specifications
    nop 
    nop
    nop 
    nop
    pagesel delay01
    banksel PORTC
    btfsc PORTC,0x05    ;RC5
    call delay01

;output 5 to decoder
    banksel PORTB
    bsf PORTB, 0x04     ;output=0=RB4=decoder input A (low order)
    bcf PORTB, 0x05     ;output=0=RB5=decoder input B
    bsf PORTB, 0x06     ;output=0=RB6=decoder input C (high order)      
    nop                 ;NOPs to compensate for chips with slightly slower specifications
    nop
    nop 
    nop 
    nop
    pagesel delay01
    banksel PORTC
    btfsc PORTC,0x05    ;RC5
    call delay01

;output 6 to decoder
    banksel PORTB
    bcf PORTB, 0x04     ;output=0=RB4=decoder input A (low order)
    bsf PORTB, 0x05     ;output=0=RB5=decoder input B
    bsf PORTB, 0x06     ;output=0=RB6=decoder input C (high order)      
    nop                 ;NOPs to compensate for chips with slightly slower specifications
    nop
    nop 
    nop 
    nop
    pagesel delay01
    banksel PORTC
    btfsc PORTC,0x05    ;RC5
    call delay01

;output 7 to decoder
    banksel PORTB
    bsf PORTB, 0x04     ;output=0=RB4=decoder input A (low order)
    bsf PORTB, 0x05     ;output=0=RB5=decoder input B
    bsf PORTB, 0x06     ;output=0=RB6=decoder input C (high order)      
    nop                 ;NOPs to compensate for chips with slightly slower specifications
    nop
    nop 
    nop 
    nop
    pagesel delay01
    banksel PORTC
    btfsc PORTC,0x05    ;RC5
    call delay01

    pagesel Check_switches
    goto Check_switches

;----------------------------------------------------------------------------       
Blink
    pagesel Light_on
    call Light_on
    pagesel delay01
    call delay01
    pagesel Light_off
    call Light_off
    pagesel delay01
    call delay01
    return 

Light_on
    banksel PORTB
    bsf PORTB, 0x07     ;RB7 = power on high
    return

Light_off
    banksel PORTB
    bcf PORTB, 0x07     ;RB7 = power off low
    return      

;----------------------------------------------------------------------------
delay01                     ;152ms counter1=00, counter2=00
    movlw 0x00
    banksel counter1
    movwf counter1
    movlw 0x00
    banksel counter2
    movwf counter2
    movlw 0x05  
    banksel counter3
    movwf counter3

delay02 
    banksel counter1    ;1
    nop                 ;1 
    decfsz counter1     ;1
    goto delay02
    decfsz counter2
    goto delay02
    decfsz counter3
    goto delay02
    return

;-----------------------------------------------------------------------------
;Transmit byte in W register to USART
; transmit_data_in_w
;   check for PORTB CTS_INPUT, clear to send with btfsc
;   check for PIR1 TXIF, transmit buffer empty with btfss
;   move w to TXREG to transmit byte

transmit_data_in_w
    banksel PORTB       ;change bank to PORTB
    ;btfsc  PORTB, CTS_INPUT    ;check CTS to see if data can be sent
    ;goto   $-1
    btfss   PIR1, TXIF  ;check that buffer is empty
    goto    $-1
    movwf   TXREG       ;transmit byte
    return

receive_data_in_w
    banksel PORTB       ;change bank to PORTB
    bcf     PORTB,RTS_OUTPUT ;set RTS on for data to be received
    ;btfss  PIR1,RCIF   ;check if data received
    ;goto   $-1     ;wait until new data
    movf    RCREG,W     ;get received data into W
    return

    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop

    end
4

1 回答 1

0

您似乎从未扫描过开关。您的无限循环会闪烁,切换 UART 接收器的启用,然后重复。您需要调用 Check_Switches 子例程。

Send
    banksel RCSTA
    bcf RCSTA, CREN         ;disable reception - clear reception
    bsf RCSTA, CREN         ;enable reception

    pagesel Blink
    call Blink

check

    pagesel Send
    goto Send
于 2012-04-05T17:31:35.367 回答