如何在Assembler中更改函数的按钮

时间:2014-01-02 14:44:13

标签: assembly stopwatch picoblaze

我在Assembler中的代码有问题。我有这些秒表。通过按下按钮1,它将开始计数,再次按下它将暂停计数,按下按钮2,它将复位,并通过翻转开关编号1,它将开始递减当前的值。但我现在需要使用按钮编号3,而不是开关编号1.我编程后已经有一段时间了。请问有人可以帮帮我吗?感谢

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
updown equ s9

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

    in updown, SWITCH
    comp updown, 1
    jump z, odecitej

    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
    call decode

odecitej:
    sub cnt0, 1
    comp cnt0, 255
    jump nz, decode
    load cnt0, 9
    sub cnt1, 1
    comp cnt1, 255
    jump nz, decode
    load cnt1, 9
    sub cnt2, 1
    comp cnt2, 255
    jump nz, decode
    load cnt2, 9
    sub cnt3, 1
    comp cnt3, 255
    jump nz, decode
    load cnt3, 9

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)

就像我说的那样,您可以通过用第二个按钮替换开关来改变它。

您可以将重置移至button3 - 可能 - 通过:

; reset check
    and temp, 4

添加两个新的开关状态和“状态变量”:

switch_state0 equ 0
switch_state1 equ 1
...
switch_state equ s? ; Whichever fits you best

startstop_check:
    in temp, BUTTON
    and temp, 3 ; mask extra bits off

    comp temp, 2 ; check if button 2 only
    jump nz,button1_check ; no
    comp temp, 3 ; check if buttons 1 and 2
    jump nz,button1_check ; no
    ; button 2 was pressed - "simulate" switch
    comp switch_state switch_state0 ; still being pressed
    jump nz button1_check ; yes - don't toggle
    load switch_state switch_state0 ; no, mark new press
    xor updown 1 ; toggle switch

button1_check:
    comp switch_state switch_state0 ; "switch"-button been pressed
    jump nz button1_check ; don't toggle
    load switch_state switch_state1 ; mark "switch"-button released

    and temp, 1
    ...

然后,放弃这个:

in updown, SWITCH

也许这有帮助?