//*********************************************************************** 
//Copyright (c) 2024   HuaXin Micro-electronics Co.,Ltd 

//HS16F1211
//COMPCON
//configuration introduction

//designer:LvBuWei
//updata：20240612

//*********************************************************************** 
//Files include 
#include 	"hs16f1211.h" 
#include 	"main.h"		


//****main function****//
void main(void) 
{ 
	GPIO_Init(); 
	CMPCON_Init(MODE1);
	//CMPCON_Init(MODE2);
	//CMPCON_Init(MODE3);
	
	while(1) 
	{ 
		CLRWDT 
		PORTA = PORTA^0xff;	
		
		DelayUs(100);		
	} 
} 
 
 
//****interrupt function****//
void interrupt myIsr(void)    
{ 
	if(FT0IF) 
	{ 
		FT0IF = 0; 
	} 
} 


//****CMPCON configuration****//
/*
CMPCON'+'positive choose：External base input "PB0" or internal base ；
CMPCON'-'negative choose: VDD、PB4、PB6、PB1；
*/
/*
program_1：CMPCON'-'negative CONFIG{VDD}；CMPCON'+'positive CONFIG internal base；
program_2：CMPCON'-'negative CONFIG{PIN}；CMPCON'+'positive CONFIG internal base；
program_3：CMPCON'-'negative CONFIG{PIN}；CMPCON'+'positive CONFIG External base input "PB0"；
*/
void CMPCON_Init(unsigned char mode)
{
	if(mode==MODE1)		//program_1：CMPCON'-'negative config[VDD]D；CMPCON'+'positive config internal base ;
	{
		CMPM = 0x10;	//0001 0000 //CMPCON'-'negative CONFIG[VDD]；XRVSEL=1；VRS<3:0>=0；(voltage of cmopare:4.69);
						//;Bit7:XRVSEL1 比较器+端-基准选择
						//;Bit6~Bit5：CPIINS[1:0]: 比较器负端输入选择;	00=VDD;01=PB4;10=PB6;11=PB1
						//;Bit4：XRVSEL 比较电压点档位选择
						//;Bit3~Bit0：负端电压档位选择
						
		CMPCON= 0xD4;	//1*11 010- //System clock；3-clock delay；'+'positive config internal base 1.25V；polarity not contrary；
						//;Bit7：CMPEN: CMP 使能位
						//;Bit6：COMIF: 比较器转换标志
						//;Bit5：FTCKS: 输出使能滤波时钟选择
						//;Bit4：CODEB: 输出滤波
						//;Bit3：CPEN+: 比较器+端
						//;Bit2：CVEN+: 比较器+端
						//;Bit1：COPOL: 比较器输出极性选择
	
	}
	else if(mode == MODE2)//program_2：CMPCON'-'negative config[pin]；CMPCON'+'positive config internal base；
	{
		CMPM = 0x70;	//0111 0000 //CMPCON'-'negative CONFIG[PB1]；XRVSEL=1；VRS<3:0>=0；(voltage of cmopare:4.69)
						//;Bit7:XRVSEL1 比较器+端-基准选择
						//;Bit6~Bit5：CPIINS[1:0]: 比较器负端输入选择;	00=VDD;01=PB4;10=PB6;11=PB1
						//;Bit4：XRVSEL 比较电压点档位选择
						//;Bit3~Bit0：负端电压档位选择
						
		CMPCON= 0xD4;	//1*11 010- //System clock；3-clock delay；'+'positive config internal base 1.25V；polarity not contrary；
						//;Bit7：CMPEN: CMP 使能位
						//;Bit6：COMIF: 比较器转换标志
						//;Bit5：FTCKS: 输出使能滤波时钟选择
						//;Bit4：CODEB: 输出滤波
						//;Bit3：CPEN+: 比较器+端
						//;Bit2：CVEN+: 比较器+端
						//;Bit1：COPOL: 比较器输出极性选择	
	}
	else if(mode == MODE3)//program_3：CMPCON'-'negative config[pin]；CMPCON'+'positive config External base input "PB0"；
	{
		CMPM = 0x70;	//0111 0000 //CMPCON'-'negative CONFIG[PB1]；XRVSEL=1；VRS<3:0>=0；(voltage of cmopare：Calculated from external base PB0)
						//;Bit7:XRVSEL1 比较器+端-基准选择
						//;Bit6~Bit5：CPIINS[1:0]: 比较器负端输入选择;	00=VDD;01=PB4;10=PB6;11=PB1
						//;Bit4：XRVSEL 比较电压点档位选择
						//;Bit3~Bit0：负端电压档位选择
						
		CMPCON= 0xD8;	//1*11 100- //System clock；3-clock delay；+positive config External base(PB0)；polarity not contrary；
						//;Bit7：CMPEN: CMP 使能位
						//;Bit6：COMIF: 比较器转换标志
						//;Bit5：FTCKS: 输出使能滤波时钟选择
						//;Bit4：CODEB: 输出滤波
						//;Bit3：CPEN+: 比较器+端
						//;Bit2：CVEN+: 比较器+端
						//;Bit1：COPOL: 比较器输出极性选择	
	}
	
	FCOMINTF = 0;	//CMPCON overflow flag
	FCOMINTEN = 1; 	//CMPCON overflow enable bit(1=EN,0=DIS)
		
	FGIE  = 1;		//【INTEN.bit7】GIE:global interrupt enable bit  
}



//****GPIO configuration****//
void GPIO_Init(void) 
{
	OPTION= 0b000000000;//0000 0000	
			//;Bit7：INT1EDG：外部中断1触发方式选择位（PB1）;0-falling edge, 1-rising edge
			//;Bit6：INTEDG：外部中断触发方式选择位（PB0）	;0-falling edge, 1-rising edge
			//;Bit5：T0CS：TMR0时钟源选择位					;0-fcpu, 1-ExitClock
			//;Bit4：T0SE：TMR0时钟源边沿选择位				;0-falling edge, 1-rising edge
			//;Bit3：PSA：预分频分配位						;0-TMR0, 1-WDT
			//;Bit2~Bit0：PS2:PS0：预分频比率选择位

	PCON  = 0b00100000;	//101- ----		
			//;Bit7：WDTE: WDT（看门狗定时器）使能位	;0-DIS, 1-EN
			//;Bit6：EIS: IOB0/INT引脚定义位			;0-DIS, 1-EN
			//;Bit5：LVREN: 低电压复位使能位			;0-DIS, 1-EN
					
//PORTA //----------------------------------------------------------------------
	PORTA = 0b00000000;	
	TRISA = 0b00000000;	//(0-OUT;1-IN) 		
	PAHCON= 0b11111111;	//--11 1111 :1-DIS;0-EN 
	PADCON= 0b11111111;	//--11 ---- :1-DIS;0-EN 	
	PAIC0 = 0b00000000;	//--00 0000 :1-EN;0-DIS
	
	PDCON = 0b11111111;	//0000 0000 :1-DIS;0-EN[pdb3/pdb2/pdb1/pdb0/ /pda3/pda2/pda1/pda0]
	PDCON2= 0b11111111;	//--00 0000 :1-DIS;0-EN[-/-/pda5/pda4/ /pdb7/pdb6/pdb5/pdb4]
//PORTB //----------------------------------------------------------------------
	PORTB = 0b00000000;	
	TRISB = 0b00000000;	//(0-OUT;1-IN) 		
	PBIC0 = 0b00000000;	//1111 1111 :1-EN;0-DIS	
	WUCON = 0b00000000;	//0000 0000	:1-EN;0=DIS 
	ODCON = 0b00000000;	//1111 -111 :1-EN;0-DIS 	
	PHCON = 0b11111111;	//1111 1111 :1-DIS;0-EN 
	//----------------------------------------------------------------------
}



//****************************************************  
//CLR_BANK0_RAM: 
//C中嵌入ASM，指令操作特殊寄存器前面需要加下划线“_”;
//****************************************************    
void CLR_BANK0_RAM(void)
{
#asm  
	MOVLW	0X5F
	MOVWF	020H
	MOVLW	0X7F
	MOVWF	_FSR
	
BANK0_RAM_CLR_LOOP:	
	CLRF	_INDF
	DECF	_FSR,F
	DECFSZ	020H,F
	GOTO	BANK0_RAM_CLR_LOOP		
#endasm	   
}


//****************************************************  
//CLR_BANK1_RAM: 
//C中嵌入ASM，指令操作特殊寄存器前面需要加下划线“_”;
//****************************************************    
void CLR_BANK1_RAM(void)
{
#asm  	
	MOVLW	(0x80 | 03FH)
	MOVWF	_FSR
	MOVLW	020H	//;32*reg-bank1
	MOVWF	020H
	
BANK1_RAM_CLR_LOOP:	
	CLRF	_INDF
	DECF	_FSR,F
	DECFSZ	020H,F
	GOTO	BANK1_RAM_CLR_LOOP	
	RETURN	
#endasm	   
}


//****delay function****// 
//;Fcpu=1MHz;DelayUs = 11+7x     
void DelayUs(unsigned char delayUs)     
{      
	while(delayUs--);    
} 
