;TIMER1以HIRC/128作为时钟源
;计数250溢出共2ms
;TIMER0以LIRC作为时钟源
;TIMER1溢出4次读取TIMER0的CNT值是否为ff/00
;根据T0CNT大小调整LIRCCAL值
;手调值0x6a/6b
#include       	"mc9941.inc"
cblock 	0x00
acc_store
pflag_store
of_cnt
shift_cnt
user_flag
shift_data0
shift_data1
endc
#define	       	adjust_direction_flag  	user_flag,1
#define	       	cal_adjust_finish      	user_flag,7
       	org    	0x00
       	goto   	       	start

       	org    	0x08
inte:
       	movra  	       	acc_store
       	swapar 	       	PFLAG
       	movra  	       	pflag_store

       	jbset  	       	T1IF
       	goto   	       	inte_return
       	goto   	       	tmr1_inte

tmr1_inte:
       	bclr   	       	T1IF
       	movar  	       	shift_cnt
       	isubai 	       	0x0a
       	jbclr  	       	Z
       	goto   	       	adjust_finish_fun
       	djzr   	       	of_cnt
       	goto   	       	inte_return
       	goto   	       	lirc_t0cnt_judge_fun

inte_return:
       	swapar 	       	pflag_store
       	movra  	       	PFLAG
       	swapr  	       	acc_store
       	swapar 	       	acc_store
       	retie

start:
       	call   	       	io_init
       	call   	       	tmr0_init
       	call   	       	tmr1_init
       	call   	       	inte_init
       	movai  	       	0x04
       	movra  	       	of_cnt
       	movai  	       	0x00
       	movra  	       	shift_cnt
       	movai  	       	0x7f
       	movra  	       	shift_data0
       	movai  	       	0x80
       	movra  	       	shift_data1
       	clrr   	       	user_flag
       	movai  	       	0x80
       	movra  	       	LIRCCAL
       	bset   	       	T1EN
       	bset   	       	T0EN
       ;   	bset   	       	CLKS
       	goto   	       	loop

loop:
       	movai  	       	0x08
       	xorra  	       	IOP0
       	jbset  	       	cal_adjust_finish
       	goto   	       	loop
       	bset   	       	CLKS
       	
slow_loop:
       	movai  	       	0x08
       	xorra  	       	IOP0
       	nop
       	goto   	       	slow_loop


io_init:
       	movai  	       	0xff
       	movra  	       	OEP0
       	movra  	       	OEP1
       	movra  	       	OEP2
       	movra  	       	OEP3
       	clrr   	       	IOP0
       	clrr   	       	IOP1
       	clrr   	       	IOP2
       	clrr   	       	IOP3
       	return

tmr0_init:
       	movai  	       	0x10   	       	;Flosc/1
       	movra  	       	T0CR
       	movai  	       	0xff
       	movra  	       	T0CNT
       	movra  	       	T0LOAD
       	return

tmr1_init:
       	movai  	       	0x0f   	       	;Fhosc/128
       	movra  	       	T1CR
       	movai  	       	0xf9   	       	;cnt=250
       	movra  	       	T1CNT
       	movra  	       	T1LOAD
       	movai  	       	0x63
       	movra  	       	T1DATA
       	movai  	       	0x01
       	movra  	       	PWM1CR
       	return

inte_init:
       	clrr   	       	INTE0
       	clrr   	       	INTE1
       	clrr   	       	INTF0
       	clrr   	       	INTF1
       	bset   	       	T1IE
       	bset   	       	GIE
       	return

lirc_t0cnt_judge_fun:
       	movai  	       	0x10
       	xorra  	       	IOP0
       	movai  	       	0x04
       	movra  	       	of_cnt
       ;   	goto   	       	inte_return    	;调试用，确认8ms精度
       	incr   	       	shift_cnt
       	movar  	       	shift_cnt
       	addra  	       	PCL
       	nop
       	goto   	       	cal_bit7_fun
       	goto   	       	cal_bit6_fun
       	goto   	       	cal_bit5_fun
       	goto   	       	cal_bit4_fun
       	goto   	       	cal_bit3_fun
       	goto   	       	cal_bit2_fun
       	goto   	       	cal_bit1_fun
       	goto   	       	cal_bit0_fun
       	goto   	       	adjust_finish_fun

cal_bit7_fun:
       	jbset  	       	T0IF
       	goto   	       	bit7_higher_fun
       	jbclr  	       	T0IF
       	goto   	       	bit7_lower_fun
bit7_lower_fun:
       	bclr   	       	T0IF
       	bclr   	       	LIRCCAL,7
       	bset   	       	LIRCCAL,6
       	call   	       	tmr_cnt_reload
       	goto   	       	inte_return
bit7_higher_fun:
       	bclr   	       	T0IF
       	bset   	       	LIRCCAL,6
       	call   	       	tmr_cnt_reload
       	goto   	       	inte_return

cal_bit6_fun:
       	jbset  	       	T0IF
       	goto   	       	bit6_higher_fun
       	jbclr  	       	T0IF
       	goto   	       	bit6_lower_fun
bit6_lower_fun:
       	bclr   	       	T0IF
       	bclr   	       	LIRCCAL,6
       	bset   	       	LIRCCAL,5
       	call   	       	tmr_cnt_reload
       	goto   	       	inte_return
bit6_higher_fun:
       	bclr   	       	T0IF
       	bset   	       	LIRCCAL,5
       	call   	       	tmr_cnt_reload
       	goto   	       	inte_return

cal_bit5_fun:
       	jbset  	       	T0IF
       	goto   	       	bit5_higher_fun
       	jbclr  	       	T0IF
       	goto   	       	bit5_lower_fun
bit5_lower_fun:
       	bclr   	       	T0IF
       	bclr   	       	LIRCCAL,5
       	bset   	       	LIRCCAL,4
       	call   	       	tmr_cnt_reload
       	goto   	       	inte_return
bit5_higher_fun:
       	bclr   	       	T0IF
       	bset   	       	LIRCCAL,4
       	call   	       	tmr_cnt_reload
       	goto   	       	inte_return

cal_bit4_fun:
       	jbset  	       	T0IF
       	goto   	       	bit4_higher_fun
       	jbclr  	       	T0IF
       	goto   	       	bit4_lower_fun
bit4_lower_fun:
       	bclr   	       	T0IF
       	bclr   	       	LIRCCAL,4
       	bset   	       	LIRCCAL,3
       	call   	       	tmr_cnt_reload
       	goto   	       	inte_return
bit4_higher_fun:
       	bclr   	       	T0IF
       	bset   	       	LIRCCAL,3
       	call   	       	tmr_cnt_reload
       	goto   	       	inte_return

cal_bit3_fun:
       	jbset  	       	T0IF
       	goto   	       	bit3_higher_fun
       	jbclr  	       	T0IF
       	goto   	       	bit3_lower_fun
bit3_lower_fun:
       	bclr   	       	T0IF
       	bclr   	       	LIRCCAL,3
       	bset   	       	LIRCCAL,2
       	call   	       	tmr_cnt_reload
       	goto   	       	inte_return
bit3_higher_fun:
       	bclr   	       	T0IF
       	bset   	       	LIRCCAL,2
       	call   	       	tmr_cnt_reload
       	goto   	       	inte_return

cal_bit2_fun:
       	jbset  	       	T0IF
       	goto   	       	bit2_higher_fun
       	jbclr  	       	T0IF
       	goto   	       	bit2_lower_fun
bit2_lower_fun:
       	bclr   	       	T0IF
       	bclr   	       	LIRCCAL,2
       	bset   	       	LIRCCAL,1
       	call   	       	tmr_cnt_reload
       	goto   	       	inte_return
bit2_higher_fun:
       	bclr   	       	T0IF
       	bset   	       	LIRCCAL,1
       	call   	       	tmr_cnt_reload
       	goto   	       	inte_return

cal_bit1_fun:
       	jbset  	       	T0IF
       	goto   	       	bit1_higher_fun
       	jbclr  	       	T0IF
       	goto   	       	bit1_lower_fun
bit1_lower_fun:
       	bclr   	       	T0IF
       	bclr   	       	LIRCCAL,1
       	bset   	       	LIRCCAL,0
       	call   	       	tmr_cnt_reload
       	goto   	       	inte_return
bit1_higher_fun:
       	bclr   	       	T0IF
       	bset   	       	LIRCCAL,0
       	call   	       	tmr_cnt_reload
       	goto   	       	inte_return


cal_bit0_fun:
       	jbset  	       	T0IF
       	goto   	       	bit0_higher_fun
       	jbclr  	       	T0IF
       	goto   	       	bit0_lower_fun
bit0_lower_fun:
       	bclr   	       	T0IF
       	goto   	       	adjust_finish_fun
bit0_higher_fun:
       	bclr   	       	T0IF
       	goto   	       	adjust_finish_fun


adjust_finish_fun:
       	bset   	       	cal_adjust_finish
       	bclr   	       	T1IE
       	bclr   	       	GIE
       	bclr   	       	T0EN
       	bclr   	       	T1EN
       	goto   	       	inte_return

tmr_cnt_reload:
       	bclr   	       	T0IF
       	bclr   	       	T1IF
       	movai  	       	0xff
       	movra  	       	T0CNT
       	movai  	       	0xf9   	       	;cnt=250
       	movra  	       	T1CNT
       	return
    end
       	