#include "OB6710.h"
#include "CCU.h"

bit g_CcuInterrupt = 0;
unsigned char g_CcuStatus = 0;

void CCU_Interrupt(void) interrupt d_T2_Vector  // ADC Interrupt Vector Address 0053h => interrupt 10
{  
   g_CcuInterrupt = 1;
	 g_CcuStatus= CCCON & 0x0F;
	 CCCON = CCCON & 0xF0;
}

void CCU_SetMode(unsigned char nChannel,unsigned char nMode)
{

	if(nChannel == d_CCU_EN_CH0)
		CCEN = (CCEN & 0xF0) | nMode;
	
	else if(nChannel == d_CCU_EN_CH1)
		CCEN = (CCEN & 0x0F) | (nMode << 0x04);
	
	else if(nChannel == d_CCU_EN_CH2)
		CCEN2 = (CCEN2 & 0xF0) | nMode ;
	
	else
		CCEN2 = (CCEN2& 0x0F) | (CCEN2 << 0x04);
	
}

unsigned int CCU_GetResult(unsigned char nChannel, unsigned char  nMode)
{
	unsigned int Buff;
	CCU_UPDATE_DISABLE = 1;
	if( (nMode == d_CCU_MODE_CONTINUE_HI_LOW) || (nMode == d_CCU_MODE_CONTINUE_LOW_HI))
		Buff = (CRCH << 0x08) | CRCL;
	
	else if( (nMode == d_CCU_MODE_SINGLE_HI_LOW) || (nMode == d_CCU_MODE_SINGLE_LOW_HI))
		Buff = (CRCH << 0x08) | CRCL;
	
	else if(nChannel == d_CCU_EN_CH0)
		Buff = (CRCH << 0x08) | CRCL;
	
	else if(nChannel == d_CCU_EN_CH1)
		Buff = (CCH1 << 0x08) | CCL1;	

	else if(nChannel == d_CCU_EN_CH2)
		Buff = (CCH2 << 0x08) | CCL2;	

	else 
		Buff = (CCH3 << 0x08) | CCL3;	
	 CCU_UPDATE_DISABLE = 0;
	 return Buff;
}

void CCU_Start(unsigned char nPreScalar)
{
  T2CON = nPreScalar | 0x01;   // clk=Fosc; reload disable; 
}

void CCU_EnableIE(unsigned char nChannel)
{
  CCCON = nChannel;   // CC2 Int enable
  EA = 1;         // enable all int
}