#include "OB6710.h"
#include "ACMP1.h"

bit g_Acmp1Interrupt = 0;


void ACMP1_ISR(void) interrupt d_ACMP1_Vector
{	 
	ACMP1OUTPUT &= 0x7F;	// 0111_1111 clear ACMP1 INT Flag
	g_Acmp1Interrupt = 0x01;
}

void ACMP1_Open(unsigned char nPSEL, unsigned char nNSEL, unsigned char nHYS_RANGE)
{
	ACMP1CON = 0x00;
	ACMP1CON = (nNSEL << 4) | (nPSEL << 1);
  ACMP1CON = ACMP1CON | 0x01;
	ACMPHYS = nHYS_RANGE << 0x04;
}

void ACMP1_Close(void)
{
	ACMP1CON = 0x00;
}

void ACMP1_DebounceCount(unsigned char nDebounceCount)
{
	ACMP1DBCNT = nDebounceCount;
}

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

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

void ACMP1_EnableIE(unsigned char nIE_TYPE)
{
  EA = 1;					
	IEN2 = IEN2 | 0x08;			
	ACMP1OUTPUT = ACMP1OUTPUT & 0x9F;
	ACMP1OUTPUT = ACMP1OUTPUT | (nIE_TYPE << 0x05);
}

void ACMP1_DisableIE(void)
{	
	IEN2 = IEN2 &  0xF7;			
	ACMP2OUTPUT = ACMP2OUTPUT & 0x9F;
}

void ACMP1_Set_Input(unsigned char pChannel, unsigned char nChannel, unsigned char Level)
{
   if(Level == 0x01)
	 {
			if(pChannel == 0x00)
				CMP1_P0 = 0x01;
			if(pChannel == 0x01)
				CMP1_P1 = 0x01;

	
			if(nChannel == 0x00)
				CMP1_N0 = 0x00;
      else
				DACCON = DACCON & 0xFD;
	 }
    else
		{
		
			if(pChannel == 0x00)
				CMP1_P0 = 0x00;
			if(pChannel == 0x01)
				CMP1_P1 = 0x00;
	
			if(nChannel == 0x00)
				CMP1_N0 = 0x01;		
		   else
		    DACCON = DACCON | 0x02;
		}
}

