;//*****************************************************   
;//Copyright (c) 2025, 苏州华芯微电子股份有限公司
;//All rights reserved                                 
;//
;//文件名称: HS16F1886 
;//摘    要: HS16F1886-8模式灯，带24小时循环定时
;//  	    TP0周期定时溢出中断做8模式基准，TC0周期定时溢出中断做24小时循环定时基准 
;//
;//当前版本: 1.0                                      
;//作    者: 华芯微MCU组
;//完成日期: 2025.08.28
;//*****************************************************   
;files include
#include "HS16P1886.inc"
;***********************************************************

;用户IO定义
#define LED		PORTB,0

#define LED_W		PORTB,1 
#define	LED_Y		PORTB,2 

#define KEY		PORTB,3

#define OSCO		PORTB,4
#define OSCI		PORTB,5
;***********************************************************
;user general reg define
TIME_Y          EQU     040H
TIME_W          EQU     041H
TIME_SUM        EQU     042H
TIME8           EQU     043H
TIMES_8         EQU     044H
TIME6_H     	EQU     045H             
KEY_CNT	   	EQU     046H
KEY_RELEASE	EQU     047H
TEMP6_L		EQU     048H
TIME4_H         EQU     049H             
TIME4_L		EQU	04AH
TEMP4_H         EQU     04BH
TEMP4_L		EQU	04CH
TIMES_4		EQU	04DH
TEMPS_4		EQU	04EH
CNT_4		EQU	04FH

WORK_MODE       EQU     050H             
TIME6_L		EQU	051H
TIMES_6		EQU	052H
DELAYMS_CNT     EQU     053H             
TEMPS_6         EQU     054H             
TEMP6_H         EQU     055H             
CNT_6         	EQU     056H              


DELAY_US	EQU     058H		
DELAY_MS_CNT	EQU     059H		
DELAY_OUTSIDE	EQU     05AH		
DELAY_INSIDE    EQU     05BH

FLASH_MODE6	EQU	05CH
TEMP5		EQU	05DH
CNT_5		EQU	05EH
TIME5		EQU	05FH

TIME7		EQU	060H
TEMP7		EQU	061H
CNT_7		EQU	062H
TIME3		EQU	063H 
TEMP3		EQU	064H
CNT_3		EQU	065H
AUTO_20S_L	EQU	066H
AUTO_20S_M	EQU	067H
AUTO_20S_H	EQU	068H
WORK_MODE_TMP	EQU	069H
AUTO_TMP_L      EQU     06AH
AUTO_TMP_H	EQU     06BH
AUTO_TMP_M      EQU     06CH

FLAG            EQU     06DH
FLASH_CNT_H	EQU     06EH


FLAG1		EQU	070H
SET_CONFIG	EQU	071H
TIME_BASEE	EQU	072H
TEMP_Y		EQU	073H
TEMP_W		EQU	074H

TIME_L		EQU	075H
TIME_M		EQU	076H
TIME_H		EQU	077H

FLAG2		EQU	078H
KEY_CNT_H	EQU	079H

OSC_CNT		EQU	07AH	
OSC_CNT_H 	EQU	07BH

FLASH_CNT	EQU	07CH	
FLASH_CNT_L 	EQU	07DH

STATUS_BUF	EQU	07EH;中断保护寄存器必须放 0x70-0x7F不要动！！！
ACC_BUF		EQU	07FH;中断保护寄存器必须放 0x70-0x7F不要动！！！
;--------------------------------
#define F_SMOOTH_UP     FLAG,0
#define F_SMOOTH_UP_Y   FLAG,1
#define F_SMOOTH_UP_W 	FLAG,2                          
#define F_AUTO      	FLAG,3 
#define	F_ONCE		FLAG,4
#define F_KEY_ENB	FLAG,5  
#define F_KEY_SCAN	FLAG,6                                               
#define	F_FLSAH_8	FLAG,7
;---------------------------------
 
#define F_LED_W     	FLAG1,1
#define F_LED_Y     	FLAG1,2
#define F_SET_TIME     	FLAG1,3
#define F_SHORT_KEY     FLAG1,4
#define F_LONG_KEY     	FLAG1,5
#define F_ON      	FLAG1,6
#define F_TIMING_SEL	FLAG1,7	

 
#define F_FLASH		FLAG2,1
;***********************************************************
;/函数入口地址
	ORG	00H
	NOP
	NOP
	GOTO	MAIN_SETUP
	
;/中断入口地址04H
	ORG	04H
	GOTO	INT_PRO
	
WORK_MODE_TAB:
	BTRSC	F_AUTO
	GOTO	$+3
	MOVR    WORK_MODE,A
	GOTO	$+2
	MOVR    WORK_MODE_TMP,A
        ANDIA   00FH
        ADDAR   PCL,R
        GOTO    INT_END			;0
        GOTO	POWER_50PER		;1
        GOTO    INT_END      		;2
        GOTO    INT_END    		;3
        GOTO    FLASH_MODE1212      	;4	
        GOTO    INT_END			;5
        GOTO    FLASH_MODE12121122      ;6
	GOTO    INT_END			;7
	GOTO    FLASH_MODE11112222	;8	
	
TAB_4_TIME_L:
	ADDAR   PCL,R
        RETIA   022H            ;0   
        RETIA   082H            ;1   
        RETIA   03BH            ;2    
        RETIA   0BDH            ;3  

TAB_4_TIME_H:
	ADDAR   PCL,R
        RETIA   003H            ;0   
        RETIA   002H            ;1   
        RETIA   001H            ;2    
        RETIA   000H            ;3  

TAB_4_TIMES:
	ADDAR   PCL,R
        RETIA   12            ;0   
        RETIA   17            ;1   
        RETIA   31            ;2    
        RETIA   65            ;3
	
TAB_5_TIME:
	ADDAR   PCL,R
        RETIA   3            ;0   
        RETIA   6            ;1   
        RETIA   3            ;2    
        RETIA   4            ;3
	RETIA   10           ;4
	RETIA   4            ;5
	RETIA   3            ;6
	RETIA   7            ;7
	RETIA   3            ;8   
        RETIA   2            ;9   
        RETIA   6            ;10    
        RETIA   2            ;11
	RETIA   2            ;12
	RETIA   4            ;13
	RETIA   2            ;14
	RETIA   1            ;15
	RETIA   4            ;16
	RETIA   1            ;17
	RETIA   1            ;18
	RETIA   2            ;19
	RETIA   1            ;20
	RETIA   1            ;21
	RETIA   2            ;22
	RETIA   1            ;23

TAB_6_TIME_L:
	ADDAR   PCL,R
        RETIA   050H            ;0   
        RETIA   050H            ;1   
        RETIA   09EH            ;2    
        RETIA   09EH            ;3  

TAB_6_TIME_H:
	ADDAR   PCL,R
        RETIA   000H            ;0   
        RETIA   000H            ;1   
        RETIA   000H            ;2 
	RETIA   000H            ;3
TAB_6_TIMES:
	ADDAR   PCL,R
        RETIA   7             ;0   
        RETIA   5             ;1
	RETIA   2             ;2
        RETIA   10            ;3
	

TAB_6_FLASH_MODE:
	ADDAR   PCL,R
        RETIA   004H          ;0   
        RETIA   002H          ;1
	RETIA   002H          ;2
        RETIA   006H          ;3

TAB_7_TIME:
      	ADDAR   PCL,R
        RETIA   5             ;0   
        RETIA   14            ;1   
        RETIA   5             ;2    
        RETIA   8             ;3
	RETIA   18            ;4
	RETIA   8             ;5
	RETIA   6             ;6
	RETIA   16            ;7
	RETIA   6             ;8   
        RETIA   4             ;9   
        RETIA   12            ;10    
        RETIA   4             ;11
	RETIA   4             ;12
	RETIA   8             ;13
	RETIA   4             ;14
	RETIA   2             ;15
	RETIA   6             ;16  
        RETIA   2             ;17   
        RETIA   2             ;18   
        RETIA   3             ;19
	RETIA   2             ;20
	RETIA   2             ;21
	RETIA   3             ;22
	RETIA   2             ;23
	
TAB_AUTO_TIME_L:
	ADDAR   PCL,R
        RETIA   000H            ;0   
        RETIA   042H            ;1   
        RETIA   04BH            ;2     
        RETIA   000H            ;3
	RETIA   038H            ;4
	RETIA   018H            ;5
	RETIA   012H            ;6
	RETIA   0D6H            ;7
	RETIA   009H            ;8

TAB_AUTO_TIME_M:
	ADDAR   PCL,R
        RETIA   000H            ;0   
        RETIA   00FH            ;1   
        RETIA   04CH            ;2    
        RETIA   000H            ;3	
	RETIA   0A0H            ;4
	RETIA   053H            ;5
	RETIA   07AH            ;6
	RETIA   043H            ;7
	RETIA   03DH            ;8

TAB_AUTO_TIME_H:
	ADDAR   PCL,R
        RETIA   000H            ;0   
        RETIA   000H            ;1   
        RETIA   000H            ;2    
        RETIA   000H            ;3	
	RETIA   000H            ;4
	RETIA   002H            ;5
	RETIA   000H            ;6
	RETIA   002H            ;7
	RETIA   000H            ;8	
		
INT_PRO:	
	MOVAR	ACC_BUF		;保存ACC
	SWAPR	STATUS,0
	MOVAR	STATUS_BUF	;保存STATUS
	
	BCR	STATUS,5	;切换到BANK0
	BCR	STATUS,6
	
	BTRSS	FTP0IF
	GOTO	TP0_INT_END
	BCR	FTP0IF
	
	; MOVIA	0X80
	; XORAR	PORTB,R	
	
	BCR	STATUS,5	;切换到BANK2
	BSR	STATUS,6
	MOVIA	217
	MOVAR	PWM0TL		;定时器TP0计数值
	BCR	STATUS,5	;切换到BANK0
	BCR	STATUS,6
	
TIME_BASE:	
	INCR	TIME_BASEE,R 
	MOVIA	22
	SUBAR	TIME_BASEE,A
	BTRSS	C 
	GOTO	TIME_BASE_END
	CLRR	TIME_BASEE
	BSR	F_KEY_SCAN	
TIME_BASE_END:	
;-------------------------------------------------
WORKMODE_TAB:
	BTRSS	F_ON 
	GOTO	CLOSE_ALL

	BTRSC	F_AUTO
	GOTO	$+3
	MOVR    WORK_MODE,A
	GOTO	$+2
	MOVR    WORK_MODE_TMP,A
        ANDIA   00FH
        ADDAR   PCL,R
        GOTO    T0_JUDGE_END		;0
        GOTO	HALF_MODE		;1
        GOTO    MODE_SMOOTH22      	;2
        GOTO    MODE_SMOOTH33    	;3
        GOTO    FLASH_MODE       	;4	
        GOTO    MODE_SMOOTHSLOW		;5
        GOTO    FLASH_MODE      	;6
	GOTO    MODE_SMOOTHTWO		;7
	GOTO    FLASH_MODE		;8
;------------------------------------------------
HALF_MODE:
	MOVR    TIME_SUM,A        
        BTRSS   Z        
        GOTO    ON_TIME_JUDGE
	
	MOVIA   112
        MOVAR   TIME_SUM
        MOVIA   56
        MOVAR   TIME_Y
	MOVAR   TIME_W
	
ON_TIME_JUDGE: 
	DECR	TIME_SUM,R
ON_TIME_Y:        
        MOVR    TIME_Y,A        
        BTRSS   Z        
        GOTO    $+3                
        BSR     LED_Y   
        GOTO    ON_TIME_W
        BCR     LED_Y
        DECR    TIME_Y,R

ON_TIME_W:
        MOVR    TIME_W,A        
        BTRSS   Z       
        GOTO    $+3                 
        BCR     LED_W        
        GOTO    ON_TIME_JUDGE_END              
        BSR     LED_W
        DECR    TIME_W,R        
ON_TIME_JUDGE_END:
	GOTO	T0_JUDGE_END
;------------------------------------------	
FLASH_MODE:
	BTRSS	F_LED_W
	GOTO	CLOSE_W
	
	MOVR    TIME_SUM,A        
        BTRSS   Z        
        GOTO    ON_TIME_JUDGE_W
	
	BTRSC	F_AUTO
	GOTO	$+6
	
	MOVIA	4
	XORAR	WORK_MODE,A 
	BTRSC	Z 
	GOTO	GET_4
	GOTO	$+5
	
	MOVIA	4
	XORAR	WORK_MODE_TMP,A 
	BTRSC	Z 
	GOTO	GET_4
		
	MOVIA   112
        MOVAR   TIME_SUM
        MOVIA   56
	MOVAR   TIME_W
	GOTO	ON_TIME_JUDGE_W
	
GET_4:	
	MOVIA   124 
        MOVAR   TIME_SUM
        MOVIA   112 
	MOVAR   TIME_W
	
ON_TIME_JUDGE_W: 
	DECR	TIME_SUM,R
ONTIME_W:
        MOVR    TIME_W,A        
        BTRSS   Z       
        GOTO    $+3                 
        BCR     LED_W        
        GOTO    ON_TIME_JUDGE_W_END              
        BSR     LED_W
        DECR    TIME_W,R        
ON_TIME_JUDGE_W_END:
	GOTO	LEDY_JUDGE 
	
CLOSE_W:
	BCR	LED_W
	
LEDY_JUDGE:
	BTRSS	F_LED_Y
	GOTO	CLOSE_Y
	
	MOVR    TIME_SUM,A        
        BTRSS   Z        
        GOTO    ON_TIME_JUDGE_Y
	
	BTRSC	F_AUTO
	GOTO	$+6
	
	MOVIA	4
	XORAR	WORK_MODE,A 
	BTRSC	Z 
	GOTO	GET_44
	GOTO	$+5
	
	MOVIA	4
	XORAR	WORK_MODE_TMP,A 
	BTRSC	Z 
	GOTO	GET_44
		
	MOVIA   112
        MOVAR   TIME_SUM
        MOVIA   56
	MOVAR   TIME_Y
	GOTO	ON_TIME_JUDGE_Y
	
GET_44:	
	MOVIA   124 
        MOVAR   TIME_SUM
        MOVIA   112 
	MOVAR   TIME_Y

ON_TIME_JUDGE_Y:
	DECR	TIME_SUM,R	
ONTIME_Y:        
        MOVR    TIME_Y,A        
        BTRSS   Z        
        GOTO    $+3                
        BCR     LED_Y   
        GOTO    ON_TIME_JUDGE_Y_END 
        BSR     LED_Y
        DECR    TIME_Y,R	
	
ON_TIME_JUDGE_Y_END:	
	GOTO	T0_JUDGE_END
	
CLOSE_Y:	
	BCR	LED_Y
	GOTO	T0_JUDGE_END	
;--------------------------------------------------------
MODE_SMOOTHSLOW:
	BTRSC	F_ONCE	
	GOTO	MODE_SMOOTHSLOW_LOOP
MODE_SMOOTHSLOW_INT:	
	BSR	F_ONCE		
	BSR	F_SMOOTH_UP
	BCR	F_SMOOTH_UP_Y
	CLRR	CNT_5
	MOVIA	3
	MOVAR	TEMP5
	CLRR	TIME5
	CLRR	TIME_Y
	CLRR	TIME_W
	MOVIA	176
	MOVAR	TIME_SUM
	MOVIA	0
	MOVAR	TEMP_Y
	MOVIA	1
	MOVAR	TEMP_W
MODE_SMOOTHSLOW_LOOP:
	MOVR    TIME_SUM,A        
        BTRSS   Z        
        GOTO    ONTIME_JUDGE

	INCR	TIME5,R 
	MOVR	TEMP5,A 
	SUBAR	TIME5,A 
	BTRSS	C 
	GOTO	GET_VAL 
	CLRR	TIME5
	
	BTRSS	F_SMOOTH_UP_Y
	GOTO	SMOOTH_W_JUDGE
	
	BTRSS	F_SMOOTH_UP
	GOTO	SMOOTH_DOWN_Y
SMOOTH_UP_Y:
	INCR	TEMP_Y,R 
	MOVIA	116 
	XORAR	TEMP_Y,A 
	BTRSC	Z
	GOTO	$+6 

	MOVIA	176 
	SUBAR	TEMP_Y,A 
	BTRSS	C
	GOTO	GET_VAL 
	BCR	F_SMOOTH_UP
	GOTO	GET_VAL
		
	INCR	CNT_5,R
	MOVR    CNT_5,A
        ANDIA   0X1F        
        CALL    TAB_5_TIME 
	MOVAR	TEMP5
	GOTO	GET_VAL
	
SMOOTH_DOWN_Y:	
	DECR 	TEMP_Y,R 
	
	MOVIA	116
	XORAR	TEMP_Y,A 
	BTRSC	Z
	GOTO	$+6
	
	MOVR	TEMP_Y,A 
	BTRSS	Z
	GOTO	GET_VAL
	BSR	F_SMOOTH_UP
	BCR	F_SMOOTH_UP_Y
		
	INCR	CNT_5,R
	MOVR    CNT_5,A
        ANDIA   0X1F       
        CALL    TAB_5_TIME 
	MOVAR	TEMP5
	GOTO	GET_VAL
	
SMOOTH_W_JUDGE:	
	BTRSS	F_SMOOTH_UP
	GOTO	SMOOTH_DOWN_W
SMOOTH_UP_W:
	INCR	TEMP_W,R 
	MOVIA	116 
	XORAR	TEMP_W,A 
	BTRSC	Z
	GOTO	$+6 

	MOVIA	176 
	SUBAR	TEMP_W,A 
	BTRSS	C
	GOTO	GET_VAL 
	BCR	F_SMOOTH_UP
	GOTO	GET_VAL
		
	INCR	CNT_5,R
	MOVR    CNT_5,A
        ANDIA   0X1F        
        CALL    TAB_5_TIME 
	MOVAR	TEMP5
	GOTO	GET_VAL
	
SMOOTH_DOWN_W:	
	DECR 	TEMP_W,R 
	
	MOVIA	116
	XORAR	TEMP_W,A 
	BTRSC	Z
	GOTO	$+6
	
	MOVR	TEMP_W,A 
	BTRSS	Z
	GOTO	GET_VAL
	BSR	F_SMOOTH_UP
	BSR	F_SMOOTH_UP_Y
		
	INCR	CNT_5,R
	MOVIA	24
	SUBAR	CNT_5,A 
	BTRSS	C 
	GOTO	$+2 
	CLRR	CNT_5
	
	MOVR    CNT_5,A
        ANDIA   0X1F        
        CALL    TAB_5_TIME 
	MOVAR	TEMP5
	GOTO	GET_VAL	
GET_VAL:        
        MOVIA   176
        MOVAR   TIME_SUM
        MOVR    TEMP_W,A
        MOVAR   TIME_W
        MOVR    TEMP_Y,A
        MOVAR   TIME_Y

ONTIME_JUDGE:
	DECR    TIME_SUM,R 
ON_TIMEY:
        MOVR    TIME_Y,A        
        BTRSS   Z       
        GOTO    $+3                 
        BCR     LED_Y        
        GOTO    ON_TIMEW              
        BSR     LED_Y
        DECR    TIME_Y,R
        
ON_TIMEW:
        MOVR    TIME_W,A        
        BTRSS   Z       
        GOTO    $+3                 
        BCR     LED_W        
        GOTO    ONTIME_JUDGE_END              
        BSR     LED_W
        DECR    TIME_W,R        
ONTIME_JUDGE_END:
	GOTO	T0_JUDGE_END	
;---------------------------------------------------	
MODE_SMOOTH22:	
MODE_SMOOTH33:
	BTRSC	F_ONCE
	GOTO	MODE_SMOOTH3_LOOP
MODE_SMOOTH3_INT:
	BSR	F_ONCE 	 
	BCR	F_SMOOTH_UP_Y
	CLRR	TIME3
	CLRR	TIME_Y
	CLRR	TIME_W
	CLRR	CNT_3
	MOVIA	3
	MOVAR	TEMP3	
	MOVIA	49
	MOVAR	TEMP_Y
MODE_SMOOTH3_LOOP:
	MOVR    TIME_SUM,A        
        BTRSS   Z        
        GOTO    ON_TIMEJUDGE

	INCR	TIME3,R 
	MOVR	TEMP3,A 
	SUBAR	TIME3,A 
	BTRSS	C 
	GOTO	GETVAL 
	CLRR	TIME3

	BTRSS	F_SMOOTH_UP_Y
	GOTO	MODE_SMOOTH3_DOWN_Y
MODE_SMOOTH3_UP_Y:
	INCR	TEMP_Y,R
	MOVIA	50
	SUBAR	TEMP_Y,A 
	BTRSS	C 
	GOTO	MODE_SMOOTH3_W_JUDGE
	BCR	F_SMOOTH_UP_Y
	
	INCR	CNT_3,R	
	MOVIA	96
	SUBAR	CNT_3,A 
	BTRSS	C 
	GOTO	MODE_SMOOTH3_W_JUDGE
	CLRR	CNT_3	
	MOVIA	3
	MOVAR	TEMP3
	
	BTRSS	F_AUTO
	GOTO	MODE_SMOOTH3_W_JUDGE 
	MOVIA	4
	MOVAR	WORK_MODE_TMP
	CALL	GET_AUTO_TIME
	BCR	F_ONCE
	CLRR	AUTO_20S_L
	CLRR	AUTO_20S_M
	CLRR	AUTO_20S_H	
	GOTO	T0_JUDGE_END 
	 	 
MODE_SMOOTH3_DOWN_Y:       
        DECRSZ  TEMP_Y,R
        GOTO	MODE_SMOOTH3_W_JUDGE
	BSR	F_SMOOTH_UP_Y

	INCR	CNT_3,R
	MOVIA	31
	SUBAR	CNT_3,A 
	BTRSS	C 
	GOTO	MODE_SMOOTH3_W_JUDGE
	MOVIA	1
	MOVAR	TEMP3
	
MODE_SMOOTH3_W_JUDGE:
GETVAL:
	MOVIA   50
        MOVAR   TIME_SUM
        MOVR    TEMP_Y,A
        MOVAR   TIME_Y
	MOVAR   TIME_W

ON_TIMEJUDGE:
	DECR    TIME_SUM,R 
ONTIMEY:
        MOVR    TIME_Y,A        
        BTRSS   Z       
        GOTO    $+4                
        BCR     LED_Y
	BSR	LED_W
        GOTO    ONTIMEW              
        BSR     LED_Y
	BCR	LED_W
        DECR    TIME_Y,R       
ONTIMEW:        
ON_TIMEJUDGE_END:
	GOTO	T0_JUDGE_END
;---------------------------------------	
MODE_SMOOTHTWO:	
	BTRSC	F_ONCE
	GOTO	MODE_SMOOTH_LOOP
MODE_SMOOTH_INT:
	BSR	F_ONCE 	 
	BCR	F_SMOOTH_UP_Y
	BSR	F_SMOOTH_UP_W
	CLRR	TIME3
	CLRR	TIME_Y
	CLRR	TIME_W
	CLRR	CNT_3
	MOVIA	5
	MOVAR	TEMP3
	MOVIA	176
	MOVAR	TEMP_Y
	MOVIA	0
	MOVAR	TEMP_W
MODE_SMOOTH_LOOP:
	MOVR    TIME_SUM,A        
        BTRSS   Z        
        GOTO    ONTIMEJUDGE

	INCR	TIME3,R 
	MOVR	TEMP3,A 
	SUBAR	TIME3,A 
	BTRSS	C 
	GOTO	GGETVAL 
	CLRR	TIME3

	BTRSS	F_SMOOTH_UP_Y
	GOTO	MODE_SMOOTH_DOWN_Y
MODE_SMOOTH_UP_Y:
	INCR	TEMP_Y,R
	MOVIA	176
	SUBAR	TEMP_Y,A 
	BTRSS	C 
	GOTO	MODE_SMOOTH_W_JUDGE
	BCR	F_SMOOTH_UP_Y
	GOTO	MODE_SMOOTH_W_JUDGE 
	
MODE_SMOOTH_DOWN_Y:       
        DECR    TEMP_Y,R
	MOVIA	92
	SUBAR	TEMP_Y,A 
	BTRSC	C 	
        GOTO	MODE_SMOOTH_W_JUDGE
	BSR	F_SMOOTH_UP_Y
	
MODE_SMOOTH_W_JUDGE:
	BTRSS	F_SMOOTH_UP_W
	GOTO	MODE_SMOOTH_DOWN_W
MODE_SMOOTH_UP_W:
	INCR	TEMP_W,R
	
	MOVIA	42
	XORAR	TEMP_W,A 
	BTRSS	Z
	GOTO	$+2
	INCR	CNT_3,R 
		
	MOVIA	85
	SUBAR	TEMP_W,A 
	BTRSS	C 
	GOTO	GGETVAL 
	BCR	F_SMOOTH_UP_W
	GOTO	GGETVAL 
MODE_SMOOTH_DOWN_W:       
        DECR    TEMP_W,R
	
	MOVIA	41
	XORAR	TEMP_W,A 
	BTRSS	Z
	GOTO	$+2
	INCR	CNT_3,R
	
	MOVR	TEMP_W,A 
	BTRSS	Z
        GOTO	GGETVAL 
	BSR	F_SMOOTH_UP_W
	INCR	CNT_3,R
	MOVIA	24 
	SUBAR	CNT_3,A 
	BTRSS	C 
	GOTO	$+2
	CLRR	CNT_3

GGETVAL:	 
	MOVR    CNT_3,A
        ANDIA   0X1F        
        CALL    TAB_7_TIME 
	MOVAR	TEMP3

	MOVIA   176
        MOVAR   TIME_SUM
        MOVR    TEMP_W,A
        MOVAR   TIME_W
        MOVR    TEMP_Y,A
        MOVAR   TIME_Y

ONTIMEJUDGE:
	DECR    TIME_SUM,R 
ONTIMEYY:
        MOVR    TIME_Y,A        
        BTRSS   Z       
        GOTO    $+3                
        BSR     LED_Y
        GOTO    ONTIMEWW              
        BCR     LED_Y
        DECR    TIME_Y,R
        
ONTIMEWW:
        MOVR    TIME_W,A        
        BTRSS   Z       
        GOTO    $+3                 
        BCR     LED_W        
        GOTO    ONTIMEJUDGE_END              
        BSR     LED_W
        DECR    TIME_W,R        
ONTIMEJUDGE_END:
	GOTO	T0_JUDGE_END
;-------------------------------------
CLOSE_ALL:
	BCR	LED_W
	BCR	LED_Y 	
T0_JUDGE_END:
;-------------------------------------------------		
TP0_INT_END:
;====================================================
TC0_JUDGE:
	BTRSS	TC0IF
	GOTO	TC0_JUDGE_END
	BCR	TC0IF
	
	INCR	TIME_L,R
	BTRSC	Z
	INCR	TIME_M,R
	BTRSC	Z
	INCR	TIME_H,R
	
	; MOVIA	0X01 
	; XORAR	PORTB,R
	
	
TC0_JUDGE_END: 
	GOTO	INT_BACK
	
INT_BACK:	
	SWAPR	STATUS_BUF,0
	MOVAR	STATUS		;恢复STATUS
	SWAPR	ACC_BUF,1
	SWAPR	ACC_BUF,0	;恢复ACC
	RETFIE
;//中断代码结束
;==========================================================================
;主程序开始
MAIN_SETUP:
	CALL	SYS_CPIO_INIT	;系统寄存器和GPIO配置
	CALL	CLR_ALL_RAM  	;清所有ram
	CALL	TC0_XT_Init
	CALL	tp0_tim_init	;tc0初始化
	BSR	GIE		;开总中断
	
;DEBUG	
	MOVIA	2
	MOVAR	WORK_MODE
	BSR	F_ON
	BSR	F_AUTO
	MOVR	WORK_MODE,A 
	MOVAR	WORK_MODE_TMP
	CALL	GET_AUTO_TIME	
;DEBUG

;上电默认开启定时	
	BSR	F_SET_TIME
	BSR	LED 
	BSR	F_TIMING_SEL
;-----------------------------------------
	
MAIN:
	CLRWDT	  		
	
TIME_BASE_JUDGE:
	BTRSS 	F_KEY_SCAN
	GOTO	TIME_BASE_JUDGE_END
	BCR	F_KEY_SCAN
;-----------------------------------------
;按键扫描		
SIGN_IN_JUDGE:	
	BTRSS	KEY
	GOTO	SIGN_ON
;无按键
SIGN_OFF:
	CLRR	KEY_CNT	
	CLRR	KEY_CNT_H 
	
	INCR	KEY_RELEASE,R
	MOVIA	20
	SUBAR	KEY_RELEASE,A 
	BTRSS	C 
	GOTO	SIGN_IN_JUDGE_END
	CLRR	KEY_RELEASE
 	
	BCR	F_LONG_KEY
	BCR	F_KEY_ENB
		
SHORT_KEY_RELEASE_JUDGE:	
	BTRSS	F_SHORT_KEY
	GOTO	SHORT_KEY_RELEASE_JUDGE_END
	BCR	F_SHORT_KEY
	
	BTRSC	F_ON
	GOTO	WORK_MODE_ADD
	BSR	F_ON 
	CLRR    TIME_L
        CLRR    TIME_M
        CLRR    TIME_H
        BSR     F_TIMING_SEL
	BCR	F_ONCE
	GOTO	SHORT_KEY_RELEASE_JUDGE_END
		
WORK_MODE_ADD:	
	INCR	WORK_MODE,R
	MOVIA	9
	SUBAR	WORK_MODE,A 
	BTRSS	C 
	GOTO	SAVE_TO_E2
	MOVIA	1
	MOVAR	WORK_MODE
  	
SAVE_TO_E2:
	BCR	F_AUTO	
	BCR	F_ONCE
	MOVR	WORK_MODE,A 
	MOVAR	WORK_MODE_TMP
	CALL	GET_AUTO_TIME

	MOVIA	2
	XORAR	WORK_MODE,A 
	BTRSS	Z 
	GOTO	SHORT_KEY_RELEASE_JUDGE_END	
	BSR	F_AUTO			
SHORT_KEY_RELEASE_JUDGE_END:
	GOTO	SIGN_IN_JUDGE_END 	
;有按键
SIGN_ON:
	CLRR	KEY_RELEASE
		
	INCR	KEY_CNT,R
	BTRSC	Z
	INCR	KEY_CNT_H,R
	
;3S 秒长按
LONG_KEY_JUDGE:
	MOVIA	0X71
	SUBAR	KEY_CNT,A
	MOVIA	0X0B
	SBCAR	KEY_CNT_H,A 
	BTRSS	C 
	GOTO	LONG_KEY_JUDGE_END
	CLRR	KEY_CNT
	CLRR	KEY_CNT_H 
	
	BCR	F_SHORT_KEY
	BSR	F_LONG_KEY
;切换定时	
	BTRSC	F_KEY_ENB
	GOTO	LONG_KEY_JUDGE_END
	BSR	F_KEY_ENB
	
	BTRSC	F_SET_TIME	
	GOTO	STE_TIME_OFF
;开启定时	
STE_TIME_ON:
	BSR	F_SET_TIME
	BSR	LED
	CLRR	TIME_L 
	CLRR	TIME_M
	CLRR	TIME_H
	BSR	F_TIMING_SEL
	GOTO	LONG_KEY_JUDGE_END
;关闭定时	
STE_TIME_OFF:
	BCR	F_SET_TIME
	BCR	LED 	
LONG_KEY_JUDGE_END:	
	
SHORT_KEY_JUDGE:	
	MOVIA	0X20
	SUBAR	KEY_CNT,A
	MOVIA	0X00
	SBCAR	KEY_CNT_H,A 
	BTRSS	C 
	GOTO	SHORT_KEY_JUDGE_END
	
	BTRSS	F_LONG_KEY
	BTRSC	F_SHORT_KEY
	GOTO	SHORT_KEY_JUDGE_END
	BSR	F_SHORT_KEY
		
SHORT_KEY_JUDGE_END:	 	
SIGN_IN_JUDGE_END:
;-----------------------------------------
;自动模式20s切换
AUTO_20S_JUDGE:
	BTRSS	F_ON 
	GOTO	AUTO_20S_JUDGE_END

	BTRSS	F_AUTO
	GOTO	AUTO_20S_JUDGE_END

	INCR	AUTO_20S_L,R 
	BTRSC	Z 
	INCR	AUTO_20S_M,R
	BTRSC	Z
	INCR	AUTO_20S_H,R

	MOVR	AUTO_TMP_L,A  
	SUBAR	AUTO_20S_L,A
	MOVR	AUTO_TMP_M,A 
	SBCAR	AUTO_20S_M,A
	MOVR	AUTO_TMP_H,A
	SBCAR	AUTO_20S_H,A
	BTRSS	C 
	GOTO	AUTO_20S_JUDGE_END
	CLRR	AUTO_20S_L
	CLRR	AUTO_20S_M
	CLRR	AUTO_20S_H
	BCR	F_ONCE

	INCR	WORK_MODE_TMP,R 
	MOVIA	9
	SUBAR	WORK_MODE_TMP,A 
	BTRSS	C 
	GOTO	$+3;AUTO_20S_JUDGE_END
	MOVIA	1
	MOVAR	WORK_MODE_TMP
	
	CALL	GET_AUTO_TIME

AUTO_20S_JUDGE_END:
;-----------------------------------
	BTRSS	F_ON 
	GOTO	INT_END	
	GOTO	WORK_MODE_TAB
;-----------------------------------------------------
POWER_50PER:
	BTRSC	F_ONCE 
	GOTO	POWER_50PER_LOOP
POWER_50PER_INT:		
	BSR	F_ONCE 
	BSR	LED_W
	BCR     LED_Y
	CLRR	TIME_SUM

POWER_50PER_LOOP:
	GOTO	INT_END
;----------------------------------------       
;----------------------------------------    
FLASH_MODE1212: 
	BTRSC	F_ONCE 
	GOTO	FLASH_MODE1212_LOOP
FLASH_MODE1212_INT:
	BSR	F_ONCE 
	BCR	LED_W
	BCR	F_LED_W
	BSR	LED_Y
	BSR	F_LED_Y
	CLRR	TIME_SUM
	CLRR	CNT_4
	CLRR	TIME4_H
	CLRR	TIME4_L
	CLRR	TIMES_4 
	MOVIA	12
	MOVAR	TEMPS_4
	MOVIA	0X03
	MOVAR	TEMP4_H
	MOVIA	0X22
	MOVAR	TEMP4_L
FLASH_MODE1212_LOOP:
	INCR	TIME4_L,R 
	BTRSC	Z
	INCR	TIME4_H,R 
	
	MOVR	TEMP4_L,A 
	SUBAR	TIME4_L,A 
	MOVR	TEMP4_H,A 
	SBCAR	TIME4_H,A 
	BTRSS	C 
	GOTO	INT_END
	CLRR	TIME4_H
	CLRR	TIME4_L
	
	MOVIA	0X06
	XORAR	FLAG1,R;PORTB,R 
	
	INCR	TIMES_4,R 
	MOVR	TEMPS_4,A 
	SUBAR	TIMES_4,A
	BTRSS	C 
	GOTO	INT_END
	CLRR	TIMES_4 
	
;获取下个小周期参数
	INCR	CNT_4,R 
	MOVIA	4
	SUBAR	CNT_4,A 
	BTRSS	C 
	GOTO	$+2
	CLRR	CNT_4

	MOVR    CNT_4,A
        ANDIA   0X03        
        CALL    TAB_4_TIME_H
	MOVAR	TEMP4_H
	 
	MOVR    CNT_4,A
        ANDIA   0X03        
        CALL    TAB_4_TIME_L
	MOVAR	TEMP4_L
	
	MOVR    CNT_4,A
        ANDIA   0X03        
        CALL    TAB_4_TIMES
	MOVAR	TEMPS_4
	GOTO	INT_END
;--------------------------------------------	
;-------------------------------------------
FLASH_MODE12121122: 
	BTRSC	F_ONCE 
	GOTO	FLASH_MODE12121122_LOOP
FLASH_MODE12121122_INT:
	BSR	F_ONCE 
	BCR	LED_W
	BCR	F_LED_W
	BSR	F_LED_Y
	BSR	LED_Y 
	CLRR	TIME_SUM
	CLRR	TIME6_H
	CLRR	TIME6_L
	CLRR	TIMES_6
	CLRR	CNT_6	
	MOVIA	7
	MOVAR	TEMPS_6
	MOVIA	0X00
	MOVAR	TEMP6_H
	MOVIA	0X50
	MOVAR	TEMP6_L
	MOVIA	0X04
	MOVAR	FLASH_MODE6		
FLASH_MODE12121122_LOOP:
	INCR	TIME6_L,R 
	BTRSC	Z
	INCR	TIME6_H,R 
	
	MOVR	TEMP6_L,A 
	SUBAR	TIME6_L,A 
	MOVR	TEMP6_H,A 
	SBCAR	TIME6_H,A 
	BTRSS	C 
	GOTO	INT_END
	CLRR	TIME6_H
	CLRR	TIME6_L
	
	MOVR	FLASH_MODE6,A 
	XORAR	FLAG1,R;PORTB,R 
	CLRR	TIME_SUM
	
	INCR	TIMES_6,R 
	MOVR	TEMPS_6,A 
	SUBAR	TIMES_6,A
	BTRSS	C 
	GOTO	INT_END	
	CLRR	TIMES_6

;获取下个小周期参数
	INCR	CNT_6,R 
	MOVIA	4;3
	SUBAR	CNT_6,A 
	BTRSS	C 
	GOTO	$+2
	CLRR	CNT_6
	;BCR	LED_Y
	;BCR	F_LED_Y
	
	MOVIA	1
	XORAR	CNT_6,A 
	BTRSS	Z
	GOTO	$+3
	BSR	LED_W
	BSR	F_LED_W

	MOVIA	3;2
	XORAR	CNT_6,A 
	BTRSS	Z
	GOTO	$+3
	BSR	LED_Y
	BSR	F_LED_Y
	
	MOVR    CNT_6,A
        ANDIA   0X03        
        CALL    TAB_6_TIME_H
	MOVAR	TEMP6_H
	 
	MOVR    CNT_6,A
        ANDIA   0X03        
        CALL    TAB_6_TIME_L
	MOVAR	TEMP6_L
	
	MOVR    CNT_6,A
        ANDIA   0X03        
        CALL    TAB_6_TIMES
	MOVAR	TEMPS_6
	
	MOVR    CNT_6,A
        ANDIA   0X03        
        CALL    TAB_6_FLASH_MODE
	MOVAR	FLASH_MODE6
	GOTO	INT_END
;--------------------------------------------
;--------------------------------------------	 
FLASH_MODE11112222: 
	BTRSC	F_ONCE
	GOTO	FLASH_MODE11112222_LOOP
FLASH_MODE11112222_INT:	
	BSR	F_ONCE
	BCR	LED_W
	BCR	LED_Y
	BCR	F_LED_W
	BCR	F_LED_Y
	BCR	F_FLSAH_8
	CLRR	TIME8
	CLRR	TIME_SUM
	CLRR	TIMES_8

FLASH_MODE11112222_LOOP: 
	INCR	TIME8,R 
	MOVIA	80
	SUBAR	TIME8,A 
	BTRSS	C 
	GOTO	INT_END
	CLRR	TIME8
 
	MOVIA	0X02
	BTRSC	F_FLSAH_8
	MOVIA	0X04 
	XORAR	FLAG1,R;PORTB,R
	CLRR	TIME_SUM
 
	INCR	TIMES_8,R 
	MOVIA	8
	SUBAR	TIMES_8,A 
	BTRSS	C 
	GOTO	INT_END
	CLRR	TIMES_8
 
	MOVIA	0X80
	XORAR	FLAG,R
	GOTO	INT_END
INT_END:

TIME_BASE_JUDGE_END:
;=====================================================
;定时判断  8+16H定时循环  15.625*2MS基准
;=====================================================
TIMEING_JUDGE:
	BTRSS	F_SET_TIME
	GOTO	TIMEING_JUDGE_END
HOUR_8_JUDGE:  
        BTRSS   F_TIMING_SEL                 ;定时关闭后 自动打开
        GOTO    HOUR_8_JUDGE_END
        MOVIA   0X00     
        SUBAR   TIME_L,A
        MOVIA   0X10       
        SBCAR   TIME_M,A
        MOVIA   0X0E    
        SBCAR   TIME_H,A
        BTRSS   C
        GOTO    HOUR_8_JUDGE_END

        CLRR    TIME_L
        CLRR    TIME_M
        CLRR    TIME_H      
        BCR     F_TIMING_SEL
        BCR     F_ON
        GOTO    TIMEING_JUDGE_END
HOUR_8_JUDGE_END:       
 
CLOSE_16H_JUDGE:
        MOVIA   0X00 
        SUBAR   TIME_L,A
        MOVIA   0X20 
        SBCAR   TIME_M,A
        MOVIA   0X1C
        SBCAR   TIME_H,A
        BTRSS   C
        GOTO    TIMEING_JUDGE_END
        CLRR    TIME_L
        CLRR    TIME_M
        CLRR    TIME_H
        BSR     F_TIMING_SEL      
        BSR     F_ON 
	BCR	F_ONCE
TIMEING_JUDGE_END:	 
 	GOTO	MAIN	

;获取时间
GET_AUTO_TIME:
	MOVR    WORK_MODE_TMP,A
        ANDIA   0X0F       
        CALL    TAB_AUTO_TIME_L
	MOVAR	AUTO_TMP_L
	 
	MOVR    WORK_MODE_TMP,A
        ANDIA   0X0F        
        CALL    TAB_AUTO_TIME_M
	MOVAR	AUTO_TMP_M
	
	MOVR    WORK_MODE_TMP,A
        ANDIA   0X0F        
        CALL    TAB_AUTO_TIME_H
	MOVAR	AUTO_TMP_H
	
	CLRR	AUTO_20S_L
	CLRR	AUTO_20S_M
	CLRR	AUTO_20S_H
	RETURN
;=============================================
;MS延时
;=============================================
DELAY_MS:
        MOVAR   DELAYMS_CNT
DELAYMS_LOOP:
        MOVIA   250
        MOVAR   DELAY_INSIDE
DELAY_INSIDE_LOOP:
        CLRWDT
        GOTO    $+1
        GOTO    $+1
        DECRSZ  DELAY_INSIDE,R
        GOTO    DELAY_INSIDE_LOOP
        DECRSZ  DELAYMS_CNT,R
        GOTO    DELAYMS_LOOP
DELAY_2US:       
        CLRWDT
        NOP
        RETURN		
;***********************************************************
;函数名：SYS_CPIO_INIT
;注意寄存器所在 bank 区域
;***********************************************************
SYS_CPIO_INIT:
	BCR	STATUS,5	;保证转到bank0/2再操作PORTB
	BCR	STATUS,6
	MOVIA	000H		;0000 0000
	MOVAR	PORTB
	BSR	STATUS,5	;转到bank1
	BCR	STATUS,6
	;OPTION_REG(BANK1)
	MOVIA	000H		;0000 0000
	MOVAR	OPTION_REG	
			;Bit7：INT1EDG：外部中断1触发方式选择位（PB1）;0-falling edge, 1-rising edge
			;Bit6：INTEDG：外部中断触发方式选择位（PB0）	;0-falling edge, 1-rising edge		
	;PCON(BANK1)(芯片控制寄存器)
	MOVIA	0A0H		;101- ----   	
	MOVAR	PCON		;Bit7：WDTE: WDT（看门狗定时器）使能位	;0-DIS, 1-EN					
						;Bit5：LVREN: 低电压复位使能位			;0-DIS, 1-EN
	;TRISB(BANK1)(I/O口PB控制寄存器)
	MOVIA	28H		;--10 1000 :PORTB输入输出配置寄存器 (0-OUT;1-IN) 
	MOVAR	TRISB		;0-OUT, 1-IN
	
	;WUCON(BANK1)(PORB输入状态变化中断/唤醒控制寄存器)
	MOVIA	00H		;--00 0000 :PORTB变化中断唤醒寄存器 (1-EN;0=DIS)
	MOVAR	WUCON		;0-DIS, 1-EN
	       
	
	
	BCR	STATUS,5	;转到bank0
	BCR	STATUS,6		
	;PHCON(BANK0)(PB 内部上拉控制寄存器)
	MOVIA	0F7H		;--11 0111 :PORTB上拉寄存器 (1-DIS;0-EN)
	MOVAR	PHCON		;0-EN, 1-DIS
	
	;DRVC0(BANK0)(PB0 端口大驱动控制寄存器)
	MOVIA	007H		;---- -000 :
	MOVAR	DRVC0		;1-EN, 0-DIS
		
	;DRVC1(BANK0)(PB1 端口大驱动控制寄存器)
	MOVIA	007H		;---- -000 :
	MOVAR	DRVC1		;1-EN, 0-DIS
		
	;DRVC2(BANK0)(PB2 端口大驱动控制寄存器)
	MOVIA	007H		;---- -000 :
	MOVAR	DRVC2		;1-EN, 0-DIS
	
	RETURN
	
;***********************************************************
;函数名：BANK0_RAM_CLR
;***********************************************************
CLR_ALL_RAM:
	MOVIA	0X3F
	MOVAR	40H
	MOVIA	0X7F
	MOVAR	FSR
	
RAM_CLR_LOOP:	
	CLRR	INDF
	DECR	FSR,1
	DECRSZ	40H,1
	GOTO	RAM_CLR_LOOP
	RETURN

;***********************************************************
;***********************************************************
  ; * @brief  tp0定时器初始化函数,
  ; *
  ; * @note   tp0如果选Fosc,Fosc是固定的16Mhz
  ; * @note   - 本示例使用Fpcu的4分频作为tp0时钟，也就是tp0时钟为1Mhz
  ; * @note   - 注意TP0只支持上溢出，所以重装载值向上增加到256溢出
  ; *           
  ; * @retval None
  
tp0_tim_init:	
	BCR	STATUS,5;切换到BANK2
	BSR	STATUS,6
	MOVIA	217
	MOVAR	PWM0TL;定时器TP0计数值
	MOVIA	0
	MOVAR	PWM0PL;周期值
	MOVIA	0X00
	MOVAR	PWMPSCON;PWM0 输出端口选择； 
			; Bit2：PWMPS2：PWM2 输出端口选择； 0-PB2；1-PB5
			; Bit1：PWMPS1：PWM1 输出端口选择； 0-PB1；1-PB4
			; Bit0：PWMPS0：PWM0 输出端口选择； 0-PB0；1-PB3
	
	MOVIA   0X00;   
	MOVAR   PWM0C1  ;bit0:PWMOUTS：PWM 波形控制； 0-默认，关闭 PWM 使能，端口停止输出 PWM；1-关闭 PWM 使能，端口待定时器溢出后停止输出 PWM
	
	MOVIA	0X43	;01-0 0011//Fcpu/2=2MHz/2=1MHz
	MOVAR	PWM0C	;重装载值/周期值
                ;Bit7: CK0[3]：tp0时钟源选择位 0:Fcpu作为时钟; 1:Fosc/16M作为时钟
				;Bit4：PWM0S2：PWM2 输出取反位; 	0=DIS,1=EN
				;Bit3：PWM0S1：PWM0 输出取反位;     0=DIS,1=EN
				;Bit2：PWM0S0：PWM1 输出取反位; 	0=DIS,1=EN
                ;Bit6:Bit1-0  => CK0[2:0]:TC0 时钟分频选择位
                    ;CK0[2:0]		Fcpu作为时钟(CK0[3]=0)		Fosc/16M作为时钟(CK0[3]=1)
                    ; 000				Fcpu/256			Fosc/128
                    ; 001				Fcpu/128                	Fosc/64
                    ; 010				Fcpu/64                 	Fosc/32
                    ; 011				Fcpu/32                 	Fosc/16
                    ; 100				Fcpu/16                 	Fosc/8
                    ; 101				Fcpu/8                  	Fosc/4
                    ; 110				Fcpu/4                  	Fosc/2
                    ; 111				Fcpu/2				Fosc/1
	
	
	MOVIA	0x81
	MOVAR	PWM0EN;1--0 -001
			;Bit7：TP0EN:TP0 定时器总使能; 0=DIS,1=EN
			;Bit4：PWM2EN:PWM2 输出控制位; 0=DIS,1=EN
			;Bit2：PWM1EN:PWM1 输出控制位; 0=DIS,1=EN
			;Bit1：PWM0EN:PWM0 输出控制位; 0=DIS,1=EN
			;Bit0：LOADEN:PWM重装使能(重装周期寄存器);0=DIS,1=EN
	
	BSR	TP0IE	;开启TP0溢出中断
	BCR	STATUS,5
	BCR	STATUS,6
	RETURN

;***********************************************************
;
;***********************************************************
TC0_XT_Init:
	BSR	STATUS,5	;转到bank1
	BCR	STATUS,6
	
	;XTCON(BANK1)(外振模式控制寄存器)
	MOVIA	0X07		;---0 0111 :
	MOVAR	XTCON		;		
				;bit4: XTSEN0：外振软件增强使能位 0 		   0-en;1-dis
				;bit3: XTEN：外振软件使能位（与上位机配置共同控制）0-en;1-dis
				;bit2: XT_TC0S：TC0 低速时钟选择		0-内部低速时钟；1-外部32.768K 
				;bit1: XT_LRCS：低速模式系统时钟选择	0-dis ; 1-EN
				;bit0:	XT_MONE：外振监测使能


	BSR	OSCM,4
				;Bit6：WAKEMODE：睡眠唤醒后系统时钟选择位;0=InsideLowRC,1=HighRC	
				;Bit5：GREENEN：绿色模式使能位		;0=GeneralMode，1=GreenMode
				;Bit4：SLT0CON：TC0低速时钟控制位(用于唤醒睡眠、绿色);0=HighRC,1=LowRC
				;Bit3：SLCON：内外低速时钟控制位	;0=OpenLowRC,1=CloseLowRC
				
				;Bit1：STPHX：内部高速振荡器控制位 ;0=Work,1=Stop
				;Bit0：CLKMD：系统时钟模式控制位

	BCR	STATUS,5	;转到bank0
	BCR	STATUS,6

	MOVIA	00H
	MOVAR	TC0L	;定时器初值
;	MOVAR	TC0H	;定时器初值	
	
	MOVIA	0F0H	;1111 0000  fcpu/2
	MOVAR	TC0M	;Bit7：TC0TR:TC0 启动控制位。
			;Bit6~Bit4：TC0S[2:0]:TC0 分频选择位
			;Bit3：TCCKS:TC0 时钟信号控制位	

	BCR	TC0IF
	BSR	TC0IE
	BSR	FGIE
	RETURN









