.include "tn13adef.inc"

.org $0
rjmp start
;部署int0低电平中断入口
rjmp shit

.org $100
start:
;初始化int0
inint0:
ldi r16,0b00000000;int0 low
out mcucr,r16
ldi r16,0b01000000
out gimsk,r16
;初始化io口
iniport:
ldi r16,0b00011101
out ddrb,r16
sbi portb,1
;初始化机器人开机动作，默认前进 低四位1010
cbi portb,4
sbi portb,4

ldi r28,0b1000
rcall train
;初始解码需要的工作寄存器
sei

loop:rjmp loop
;rcall delay15

;中断open

;整个机器人的工作循环，发生扫描波，关闭扫描波，等待中断发生，触发可能是障碍触发，也可能是遥控信号


shit:
clc
ldi r20,8
clr r19
rcall delay4

decoder:
rcall delay8
                
sbis pinb,1
rjmp b0
rjmp b1

b0:clc
ror r19
dec r20
brne decoder
mov r28,r19


rjmp work

b1:
sec 
ror r19
dec r20
brne decoder

mov r28,r19



work:
cpi r28,0x45
breq reply
cpi r28,0x44
breq front
cpi r28,0x43
breq back
cpi r28,0x42
breq left
cpi r28,0x41
breq right
cpi r28,0x53
breq stop
//breq send
reti

front:
ldi r28,0b10100000
rcall train
reti



back:
ldi r28,0b01010000
rcall train
reti



left:
ldi r28,0b10010000
rcall train
reti



right:
ldi r28,0b01100000
rcall train
reti


stop:
ldi r28,0b1
rcall train
reti


reply:
cbi portb,0
rcall delay8
ldi r28,0x70
ldi r26,8
send_b:
sbrs r28,0
rjmp c_0
rjmp c_1
c_0:
cbi portb,0
rcall delay8
rjmp next_b

c_1:
sbi portb,0
rcall delay8

next_b:
lsr r28
dec r26
brne send_b
sbi portb,0
reti


send:
ldi r27,0x20
add r28,r27
ldi r26,8
cbi portb,2
rcall delay8
did_loop1:
sbrc r28,0
rjmp cls_pb31
rjmp clc_pb31
cls_pb31:sbi portb,2
rcall delay8
rjmp next1
clc_pb31:cbi portb,2
rcall delay8
next1:
lsr r28

dec r26
brne did_loop1

sbi portb,2
reti







train:
ldi r26,8
did_loop:
sbrc r28,0
rjmp cls_pb3
rjmp clc_pb3
cls_pb3:sbi portb,3
rjmp next
clc_pb3:cbi portb,3
next:
lsr r28
cbi portb,2
sbi portb,2
dec r26
brne did_loop
delayx:
ldi r16,130
shit1:
rcall delay8
dec r16
brne shit1

ret






;open:ldi r16,0b00000001
;rjmp movi

;shut:ldi r16,0b00000000

;movi:out tccr0b,r16
;ret

;0.05ms delay 2xy+3X+1
delay4:
ldi r23,15
k12:ldi r24,10
k21:dec r24
brne k21
dec r23
brne k12
ret


;0.1MS
delay8:
ldi r23,30
l2:ldi r24,10
l1:dec r24
brne l1
dec r23
brne l2
ret


