
// USER CODE BEGIN (CCU60_General,1)

// USER CODE END


// USER CODE BEGIN (CCU60_General,2)
// CCU60_General_2_IAF_CODE_BEGIN
#include "CONFIG.H"
#include "CTRL.H"
// CCU60_General_2_IAF_CODE_END
// USER CODE END


// USER CODE BEGIN (CCU60_General,3)

// USER CODE END



// USER CODE BEGIN (CCU60_General,4)

// USER CODE END


// USER CODE BEGIN (CCU60_General,5)

// USER CODE END


// USER CODE BEGIN (CCU60_General,6)
// CCU60_General_6_IAF_CODE_BEGIN
	extern  bit 				gb_ctrap;             
 	extern  bit					gb_time_out; 
 #if DAVECG_ControlAlgoKBS.BEMF
	extern  uword	C_SDATA		g_state;
	extern 	uword	C_SDATA		g_slope;
 #endif  //End of BEMF 
 #if DAVECG_ControlAlgoKBS.HALL
 	extern  bit					gb_On;
	extern 	bit 				gb_hs_error;
	extern  ubyte 	C_SDATA		Curr_HP;
    extern  const 	uword 		Close_Patt[];
 #endif //End of HAll
 // CCU60_General_6_IAF_CODE_END
// USER CODE END



// USER CODE BEGIN (CCU60_General,7)

// USER CODE END


// USER CODE BEGIN (CCU60_General,8)
   // CCU60_General_8_IAF_CODE_BEGIN
	#if DAVECG_ControlAlgoKBS.BEMF
	extern void Ramp(void);
	extern void Commutation(void); 
	#endif //End of Bemf
	#if DAVECG_ControlAlgoKBS.SPEED_CALCU
	extern int  SpeedCalculation(void);
	#endif
	#if DAVECG_ControlAlgoKBS.SPEED_CTRL
	extern sword PIControllerSpeed(uword *PI_Parameter,uword Actual, uword Ref);
	#endif //End of SC
	#if DAVECG_ControlAlgoKBS.CURRENT_CTRL
	extern uword PIControllerCurrent(uword *PI_Parameter,uword Actual, uword Ref);
	#endif //End of CC
	#if DAVECG_ControlAlgoKBS.SPEED_CURRENT_CTRL
	extern uword PIControllerSpeed(uword*PI_Parameter,uword Actual, uword Ref);
	extern uword PIControllerCurrent(uword*PI_Parameter,uword Actual, uword Ref);
	#endif //End of SCC
	// CCU60_General_8_IAF_CODE_END
// USER CODE END


// USER CODE BEGIN (CCU60_General,9)

// USER CODE END


// USER CODE BEGIN (Init,1)

// USER CODE END


  // USER CODE BEGIN (Init,2)

  // USER CODE END

  
  // USER CODE BEGIN (NodeI3,3)

  // USER CODE END


// USER CODE BEGIN (NodeI1,1)

// USER CODE END


  // USER CODE BEGIN (NodeI1,2)

  // USER CODE END

 
    // USER CODE BEGIN (NodeI1,19)
	// NODEI1_19_IAF_CODE_BEGIN
	 gb_time_out=1;
	 MotorStop();
	// NODEI1_19_IAF_CODE_END
    // USER CODE END


    // USER CODE BEGIN (NodeI1,17)
	 // NODEI1_17_IAF_CODE_BEGIN
	 #if DAVECG_DriveKBS.CTrap
	 gb_ctrap=1;
	 MotorStop();
	 #endif  
	// NODEI1_17_IAF_CODE_END
    // USER CODE END

	 // USER CODE BEGIN (NodeI1,18)
	 // NODEI1_18_IAF_CODE_BEGIN
	 #if DAVECG_ControlAlgoKBS.HALL
	 if(gb_On)
	 {
		gb_hs_error=1;
    	MotorStop();
	 }
	#endif  //End of Hall
	// NODEI1_18_IAF_CODE_END
	// USER CODE END
  
// USER CODE BEGIN (NodeI2,1)

// USER CODE END

 // USER CODE BEGIN (NodeI2,2)

  // USER CODE END

   // USER CODE BEGIN (NodeI2,14)
   // NodeI2_14_IAF_CODE_BEGIN
	#if DAVECG_ControlAlgoKBS.BEMF
	if (g_slope == 2)									   
	{
		g_slope = 0;											// we need to look for 'rising' edge.
	}
	if (g_slope == 3)									
	{	
		g_slope = 1;											// we need to look for 'falling' edge.
	}
	#if DAVECG_ControlAlgoKBS.SPEED_CALCU
	if (g_state == 3)											// if motor is running in steady state( state 3)...
   	{
   		g_speed_CC60 = SpeedCalculation();
		
		if(g_speed_counter < PP_FACTOR)	
		{
		 	g_speed_CC60=0;
			#if DAVECG_ControlAlgoKBS.VOLTAGE_CTRL
		  	cVCB0.Motor_Speed=0;
			#endif //End of VCB
			#if DAVECG_ControlAlgoKBS.SPEED_CTRL
		  	cSCB0.Motor_Speed=0;
			#endif //End of SCB
			#if DAVECG_ControlAlgoKBS.CURRENT_CTRL
			cCCB0.Motor_Speed=0;
			#endif //End of CCB
			#if DAVECG_ControlAlgoKBS.SPEED_CURRENT_CTRL
			cSCCB0.Motor_Speed=0;
			#endif //End of SCCB
		  	speed_buf=0;
		}
		Speed_Filter();
	}
	#endif
	#endif //End of Bemf
	// NodeI2_14_IAF_CODE_END
    // USER CODE END


// USER CODE BEGIN (NodeI3,1)

// USER CODE END

 // USER CODE BEGIN (NodeI3,2)

  // USER CODE END

  
	 // USER CODE BEGIN (NodeI3,14)
	// NodeI3_14_IAF_CODE_BEGIN
	 #if DAVECG_ControlAlgoKBS.HALL
	 #if DAVECG_ControlAlgoKBS.VOLTAGE_CTRL
	 Curr_HP				= (ubyte)(((CCU60_MCMOUT>>8) & 0x38)>> 3);
	 CCU60_MCMOUTS		= Close_Patt[Curr_HP+cVCH0.Motor_Direction];

	 #if DAVECG_ControlAlgoKBS.SPEED_CALCU
	 cVCH0.Time_Measure 	= CCU60_CC60R;

	 cVCH0.Motor_Speed	= SpeedCalculation();
		
	 if(cVCH0.Speed_Counter < PP_FACTOR)	
	 {
		cVCH0.Motor_Speed=0;
	 }
	 #endif 
	 #endif  //End of VCH
	 #if DAVECG_ControlAlgoKBS.SPEED_CTRL
	Curr_HP				= (ubyte)(((CCU60_MCMOUT>>8) & 0x38)>> 3);
	CCU60_MCMOUTS		= Close_Patt[Curr_HP+cSCH0.Motor_Direction];

	cSCH0.Time_Measure 	= CCU60_CC60R;
	cSCH0.Motor_Speed	= SpeedCalculation();
		
	if(cSCH0.Speed_Counter < PP_FACTOR)	
	{
		cSCH0.Motor_Speed=0;
	}
	#endif  //End of SCH
	#if DAVECG_ControlAlgoKBS.CURRENT_CTRL
	Curr_HP				= (ubyte)(((CCU60_MCMOUT>>8) & 0x38)>> 3);
	CCU60_MCMOUTS		= Close_Patt[Curr_HP+cCCH0.Motor_Direction];
	
	#if DAVECG_ControlAlgoKBS.SPEED_CALCU
	cCCH0.Time_Measure 	= CCU60_CC60R;
	cCCH0.Motor_Speed	= SpeedCalculation();
		
	if(cCCH0.Speed_Counter < PP_FACTOR)	
	{
		cCCH0.Motor_Speed=0;
	}
	#endif
	#endif //End of CCH
	#if DAVECG_ControlAlgoKBS.SPEED_CURRENT_CTRL
	Curr_HP				= (ubyte)(((CCU60_MCMOUT>>8) & 0x38)>> 3);
	CCU60_MCMOUTS		= Close_Patt[Curr_HP+cSCCH0.Motor_Direction];

	cSCCH0.Time_Measure 	= CCU60_CC60R;
	cSCCH0.Motor_Speed	= SpeedCalculation();
		
	if(cSCCH0.Speed_Counter < PP_FACTOR)	
	{
		cSCCH0.Motor_Speed=0;
	}
	#endif //End of SCCH
 #endif //End of Hall
	// NodeI3_14_IAF_CODE_END

    // USER CODE END



    // USER CODE BEGIN (NodeI3,22)
	 // NodeI3_22_IAF_CODE_BEGIN
	 #if DAVECG_ControlAlgoKBS.BEMF
   		Channel_Select();
   		Current_CV_Measurement();
	 #endif //End of Bemf
	#if DAVECG_ControlAlgoKBS.HALL
	#if DAVECG_ControlAlgoKBS.CURRENT_SENSE
 		Current_Measurement();
	#endif
	#endif
	// NodeI3_22_IAF_CODE_END
    // USER CODE END

   
    // USER CODE BEGIN (NodeI3,21)

	// NodeI3_21_IAF_CODE_BEGIN
	#if DAVECG_ControlAlgoKBS.BEMF
	#if DAVECG_ControlAlgoKBS.VOLTAGE_CTRL
	if(g_slope==1 || g_slope==0)
	{
	   Commutation();
	}
   	if(g_state==3)
	{			
		VoltageRampup();					
		DutyCycleUpdate();
	}
	#endif //End of VCB
	#if DAVECG_ControlAlgoKBS.SPEED_CTRL
	if(g_slope==1 || g_slope==0)
	{
	   Commutation();
	}
		
	if(g_speed_counter >= PP_FACTOR)	
	{
		SpeedRampup();
		cSCB0.Speed.PI_Output= PIControllerSpeed(&cSCB0.Speed.Kp,g_speed_CC60,cSCB0.Speed_Ref);
		cSCB0.Duty_Cyc	=  PULSE_PERIOD - cSCB0.Speed.PI_Output;
		DutyCycleUpdate();
	}
	#endif  //End of SCB
	#if DAVECG_ControlAlgoKBS.CURRENT_CTRL
	 if(g_slope==1 || g_slope==0)
	{
	   Commutation();
	}
	if(g_state==3)
	{
		cCCB0.Current.PI_Output= PIControllerCurrent(&cCCB0.Current.Kp,cCCB0.Motor_Current,Current_Ref);
		cCCB0.Duty_Cyc	=  PULSE_PERIOD - cCCB0.Current.PI_Output;
		
		DutyCycleUpdate();
	 }
	#endif //End of CCB
	#if DAVECG_ControlAlgoKBS.SPEED_CURRENT_CTRL
	if(g_slope==1 || g_slope==0)
	{
	   Commutation();
	}
	
	if(g_speed_counter >= PP_FACTOR)	
	{
		SpeedRampup();
		
		cSCCB0.Speed.PI_Output= PIControllerSpeed(&cSCCB0.Speed.Kp,g_speed_CC60,cSCCB0.Speed_Ref);
		cSCCB0.Current.PI_Output= PIControllerCurrent(&cSCCB0.Current.Kp, cSCCB0.Motor_Current,cSCCB0.Speed.PI_Output);
		cSCCB0.Duty_Cyc	=  PULSE_PERIOD - cSCCB0.Current.PI_Output;
		
		DutyCycleUpdate();
						
	}
	#endif //End of SCCB
	#endif //End of Bemf
	#if DAVECG_ControlAlgoKBS.HALL
	#if DAVECG_ControlAlgoKBS.VOLTAGE_CTRL
		VoltageRampup();					
		DutyCycleUpdate();
	 #endif  //End of VCH
	 #if DAVECG_ControlAlgoKBS.SPEED_CTRL
		SpeedRampup();
		cSCH0.Speed.PI_Output= PIControllerSpeed(&cSCH0.Speed.Kp,cSCH0.Motor_Speed,cSCH0.Speed_Ref);
		cSCH0.Duty_Cyc	=  PULSE_PERIOD - cSCH0.Speed.PI_Output;
		DutyCycleUpdate();
	#endif  //End of SCH
	#if DAVECG_ControlAlgoKBS.CURRENT_CTRL
		cCCH0.Current.PI_Output= PIControllerCurrent(&cCCH0.Current.Kp,cCCH0.Motor_Current,Current_Ref);
		cCCH0.Duty_Cyc	=  PULSE_PERIOD - cCCH0.Current.PI_Output;
		DutyCycleUpdate();
	#endif //End of CCH
	#if DAVECG_ControlAlgoKBS.SPEED_CURRENT_CTRL
		SpeedRampup();
		cSCCH0.Speed.PI_Output= PIControllerSpeed(&cSCCH0.Speed.Kp,cSCCH0.Motor_Speed,cSCCH0.Speed_Ref);
		cSCCH0.Current.PI_Output= PIControllerCurrent(&cSCCH0.Current.Kp, cSCCH0.Motor_Current,cSCCH0.Speed.PI_Output);
		cSCCH0.Duty_Cyc	=  PULSE_PERIOD - cSCCH0.Current.PI_Output;
		DutyCycleUpdate();
	#endif //End of SCCH
	#endif //End of HALL


	// NodeI3_21_IAF_CODE_END 

    // USER CODE END

  
// USER CODE BEGIN (CCU60_General,10)

// USER CODE END

