#include "OB6710.h"
#include "ACMP2.h"

bit g_Acmp2Interrupt = 0;

void ACMP2_ISR(void) interrupt d_ACMP2_Vector
{	 
	ACMP2OUTPUT &= 0x7F;	// 0111_1111 clear ACMP1 INT Flag
	g_Acmp2Interrupt = 0x01;
}

void ACMP2_DisableIE(void)
{
	IEN1 = IEN1 &  0xF7;			
	ACMP2OUTPUT = ACMP2OUTPUT & 0x7F;
}

void ACMP2_Open(unsigned char nPSEL, unsigned char nNSEL)
{
	ACMP2CON = 0x00;
	ACMP2CON = (nNSEL << 4) | (nPSEL << 1);
  ACMP2CON = ACMP2CON | 0x01;
}

void ACMP2_Close(void)
{
	ACMP2CON = 0x00;
}

void ACMP2_DebounceCount(unsigned char nDebounceCount)
{
	ACMP2DBCNT = nDebounceCount;
}

unsigned char ACMP2_OutputStatus(void)
{
  unsigned char Buff;
	Buff = (ACMP2OUTPUT >> 0x04) & 0x01;
  return Buff;
}

//----------------------------------------------------------------

void ACMP2_EnableIE(unsigned char nIE_TYPE)
{
  EA = 1;					
	IEN1 = IEN1 | 0x08;			
	ACMP2OUTPUT = ACMP2OUTPUT & 0x9F;
	ACMP2OUTPUT = ACMP2OUTPUT | (nIE_TYPE << 0x05);
}

void ACMP2_Set_Input(unsigned char Level)
{
   if(Level == 0x01)
	 {
				PGA_P = 1;
				PGA_N = 0;
				DACCON = DACCON & 0xFE;
	 }
    else
		{
		
				PGA_P = 0;
				PGA_N = 1;
		    DACCON = DACCON | 0x01;
		}
}
