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

//HS23F1820
//CMPCON
//configuration introduction

//designer:LvBuWei
//updata：20210909

//*********************************************************************** 
#include 	"hs23p1820.h" 
#include 	"main.h"		



//****interrupt function****//
void interrupt myIsr(void)    
{ 
	if(FCOMINTF) 
	{ 
		FCOMINTF = 0; 
		PB = PB^0x04;
		if(CMPIF)	PB5 = 1;
		else		PB5 = 0;
	} 
} 



//****main function****//
void main(void) 
{ 
	GPIO_Init(); 
	CLR_BANK0_RAM();
	
	CMPCON_Init(MODE1);
	//CMPCON_Init(MODE2);
	//CMPCON_Init(MODE3);
	
	while(1) 
	{ 
		CLRWDT  
		PORTB = PORTB^0x20;	
		
		DelayUs(100);
		
	} 
} 
 
 

//****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;	//-001 0000 //CMPCON'-'negative CONFIG[VDD]；XRVSEL=1；VRS<3:0>=0；(voltage of cmopare:4.69);
						//;Bit6~Bit5：CPIINS[1:0]: 比较器负端输入选择
						//;Bit4：XRVSEL 比较电压点档位选择
						//;Bit3~Bit0：负端电压档位选择
		CMPCON= 0xD4;	//1*11 010- //System clock；3-clock delay；'+'positive config internal base 1.25V；polarity not contrary；
						//;/bit1:COPOL	比较器输出极性选择	
						//;/bit2:CVEN		比较器+端
						//;/bit3:CPEN		比较器+端
						//;/bit4:CODEB	输出滤波	
						//;/bit5:FTCKS	输出使能滤波时钟选择	
						//;/bit6:CMPIF	比较器转换标志	
						//;/bit7:CMPEN	CMP 使能位
	}
	else if(mode == MODE2)//program_2：CMPCON'-'negative config[pin]；CMPCON'+'positive config internal base；
	{
		CMPM = 0x70;	//-111 0000 //CMPCON'-'negative CONFIG[PB1]；XRVSEL=1；VRS<3:0>=0；(voltage of cmopare:4.69)
						//;Bit6~Bit5：CPIINS[1:0]: 比较器负端输入选择
						//;Bit4：XRVSEL 比较电压点档位选择
						//;Bit3~Bit0：负端电压档位选择
		CMPCON= 0xD4;	//1*11 010- //System clock；3-clock delay；'+'positive config internal base 1.25V；polarity not contrary；
						//;/bit1:COPOL	比较器输出极性选择	
						//;/bit2:CVEN		比较器+端
						//;/bit3:CPEN		比较器+端
						//;/bit4:CODEB	输出滤波	
						//;/bit5:FTCKS	输出使能滤波时钟选择	
						//;/bit6:CMPIF	比较器转换标志	
						//;/bit7:CMPEN	CMP 使能位
	}
	else if(mode == MODE3)//program_3：CMPCON'-'negative config[pin]；CMPCON'+'positive config External base input "PB0"；
	{
		CMPM = 0x70;	//-111 0000 //CMPCON'-'negative CONFIG[PB1]；XRVSEL=1；VRS<3:0>=0；(voltage of cmopare：Calculated from external base PB0)
						//;Bit6~Bit5：CPIINS[1:0]: 比较器负端输入选择
						//;Bit4：XRVSEL 比较电压点档位选择
						//;Bit3~Bit0：负端电压档位选择
		CMPCON= 0xD8;	//1*11 100- //System clock；3-clock delay；+positive config External base(PB0)；polarity not contrary
						//;/bit1:COPOL	比较器输出极性选择	
						//;/bit2:CVEN		比较器+端
						//;/bit3:CPEN		比较器+端
						//;/bit4:CODEB	输出滤波	
						//;/bit5:FTCKS	输出使能滤波时钟选择	
						//;/bit6:CMPIF	比较器转换标志	
						//;/bit7:CMPEN	CMP 使能位；
	}
	
	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;//00-- -000	
						//;Bit7：INT1EDG：外部中断1触发方式选择位（PB1）;0-falling edge, 1-rising edge
						//;Bit6：INTEDG：外部中断触发方式选择位（PB0）	;0-falling edge, 1-rising edge			
						//;Bit2~Bit0：PS2:PS0：预分频比率选择位

	PCON  = 0b10100000;	//101- ----		
						//;Bit7：WDTE: WDT（看门狗定时器）使能位	;0-DIS, 1-EN
						//;Bit6：EIS: IOB0/INT引脚定义位			;0-DIS, 1-EN
						//;Bit5：LVREN: 低电压复位使能位			;0-DIS, 1-EN
			
//PORTB //----------------------------------------------------------------------

	PORTB = 0b00000000;	

	TRISB = 0b00000000;	//(0-OUT;1-IN) 	
	
	PBIC0 = 0b00000000;	//--11 1111 :1-EN;0-DIS
	
	WUCON = 0b00000000;	//--00 0000	:1-EN;0=DIS 

	PDCON = 0b00000000;	//1111 ---- :1-DIS;0-EN
	
	ODCON = 0b00000000;	//--11 -111 :1-EN;0-DIS

	PHCON = 0b11111111;	//--11 1111 :1-DIS;0-EN 	
	
	PORTB = 0b00000000;	
	 //----------------------------------------------------------------------
} 



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

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