#include "OB6710.h"
#include "ACMP0.h"

extern void ACMP0_Test_Channel(void);
extern bit g_Acmp0Interrupt;
extern void ACMP0_Set_Input(unsigned char pChannel, unsigned char nChannel, unsigned char Level);
extern void ACMP0_Test_Interrupt(void);
extern void ACMP0_Test_Debounce(void);
extern void ACMP0_Test_Interrup_Stop(void);

sbit CMP0_P0      = P3^0;
sbit CMP0_P1      = P3^1;
sbit CMP0_P2      = P3^2;
sbit CMP0_N0      = P0^3;
sbit CMP0_OUt      = P0^2;

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

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

void main(void)
{
  ACMP0_Test_Interrup_Stop();
	ACMP0_Test_Channel();
	ACMP0_Test_Interrupt();
	ACMP0_Test_Debounce();
	while(1);
}

void ACMP0_Test_Channel(void)
{
	//P3.0~P3.2 <-> P0.0~P0.2
	P3M0 = 0x00;
	P3M1 = 0x00;	
	P0M0 = 0x00;
	P0M1 = 0xff;
	ACMPHYS = 0x11;
	
	for(P_Channel = 0x00;P_Channel<3;P_Channel++)
	{
		CMP0_P0 = 0x00;
		CMP0_P1 = 0x00;
		CMP0_P2 = 0x00; 	
		CMP0_N0 = 0x00;
		//------------------------------------
		ACMP0_Open(P_Channel, d_ACMP0_N_CHANNEL_0,  d_HYS_RANGE_30);	
		for(Loop = 0x00;Loop<100;Loop++)
		{
			ACMP0_Set_Input(P_Channel, 0x00, 1);
			Delay(100);
			Buff = ACMP0_OutputStatus();
			if(Buff != 0x01)
				while(1);
			//-----------------------------
			ACMP0_Set_Input(P_Channel, 0x00, 0);
			Delay(100);
			Buff = ACMP0_OutputStatus();
			if(Buff != 0x00)
				while(1);
		}	
	}	
}
	
void ACMP0_Set_Input(unsigned char pChannel, unsigned char nChannel, unsigned char Level)
{
   if(Level == 0x01)
	 {
			if(pChannel == 0x00)
				CMP0_P0 = 0x01;
			if(pChannel == 0x01)
				CMP0_P1 = 0x01;
			if(pChannel == 0x02)
				CMP0_P2 = 0x01;
	
			if(nChannel == 0x00)
				CMP0_N0 = 0x00;
	 }
    else
		{
		
			if(pChannel == 0x00)
				CMP0_P0 = 0x00;
			if(pChannel == 0x01)
				CMP0_P1 = 0x00;
			if(pChannel == 0x02)
				CMP0_P2 = 0x00;
	
			if(nChannel == 0x00)
				CMP0_N0 = 0x01;		
		}
}

void ACMP0_Test_Interrupt(void)
{
	P0M0 = 0x00;
	P0M1 = 0x00;
	P2M0 = 0x00;
	P2M1 = 0x00;
	IEN2 = IEN2 | 0x04;
	EA = 1;
	ACMP0_DebounceCount(100);
	
	for(Mode = 0x00;Mode<3;Mode++)
	{
		CMP0_P0 = 0x00;
		CMP0_P1 = 0x00;
		CMP0_P2 = 0x00; 	
		CMP0_N0 = 0x01;
		ACMP0OUTPUT = (ACMP0OUTPUT& 0x9F) | (Mode<<0x05);
		//------------------------------------
		ACMP0_Open(0x00, 0x00,  d_HYS_RANGE_30);	
		for(Loop = 0x00;Loop<100;Loop++)
		{
			g_Acmp0Interrupt = 0x00;
			ACMP0_Set_Input(0x00, 0x00, 1);
			Delay(100);
			if( (Mode == 0x00) || (Mode == 0x02))
			{
				if(g_Acmp0Interrupt == 0x00)
					while(1);
			}
			else
			{
				if(g_Acmp0Interrupt != 0x00)
					while(1);			
			}	
			//-----------------------------
			g_Acmp0Interrupt = 0x00;
			ACMP0_Set_Input(0x00, 0x00, 0);
			Delay(100);
			if( (Mode == 0x00) || (Mode == 0x01))
			{
				if(g_Acmp0Interrupt == 0x00)
					while(1);
			}
			else
			{
				if(g_Acmp0Interrupt != 0x00)
					while(1);			
			}
		}
	}
}

void ACMP0_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++)
	{
		CMP0_P0 = 0x00;
		CMP0_P1 = 0x00;
		CMP0_P2 = 0x00; 	
		CMP0_N0 = 0x01;
	
		if(Debounce == 0x00)
			ACMP0DBCNT = 0x00;
		else
			ACMP0DBCNT = 0xFF;
			
	//------------------------------------
		ACMP0_Open(P_Channel, N_Channel,  d_HYS_RANGE_30);	
		for(Loop = 0x00;Loop<100;Loop++)
		{
			Delay(200);
			CMP0_P0 = 0x01;
			CMP0_N0 = 0x00;				
			Delay(10);
			Buff = (ACMP0OUTPUT >> 0x04) & 0x01;
			CMP0_P0 = 0x00;
			CMP0_N0 = 0x01;			
			
			if(Debounce == 0x00)
			{
				if(Buff != 0x01)
					while(1);
			}
			else
			{
				if(Buff != 0x00)
					while(1);
			}
		}
	}
}

void ACMP0_Test_Interrup_Stop(void)
{
	P0M0 = 0x00;
	P0M1 = 0x00;
	P2M0 = 0x00;
	P2M1 = 0x00;
  P0 = 0xFF;
	IEN2 = IEN2 | 0x04;
	EA = 1;
	Mode = 0x00;
	CMP0_P0 = 0x00;
	CMP0_P1 = 0x00;
	CMP0_P2 = 0x00; 	
	CMP0_N0 = 0x00;
	ACMP0OUTPUT = (ACMP0OUTPUT& 0x9F) | (Mode<<0x05);
	//------------------------------------
	ACMP0_Open(0x00, 0x00,  d_HYS_RANGE_30);	
	PCON = PCON | 0x02;	
	while(1)
	{
		CMP0_OUt = (ACMP0OUTPUT >> 0x04) & 0x01;
	}      
}

