/*
******************************************************************************************************
 @Module        	
 @Filename     	Ct_Detector.c
 @Project        	SEM-3000
-----------------------------------------------------------------------------------------------------
 @Controller     	Si1001

 @Compiler      	Keil 8.12

 @ Frequency   	Using Internal clock 24.5MHz

 @Codegenerator 	0.1

 @Description   

------------------------------------------------------------------------------------------------------
 @Date          		2013-07-05 11:00:00

******************************************************************************************************
*/


#define _Ct_Detector_C_

/*
********************************************************************************************************
									 @Project Includes
********************************************************************************************************
*/

#include "PROJECT_INCLUDES.H"



/*
*********************************************************************************************************
 									@Macros
*********************************************************************************************************
*/


/*
*********************************************************************************************************
									Define INVERTER Class  data type 
*********************************************************************************************************
*/


/*
***************************************************************************************************************************
									@Global Variables
***************************************************************************************************************************
*/

idata volatile U8 CT_Sensor;

idata  volatile U32 ADC_CT_Dect_Channel_mV;


static void Ct_Detector__ADC0_Interrupt_Handler(void);

/*
*********************************************************************************************************
*                                                             INITIALIZE THE ADC0
*
* Description:    ADC0 Init
*
* Arguments  : 
*
* Returns      :   NONE

*********************************************************************************************************
*/

#define CT1_PORT_ANALOG_MODE  		P2MDIN &=~P26_ADC_CT1;
#define CT2_PORT_ANALOG_MODE  		P2MDIN &=~P25_ADC_CT2;
#define CT3_PORT_ANALOG_MODE  		P2MDIN &=~P24_ADC_CT3;
#define CT_DECT_PORT_ANALOG_MODE  	P2MDIN &=~P23_ADC_CT_DECT;


#define CT1_PORT__OPEN_DRAIN_IN_MODE 		P2MDOUT  &=~P26_ADC_CT1;
#define CT2_PORT__OPEN_DRAIN_IN_MODE 		P2MDOUT  &=~P25_ADC_CT2;
#define CT3_PORT__OPEN_DRAIN_IN_MODE 		P2MDOUT  &=~P24_ADC_CT3;
#define CT_DECT_PORT__OPEN_DRAIN_IN_MODE 	P2MDOUT  &=~P23_ADC_CT_DECT;

void Ct_Detector_Init(void)
{
	CT1_PORT_ANALOG_MODE;      // 아날로그 모드 선택 
	CT2_PORT_ANALOG_MODE;
	CT3_PORT_ANALOG_MODE;;
	CT_DECT_PORT_ANALOG_MODE;

	CT1_PORT__OPEN_DRAIN_IN_MODE ; // 아날로그 모드 일경우  OPEN DRAIN 이여야 함 
	CT2_PORT__OPEN_DRAIN_IN_MODE ;;
	CT3_PORT__OPEN_DRAIN_IN_MODE ;
	CT_DECT_PORT__OPEN_DRAIN_IN_MODE ;


	ADC_CT_Dect_Channel_mV=0;
	CT_Sensor=0;
	
	CT_DECT_Monitor_Intertupt_Service_Init( Ct_Detector__ADC0_Interrupt_Handler );   // Set ADC0 interrupt service function
}





#define CT_TOLERANCE			80		// 80       

#define CT_NONE_AD_VALUE		3710      //2300	//1310

#define CT1_STAND_AD_VALUE		5326    //  3300	//1866
#define CT2_STAND_AD_VALUE		4480    //2556	//1573
#define CT3_STAND_AD_VALUE		3972    //2470	//1407

#define CT_1_2_STAND_AD_VALUE		7100    //4930	//2462
#define CT_2_3_STAND_AD_VALUE		4868    //3674	//1716
#define CT_1_3_STAND_AD_VALUE		5912    //4104	//2060

#define THREE_PHASE_AD_VALUE		8180    //5700 // 	2826	mV ( 3 상 전력 측정시 )



U8 ValueScopeCheck( U16 RefernceValue , U16 CheckValue)
{
	#if 1
    if(RefernceValue==CT2_STAND_AD_VALUE)
    	{
	if((RefernceValue-(CT_TOLERANCE+10) ) <= CheckValue && CheckValue <= (RefernceValue+(CT_TOLERANCE+10))) return TRUE;
       return FALSE;
    	}
    else if(RefernceValue==CT_1_2_STAND_AD_VALUE)
    	{
	if((RefernceValue-(CT_TOLERANCE+60) ) <= CheckValue && CheckValue <= (RefernceValue+(CT_TOLERANCE+60))) return TRUE;
       return FALSE;
    	}
  else
    	{
	if((RefernceValue-CT_TOLERANCE ) <= CheckValue && CheckValue <= (RefernceValue+CT_TOLERANCE)) return TRUE;
       return FALSE;
    	}
  #else
	if((RefernceValue-CT_TOLERANCE ) <= CheckValue && CheckValue <= (RefernceValue+CT_TOLERANCE)) return TRUE;
       return FALSE;
  #endif
}


 volatile U8 Ct_Detect_Chk(void) 
{

	return (CT_Sensor&THREE_PHASE_EXIST);	
	
}


U8 Ct_Detect_ADC_Chk(void) 
{
  	idata  volatile U8 Ct_Sensor=0;

//  return CT1_EXIST;
#if DEBUG_CT_DETECT
	printf("\n ADC_CT_Dect_Channel_mV v=%ld", ADC_CT_Dect_Channel_mV);  
#endif 	
 	if(ADC_CT_Dect_Channel_mV < (CT_NONE_AD_VALUE + CT_TOLERANCE))	return NO_EXIST; 
	
 	if(ADC_CT_Dect_Channel_mV > (THREE_PHASE_AD_VALUE - CT_TOLERANCE))	return THREE_PHASE_EXIST; 

	if(ValueScopeCheck(CT1_STAND_AD_VALUE ,ADC_CT_Dect_Channel_mV)) 	Ct_Sensor |=CT1_EXIST;
	if(ValueScopeCheck(CT2_STAND_AD_VALUE ,ADC_CT_Dect_Channel_mV)) 	Ct_Sensor |= CT2_EXIST;
	if(ValueScopeCheck(CT3_STAND_AD_VALUE ,ADC_CT_Dect_Channel_mV))	Ct_Sensor |= CT3_EXIST;

	if(ValueScopeCheck(CT_1_2_STAND_AD_VALUE ,ADC_CT_Dect_Channel_mV)) 	Ct_Sensor |=(CT1_EXIST|CT2_EXIST);
	if(ValueScopeCheck(CT_2_3_STAND_AD_VALUE ,ADC_CT_Dect_Channel_mV)) 	Ct_Sensor |= (CT2_EXIST|CT3_EXIST);
	if(ValueScopeCheck(CT_1_3_STAND_AD_VALUE ,ADC_CT_Dect_Channel_mV))	Ct_Sensor |= (CT1_EXIST|CT3_EXIST);

	//printf(" \nCT_V=%ld", ADC_CT_Dect_Channel_mV);
	
	return Ct_Sensor;	
	
}


 void Put_CT_Sensor(U8 CT_Sensor_Adc)
 {
             CT_Sensor=CT_Sensor_Adc &THREE_PHASE_EXIST;
 }


 /*
*********************************************************************************************************
*                                                          Ct_Detector__ADC0_Interrupt_Handler
*
* Description:      adc 인터럽트에서 호출.
*
* Arguments  :    NONE  
*
* Returns      :    NONE
*
*********************************************************************************************************
*/

static void Ct_Detector__ADC0_Interrupt_Handler(void) 
{
	
	ADC_CT_Dect_Channel_mV = ADC_CT_Get_mV();	//16 -> 32
	
	//ADC_CT_Dect_Channel_mV = ADC_Get_mV();

	//fEnergy_Power_Measuring_State |=CT_DECT_Monitor;

 	 //printf("\n c=%ld ", ADC_CT_Dect_Channel_mV);
}





