MPLab 汇编语言延迟

发布于 2024-12-19 12:15:10 字数 8787 浏览 2 评论 0原文

到目前为止,我编写的代码仅不断闪烁,无论哪个开关为高电平或低电平。我似乎无法弄清楚如何使一个开关比其他开关更快,因为它要么以相同的速率闪烁所有开关,要么保持稳定(不闪烁)。有人可以帮我吗?

任务:

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

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

  1. 扫描开关
    一个。如果开关 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

The code I have written so far only blinks constantly regardless of which switch is HIGH or LOW. I can't seem to figure out how to make one switch faster than the others because it would either blink all switches at same rate or remain steady (doesn't blink). Could anyone help me out?

Task:

Write a program that scans the switch settings and adjusts the blink rate of and LED according to those switch settings.

Your task is to write a program that detect the switch setting and slow down the flashing of the LED as each switch is set from LOW to HIGH. Your program should look like this. Note: ICSA.asm and switches.asm contain pieces of what you need.

  1. Scan the switches
    a. If Switch 1 is HIGH then change the blink timer and make it flash slower
    b. If Switch 2 is HIGH then change the blink timer and make it flash slower
    c. If Switch 3 is HIGH then change the blink timer and make it flash slower
    d. If Switch 4 is HIGH then change the blink timer and make it flash slower
    e. If Switch 5 is HIGH then change the blink timer and make it flash slower
    f. If Switch 6 is HIGH then change the blink timer and make it flash slower
    g. If Switch 7 is HIGH then change the blink timer and make it flash slower
    h. If Switch 8 is HIGH then change the blink timer and make it flash slower
  2. Blink the LED
  3. Goto Scan the switches

My Code:

;=============================================================================
;   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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心奴独伤 2024-12-26 12:15:10

您似乎从未扫描过开关。您的无限循环会闪烁、切换 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

It doesn't appear that you are ever scanning the switches. Your infinite loop does the blink, toggles the enable for the UART receiver, and then repeats. You need to call the Check_Switches subroutine.

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

    pagesel Blink
    call Blink

check

    pagesel Send
    goto Send
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文