;**** includes **** 
.include "tn26def.inc"
;*****************************
.def	d1	= r16
.def	d2	= r17
.def	d3	= r18
.def	d4	= r19
.def	temp1	= r20
.def	temp2	= r21
.def	temp3	= r22
.def	d5	= r23
.def	d6	= r24
.def	c	= r25
.def	c1	= r26
;**  PORTB  ***
.equ	l1	=0
.equ	l2	=1
.equ	l3	=2
.equ	l4	=3
.equ	l5	=4
;**  PORTA  **
.equ	ca	=0
.equ	cb	=1
.equ	cc	=2
.equ	cd	=3
.equ	cr	=4
;*********************
.cseg 
	rjmp	start	  	 	;各種ﾘｾｯﾄ
 	reti ;RJMP	 EXT_INT0	;外部割り込み要求0
 	rjmp	pin_chg	 ;I/Oﾋﾟﾝ ﾚﾍﾞﾙ変化
 	reti ;RJMP	 TIM1_COMPA	 ;ﾀｲﾏ/ｶｳﾝﾀ1比較A一致
 	reti ;RJMP	 TIM1_COMPB	 ;ﾀｲﾏ/ｶｳﾝﾀ1比較B一致
 	reti ;RJMP	 TIM_OVF1	 ;ﾀｲﾏ/ｶｳﾝﾀ1溢れ
 	reti ;RJMP	 TIM_OVF0	 ;ﾀｲﾏ/ｶｳﾝﾀ0溢れ
 	reti ;RJMP	 USI_STRT	 ;多用途直列ｲﾝﾀｰﾌｪｰｽ開始条件検出
	reti ;RJMP	 USI_OVF	 ;多用途直列ｲﾝﾀｰﾌｪｰｽ計数器溢れ
 	reti ;RJMP	 EE_RDY	 	 ;EEPROM 操作可
 	reti ;RJMP	 ANA_COMP	 ;ｱﾅﾛｸﾞ比較器出力遷移
 	reti ;RJMP	 ADC	 	 ;A/D変換完了
	
;*********************

start:
	ldi temp1,LOW(RAMEND)
	out	sp,temp1
	ldi	temp1,0xAA	;内臓osc4MHz 電圧補正　基準5V→3V
	out	OSCCAL,temp1
	ldi	temp1,0
	out	DDRB,temp1	; pb0~7 in port
	ldi	temp1,0xff	; pb0~7 pull up
	out	PORTB,temp1		
	ldi	temp1,0x1f
	out	DDRA,temp1	; pa0~4 out port ,5~7 in port
	ldi	temp1,0xe0	; pa0~4 low	
	out	PORTA,temp1	; pa5~7 pull up

main:
	ldi	temp1,(1<<se)|(1<<sm1)	;ｽﾘｰﾌﾟ許可,ﾊﾟﾜｰﾀﾞｳﾝ動作
	out MCUCR,temp1
	ldi temp1,(1<<PCIE1)|(1<<PCIE0) ;ﾋﾟﾝ変化割り込み許可
	out GIMSK,temp1
	sei
	sleep
	rjmp main

pin_chg:
	clr temp1
	out MCUCR,temp1
	out GIMSK,temp1
	sbis 	PINB,l1
	rjmp	line1
	sbis 	PINB,l2
	rjmp	line2
	sbis 	PINB,l3
	rjmp	line3
	sbis 	PINB,l4
	rjmp	line4
	sbis 	PINB,l5
	rjmp	line5
	reti
line1:
	ldi	temp1,0xee	;ca=low
	out	PORTA,temp1 ;cb,cc,cd=high
	ldi	temp1,1
	rcall	wait_ms
	sbis 	PINB,l1
	rjmp	key1a
	sbi	PORTA,ca	;cb=low	
	cbi	PORTA,cb	;ca,cc,cd=high
	ldi	temp1,1
	rcall	wait_ms
	sbis 	PINB,l1
	rjmp	key1b
	sbi	PORTA,cb	;cc=low
	cbi	PORTA,cc	;ca,cb,cd=high
	ldi	temp1,1
	rcall	wait_ms
	sbis 	PINB,l1
	rjmp	key1c
	sbi	PORTA,cc	;cd=low
	cbi	PORTA,cd	;ca,cb,cc=high
	ldi	temp1,1
	rcall	wait_ms
	sbis 	PINB,l1
	rjmp	key1d
	rjmp	key99
line2:
	ldi	temp1,0xee	;ca=low
	out	PORTA,temp1
	ldi	temp1,1
	rcall	wait_ms
	sbis 	PINB,l2
	rjmp	key2a
	sbi	PORTA,ca
	cbi	PORTA,cb
	ldi	temp1,1
	rcall	wait_ms
	sbis 	PINB,l2
	rjmp	key2b
	sbi	PORTA,cb
	cbi	PORTA,cc
	ldi	temp1,1
	rcall	wait_ms
	sbis 	PINB,l2
	rjmp	key2c
	sbi	PORTA,cc
	cbi	PORTA,cd
	ldi	temp1,1
	rcall	wait_ms
	sbis 	PINB,l2
	rjmp	key2d
	rjmp	key99
line3:
	ldi	temp1,0xee	;ca=low
	out	PORTA,temp1
	ldi	temp1,1
	rcall	wait_ms
	sbis 	PINB,l3
	rjmp	key3a
	sbi	PORTA,ca
	cbi	PORTA,cb
	ldi	temp1,1
	rcall	wait_ms
	sbis 	PINB,l3
	rjmp	key3b
	sbi	PORTA,cb
	cbi	PORTA,cc
	ldi	temp1,1
	rcall	wait_ms
	sbis 	PINB,l3
	rjmp	key3c
	sbi	PORTA,cc
	cbi	PORTA,cd
	ldi	temp1,1
	rcall	wait_ms
	sbis 	PINB,l3
	rjmp	key3d
	rjmp	key99
line4:
	ldi	temp1,0xee	;ca=low
	out	PORTA,temp1
	ldi	temp1,1
	rcall	wait_ms
	sbis 	PINB,l4
	rjmp	key4a
	sbi	PORTA,ca
	cbi	PORTA,cb
	ldi	temp1,1
	rcall	wait_ms
	sbis 	PINB,l4
	rjmp	key4b
	sbi	PORTA,cb
	cbi	PORTA,cc
	ldi	temp1,1
	rcall	wait_ms
	sbis 	PINB,l4
	rjmp	key4c
	sbi	PORTA,cc
	cbi	PORTA,cd
	ldi	temp1,1
	rcall	wait_ms
	sbis 	PINB,l4
	rjmp	key4d
	rjmp	key99
line5:
	ldi	temp1,0xee	;ca=low
	out	PORTA,temp1
	ldi	temp1,1
	rcall	wait_ms
	sbis 	PINB,l5
	rjmp	key5a
	sbi	PORTA,ca
	cbi	PORTA,cb
	ldi	temp1,1
	rcall	wait_ms
	sbis 	PINB,l5
	rjmp	key5b
	sbi	PORTA,cb
	cbi	PORTA,cc
	ldi	temp1,1
	rcall	wait_ms
	sbis 	PINB,l5
	rjmp	key5c
	sbi	PORTA,cc
	cbi	PORTA,cd
	ldi	temp1,1
	rcall	wait_ms
	sbis 	PINB,l5
	rjmp	key5d
	rjmp	key99
	
key1a:	;V+
	ldi	d1,0x50
	ldi	d2,0xaf
	ldi	d3,0x12
	ldi	d4,0xed
	ldi	d5,0x00
	ldi	d6,0x00
	rcall	sousin
	rjmp	key99
key1b:	;12
	ldi	d1,0x02	;番号入力
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x71
	ldi	d6,0xd7
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;7
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x66
	ldi	d6,0xc0
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;5
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x64
	ldi	d6,0xc2
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;0
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x69
	ldi	d6,0xcf
	rcall	sousin
	rjmp	key99
key1c:	;11
	ldi	d1,0x02	;番号入力
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x71
	ldi	d6,0xd7
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;7
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x66
	ldi	d6,0xc0
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;5
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x64
	ldi	d6,0xc2
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;1
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x60
	ldi	d6,0xc6
	rcall	sousin
	rjmp	key99
key1d:	;10
	ldi	d1,0x02	;番号入力
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x71
	ldi	d6,0xd7
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;7
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x66
	ldi	d6,0xc0
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;5
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x64
	ldi	d6,0xc2
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;3
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x62
	ldi	d6,0xc4
	rcall	sousin
	rjmp	key99
key2a:	;電源
	ldi	d1,0x50	;TV
	ldi	d2,0xaf
	ldi	d3,0x17
	ldi	d4,0xe8
	ldi	d5,0x00
	ldi	d6,0x00
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;catv_p
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x8d
	ldi	d6,0x2b
	rcall	sousin
	rjmp	key99
key2b:	;9
	ldi	d1,0x02	;番号入力
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x71
	ldi	d6,0xd7
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;7
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x66
	ldi	d6,0xc0
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;7
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x66
	ldi	d6,0xc0
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;0
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x69
	ldi	d6,0xcf
	rcall	sousin
	rjmp	key99
key2c:	;8
	ldi	d1,0x02	;番号入力
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x71
	ldi	d6,0xd7
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;7
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x66
	ldi	d6,0xc0
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;8
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x67
	ldi	d6,0xc1
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;1
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x60
	ldi	d6,0xc6
	rcall	sousin
	rjmp	key99
key2d:	;7
	ldi	d1,0x02	;番号入力
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x71
	ldi	d6,0xd7
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;7
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x66
	ldi	d6,0xc0
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;8
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x67
	ldi	d6,0xc1
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;2
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x61
	ldi	d6,0xc7
	rcall	sousin
	rjmp	key99
key3a:	;V-
	ldi	d1,0x50
	ldi	d2,0xaf
	ldi	d3,0x15
	ldi	d4,0xea
	ldi	d5,0x00
	ldi	d6,0x00
	rcall	sousin
	rjmp	key99
key3b:	;6
	ldi	d1,0x02	;番号入力
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x71
	ldi	d6,0xd7
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;0
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x69
	ldi	d6,0xcf
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;6
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x65
	ldi	d6,0xc3
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;1
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x60
	ldi	d6,0xc6
	rcall	sousin
	rjmp	key99
key3c:	;5
	ldi	d1,0x02	;番号入力
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x71
	ldi	d6,0xd7
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;0
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x69
	ldi	d6,0xcf
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;5
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x64
	ldi	d6,0xc2
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;1
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x60
	ldi	d6,0xc6
	rcall	sousin
	rjmp	key99
key3d:	;4
	ldi	d1,0x02	;番号入力
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x71
	ldi	d6,0xd7
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;0
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x69
	ldi	d6,0xcf
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;4
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x63
	ldi	d6,0xc5
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;1
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x60
	ldi	d6,0xc6
	rcall	sousin
	rjmp	key99
key4a:	;消音	朝日ﾆｭｰｽﾀｰ
	ldi	d1,0x02	;番号入力
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x71
	ldi	d6,0xd7
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;7
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x66
	ldi	d6,0xc0
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;9
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x68
	ldi	d6,0xce
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;5
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x64
	ldi	d6,0xc2
	rcall	sousin
	rjmp	key99
key4b:	;3
	ldi	d1,0x02	;番号入力
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x71
	ldi	d6,0xd7
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;0
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x69
	ldi	d6,0xcf
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;8
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x67
	ldi	d6,0xc1
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;1
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x60
	ldi	d6,0xc6
	rcall	sousin
	rjmp	key99
key4c:	;2
	ldi	d1,0x02	;番号入力
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x71
	ldi	d6,0xd7
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;0
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x69
	ldi	d6,0xcf
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;2
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x61
	ldi	d6,0xc7
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;1
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x60
	ldi	d6,0xc6
	rcall	sousin
	rjmp	key99
key4d:	;1
	ldi	d1,0x02	;番号入力
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x71
	ldi	d6,0xd7
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;0
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x69
	ldi	d6,0xcf
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;1
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x60
	ldi	d6,0xc6
	rcall	sousin
	ldi	temp1,150
	rcall	wait_ms
	ldi	d1,0x02	;1
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x60
	ldi	d6,0xc6
	rcall	sousin
	rjmp	key99
key5a:	;C-
	ldi	d1,0x02	
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x75
	ldi	d6,0xd3
	rcall	sousin
	rjmp	key99
key5b:	;入力
	ldi	d1,0x50
	ldi	d2,0xaf
	ldi	d3,0x09
	ldi	d4,0xf6
	ldi	d5,0x00
	ldi	d6,0x00
	rcall	sousin
	rjmp	key99
key5c:	;C+
	ldi	d1,0x02	
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x74
	ldi	d6,0xd2
	rcall	sousin
	rjmp	key99
key5d:	;BS
	ldi	d1,0x02	
	ldi	d2,0x20
	ldi	d3,0x80
	ldi	d4,0x26
	ldi	d5,0x6b
	ldi	d6,0xcd
	rcall	sousin

key99:
	ldi	temp1,255
	rcall	wait_ms
	ldi	temp1,0xe0	; pa0~4 low	
	out	PORTA,temp1	; pa5~7 pull up
	reti

sousin:
	tst	d5	
	brne	sbit1 ;catv 家製協フォーマット（3.4ms）
sbit:
	ldi	c,216	;TV ＮＥＣフォーマット（9ms）
pulse_s:
	ldi	temp1,8
	sbi	PORTA,cr
	rcall	wait_us
	ldi	temp1,17
	cbi	PORTA,cr
	rcall	wait_us
	dec	c
	brne	pulse_s
sbit1:
	ldi	c,130
pulse_s1:
	ldi	temp1,8
	sbi	PORTA,cr
	rcall	wait_us
	ldi	temp1,17
	cbi	PORTA,cr
	rcall	wait_us
	dec	c
	brne	pulse_s1
	tst	d5
	brne 	pulse_s2	
	ldi	temp1,45 ;ＮＥＣフォーマット（4.5ms）
	rjmp	pulse_s3
pulse_s2:
	ldi	temp1,17 ;家製協フォーマット（1.7ms）	
pulse_s3:
	rcall	wait_100us

data:
	ldi	c1,0
	tst	d5
	brne	data1	;cavt 6byte
	subi	c1,-16	;tv 4byte
data1:
	lsr	d6
	ror	d5
	ror	d4
	ror	d3
	ror	d2
	ror	d1
	brcs	iti
zero:	
	ldi	c,21
pulse_z:
	ldi	temp1,8
	sbi	PORTA,cr
	rcall	wait_us
	ldi	temp1,17
	cbi	PORTA,cr
	rcall	wait_us
	dec	c
	brne	pulse_z
	ldi	temp1,56
	rcall	wait_10us
	rjmp	data3
iti:
	ldi	c,21
pulse_i:
	ldi	temp1,8
	sbi	PORTA,cr
	rcall	wait_us
	ldi	temp1,17
	cbi	PORTA,cr
	rcall	wait_us
	dec	c
	brne	pulse_i
	ldi	temp1,168
	rcall	wait_10us
data3:	
	inc	c1
	cpi	c1,48
	brne	data1
	
stbit:
iti1:
	ldi	c,21
pulse_i1:
	ldi	temp1,8
	sbi	PORTA,cr
	rcall	wait_us
	ldi	temp1,17
	cbi	PORTA,cr
	rcall	wait_us
	dec	c
	brne	pulse_i1
	ldi	temp1,168
	rcall	wait_10us
	ldi	temp1,22
	rcall	wait_ms
exit:
	ret

;base 4MHz	
wait_us:
	nop				; 1clocks
	dec	temp1		; 1clocks
	brne wait_us	; 2clocks
	ret

wait_10us:			;temp1x10us  40clocks
	ldi	temp2,9
wait_10us1:
	nop				; 1clocks
	dec	temp2		; 1clocks
	brne wait_10us1	; 2clocks
	nop				; 1clocks
	dec	temp1		; 1clocks	
	brne wait_10us	; 2clocks
	ret

wait_100us:			;temp1x100us  400clocks
	ldi	temp2,99		
wait_us_01:
	nop				; 1clocks
	dec	temp2		; 1clocks
	brne wait_us_01	; 2clocks
	nop				; 1clocks
	dec	temp1		; 1clocks	
	brne wait_100us	; 2clocks
	ret

wait_ms:			;temp1　ms
	ldi	temp2,125
wait_ms_01:
	ldi	temp3,7
wait_ms_02:
	nop				; 1clocks
	dec	temp3		; 1clocks
	brne wait_ms_02	; 2clocks
	nop				; 1clocks
	dec	temp2		; 1clocks
	brne wait_ms_01	; 2clocks
	dec	temp1		; 1clocks	
	brne wait_ms	; 2clocks
	ret
