#include "include.h"
void System_Int(void)
{
	Ram_Int();  	 	///RAM清零  
	
	Gpio_Int();			//IO初始化
	
	Uart_Int();			//UART初始化
	
	OP_CaliBration();	//OP失调校准
	
	Op_Int();			//op初始化
}
void Op_Int(void)
{
	////CMP2NS[1:0]		OP-负端输入选择
  	////00 = 选择PC7  01 = 选择PB6 
  	////10 = 选择PA5  11 = 选择PB0  	
  		
//	CMP2NS1 = 1;
//	CMP2NS0 = 1;	//OP- 选择PB0	

	OPASW2 = 0;		//若使用放大一倍，需配置 OPASW2 = 0 和 OPASW1 = 1
	OPASW1 = 1;		//使用外部放大倍数，需配置 OPASW2 = 0 和 OPASW1 = 0	
	
	OPAOFM = 0;		//OP正常工作模式
	OPARSP = 0;    	

	///内部放大倍数  bit0-1  00  1倍   01  5倍  10  10倍  11 20倍
	///注意放大倍数以及电源电压
	OPA0C |= 0x00;

	OPCMP2EN = 1;	//使能OP
}
//////不建议更改以下函数 -----start-------
void OP_CaliBration(void) 
{
	//OP+及OPOUT端口需配置模拟输入模式
	ANSELC6 = 0;
	TRISC6 = 0;
	
	ANSELC4 = 0;
	TRISC4 = 0;
	
	OPCMPSW = 1;	///OP模式
	OPOUTEN = 1;	///OP输出使能  PC4
	
	////输入失调校准控制 	
	OPAOFM = 1;		//OP失调校准模式
	OPARSP = 1;		////OP+

	OPA0C |= 0x00;
	
	OPCMP2EN = 1;	//使能OP
	DelayUs(200);
	OP0VREFLAG = 1;
	OPAVOS &=~ 0x1F;   ///清零OPAOF[4:0]
	
	OP0AF_F = OPFMOUT;	
	while(OP0VREFLAG)
	{
		if(FLAG == 0)
		{
			OP1AF_F = OPFMOUT;
			if(OP0AF_F == OP1AF_F)
			{
				OP0CNT ++;
				if(OP0CNT > 32)
				{
					OP0CNT = 0;
				}	
				OPAVOS &=~ 0x1F;   ///清零OPAOF[4:0]
				OPAVOS |= OP0CNT;
				DelayUs(10);
			}
			else
			{
				aaa = OP0CNT;
				FLAG = 1;			
				
				OPAVOS &=~ 0x1F;   ///清零OPAOF[4:0]
				OPAVOS |= 0x1F;
				
				OP0COT = 31;
				OP0AF_F = OPFMOUT;
				DelayUs(10);
			}
		}
		
		if(FLAG == 1)
		{
			OP1AF_F = OPFMOUT;
			DelayUs(10);
			
			if(OP0AF_F == OP1AF_F)
			{
				OP0COT --;
				if(OP0COT <= 0)
				{
					OP0COT = 31;
				}	
				OPAVOS &=~ 0x1F;   ///清零OPAOF[4:0]
				OPAVOS |= OP0COT;
				DelayUs(10);
			}
			else
			{
				bbb = OP0COT;
				FLAG = 2;
				OPACNT = (aaa + bbb) / 2;
				OP0VREFLAG = 0;
			}	
		}
	}

	OPAVOS &=~ 0x1F;   ///清零OPAOF[4:0]	
	OPAVOS |= OPACNT;   ///校准写入 写入后不允许更改此寄存器，否则OP-of异常

	DelayUs(200);		
}//////////////////////  -----end---------

void Gpio_Int(void)
{
	////数字模拟模式：   1：数字    0：模拟
	ANSELA = 0x00;
	ANSELB = 0x00;
	ANSELC = 0x00;
	
	////输入输出模式：   1：输出    0：输入
	TRISA = 0x00;
	TRISB = 0x00;
	TRISC = 0x00;
	
	////IO数据寄存器：   1：高电平    0：低电平
	PORTA = 0x00;
	PORTB = 0x00;
	PORTC = 0x00;
	
	////输入上拉控制：   1：使能上拉   0：关闭上拉
	WPUA = 0x00;
	WPUB = 0x00;
	WPUC = 0x00;
	
	////输入下拉控制：   1：使能下拉   0：关闭下拉
	WPDA = 0x00;
	WPDB = 0x00;
	WPDC = 0x00;
	
	////GPIO唤醒控制：   1：使能唤醒   0：关闭唤醒
	IOCA = 0x00;
	IOCB = 0x00;
	IOCC = 0x00;	
}

void Ram_Int(void)
{
	IRP = 0;
	FSR = 0x60;
	do{
		INDF = 0;
		FSR++;
	}while(FSR != 0x80);//BANK0 60H~7FH 地址清0
      
	FSR = 0xD0;
	do{
		INDF = 0;
		FSR++;
	}while(FSR != 0xF0);//BANK1 D0H~EFH 地址清0
	
	IRP = 1;
	FSR = 0x10;
	do{
		INDF = 0;
		FSR++;
	}while(FSR != 0x70);//BANK2 110H~16FH 地址清0
      
	FSR = 0x90;
	do{
		INDF = 0;
		FSR++;
	}while(FSR != 0xF0);//BANK3 190H~1EFH 地址清0

	IRP = 0;
}

void DelayUs(unsigned char t)
{   
	while(--t);
}

void DelayMs(unsigned int t)
{ 
	unsigned char count = 195;	   
	while(t--)
	{
		while(count--);
	}
}

void Uart_Int(void)
{
	ANSELC0   = 1;
	TRISC0    = 1;

	UART_MAP  = 0x22;   //TX PC0  RX PB5
	
	S1CON     = 0;
	
	BRTL      = 0xE6;
	BRTH      = 0xFF;  ///9600波特率   8M 2T  CPU 4M	
	S1CON2    = 0x20;
	
	TXIF = 0;
	UARTEN = 1;
}
