
	.include "KF8S1011.inc"
	
	;;// 汇编编写说明 实体建立后应该选择文件前面添加（可共C和汇编引用)

	;// .global	 (实体)  

	;;// 汇编编写说明 引用调用前应该选择文件前面添加 （引用)

	;// .extern  （引用)

;--------------------------------------------------------
; external declarations
;--------------------------------------------------------
	.extern	_init_mcu
	.extern _TOUCH_DEBUG_SEND_OUT
	.extern	_TOUCH_DEBUG_TRS_DEAL

	.extern	__KF8_LIBi_channel_flag_   ;
	.extern	__KF8_LIBc_channel_
	
	.extern START_INIT
	.extern __R2PCL
	.extern _cinit
	
	

	.extern	__KF8S_Init_Touch_General_
	.extern	__KF8S_Touch_Process_General_
;--------------------------------------------------------
; global declarations
;--------------------------------------------------------
	.global	_delay_us
	.global	_delay_ms
	.global	_display
	.global	_display_temp
;--------------------------------------------------------
	.global _STK03
	.global _STK02
	.global _STK01
	.global _STK00	

_MAIN_RAM_0	.udata  
_STK03	.res	1
_STK02	.res	1
_STK01	.res	1
_STK00	.res	1
;--------------------------------------------------------
; 开机入口准备工作
;--------------------------------------------------------
STARTUP	.code 0x0000
	NOP
	PAGESEL	init_imp
	JMP		init_imp
	
INIT_IMP	.code
init_imp

	PAGESEL		START_INIT	
	CALL		START_INIT
	PAGESEL	$	
	
	PAGESEL		_cinit	
	CALL		_cinit
	PAGESEL	$	
		
	PAGESEL	_main	
	JMP		_main	
	

;--------------------------------------------------------
; 中断函数入口
;--------------------------------------------------------
c_interrupt	.code	0x4
;压栈保护
	MOV		R3,R0
	MOV		R4,PSW
	MOV		R5,PCH
	MOV		R2,R1
	PAGESEL	__sdcc_interrupt
	JMP		__sdcc_interrupt

func.__sdcc_interrupt .code
__sdcc_interrupt
;	if(T1IF)
	JB	EIF1, 0
	JMP	_00081_DS_
;	T1IF=0;
	CLR	EIF1, 0
;
	CWDT 

;	_KF8S_Touch_Process_General_(); //更新按键标志
	PAGESEL	__KF8S_Touch_Process_General_
	CALL	__KF8S_Touch_Process_General_
	PAGESEL	$
;出栈还原	
_00081_DS_
	MOV	R1, R2
	MOV	PCH, R5
	MOV	PSW, R4
	MOV	R0, R3
	IRET	
;--------------------------------------------------------
; 主程序main入口
;--------------------------------------------------------
func._main	.code
_main
;	init_mcu();			//初始化MCU寄存器
	PAGESEL	_init_mcu
	CALL	_init_mcu
	PAGESEL	$

;	CMCTL1 = 0x20;    		//设置触摸分频和基准电压
	MOV	R0,# 0x90
	MOV	CMCTL1, R0


	CWDT 
;	delay_ms(500);					//使能等待电源稳定，采样作为基准

	MOV	R0,# 0x00					;;高位
	BANKSEL	_STK00
	MOV	_STK00, R0
	MOV	R0,# 0xC8					;;低位
	PAGESEL	_delay_ms
	CALL	_delay_ms
	PAGESEL	$

;	delay_ms(500);
;	MOV	R0,# 0xf4
;	BANKSEL	STK00
;	MOV	STK00, R0
;	MOV	R0,# 0x01
;	PAGESEL	_delay_ms
;	CALL	_delay_ms
;	PAGESEL	$
;	Sent_Vaule_To_UART(0xAA);
;	MOV	R0,# 0xaa
;	MOV	TXSDR, R0
;_Sent_END_DS_1
;	JB	TSCTL, 1
;	JMP	_Sent_END_DS_1
;;	Sent_Vaule_To_UART(0xBB);
;	MOV	R0,# 0xbb
;	MOV	TXSDR, R0
;_Sent_END_DS_2
;	JB	TSCTL, 1
;	JMP	_Sent_END_DS_2


;	delay_ms(5);
;	MOV	R0,# 0x05
;	BANKSEL	STK00
;	MOV	STK00, R0
;	MOV	R0,# 0x00
;	PAGESEL	_delay_ms
;	CALL	_delay_ms
;	PAGESEL	$

;	_KF8S_Init_Touch_General_;		//调用电容触摸库函数的初始化
	PAGESEL	_TOUCH_DEBUG_TRS_DEAL
	CALL	_TOUCH_DEBUG_TRS_DEAL
	PAGESEL	$


	PAGESEL	__KF8S_Init_Touch_General_
	CALL	__KF8S_Init_Touch_General_
	PAGESEL	$
	CWDT 

_Main_DS_003
	CWDT 
;	display();					//显示LED灯
	PAGESEL	_display
	CALL	_display
	PAGESEL	$
	
;	delay_ms(1);
	MOV	R0,# 0x00					;;高位
	BANKSEL	_STK00
	MOV	_STK00, R0
	MOV	R0,# 0x01					;;低位
	PAGESEL	_delay_ms
	CALL	_delay_ms
	PAGESEL	$
;	op : GOTO
	JMP	_Main_DS_003

	CRET	
; exit point of _main

;--------------------------------------------------------
; 微秒级别延时函数
;--------------------------------------------------------
func._delay_us	.code

_delay_us	 ;不需要切区
; void delay_us(uint us_data)  use R1,R0
;;	while(us_data--);
_delay_us_loop1
 	DEC  R0
 	JB   PSW,Z   
 	JMP _delay_us_loop1
 	
 	XOR R1,#0x00
 	JNB PSW,2
 	JMP _delay_us_loop_end
 	DEC   R1
 	MOV  R0,#0xFF
 	JMP _delay_us_loop1 	
_delay_us_loop_end 
	CRET	
; exit point of _delay_us

;--------------------------------------------------------
; 毫秒级别延时函数
;--------------------------------------------------------
func._delay_ms	.code

_delay_ms	;Function start STK00赋值使已切区，也不需要再次切
;; void delay_ms(uint ms_data)  use STK00,R0,最小值1
	INC R0
_delay_ms_loop1   ;while(ms_data--)

 	;{
_delay_ms_Deal	  ;j=200
    MOV R1,#0xC8
    CWDT
 	;while(j--)
_delay_ms_sub
	CWDT
    DEC  R1
	JB   PSW,Z
 	JMP  _delay_ms_sub
 	;}

 	DEC R0
 	JB  PSW,Z 	
 	JMP _delay_ms_loop1   ;低8位不借位时继续
 	
 	
 	MOV R1,#0x00
 	XOR _STK00,R1
 	JNB PSW,2
 	JMP _delay_ms_loop_end ;高位如果是零，无位可借结束
 	DEC _STK00
 	MOV  R0,#0xFF
 	JMP _delay_ms_loop1 	
 	
_delay_ms_loop_end  	
 	
	CRET	
; exit point of _delay_ms
;--------------------------------------------------------
; 占用代码的临时
;--------------------------------------------------------
func._display_temp	.code

_display_temp	;Function start
; if(_KF8_LIBi_channel_flag_&0x0001)		//通道0
	BANKSEL	__KF8_LIBi_channel_flag_
	JB	__KF8_LIBi_channel_flag_, 0
	JMP	__display_temp_DS_0001
;	LED1=!LED1;   P37
	JB	P3, 7
	JMP	__display_temp_DS_0002
	CLR	P3, 7
	JMP	__display_temp_DS_0001
__display_temp_DS_0002
	SET	P3, 7
;S2
__display_temp_DS_0001
;	if(_KF8_LIBi_channel_flag_&0x0002)		//通道2
	BANKSEL	__KF8_LIBi_channel_flag_
	JB	__KF8_LIBi_channel_flag_, 1
	JMP	__display_temp_DS_0003
;	LED2=!LED2;  P36
	JB	P3, 6
	JMP	__display_temp_DS_0004
	CLR	P3, 6
	JMP	__display_temp_DS_0003
__display_temp_DS_0004
	SET	P3, 6
;S3
__display_temp_DS_0003
;if(_KF8_LIBi_channel_flag_&0x0004)		//通道4
	BANKSEL	__KF8_LIBi_channel_flag_
	JB	__KF8_LIBi_channel_flag_, 2
	JMP	__display_temp_DS_0005
;	LED3=!LED3;  P35
	JB	P3, 5
	JMP	__display_temp_DS_0006
	CLR	P3, 5
	JMP	__display_temp_DS_0005
__display_temp_DS_0006
	SET	P3, 5
;S4
__display_temp_DS_0005
;	if(_KF8_LIBi_channel_flag_&0x0008)		//通道3
	BANKSEL	__KF8_LIBi_channel_flag_
	JB	__KF8_LIBi_channel_flag_, 3
	JMP	__display_temp_DS_0007
;	LED4=!LED4; P34
	JB	P3, 4
	JMP	__display_temp_DS_0008
	CLR	P3, 4
	JMP	__display_temp_DS_0007
__display_temp_DS_0008
	SET	P3, 4
;S5
__display_temp_DS_0007
;	if(_KF8_LIBi_channel_flag_&0x0010)		//通道4
	BANKSEL	__KF8_LIBi_channel_flag_
	JB	__KF8_LIBi_channel_flag_, 4
	JMP	__display_temp_DS_0009
;	LED5=!LED5; P33
	JB	P3, 3
	JMP	__display_temp_DS_0010
	CLR	P3, 3
	JMP	__display_temp_DS_0009
__display_temp_DS_0010
	SET	P3, 3
;S6
__display_temp_DS_0009
;	if(_KF8_LIBi_channel_flag_&0x0020)		//通道5
	BANKSEL	__KF8_LIBi_channel_flag_
	JB	__KF8_LIBi_channel_flag_, 5
	JMP	__display_temp_DS_0011
;	LED6=!LED6;  P30
	JB	P3, 0
	JMP	__display_temp_DS_0012
	CLR	P3, 0
	JMP	__display_temp_DS_0011
__display_temp_DS_0012
	SET	P3, 0
;end	
__display_temp_DS_0011
	CRET	
; exit point of _display_temp

;--------------------------------------------------------
; 按键结果输出指示函数
;--------------------------------------------------------
func._display	.code

_display	;Function start
;if(_KF8_LIBi_channel_flag_&0x0001)		//通道0
	BANKSEL	__KF8_LIBi_channel_flag_
	JB	__KF8_LIBi_channel_flag_, 0
	JMP	__display_DS_0002
;	LED1=1;
	SET	P3, 7
	JMP	__display_DS_0003
__display_DS_0002
;	LED1=0;
	CLR	P3, 7
__display_DS_0003
;	if(_KF8_LIBi_channel_flag_&0x0002)		//通道2
	BANKSEL	__KF8_LIBi_channel_flag_
	JB	__KF8_LIBi_channel_flag_, 1
	JMP	__display_DS_0005
;	LED2=1;
	SET	P3, 6
	JMP	__display_DS_0006
__display_DS_0005
;	LED2=0;
	CLR	P3, 6
__display_DS_0006
;	if(_KF8_LIBi_channel_flag_&0x0004)		//通道4
	BANKSEL	__KF8_LIBi_channel_flag_
	JB	__KF8_LIBi_channel_flag_, 2
	JMP	__display_DS_0007
;	LED3=1;
	SET	P3, 5
	JMP	__display_DS_0008
__display_DS_0007
;	LED3=0;
	CLR	P3, 5
__display_DS_0008
;	if(_KF8_LIBi_channel_flag_&0x0008)		//通道3
	BANKSEL	__KF8_LIBi_channel_flag_
	JB	__KF8_LIBi_channel_flag_, 3
	JMP	__display_DS_0009
;	LED4=1;
	SET	P3, 4
	JMP	__display_DS_0010
__display_DS_0009
;	LED4=0;
	CLR	P3, 4
__display_DS_0010
;	if(_KF8_LIBi_channel_flag_&0x0010)		//通道4
	BANKSEL	__KF8_LIBi_channel_flag_
	JB	__KF8_LIBi_channel_flag_, 4
	JMP	__display_DS_0011
;	LED5=1;
	SET	P3, 3
	JMP	__display_DS_0012
__display_DS_0011
;	LED5=0;
	CLR	P3, 3
__display_DS_0012
;	if(_KF8_LIBi_channel_flag_&0x0020)		//通道5
	BANKSEL	__KF8_LIBi_channel_flag_
	JB	__KF8_LIBi_channel_flag_, 5
	JMP	__display_DS_0013
;	LED6=1;
	SET	P3, 0
	JMP	__display_DS_0014
__display_DS_0013
;	LED6=0;
	CLR	P3, 0
__display_DS_0014
	CRET	
; exit point of _display


	.end
