Você está na página 1de 11

Interrupt-Driven

State Machine
Lecture L4.2a
State Machine with Delay
dtime

S0

dtime S2 S1

dtime
Use Real-time Interrupt
with 50 ms Period
State LED Delay
(sec)
S0 Red 0.5

S1 Yellow 1.0

S2 Green 0.75
State Machine with Delay
dtime

S0

dtime S2 S1

dtime
Data Structure
Variable Value
Dtime 1
Num_states 3
Cur_state 3
S0 $80
S0t 10
S1 $40
S1t 20
S2 $20
S2t 15
$7B
; LED state machine example
ptp equ $258 ; Port P
ddrp equ $25A ; Direction
ptj equ $268 ; Port J
ddrj equ $26A ; Direction
rti_ivec equ $0FF2 Note: Should be
crgflg equ $0037 $0FF0.
crgint equ $0038 Monitor error
rtictl equ $003b
ORG $800
dtime db 1
num_states db 3
cur_state db 3
s0 db $80 ;red led
s0t db 10 ;1/2 sec delay
s1 db $40 ;yellow led
s1t db 20 ;1 sec delay
s2 db $20 ;green led
s2t db 15 ;3/4 sec delay
ORG $4000
main
jsr portp_init
jsr rti_init
mn1 bra mn1

portp_init
ldaa #$E0 ;PP7..PP5 outputs
staa ddrp
ldaa #0
staa ptp ;leds off
rts

rti_init sei ;disable interrupts


ldaa #$7b
staa rtictl ;set rti to 49.15 ms
ldaa #$80
staa crgint ;enable r
ldx #rti_intser
stx rti_ivec ;store rti int vector
cli ;enable interrupts
rts
rti_intser
ldaa dtime
deca ;dec dtime
bne exit ;if not zero
ldab cur_state
cmpb num_states ; if cur_state = num_state
bne ri1
clrb
stab cur_state ; reset cur_state to 0
ri1 lslb ; ptr = cur_state * 2
ldx #s0 ; x -> state 0
abx ; x -> next state
ldaa 0,x ; get state value
staa ptp ; and display it
inc cur_state ; update cur_state
ldaa 1,x ; get delay time
exit staa dtime ; and store it in dtime
ldaa #$80
staa crgflg ;clear rti flag
rti
ORG $800
dtime db 1
num_states db 3
cur_state db 3
s0 db $80 ;red led
s0t db 10 ;1/2 sec delay
s1 db $40 ;yellow led
s1t db 20 ;1 sec delay
s2 db $20 ;green led
s2t db 15 ;3/4 sec delay