Question

I have a problem with my code in Assembler. I have these stopwatches. By pressing Button 1, it will start counting, by pressing it again ti will pause the counting, by pressing Button 2, it will reset and by flipping over the Switch number 1 it will start decrementing the value there currently is. But I need it now to use the Button number 3, rather than the Switch number 1. It has been a while since I programmed this. Can anybody help me with this please? Thanks

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
Était-ce utile?

La solution

Like I said, you can change this to work by replacing the switch with second button.

You can move the reset to button3 - probably - by:

; reset check
    and temp, 4

Add two new switch-states and the "state variable":

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

and:

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
    ...

And then, drop this:

in updown, SWITCH

Maybe this helps?

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top