如何反转我的秒表代码在Assembler中倒计时?

时间:2013-12-26 22:39:20

标签: assembly stopwatch picoblaze

我在汇编程序中为秒表编写了这段代码。如果按下按钮1,它将开始计数。如果再次按下相同的按钮,它将暂停,再按一次将恢复秒表。按下按钮编号2将重置秒表。

已经有一段时间了,因为我是在PicoBlaze汇编程序中编程的,这是几年的代码,因为我没有编程几年,我已经忘记了很多。但现在我需要略微改变它,我需要你的帮助。

所以我现在想要的是具有倒计时功能。因此,当我按下按钮1时,它将开始计数,再次按下它将暂停并再次恢复。但是按下按钮2将开始对其进行倒计时,与使用相同按钮暂停和恢复相同,并将重置从按钮2移动到新按钮3。

有人可以帮我这个吗?感谢。

VHDL    "ROM_blank.vhd", "ProgMem.vhd", "ProgMem"
BUTTON      DSIN        $00
SWITCH      DSIN        $01
LED         DSOUT       $02
SEG0        DSOUT       $03
SEG1        DSOUT       $04
SEG2        DSOUT       $05
SEG3        DSOUT       $06

wait0 equ s0
wait1 equ s1
wait2 equ s2
cnt0 equ s3
cnt1 equ s4
cnt2 equ s5
cnt3 equ s6
state equ s7
segsel equ s8
temp equ sf

state_r0 equ 0
state_r1 equ 1
state_s0 equ 2
state_s1 equ 3

decoded_dp equ 254
decoded_0 equ 3
decoded_1 equ 159
decoded_2 equ 37
decoded_3 equ 13
decoded_4 equ 153
decoded_5 equ 73
decoded_6 equ 65
decoded_7 equ 27
decoded_8 equ 1
decoded_9 equ 25
decoded_A equ 5
decoded_B equ 193
decoded_C equ 229
decoded_D equ 133
decoded_E equ 97
decoded_F equ 113


init:
     load cnt0, 0
     load cnt1, 0
     load cnt2, 0
     load cnt3, 0
     load state, state_s0
start:

; read buttons
    in temp, BUTTON
; reset check
    and temp, 2
    jump z, startstop_check
    load cnt0, 0
    load cnt1, 0
    load cnt2, 0
    load cnt3, 0

startstop_check:
    in temp, BUTTON
    and temp, 1

; state machine
    comp state, state_r0
    jump z,run0
    comp state, state_r1
    jump z,run1
    comp state, state_s0
    jump z, stop0
    comp state, state_s1
    jump z, stop1
    load state, state_s0

stop0:
    comp temp, 1
    jump nz, fsm_end
    load state, state_r0
    jump fsm_end
run0:
    comp temp, 0
    jump nz, fsm_end
    load state, state_r1
    jump fsm_end
run1:
    comp temp, 1
    jump nz, fsm_end
    load state, state_s1
    jump fsm_end
stop1:
    comp temp, 0
    jump nz, fsm_end
    load state, state_s0

fsm_end:

;wait 0.01s     
    call waitfcn

    comp state, state_s0
    jump z, decode
    comp state, state_s1
    jump z, decode

    add cnt0, 1
    comp cnt0, 10
    jump nz, decode
    load cnt0, 0
    add cnt1, 1
    comp cnt1, 10
    jump nz, decode
    load cnt1, 0
    add cnt2, 1
    comp cnt2, 10
    jump nz, decode
    load cnt2, 0
    add cnt3, 1
    comp cnt3, 10
    jump nz, decode
    load cnt3, 0

decode:
;decode for output
    load temp, cnt0
    call decodefcn
    out temp, SEG0

    load temp, cnt1
    call decodefcn
    out temp, SEG1

    load temp, cnt2
    call decodefcn
;add decimal point
    and temp, 254
    out temp, SEG2

    load temp, cnt3
    call decodefcn
    out temp, SEG3

    jump start

decodefcn:
    comp temp, 0
    jump nz, dec1
    load temp, decoded_0
    ret
dec1:
    comp temp, 1
    jump nz, dec2
    load temp, decoded_1
    ret
dec2:
    comp temp, 2
    jump nz, dec3
    load temp, decoded_2
    ret
dec3:
    comp temp, 3
    jump nz, dec4
    load temp, decoded_3
    ret
dec4:
    comp temp, 4
    jump nz, dec5
    load temp, decoded_4
    ret
dec5:
    comp temp, 5
    jump nz, dec6
    load temp, decoded_5
    ret
dec6:
    comp temp, 6
    jump nz, dec7
    load temp, decoded_6
    ret
dec7:
    comp temp, 7
    jump nz, dec8
    load temp, decoded_7
    ret
dec8:
    comp temp, 8
    jump nz, dec9
    load temp, decoded_8
    ret
dec9:
    load temp, decoded_9
    ret

waitfcn:
    load wait2, 25
wt0:
    load wait1, 25
wt1 :   
    load wait0, 200
wt2 :
    sub wait0, 1
    jump nz, wt2
    sub wait1, 1
    jump nz, wt1
    sub wait2, 1
    jump nz, wt0
    ret
END

1 个答案:

答案 0 :(得分:1)

基本上可以通过添加状态并复制编辑第二个按钮的代码+将第二个按钮移动到第三个按钮来完成。然后编辑向上计数向上计数:

;wait 0.01s     
    call waitfcn

    comp state, state_s0
    jump z, decode
    comp state, state_s1
    jump z, decode

    sub  cnt0, 1
    comp cnt0, 255 ; -1
    jump nz, decode
    load cnt0, 9
    sub  cnt1, 1
    comp cnt1, 255
    ...