#include "OB6710.h"
#include "acmp1.h"

extern void ACMP1_Tes_Hys(void);
extern void ACMP1_Test_Channel(void);
extern bit g_Acmp1Interrupt;
extern void ACMP1_Set_Input(unsigned char pChannel, unsigned char nChannel, unsigned char Level);
extern void ACMP1_Test_Interrupt(void);
extern void ACMP1_Test_Debounce(void);

unsigned char Loop, Buff, Mode, P_Channel, N_Channel, Debounce;

void Delay(unsigned int nDelay)
{
   while(nDelay--);
}

void main(void)
{
	ACMP1_Tes_Hys();
  ACMP1_Test_Channel();
	ACMP1_Test_Interrupt();
	ACMP1_Test_Debounce();
	while(1);
}

void ACMP1_Test_Channel(void)
{
//P3.0~P3.2 <-> P0.0~P0.2
	P3M0 = 0x07;
	P3M1 = 0x00;	
	P0M0 = 0x00;
	P0M1 = 0xff;
	P1M0 = 0x00;
	P1M1 = 0x03;
	ACMPHYS = 0x00;
	
	for(P_Channel = 0x00;P_Channel<2;P_Channel++)
	{
		CMP1_P0 = 0x00;
		CMP1_P1 = 0x00;
		CMP1_N0 = 0x00;
		//------------------------------------
		ACMP1_Open(P_Channel, N_Channel,  d_HYS_RANGE_30);	
		for(Loop = 0x00;Loop<100;Loop++)
		{
			ACMP1_Set_Input(P_Channel, N_Channel, 1);
			Delay(100);
			Buff = ACMP1_OutputStatus();
			if(Buff != 0x01)
				while(1);
			//-----------------------------
			ACMP1_Set_Input(P_Channel, N_Channel, 0);
			Delay(100);
			Buff = ACMP1_OutputStatus();
			if(Buff != 0x00)
				while(1);
		}
	}
}

void ACMP1_Tes_Hys(void)
{
//P3.0~P3.2 <-> P0.0~P0.2
	P2M0 = 0x00;
	P2M1 = 0x00;	

	P3M0 = 0x07;
	P3M1 = 0x00;	
	P0M0 = 0x00;
	P0M1 = 0xff;
	P1M0 = 0x00;
	P1M1 = 0x03;
	ACMPHYS = 0x00;
  ACMP1CON = 0x01	;
    
  while(1)
  {
		Buff = ACMP1_OutputStatus();
		if(Buff != 0x00)
			P2_6 = 1;
		else
			P2_6 = 0;
	}
}
void ACMP1_Test_Interrupt(void)
{
	P0M0 = 0x00;
	P0M1 = 0x00;
	P2M0 = 0x00;
	P2M1 = 0x00;
	IEN2 = IEN2 | 0x08;
	EA = 1;
	ACMP1_DebounceCount(100);
	
	for(Mode = 0x00;Mode<3;Mode++)
	{
		CMP1_P0 = 0x00;
		CMP1_P1 = 0x00;	
		CMP1_N0 = 0x01;
		ACMP1OUTPUT = (ACMP1OUTPUT& 0x9F) | (Mode<<0x05);
		//------------------------------------
		ACMP1_Open(0x00, 0x00,  d_HYS_RANGE_30);	
		for(Loop = 0x00;Loop<100;Loop++)
		{
			g_Acmp1Interrupt = 0x00;
			ACMP1_Set_Input(0x00, 0x00, 1);
			Delay(100);
			if( (Mode == 0x00) || (Mode == 0x02))
			{
				if(g_Acmp1Interrupt == 0x00)
					while(1);
			}
			else
			{
				if(g_Acmp1Interrupt != 0x00)
					while(1);			
			}	
			//-----------------------------
			g_Acmp1Interrupt = 0x00;
			ACMP1_Set_Input(0x00, 0x00, 0);
			Delay(100);
			if( (Mode == 0x00) || (Mode == 0x01))
			{
				if(g_Acmp1Interrupt == 0x00)
					while(1);
			}
			else
			{
				if(g_Acmp1Interrupt != 0x00)
					while(1);			
			}
		}
	}
}

void ACMP1_Test_Debounce(void)
{
	P0M0 = 0x00;
	P0M1 = 0x00;
	P1M0 = 0x00;
	P1M1 = 0x00;
	P2M0 = 0x00;
	P2M1 = 0x00;
	P_Channel = 0x00;
	N_Channel  = 0x00;
	for(Debounce = 0x00;Debounce<2;Debounce++)
	{
		CMP1_P0 = 0x00;
		CMP1_P1 = 0x00;
		CMP1_N0 = 0x01;
	
		if(Debounce == 0x00)
			ACMP1DBCNT = 0x00;
		else
			ACMP1DBCNT = 0xFF;
			
	//------------------------------------
		ACMP1_Open(P_Channel, N_Channel,  d_HYS_RANGE_30);	
		for(Loop = 0x00;Loop<100;Loop++)
		{
			Delay(200);
			CMP1_P0 = 0x01;
			CMP1_N0 = 0x00;				
			Delay(10);
			Buff = (ACMP1OUTPUT >> 0x04) & 0x01;
			CMP1_P0 = 0x00;
			CMP1_N0 = 0x01;			
			
			if(Debounce == 0x00)
			{
				if(Buff != 0x01)
					while(1);
			}
			else
			{
				if(Buff != 0x00)
					while(1);
			}
		}
	}	
}