到目前为止,我写的代码只是闪烁,无论哪个开关是HIGH还是LOW。我似乎无法弄清楚如何使一个开关比其他开关更快,因为它会以相同的速率闪烁所有开关或保持稳定(不会闪烁)。谁能帮助我?
任务:
编写一个扫描开关设置的程序,并根据这些开关设置调整LED的闪烁速率。
您的任务是编写一个程序来检测开关设置,并在每个开关从低电平设置为高电平时减慢LED的闪烁。你的程序应该是这样的。注意:ICSA.asm和switches.asm包含您需要的部分。
我的代码:
;=============================================================================
; 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
答案 0 :(得分: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