;*****************************************************   
;Copyright (c) 2024, 苏州华芯微电子股份有限公司
;All rights reserved                                 
;
;文件名称: HS16P2811-T3
;摘    要: HS16P2811-T3 功能演示
;
;当前版本: 1.0                                      
;作    者: LBW
;完成日期: 2024.11.18
;*****************************************************   

;***********************************************************
;files include
#include "HS16P2811.inc"

;W       EQU     0 
;F       EQU     1 
;A       EQU     0
;R       EQU     1

;***********************************************************
;用户IO定义
#define  OUT1		PORTA,2
#define  OUT2		PORTA,3

#define  KEY1		PORTC,2  
#define  KEY2		PORTC,3



;正反扫数码管扫描 第一步 更改引脚
#define 	SEG1_O	   	  PORTC,1
#define 	SEG2_O	   	  PORTC,3
#define 	SEG3_O	   	  PORTC,4
#define 	SEG4_O	   	  PORTC,5
#define 	SEG5_O	   	  PORTA,6

#define 	SEG1_M   	  TRISC,1
#define 	SEG2_M   	  TRISC,3
#define 	SEG3_M   	  TRISC,4
#define 	SEG4_M   	  TRISC,5
#define 	SEG5_M   	  TRISA,6

; 宏名 MACRO [参数1], [参数2]...
    ; ; 宏体（要替换的代码）
; ENDM


;正反扫数码管扫描 第二步 引脚输出高低的宏 SEG为高 COM为低
SEG1 MACRO
    BSR		RP0
	BCR		SEG1_M
	BCR		RP0
	BSR		SEG1_O
ENDM

SEG2 MACRO
    BSR		RP0
	BCR		SEG2_M
	BCR		RP0
	BSR		SEG2_O
ENDM

SEG3 MACRO
    BSR		RP0
	BCR		SEG3_M
	BCR		RP0
	BSR		SEG3_O
ENDM

SEG4 MACRO
    BSR		RP0
	BCR		SEG4_M
	BCR		RP0
	BSR		SEG4_O
ENDM
	
SEG5 MACRO
    BSR		RP0
	BCR		SEG5_M
	BCR		RP0
	BSR		SEG5_O
ENDM
;--------------------------------
COM1 MACRO
    BSR		RP0
	BCR		SEG1_M
	BCR		RP0
	BCR		SEG1_O
ENDM

COM2 MACRO
    BSR		RP0
	BCR		SEG2_M
	BCR		RP0
	BCR		SEG2_O
ENDM

COM3 MACRO
    BSR		RP0
	BCR		SEG3_M
	BCR		RP0
	BCR		SEG3_O
ENDM

COM4 MACRO
    BSR		RP0
	BCR		SEG4_M
	BCR		RP0
	BCR		SEG4_O
ENDM

COM5 MACRO
    BSR		RP0
	BCR		SEG5_M
	BCR		RP0
	BCR		SEG5_O
ENDM


seg_bit MACRO lab, bit_flag, seg_var, com_var, jmp_flag
lab:
    BTRSS	bit_flag
	GOTO	jmp_flag
	seg_var
	com_var
	GOTO	jmp_flag
ENDM

;***********************************************************
;user general reg define
TEMP			EQU		020H
DELAY_US		EQU		040H
DELAY_MS_CNT	EQU		041H

Timer_MODE		EQU		042H
;AD_BUF_H		EQU		043H

RF_COUNT		EQU		044H
RF_DATA			EQU		045H




STATUS_BUF		EQU		07EH	;中断保护寄存器必须放 0x70-0x7F不要动！！！
ACC_BUF			EQU		07FH	;中断保护寄存器必须放 0x70-0x7F不要动！！！




;-------------------------------------------      
;标志位                    
;-------------------------------------------

;正反扫数码管扫描 第三步 数码管显示buf 定义

seg_buf1		EQU		046H
seg_buf2		EQU		047H
seg_buf3		EQU		048H
seg_index		EQU		049H


#define b_b1_dsp		seg_buf1.1
#define b_c1_dsp		seg_buf1.2

#define b_a2_dsp		seg_buf2.0
#define b_b2_dsp		seg_buf2.1
#define b_c2_dsp		seg_buf2.2
#define b_d2_dsp		seg_buf2.3
#define b_e2_dsp		seg_buf2.4
#define b_f2_dsp		seg_buf2.5
#define b_g2_dsp		seg_buf2.6

#define b_a3_dsp		seg_buf3.0
#define b_b3_dsp		seg_buf3.1
#define b_c3_dsp		seg_buf3.2
#define b_d3_dsp		seg_buf3.3
#define b_e3_dsp		seg_buf3.4
#define b_f3_dsp		seg_buf3.5
#define b_g3_dsp		seg_buf3.6

;***********************************************************
;函数入口地址
	ORG	00H
	NOP
	NOP
	GOTO	MAIN_SETUP
	
;中断入口地址04H
	ORG		004H
	GOTO	INT_PRO
INT_PRO:
	MOVAR	ACC_BUF		;保存ACC
	SWAPR	STATUS,0
	MOVAR	STATUS_BUF	;保存STATUS
	
	;MOVIA	0xDB
	;ANDAR	INTCON,1
	
	//CLRR    PIR1     ;清中断标志	
	BTRSS	T3IF
	GOTO	INT_BACK
	BCR		T3IF
	
BSR		RP1
	MOVIA	(0xfff-400)/256
	MOVAR	T3TH	;(bk2)
	MOVIA	(0xfff-400)%256
	MOVAR	T3TL	;(bk2)
	
	
	
	
;===============200us 左右一次===========数码管扫描开始！=数码管扫描开始！=数码管扫描开始！=数码管扫描开始！====================
	;正反扫数码管扫描 第四步 引脚都设置输入
	BCR		RP1
	BSR		RP0
	BSR		SEG1_M
	BSR		SEG2_M
	BSR		SEG3_M
	BSR		SEG4_M
	BSR		SEG5_M
	BCR		RP0
	
	;正反扫数码管扫描 第五步 根据多少段写循环
	INCR	seg_index,1
	MOVIA	16			;数码管一共16段
	SUBAR	seg_index,0
	BTRSC	C
	CLRR	seg_index	;数码管一共16段
	;确保下面的表覆盖16段
	
	;正反扫数码管扫描 第六步 根据多少段 跳转表
	MOVR	seg_index,0;确保PCL不会超过255溢出!!!
	; ANDIA	0X0F
	ADDAR	PCL,1
	GOTO	SEG_SCAN_0
	GOTO	SEG_SCAN_1
	GOTO	SEG_SCAN_2
	GOTO	SEG_SCAN_3
	GOTO	SEG_SCAN_4
	GOTO	SEG_SCAN_5
	GOTO	SEG_SCAN_6
	GOTO	SEG_SCAN_7
	GOTO	SEG_SCAN_8
	GOTO	SEG_SCAN_9
	GOTO	SEG_SCAN_10
	GOTO	SEG_SCAN_11
	GOTO	SEG_SCAN_12
	GOTO	SEG_SCAN_13
	GOTO	SEG_SCAN_14
	GOTO	SEG_SCAN_15;确保PCL不会超过255溢出!!!
	
	;正反扫数码管扫描 第七步 点亮每一个段
	seg_bit	SEG_SCAN_0,		b_b1_dsp, SEG3, COM4, seg_sacn_end;第一个数码管的B段，根据数码管资料点亮
	seg_bit	SEG_SCAN_1,		b_c1_dsp, SEG2, COM4, seg_sacn_end;第一个数码管的C段，根据数码管资料点亮
	
	seg_bit	SEG_SCAN_2,		b_a2_dsp, SEG2, COM3, seg_sacn_end;第二个数码管的A段，根据数码管资料点亮
	seg_bit	SEG_SCAN_3,		b_b2_dsp, SEG3, COM2, seg_sacn_end
	seg_bit	SEG_SCAN_4,		b_c2_dsp, SEG4, COM3, seg_sacn_end
	seg_bit	SEG_SCAN_5,		b_d2_dsp, SEG4, COM2, seg_sacn_end
	seg_bit	SEG_SCAN_6,		b_e2_dsp, SEG5, COM2, seg_sacn_end
	seg_bit	SEG_SCAN_7,		b_f2_dsp, SEG5, COM3, seg_sacn_end
	seg_bit	SEG_SCAN_8,		b_g2_dsp, SEG5, COM4, seg_sacn_end
	                 
	seg_bit	SEG_SCAN_9,		b_a3_dsp, SEG1, COM2, seg_sacn_end;第三个数码管的A段，根据数码管资料点亮
	seg_bit	SEG_SCAN_10,	b_b3_dsp, SEG2, COM1, seg_sacn_end
	seg_bit	SEG_SCAN_11,	b_c3_dsp, SEG1, COM3, seg_sacn_end
	seg_bit	SEG_SCAN_12,	b_d3_dsp, SEG3, COM1, seg_sacn_end
	seg_bit	SEG_SCAN_13,	b_e3_dsp, SEG1, COM4, seg_sacn_end
	seg_bit	SEG_SCAN_14,	b_f3_dsp, SEG4, COM1, seg_sacn_end
	seg_bit	SEG_SCAN_15,	b_g3_dsp, SEG5, COM1, seg_sacn_end
	
	
	
seg_sacn_end:
;==========================数码管扫描结束！=数码管扫描结束！=数码管扫描结束！=数码管扫描结束！====================
		
INT_BACK:	
	SWAPR	STATUS_BUF,0
	MOVAR	STATUS		;恢复STATUS
	SWAPR	ACC_BUF,1
	SWAPR	ACC_BUF,0	;恢复ACC
	RETFIE
;//中断代码结束

;==========================================================================
;==========================================================================
T3_INIT:
	MOVR	Timer_MODE,0
	ANDIA	0X07
	ADDAR	PCL,1
	GOTO	T3_Timer_Fcpu_Init
	GOTO	T3_Timer_Fosc_Init
	GOTO	T3_5PWM_Indep_Fcpu
	GOTO	T3_5PWM_Indep_INV_Fosc
	GOTO	T3_22PWM_Compl_Fcpu
	GOTO	T3_22PWM_Compl_Fcpu_PWM0out0
	GOTO	T3_Timer_Fcpu_Init
	GOTO	T3_Timer_Fcpu_Init
;==========================================================================
seg_tab:
	ANDIA	0X0F
	ADDAR	PCL,1		;确保PCL不会超过255溢出!!!
	RETIA	0x3F	;0
	RETIA	0x6     ;1
	RETIA	0x5B    ;2
	RETIA	0x4F    ;3
	RETIA	0x66    ;4
	RETIA	0x6D    ;5
	RETIA	0x7D    ;6
	RETIA	0x7     ;7
	RETIA	0x7F    ;8
	
	RETIA	0x6F	;9
	RETIA	0x77
	RETIA	0x7C
	RETIA	0x39
	RETIA	0x5E
	RETIA	0x79
	RETIA	0x71
	
;==========================================================================
;* @ brief:	T3_Timer_Fcpu_Init-T3-定时器模式-Fcpu
;* @ note: 定时器模式，时钟源选Fcpu
T3_Timer_Fcpu_Init:
	BCR		T3IF	;(bk0)
BSR		RP1
	MOVIA	(0xfff-200)/256
	MOVAR	T3TH	;(bk2)
	MOVIA	(0xfff-200)%256
	MOVAR	T3TL	;(bk2)
	
	MOVIA	0x00
	MOVAR	T3C2	;00** *111(bk2)
		;bit7:PDTM2
			;0 = T3PWM3 & T3PWM2 工作与独立输出模式
			;1 = T3PWM3 & T3PWM2 工作与互补输出模式
		;bit6:PDTM0
			;0 = T3PWM1 & T3PWM0 工作与独立输出模式
			;1 = T3PWM1 & T3PWM0 工作与互补输出模式
		;bit5:T3PWM4INV 	0 = 不取反;			1 = 取反
		;bit4:T3PWM3INV  	0 = 不取反;			1 = 取反
		;bit3:T3PWM2INV  	0 = 不取反;			1 = 取反
		;bit2:T3PWM4EN		0 = 禁止 T3PWM4 输出;	1 = 允许 T3PWM4 输出 
		;bit1:T3PWM3EN		0 = 禁止 T3PWM3 输出;	1 = 允许 T3PWM3 输出 
		;bit0:T3PWM2EN		0 = 禁止 T3PWM2 输出;	1 = 允许 T3PWM2 输出 
	
	MOVIA	0x06
	MOVAR	T3C1	;-0-0 -111(bk2)
		;bit6:T3OUT0   		//0 = T3PWM 输出不受影响;	1 = T3PWM 输出全低电平 
		;bit4:T3CKS0 		//0 = Fcpu/2;-√				1 = Fosc
		;bit2~0:T3CK<2:0>
			;111 = 1分频;		110 = 2分频;-√		101 = 4分频;	100 = 8分频
			;011 = 16分频;		010 = 32分频;		001 = 64分频;	000 = 128分频
	
	MOVIA	0x80
	MOVAR	T3C0	;1-** -11-(bk2)
		;bit7:T3REN  定时器总使能;			0 = 禁止;		1 = 使能-√
		;bit5:T3PWM1INV  T3PWM1输出极性;	0 = 不取反;	1 = 取反
		;bit4:T3PWM0INV  T3PWM0输出极性;	0 = 不取反;	1 = 取反
		;bit2:T3PWM1EN 输出使能;			0 = 禁止;		1 = 启动
		;bit1:T3PWM0EN 输出使能;			0 = 禁止;		1 = 启动
		
BCR		RP1
BSR		RP0
	BSR		T3IE	;(bk1)
BCR		RP0
	BCR		T3IF	;(bk0)
	BSR		PEIE	;(bk0)
	BSR		GIE		;(bk0)
	RETURN
	
;==========================================================================
;* @ brief:	T3_Timer_Fosc_Init-T3-定时器模式-Fosc
;* @ note: 定时器模式，时钟源选-Fosc；
T3_Timer_Fosc_Init:
	BCR		T3IF	;(bk0)
BSR		RP1
	MOVIA	(0xfff-200)/256
	MOVAR	T3TH	;(bk2)
	MOVIA	(0xfff-200)%256
	MOVAR	T3TL	;(bk2)
	
	MOVIA	0x00
	MOVAR	T3C2	;00** *111(bk2)
		;bit7:PDTM2
			;0 = T3PWM3 & T3PWM2 工作与独立输出模式
			;1 = T3PWM3 & T3PWM2 工作与互补输出模式
		;bit6:PDTM0
			;0 = T3PWM1 & T3PWM0 工作与独立输出模式
			;1 = T3PWM1 & T3PWM0 工作与互补输出模式
		;bit5:T3PWM4INV 	0 = 不取反;			1 = 取反
		;bit4:T3PWM3INV  	0 = 不取反;			1 = 取反
		;bit3:T3PWM2INV  	0 = 不取反;			1 = 取反
		;bit2:T3PWM4EN		0 = 禁止 T3PWM4 输出;	1 = 允许 T3PWM4 输出 
		;bit1:T3PWM3EN		0 = 禁止 T3PWM3 输出;	1 = 允许 T3PWM3 输出 
		;bit0:T3PWM2EN		0 = 禁止 T3PWM2 输出;	1 = 允许 T3PWM2 输出 
	
	MOVIA	0x16
	MOVAR	T3C1	;-0-1 -111(bk2)
		;bit6:T3OUT0   		//0 = T3PWM 输出不受影响;	1 = T3PWM 输出全低电平 
		;bit4:T3CKS0 		//0 = Fcpu/2;				1 = Fosc-√
		;bit2~0:T3CK<2:0>
			;111 = 1分频;		110 = 2分频;-√		101 = 4分频;	100 = 8分频
			;011 = 16分频;		010 = 32分频;		001 = 64分频;	000 = 128分频
	
	MOVIA	0x80
	MOVAR	T3C0	;1-** -11-(bk2)
		;bit7:T3REN  定时器总使能;			0 = 禁止;		1 = 使能-√
		;bit5:T3PWM1INV  T3PWM1输出极性;	0 = 不取反;	1 = 取反
		;bit4:T3PWM0INV  T3PWM0输出极性;	0 = 不取反;	1 = 取反
		;bit2:T3PWM1EN 输出使能;			0 = 禁止;		1 = 启动
		;bit1:T3PWM0EN 输出使能;			0 = 禁止;		1 = 启动
		
BCR		RP1
BSR		RP0
	BSR		T3IE	;(bk1)
BCR		RP0
	BCR		T3IF	;(bk0)
	BSR		PEIE	;(bk0)
	BSR		GIE		;(bk0)
	RETURN
	
;==========================================================================
;* @ brief:	-T3-5*PWM模式-独立-Fcpu
;* @ note: 定时器模式，时钟源选-Fosc；
T3_5PWM_Indep_Fcpu:
	BCR		T3IF	;(bk0)
BSR		RP1 ;into bk2
	MOVIA	(500)/256
	MOVAR	T3PH	;(bk2)
	MOVIA	(500)%256
	MOVAR	T3PL	;(bk2)
	
	//duty0-pa0
	MOVIA	(10)/256
	MOVAR	T3D0H	;(bk2)
	MOVIA	(10)%256
	MOVAR	T3D0L	;(bk2)

	//duty1-pa1
	MOVIA	(1)/256
	MOVAR	T3D1H	;(bk2)
	MOVIA	(1)%256
	MOVAR	T3D1L	;(bk2)
	
	//duty2-pa2
	MOVIA	(300)/256
	MOVAR	T3D2H	;(bk2)
	MOVIA	(300)%256
	MOVAR	T3D2L	;(bk2)
	
	//duty3-pa3
	MOVIA	(500)/256
	MOVAR	T3D3H	;(bk2)
	MOVIA	(500)%256
	MOVAR	T3D3L	;(bk2)
	
	MOVIA	0x07
	MOVAR	T3C2	;00** *111(bk2)
		;bit7:PDTM2
			;0 = T3PWM3 & T3PWM2 工作与独立输出模式-√
			;1 = T3PWM3 & T3PWM2 工作与互补输出模式
		;bit6:PDTM0
			;0 = T3PWM1 & T3PWM0 工作与独立输出模式-√
			;1 = T3PWM1 & T3PWM0 工作与互补输出模式
		;bit5:T3PWM4INV 	0 = 不取反;	-√		1 = 取反
		;bit4:T3PWM3INV  	0 = 不取反;	-√		1 = 取反
		;bit3:T3PWM2INV  	0 = 不取反;	-√		1 = 取反
		;bit2:T3PWM4EN		0 = 禁止 T3PWM4 输出;	1 = 允许 T3PWM4 输出 -√
		;bit1:T3PWM3EN		0 = 禁止 T3PWM3 输出;	1 = 允许 T3PWM3 输出 -√
		;bit0:T3PWM2EN		0 = 禁止 T3PWM2 输出;	1 = 允许 T3PWM2 输出 -√
	
	MOVIA	0x06
	MOVAR	T3C1	;-0-0 -111(bk2)
		;bit6:T3OUT0   		//0 = T3PWM 输出不受影响;	1 = T3PWM 输出全低电平 
		;bit4:T3CKS0 		//0 = Fcpu/2;-√				1 = Fosc
		;bit2~0:T3CK<2:0>
			;111 = 1分频;		110 = 2分频;-√		101 = 4分频;	100 = 8分频
			;011 = 16分频;		010 = 32分频;		001 = 64分频;	000 = 128分频
	
	MOVIA	0x86
	MOVAR	T3C0	;1-** -11-(bk2)
		;bit7:T3REN  定时器总使能;			0 = 禁止;		1 = 使能-√
		;bit5:T3PWM1INV  T3PWM1输出极性;	0 = 不取反;	-√	1 = 取反
		;bit4:T3PWM0INV  T3PWM0输出极性;	0 = 不取反;	-√	1 = 取反
		;bit2:T3PWM1EN 输出使能;			0 = 禁止;		1 = 启动-√
		;bit1:T3PWM0EN 输出使能;			0 = 禁止;		1 = 启动-√
	
BSR		RP0	;into bk3
	MOVIA	0x01
	MOVAR	PWMSEL1	;---- **01(bk3)
		;bit1~0:T3PWM4S<1:0>
			;00 = 无					01 = PC0 输出 T3PWM4
			;10 = PC5 输出T3PWM4		11 = PA4 输出 T3PWM4	
	MOVIA	0x55
	MOVAR	PWMSEL	;0101 0101(bk3)
		;bit7~6:T3PWM3S<1:0>
			;00 = 无					01 = PA3 输出 T3PWM3
			;10 = PC4 输出T3PWM3		11 = PA7 输出 T3PWM3
		;bit5~4:T3PWM2S<1:0>
			;00 = 无					01 = PA2 输出 T3PWM2
			;10 = PC3 输出T3PWM2		11 = PA6 输出 T3PWM2
		;bit3~2:T3PWM1S<1:0>
			;00 = 无					01 = PA1 输出 T3PWM1
			;10 = PC2 输出T3PWM1		11 = PA5 输出 T3PWM1
		;bit1~0:T3PWM0S<1:0>
			;00 = PC4 输出T3PWM0		01 = PA0 输出 T3PWM0
			;10 = PC1 输出T3PWM0		11 = PA4 输出 T3PWM0
BCR		RP1	;into bk1
	//duty4-pc0
	MOVIA	(499)/256
	MOVAR	T3D4H	;(bk2)
	MOVIA	(499)%256
	MOVAR	T3D4L	;(bk2)
	
	BSR		T3IE	;(bk1)
BCR		RP0	;into bk0
	BCR		T3IF	;(bk0)
	BSR		PEIE	;(bk0)
	;BSR		GIE		;(bk0)
	RETURN
	
;==========================================================================
;* @ brief:	-T3-5*PWM模式-INV-Fosc
;* @ note: 5PWM独立输出模式，时钟源选-Fosc；
T3_5PWM_Indep_INV_Fosc:
	BCR		T3IF	;(bk0)
BSR		RP1 ;into bk2
	MOVIA	(500)/256
	MOVAR	T3PH	;(bk2)
	MOVIA	(500)%256
	MOVAR	T3PL	;(bk2)
	
	//duty0-pa0
	MOVIA	(10)/256
	MOVAR	T3D0H	;(bk2)
	MOVIA	(10)%256
	MOVAR	T3D0L	;(bk2)

	//duty1-pa1
	MOVIA	(1)/256
	MOVAR	T3D1H	;(bk2)
	MOVIA	(1)%256
	MOVAR	T3D1L	;(bk2)
	
	//duty2-pa2
	MOVIA	(300)/256
	MOVAR	T3D2H	;(bk2)
	MOVIA	(300)%256
	MOVAR	T3D2L	;(bk2)
	
	//duty3-pa3
	MOVIA	(500)/256
	MOVAR	T3D3H	;(bk2)
	MOVIA	(500)%256
	MOVAR	T3D3L	;(bk2)
	
	MOVIA	0x3F
	MOVAR	T3C2	;00** *111(bk2)
		;bit7:PDTM2
			;0 = T3PWM3 & T3PWM2 工作与独立输出模式-√
			;1 = T3PWM3 & T3PWM2 工作与互补输出模式
		;bit6:PDTM0
			;0 = T3PWM1 & T3PWM0 工作与独立输出模式-√
			;1 = T3PWM1 & T3PWM0 工作与互补输出模式
		;bit5:T3PWM4INV 	0 = 不取反;			1 = 取反-√
		;bit4:T3PWM3INV  	0 = 不取反;			1 = 取反-√
		;bit3:T3PWM2INV  	0 = 不取反;			1 = 取反-√
		;bit2:T3PWM4EN		0 = 禁止 T3PWM4 输出;	1 = 允许 T3PWM4 输出 -√
		;bit1:T3PWM3EN		0 = 禁止 T3PWM3 输出;	1 = 允许 T3PWM3 输出 -√
		;bit0:T3PWM2EN		0 = 禁止 T3PWM2 输出;	1 = 允许 T3PWM2 输出 -√
	
	MOVIA	0x16
	MOVAR	T3C1	;-0-1 -111(bk2)
		;bit6:T3OUT0   		//0 = T3PWM 输出不受影响;	1 = T3PWM 输出全低电平 
		;bit4:T3CKS0 		//0 = Fcpu/2;				1 = Fosc-√
		;bit2~0:T3CK<2:0>
			;111 = 1分频;		110 = 2分频;-√		101 = 4分频;	100 = 8分频
			;011 = 16分频;		010 = 32分频;		001 = 64分频;	000 = 128分频
	
	MOVIA	0xB6
	MOVAR	T3C0	;1-11 -11-(bk2)
		;bit7:T3REN  定时器总使能;			0 = 禁止;		1 = 使能-√
		;bit5:T3PWM1INV  T3PWM1输出极性;	0 = 不取反;		1 = 取反-√
		;bit4:T3PWM0INV  T3PWM0输出极性;	0 = 不取反;		1 = 取反-√
		;bit2:T3PWM1EN 输出使能;			0 = 禁止;		1 = 启动-√
		;bit1:T3PWM0EN 输出使能;			0 = 禁止;		1 = 启动-√
	
BSR		RP0	;into bk3
	MOVIA	0x01
	MOVAR	PWMSEL1	;---- **01(bk3)
		;bit1~0:T3PWM4S<1:0>
			;00 = 无					01 = PC0 输出 T3PWM4
			;10 = PC5 输出T3PWM4		11 = PA4 输出 T3PWM4	
	MOVIA	0x55
	MOVAR	PWMSEL	;0101 0101(bk3)
		;bit7~6:T3PWM3S<1:0>
			;00 = 无					01 = PA3 输出 T3PWM3
			;10 = PC4 输出T3PWM3		11 = PA7 输出 T3PWM3
		;bit5~4:T3PWM2S<1:0>
			;00 = 无					01 = PA2 输出 T3PWM2
			;10 = PC3 输出T3PWM2		11 = PA6 输出 T3PWM2
		;bit3~2:T3PWM1S<1:0>
			;00 = 无					01 = PA1 输出 T3PWM1
			;10 = PC2 输出T3PWM1		11 = PA5 输出 T3PWM1
		;bit1~0:T3PWM0S<1:0>
			;00 = PC4 输出T3PWM0		01 = PA0 输出 T3PWM0
			;10 = PC1 输出T3PWM0		11 = PA4 输出 T3PWM0
BCR		RP1	;into bk1
	//duty4-pc0
	MOVIA	(499)/256
	MOVAR	T3D4H	;(bk2)
	MOVIA	(499)%256
	MOVAR	T3D4L	;(bk2)
	
	BSR		T3IE	;(bk1)
BCR		RP0	;into bk0
	BCR		T3IF	;(bk0)
	BSR		PEIE	;(bk0)
	;BSR		GIE		;(bk0)
	RETURN
	
;==========================================================================
;* @ brief:	-T3-2*2PWM模式-互补-Fcpu
;* @ note: 2PWM互补模式，时钟源选-Fcpu；
T3_22PWM_Compl_Fcpu:
	BCR		T3IF	;(bk0)
BSR		RP1 ;into bk2
	MOVIA	(500)/256
	MOVAR	T3PH	;(bk2)
	MOVIA	(500)%256
	MOVAR	T3PL	;(bk2)
	
	//duty0-pa0
	MOVIA	(10)/256
	MOVAR	T3D0H	;(bk2)
	MOVIA	(10)%256
	MOVAR	T3D0L	;(bk2)

	//duty1-pa1
	MOVIA	(20)/256
	MOVAR	T3D1H	;(bk2)
	MOVIA	(20)%256
	MOVAR	T3D1L	;(bk2)
	
	//duty2-pa2
	MOVIA	(300)/256
	MOVAR	T3D2H	;(bk2)
	MOVIA	(300)%256
	MOVAR	T3D2L	;(bk2)
	
	//duty3-pa3
	MOVIA	(500)/256
	MOVAR	T3D3H	;(bk2)
	MOVIA	(500)%256
	MOVAR	T3D3L	;(bk2)
	
	MOVIA	0xC7
	MOVAR	T3C2	;00** *111(bk2)
		;bit7:PDTM2
			;0 = T3PWM3 & T3PWM2 工作与独立输出模式
			;1 = T3PWM3 & T3PWM2 工作与互补输出模式-√
		;bit6:PDTM0
			;0 = T3PWM1 & T3PWM0 工作与独立输出模式
			;1 = T3PWM1 & T3PWM0 工作与互补输出模式-√
		;bit5:T3PWM4INV 	0 = 不取反;			1 = 取反
		;bit4:T3PWM3INV  	0 = 不取反;			1 = 取反
		;bit3:T3PWM2INV  	0 = 不取反;			1 = 取反
		;bit2:T3PWM4EN		0 = 禁止 T3PWM4 输出;	1 = 允许 T3PWM4 输出 -√
		;bit1:T3PWM3EN		0 = 禁止 T3PWM3 输出;	1 = 允许 T3PWM3 输出 -√
		;bit0:T3PWM2EN		0 = 禁止 T3PWM2 输出;	1 = 允许 T3PWM2 输出 -√
	
	MOVIA	0x05
	MOVAR	T3C1	;-0-1 -111(bk2)
		;bit6:T3OUT0   		//0 = T3PWM 输出不受影响;	1 = T3PWM 输出全低电平 
		;bit4:T3CKS0 		//0 = Fcpu/2;-√				1 = Fosc
		;bit2~0:T3CK<2:0>
			;111 = 1分频;		110 = 2分频;		101 = 4分频;-√	100 = 8分频
			;011 = 16分频;		010 = 32分频;		001 = 64分频;	000 = 128分频
	
	MOVIA	0x86
	MOVAR	T3C0	;1-11 -11-(bk2)
		;bit7:T3REN  定时器总使能;			0 = 禁止;		1 = 使能-√
		;bit5:T3PWM1INV  T3PWM1输出极性;	0 = 不取反;		1 = 取反
		;bit4:T3PWM0INV  T3PWM0输出极性;	0 = 不取反;		1 = 取反
		;bit2:T3PWM1EN 输出使能;			0 = 禁止;		1 = 启动-√
		;bit1:T3PWM0EN 输出使能;			0 = 禁止;		1 = 启动-√
	
BSR		RP0	;into bk3
	MOVIA	0x01
	MOVAR	PWMSEL1	;---- **01(bk3)
		;bit1~0:T3PWM4S<1:0>
			;00 = 无					01 = PC0 输出 T3PWM4
			;10 = PC5 输出T3PWM4		11 = PA4 输出 T3PWM4	
	MOVIA	0x55
	MOVAR	PWMSEL	;0101 0101(bk3)
		;bit7~6:T3PWM3S<1:0>
			;00 = 无					01 = PA3 输出 T3PWM3
			;10 = PC4 输出T3PWM3		11 = PA7 输出 T3PWM3
		;bit5~4:T3PWM2S<1:0>
			;00 = 无					01 = PA2 输出 T3PWM2
			;10 = PC3 输出T3PWM2		11 = PA6 输出 T3PWM2
		;bit3~2:T3PWM1S<1:0>
			;00 = 无					01 = PA1 输出 T3PWM1
			;10 = PC2 输出T3PWM1		11 = PA5 输出 T3PWM1
		;bit1~0:T3PWM0S<1:0>
			;00 = PC4 输出T3PWM0		01 = PA0 输出 T3PWM0
			;10 = PC1 输出T3PWM0		11 = PA4 输出 T3PWM0
BCR		RP1	;into bk1
	//duty4-pc0
	MOVIA	(100)/256
	MOVAR	T3D4H	;(bk2)
	MOVIA	(100)%256
	MOVAR	T3D4L	;(bk2)
	
	BSR		T3IE	;(bk1)
BCR		RP0	;into bk0
	BCR		T3IF	;(bk0)
	BSR		PEIE	;(bk0)
	;BSR		GIE		;(bk0)
	RETURN
	
;==========================================================================
;* @ brief:	-T3-2*2PWM模式-互补-Fcpu-with-PWM0输出全0
;* @ note: 2组PWM互补模式，时钟源选-Fcpu；PWM0输出全0
T3_22PWM_Compl_Fcpu_PWM0out0:
	BCR		T3IF	;(bk0)
BSR		RP1 ;into bk2
	MOVIA	(500)/256
	MOVAR	T3PH	;(bk2)
	MOVIA	(500)%256
	MOVAR	T3PL	;(bk2)
	
	//duty0-pa0
	MOVIA	(10)/256
	MOVAR	T3D0H	;(bk2)
	MOVIA	(10)%256
	MOVAR	T3D0L	;(bk2)

	//duty1-pa1
	MOVIA	(1)/256
	MOVAR	T3D1H	;(bk2)
	MOVIA	(1)%256
	MOVAR	T3D1L	;(bk2)
	
	//duty2-pa2
	MOVIA	(300)/256
	MOVAR	T3D2H	;(bk2)
	MOVIA	(300)%256
	MOVAR	T3D2L	;(bk2)
	
	//duty3-pa3
	MOVIA	(500)/256
	MOVAR	T3D3H	;(bk2)
	MOVIA	(500)%256
	MOVAR	T3D3L	;(bk2)
	
	MOVIA	0xC7
	MOVAR	T3C2	;00** *111(bk2)
		;bit7:PDTM2
			;0 = T3PWM3 & T3PWM2 工作与独立输出模式
			;1 = T3PWM3 & T3PWM2 工作与互补输出模式-√
		;bit6:PDTM0
			;0 = T3PWM1 & T3PWM0 工作与独立输出模式
			;1 = T3PWM1 & T3PWM0 工作与互补输出模式-√
		;bit5:T3PWM4INV 	0 = 不取反;			1 = 取反
		;bit4:T3PWM3INV  	0 = 不取反;			1 = 取反
		;bit3:T3PWM2INV  	0 = 不取反;			1 = 取反
		;bit2:T3PWM4EN		0 = 禁止 T3PWM4 输出;	1 = 允许 T3PWM4 输出 -√
		;bit1:T3PWM3EN		0 = 禁止 T3PWM3 输出;	1 = 允许 T3PWM3 输出 -√
		;bit0:T3PWM2EN		0 = 禁止 T3PWM2 输出;	1 = 允许 T3PWM2 输出 -√
	
	MOVIA	0x06
	MOVAR	T3C1	;-0-1 -111(bk2)
		;bit6:T3OUT0   		//0 = T3PWM 输出不受影响;	1 = T3PWM 输出全低电平 
		;bit4:T3CKS0 		//0 = Fcpu/2;				1 = Fosc
		;bit2~0:T3CK<2:0>
			;111 = 1分频;		110 = 2分频;-√		101 = 4分频;	100 = 8分频
			;011 = 16分频;		010 = 32分频;		001 = 64分频;	000 = 128分频
	
	MOVIA	0x86
	MOVAR	T3C0	;1-11 -11-(bk2)
		;bit7:T3REN  定时器总使能;			0 = 禁止;		1 = 使能-√
		;bit5:T3PWM1INV  T3PWM1输出极性;	0 = 不取反;		1 = 取反
		;bit4:T3PWM0INV  T3PWM0输出极性;	0 = 不取反;		1 = 取反
		;bit2:T3PWM1EN 输出使能;			0 = 禁止;		1 = 启动-√
		;bit1:T3PWM0EN 输出使能;			0 = 禁止;		1 = 启动-√
	
BSR		RP0	;into bk3
	MOVIA	0x01
	MOVAR	PWMSEL1	;---- **01(bk3)
		;bit1~0:T3PWM4S<1:0>
			;00 = 无					01 = PC0 输出 T3PWM4
			;10 = PC5 输出T3PWM4		11 = PA4 输出 T3PWM4	
	MOVIA	0x55
	MOVAR	PWMSEL	;0101 0101(bk3)
		;bit7~6:T3PWM3S<1:0>
			;00 = 无					01 = PA3 输出 T3PWM3
			;10 = PC4 输出T3PWM3		11 = PA7 输出 T3PWM3
		;bit5~4:T3PWM2S<1:0>
			;00 = 无					01 = PA2 输出 T3PWM2
			;10 = PC3 输出T3PWM2		11 = PA6 输出 T3PWM2
		;bit3~2:T3PWM1S<1:0>
			;00 = 无					01 = PA1 输出 T3PWM1
			;10 = PC2 输出T3PWM1		11 = PA5 输出 T3PWM1
		;bit1~0:T3PWM0S<1:0>
			;00 = PC4 输出T3PWM0		01 = PA0 输出 T3PWM0
			;10 = PC1 输出T3PWM0		11 = PA4 输出 T3PWM0
BCR		RP1	;into bk1
	//duty4-pc0
	MOVIA	(499)/256
	MOVAR	T3D4H	;(bk2)
	MOVIA	(499)%256
	MOVAR	T3D4L	;(bk2)
	
	BSR		T3IE	;(bk1)
BCR		RP0	;into bk0
	BCR		T3IF	;(bk0)
	BSR		PEIE	;(bk0)
	;BSR		GIE		;(bk0)
	RETURN
		
	
;==========================================================================
;主程序开始
;***********************************************************
;系统寄存器和GPIO配置
;注意寄存器所在 bank 区域
;***********************************************************
MAIN_SETUP:	
	CALL	SYSTEM_INIT
	CALL	GPIO_INIT
	CALL	CLR_RAM
	
	MOVIA	100
	CALL	DELAY_MS
	
	MOVIA	0x00	;选择 T3 工作模式
	MOVAR	Timer_MODE
	CALL	T3_INIT
	
	BCR		RP0		;转到bank0
	BCR		RP1
	
	
	;数码管显示 '168'
	MOVIA	1
	CALL	seg_tab
	MOVAR	seg_buf1
	
	MOVIA	6
	CALL	seg_tab
	MOVAR	seg_buf2
	
	MOVIA	8
	CALL	seg_tab
	MOVAR	seg_buf3
	
	
	GOTO	MAIN_LOOP
	
;***********************************************************
SYSTEM_INIT:
	BSR		RP0		;转到bank1
	CLRR	20H		;bank1-20H地址 RAM 清0
	MOVIA	000H		;-0-- ----
	MOVAR	OPTIONR		;OPTIONR(BANK1)
			;Bit6：INTEDG：外部中断触发方式选择位（PC1）;0-falling edge, 1-rising edge
			;Bitn：无关位仿真须写0
	
	MOVIA	0A0H		;101- ----   	
	MOVAR	PCON		;PCON(BANK1)(芯片控制寄存器)
			;Bit7：WDTE: WDT（看门狗定时器）使能位	;0-DIS, 1-EN
			;Bit5：LVREN: 低电压复位使能位			;0-DIS, 1-EN
			;Bitn：无关位仿真须写0
	
	MOVIA	000H		;-0-- ---0 
	MOVAR	PIE1		;PIE1(BANK1)
			;Bit6:ADCIE ADC中断使能位
			;Bit0:T1IE 定时器T1中断使能位 
			;Bitn：无关位仿真须写0
			
	MOVIA	000H		;---- -0-0
	MOVAR	PIE2		;PIE2(BANK1)
			;Bit1:T3IE 定时器T3中断使能位
			;Bit2:T4IE 定时器T4中断使能位
			;Bit5:C1IE 比较器C1中断使能位
			;Bitn：无关位仿真须写0
			
	BCR		RP0		;转到bank0
	
	MOVIA	000H		;00-0 0-00
	MOVAR	INTCON		;INTCON(BANK0)
			;bit7:GIE-全局中断使能位
			;bit6:PEIE-外设总中断使能位
			;bit4:INTIE-外部中断使能位(PC1)
			;bit3:PAIE-PORTA输入变化中断使能位
			;bit1:INTIF-外部中断标志位
			;bit0:PAIF-PORTA输入变化中断标志位	
			;Bitn：无关位仿真须写0
			
	MOVIA	000H		;---- -0-0
	MOVAR	PIR2		;PIR2(BANK0)
			;Bit1:T3IF-定时器T3溢出标志位,软件清0
			;Bit2:T4IF-定时器T4溢出标志位,软件清0
			;Bit5:C1IF-比较器C1输出变化时被置位,软件清0
			;Bitn：无关位仿真须写0
	
	MOVIA	000H		;-0-- ---0
	MOVAR	PIR1		;PIR1(BANK0)
			;Bit6:ADCIF-ADC转换完成标志位
			;Bit0:T1IF-T1溢出标志位 
			;Bitn：无关位仿真须写0
	RETURN
	
;***********************************************************
GPIO_INIT:	
	BSR		RP0		;转到bank1
	
	MOVIA	00H			;0000 0000 
	MOVAR	TRISA		;TRISA(BANK1)
			;PORTA输入输出配置寄存器 (0-OUT;1-IN) 
				
	MOVIA	04H			;--00 0100 
	MOVAR	TRISC		;TRISC(BANK1)
			;PORTC输入输出配置寄存器 (0-OUT;1-IN) 
				
	MOVIA	00H			;0000 0000 
	MOVAR	PAODCON		;PAODCON(BANK1)
			;开漏配置:1-EN;0-DIS
	
	MOVIA	0FFH		;1111 1111
	MOVAR	PAHCON		;PAHCON(BANK1)
			;内部上拉配置:1-DIS;0-EN
			
	MOVIA	0FFH		;1111 1111
	MOVAR	PADCON		;PADCON(BANK1)
			;内部下拉配置:1-DIS;0-EN
			
	MOVIA	00H			;0000 0000 
	MOVAR	WUACON		;WUACON(BANK1)
			;端口唤醒配置:1-EN;0=DIS

	BCR		RP0		;转到bank0

	CLRR	PORTA		;0000 0000	//端口输出赋值
	CLRR	PORTC		;--00 0000	//端口输出赋值
	
	MOVIA	0FFH		;--11 1111
	MOVAR	PCDCON		;PCDCON(BANK0)
			;内部下拉配置:1-DIS;0-EN
			
	MOVIA	0FFH		;--11 1111
	MOVAR	PCHCON		;PCHCON(BANK0)
			;内部上拉配置:1-DIS;0-EN
	
	RETURN
	
;***********************************************************
;函数名：清所有ram
;IRP:FSR7：00-BANK0 区间接寻址;01 = BANK1 区间接寻址
;***********************************************************
CLR_RAM:
	BCR		IRP
	MOVIA	0x5F  	;个数
	MOVAR	0x20  	;个数存放地址 
	MOVIA 	0x7F  	;REG从后往前 清理 
	MOVAR	FSR	
RAM_CLR_LOOP:	
	BSR		FSR,7	;bank1
	CLRR	INDF
	
	BCR		FSR,7	;bank0
	CLRR	INDF
	DECR	FSR,1
	DECRSZ	20H,1
	GOTO	RAM_CLR_LOOP	
	RETURN
	
;***********************************************************
;main
;***********************************************************
MAIN_LOOP:	
	CLRWDT	  		;清看门狗	
	
	MOVIA	200
	CALL	DELAY	
	
	; MOVIA	0X20 
	; XORAR	PORTA,R
	
	GOTO	MAIN_LOOP		;主函数循环	 


;***********************************************************



;***********************************************************
#define  TXD		PORTA,7
SEND_8BIT_RF:
	MOVAR	RF_DATA
	MOVIA	8
	MOVAR	RF_COUNT
SEND_8BIT_ADJ:
	BCR		TXD
	BTRSS	RF_DATA,7
	GOTO	SEND_BIT_0
	;GOTO	SEND_BIT_1
SEND_BIT_1:	
	BSR		TXD
	MOVIA	30
	CALL	DELAY
	BCR		TXD
	MOVIA	10
	CALL	DELAY
	GOTO	SEND_LOOP
SEND_BIT_0:
	BSR		TXD
	MOVIA	10
	CALL	DELAY
	BCR		TXD
	MOVIA	30
	CALL	DELAY
	;GOTO	SEND_LOOP
SEND_LOOP:
	RLR		RF_DATA,1
	DECRSZ	RF_COUNT,1
	GOTO	SEND_8BIT_ADJ
	RETURN
;***********************************************************
;函数名：DELAY
;传参：循环体调用次数
;返回值：
;功能：延时4个指令周期  Fcpu=4M时候延时1us，延时有误差
;引脚：
;备注：
;***********************************************************
DELAY:					
	MOVAR	DELAY_US	
DELAY_LOOP:	
	CLRWDT				;1;/清看门狗
	DECRSZ	DELAY_US,1	;1/(2)
	GOTO	DELAY_LOOP	;2	
	RETURN				;2	
	
	
	
;***********************************************************
;函数名：DELAY_MS
;传参：1-255ms
;返回值：
;功能：ms级延时
;引脚：
;备注：
;***********************************************************
DELAY_MS:					
	MOVAR	DELAY_MS_CNT		
DELAY_LOOP1:
	MOVIA	250
	CALL	DELAY
	CALL	DELAY
	CALL	DELAY
	CALL	DELAY
	DECRSZ	DELAY_MS_CNT,1	;1/(2)
	GOTO	DELAY_LOOP1	;2		
	RETURN				;2	
;***********************************************************
 