#include "OB6710.h"
#include "PWM.h"
#include "ACMP2.h"
#include "ACMP1.h"

extern bit g_PwmInterrupt;

extern unsigned int PWM_DUTY0  ;      //PWM0 Duty
extern unsigned int PWM_DUTY1  ;      //PWM1 Duty
extern unsigned int PWM_DUTY2  ;      //PWM2 Duty

extern void Delay (unsigned int nDelay);
extern void Test_Fault_A_ACMP1(void);
extern void Test_Fault_A_FLT(void);	
extern void Set_Fault_A_Pin(unsigned char Level);
extern void Test_Fault_B(void);

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

void main(void)
{
	P0M0 = 0x00;
	P0M1 = 0x00;
	P1M0 = 0x00;
	P1M1 = 0x02;
	P2M0 = 0x00;
	P2M1 = 0x00;
	P3M0 = 0x00;
	P3M1 = 0x00;

	PWM_Duty(PWM_DUTY0,PWM_DUTY1, PWM_DUTY2);
	PWM_Init();

	Test_Fault_A_ACMP1();
	Test_Fault_A_FLT();
  Test_Fault_B();
	while(1);

}

void Test_Fault_B(void)
{
  unsigned char Loop ;
	unsigned int Counter;
  OPACON = 0x01;
	ACMP2_Open(0, 0);
  ACMP2_Set_Input(0);
  ACMP2DBCNT = 0xFF;
	for(Loop = 0x00;Loop<0x02;Loop++)
	{
		FLTCONFIG = 0x00;  
    FLTNF = 0;
		if(Loop == 0x00)
		{
			FLTNF = 0x00; //Active Low	
			ACMP2_Set_Input(1);
		}
    else
		{
 	  	FLTNF = 0x20; //Active High	
			ACMP2_Set_Input(0);	
		}
	  Delay(200);
		FLTCONFIG = 0x10; //ENABLE  
    Counter = 2000;
		while(Counter--)
		{
		    if( (FLTCONFIG & 0x40) == 0x40)
					while(1);
		}
				
		if(Loop == 0x00)
			ACMP2_Set_Input(0);
    else
			ACMP2_Set_Input(1);	
		Delay(200);	
    Counter = 2000;
		while(Counter--)
		{
		    if( (FLTCONFIG & 0x40) == 0x00)
					while(1);		
		}
	}
	OPACON = 0x00;
	ACMP2CON= 0x00;
}

void Test_Fault_A_ACMP1(void)
{
  unsigned char Level , Mode;
	unsigned int Counter;
	ACMP1_Open(0, 0, d_HYS_RANGE_10);
  ACMP1_Set_Input(0x00, 0x00, 0);
  ACMP1DBCNT = 0xFF;
	for(Mode = 0x00;Mode<4;Mode++)
	{
		for(Level = 0x00;Level<0x02;Level++)
		{
			FLTCONFIG = 0x00;  

			if(Level == 0x00)
			{
				FLTNF = 0x00 | Mode; //Active Low	
				ACMP1_Set_Input(0x00, 0x00, 1);
				Set_Fault_A_Pin(1);
			}
			else
			{
				FLTNF = 0x10| Mode; //Active High	
				ACMP1_Set_Input(0x00, 0x00, 0);
      	Set_Fault_A_Pin(0);				
			}
			Delay(200);
			FLTCONFIG = 0x01; //ENABLE  
			Counter = 2000;
			while(Counter--)
			{
		    if( (FLTCONFIG & 0x04) == 0x04)
					while(1);
			}			
			if(Level == 0x00)
				ACMP1_Set_Input(0x00, 0x00, 0);
			else
				ACMP1_Set_Input(0x00, 0x00, 1);	
			Delay(200);	
			Counter = 2000;
			while(Counter--)
			{
		    if( (Mode == 0x01) || (Mode == 0x02))
				{
					if( (FLTCONFIG & 0x04) == 0x00)
						while(1);
				}					
				else
				{
					if( (FLTCONFIG & 0x04) == 0x04)
						while(1);
				}	
			}
		}
	}
}

void Test_Fault_A_FLT(void)
{
	unsigned char Level , Mode;
	unsigned int Counter;
	ACMP1_Open(0, 0, d_HYS_RANGE_10);
  ACMP1_Set_Input(0x00, 0x00, 0);
  ACMP1DBCNT = 0xFF;
	for(Mode = 0x00;Mode<4;Mode++)
	{
		for(Level = 0x00;Level<0x02;Level++)
		{
			FLTCONFIG = 0x00;  

			if(Level == 0x00)
			{
				FLTNF = 0x00 | Mode; //Active Low	
				ACMP1_Set_Input(0x00, 0x00, 1);
				Set_Fault_A_Pin(1);
			}
			else
			{
				FLTNF = 0x10| Mode; //Active High	
				ACMP1_Set_Input(0x00, 0x00, 0);
      	Set_Fault_A_Pin(0);				
			}
			Delay(200);
			FLTCONFIG = 0x01; //ENABLE  
			Counter = 2000;
			while(Counter--)
			{
		    if( (FLTCONFIG & 0x04) == 0x04)
					while(1);
			}
			if(Level == 0x00)
				Set_Fault_A_Pin(0);
			else
				Set_Fault_A_Pin(1);	
			Delay(200);	
			Counter = 2000;
			while(Counter--)
			{
		    if( (Mode == 0x00) || (Mode == 0x02) || (Mode == 0x03))
				{
					if( (FLTCONFIG & 0x04) == 0x00)
						while(1);
				}					
				else
				{
					if( (FLTCONFIG & 0x04) == 0x04)
						while(1);
				}	
			}
		}
	}
}

void Set_Fault_A_Pin(unsigned char Level)
{
	P3_6 = Level;
}