// USER CODE BEGIN (MAIN_General,1)


// USER CODE END


// USER CODE BEGIN (MAIN_General,2)

// MAIN_General_2_IAF_CODE_BEGIN
#if DAVECG_BLOCK_COMMUTATION
#include "CTRL.H"
#endif
// MAIN_General_2_IAF_CODE_END

// USER CODE END


// USER CODE BEGIN (MAIN_General,3)

// MAIN_General_3_IAF_CODE_BEGIN
#if DAVECG_BLOCK_COMMUTATION
#define		REQUESTED				1
#define		RESET					0
#define		MOTOR_READY_TO_START	0
#define		MOTOR_STOPPED			1
#endif
// MAIN_General_3_IAF_CODE_END

// USER CODE END


// USER CODE BEGIN (MAIN_General,4)

// USER CODE END

// USER CODE BEGIN (MAIN_General,5)


// USER CODE END


// USER CODE BEGIN (MAIN_General,6)

// MAIN_General_6_IAF_CODE_BEGIN

// MAIN_General_6_IAF_CODE_END

// USER CODE END


// USER CODE BEGIN (MAIN_General,7)
// MAIN_General_7_IAF_CODE_BEGIN
#if	DAVECG_FIELD_ORIENTED_CONTROL	//FOC 
#if DAVECG_KEIL			//Keil 
bdata 	unsigned char 	Status_word;
		sbit 			gb_Once      = Status_word^4;
		sbit 			gb_Trap      = Status_word^3;
		sbit 			gb_EnableFOC = Status_word^2;
		sbit 			gb_On        = Status_word^1;
		sbit 			gb_Off       = Status_word^0;

	 	bit 			bitNewCMD0;
	 	bit 			bitNewCMD1;

data 	unsigned char 	guc_StartupCounter;
data 	unsigned char 	guc_SpeedControlCounter;
data 	unsigned char 	guc_sector;
data 	unsigned char 	guc_sector_s;

data 	unsigned char 	ucT2state;
data 	unsigned char 	ucNewState;				   
data 	unsigned char 	ucNewCMD0;
#if DAVECG_CONTROL_PANEL_ENABLE
data 	unsigned char 	ucNewCMD1;
		
pdata 	unsigned char 	CANRxdBuf0[8];
pdata 	unsigned char 	CANRxdBuf1[8];
pdata 	unsigned char  	CANTrxBuf0[8];
pdata 	unsigned char  	CANTrxBuf1[8];
#endif

data 	int 			gi_Angle;
data 	int 			gi_angle_mem;
data 	int 			gi_delta_angle;
data 	int 			gi_I_phaseA;
data 	int 			gi_I_phaseB;
data 	int 			gi_I_alpha;
data 	int 			gi_I_beta;
data 	int 			gi_I_d;
data 	int 			gi_I_q;
data 	int 			gi_V_alpha;
data 	int 			gi_V_beta;
data 	int 			gi_PI_output_Id;
data 	int 			gi_PI_output_Iq;
data 	int 			gi2_Speed[2];
data 	int 			gi3_Flux_beta[3];
data 	int 			gi3_Flux_alpha[3];
data 	int 			gi_Amplitude;
data 	int 			gi_Speed_reference       _at_(0x0030);
data 	int 			gi_Speed_reference_user  _at_(0x0032);	
data 	int 			gi_Iq_reference          _at_(0x0034);
data 	int 			gi_Speed_startup         _at_(0x0036);	
data 	int 			gi_Iq_startup            _at_(0x0038);
				
data 	unsigned int 	uiT2countCANTrx;

idata 	PI_parameter 	gt_Id_control     _at_(0x0080);	// char y[3] @ 0x80, int k.p @ 0x83, int k.i @ 0x85
idata 	PI_parameter 	gt_Iq_control     _at_(0x0088); // char y[3] @ 0x88, int k.p @ 0x8B, int k.i @ 0x8D
idata 	PI_parameter 	gt_Speed_control  _at_(0x0090); // char y[3] @ 0x90, int k.p @ 0x93, int k.i @ 0x95
#if DAVECG_CONTROL_PANEL_ENABLE
const	unsigned char 	CANTrxBuf0Rate 	= 125;  // slow CAN communication every 200ms
data unsigned int SteckerCode = FOC_STECKER_CODE;  //Graph varaible 
#endif
int API_Ref;

#endif // end of keil
#if DAVECG_SDCC    ///SDCC compiler selection

		bit  			gb_Off;
		bit 			gb_On;
		bit 			gb_EnableFOC;
		bit  			gb_Trap;
		bit 			gb_Once;

	 	bit 			bitNewCMD0;
	 	bit 			bitNewCMD1;
data at 0x0020	unsigned char Status_word;
data 	unsigned char 	guc_StartupCounter;
data 	unsigned char 	guc_SpeedControlCounter;
data 	unsigned char 	guc_sector;
data 	unsigned char 	guc_sector_s;

data at 0x0028 unsigned char 	ucT2state;
data at 0x0029 unsigned char 	ucNewState;				   
data at 0x002a unsigned char 	ucNewCMD0;
#if DAVECG_CONTROL_PANEL_ENABLE
data at 0x002b unsigned char 	ucNewCMD1;
			
xdata 	unsigned char 	CANRxdBuf0[8];
xdata 	unsigned char 	CANRxdBuf1[8];
xdata 	unsigned char  	CANTrxBuf0[8];
xdata 	unsigned char  	CANTrxBuf1[8];
#endif
data 	int 			gi_delta_angle;
data 	int 			gi_angle;
data 	int 			gi_angle_mem;
data 	int 			gi_I_phaseA;
data 	int 			gi_I_phaseB;
data 	int 			gi_I_alpha;
data 	int 			gi_I_beta;
data 	int 			gi_I_d;
data 	int 			gi_I_q;
data 	int 			gi_V_alpha;
data 	int 			gi_V_beta;
data 	int 			gi_PI_output_Id;
data 	int 			gi_PI_output_Iq;
data 	int 			gi2_Speed[2];
data 	int 			gi3_Flux_beta[3];
data 	int 			gi3_Flux_alpha[3];
data 	int 			gi_Amplitude;
data at 0x0030		int 			gi_Speed_reference;
data at 0x0032 	int 			gi_Speed_reference_user;	
data at 0x0034 	int 			gi_Iq_reference;
data at 0x0036 	int 			gi_Speed_startup;	
data at 0x0038 	int 			gi_Iq_startup;
				
data 	unsigned int 	uiT2countCANTrx;

idata at 0x0080	PI_parameter 	gt_Id_control;		// char y[3] @ 0x80, int k.p @ 0x83, int k.i @ 0x85
idata at 0x0088	PI_parameter 	gt_Iq_control; 		// char y[3] @ 0x88, int k.p @ 0x8B, int k.i @ 0x8D
idata at 0x0090	PI_parameter 	gt_Speed_control; 	// char y[3] @ 0x90, int k.p @ 0x93, int k.i @ 0x95

#if DAVECG_CONTROL_PANEL_ENABLE
const	unsigned char 	CANTrxBuf0Rate 	= 125;  // slow CAN communication every 200ms

data unsigned int SteckerCode = FOC_STECKER_CODE;  //Graph varaible 

#endif
int API_Ref;

#endif //end of SDCC

#endif //end of Field Oriented Control
// MAIN_General_7_IAF_CODE_END
// USER CODE END


// USER CODE BEGIN (MAIN_General,8)

// MAIN_General_8_IAF_CODE_BEGIN
#if	DAVECG_FIELD_ORIENTED_CONTROL	//FOC 
#if DAVECG_KEIL  //keil
extern void XRAM_vInit(void);
#endif   //end of keil
extern void MotorControl_vInit( void );
#endif

// MAIN_General_8_IAF_CODE_END
	
// USER CODE END


// USER CODE BEGIN (MAIN_General,9)


// USER CODE END


// USER CODE BEGIN (Init,1)


// USER CODE END


	// USER CODE BEGIN (Init,2)


	// USER CODE END


	// USER CODE BEGIN (Init,3)

	// Init_3_IAF_CODE_BEGIN

	// Init_3_IAF_CODE_END

	// USER CODE END


// USER CODE BEGIN (UnlockProtecReg,1)

// USER CODE END

// USER CODE BEGIN (MAIN_Init,3)

// MAIN_Init_3_IAF_CODE_BEGIN
#if	DAVECG_FIELD_ORIENTED_CONTROL	//FOC 
	
	#if DAVECG_KEIL    // Keil 
	XRAM_vInit();
	#endif
	MotorControl_vInit();

#endif

// MAIN_Init_3_IAF_CODE_END

// USER CODE END


// USER CODE BEGIN (Main,1)


// USER CODE END


  // USER CODE BEGIN (MAIN_Main,2)



	// USER CODE END

  // USER CODE BEGIN (MAIN_Main,3)
	// Main_3_IAF_CODE_BEGIN
	#if DAVECG_BLOCK_COMMUTATION
	MCU_INIT();														// Initialise the MCU related variables & SFRs.
	INIT_CTRL_VAR();												// Initialise the control variables.
	#endif
		// Main_3_IAF_CODE_END		
  // USER CODE END

  // USER CODE BEGIN (MAIN_Main,4)

	// Main_4_IAF_CODE_BEGIN

		#if DAVECG_BLOCK_COMMUTATION
		//Reset the software 'trap' flag if requested by the user.
		    if (g_trap_reset == REQUESTED)						   // 'trap' reset requested ?
			{ 
				g_trap_reset	= RESET;						   // if yes, acknowledge the request....
				gb_ctrap		= RESET;						   // ...and reset the s/w 'trap' flag.
			}
		//---------------------------------------------------------------------------------------------------
		//Start or Stop the Motor as per the present conditions.
			if (gb_ctrap)										   // 'TRAP' event recorded ?
			{													   // if yes.....
				gb_motor_status = MOTOR_STOPPED;				   // ....then stop the motor.
			} 
			else												   // otherwise....
			{ 
				if (gb_motor_status == MOTOR_READY_TO_START)	   // ....motor start requested by user ?
				{												   // if yes.....
					motor_start();								   // execute motor_start routine.
				} 
				else											   // if not requested....
				{ 
					motor_stop();								   // then execute motor_stop routine.
				} 
			}
		//---------------------------------------------------------------------------------------------------
		//Ramp up(down) the motor to the new speed/current.
		#if DAVECG_VOLTAGE_CTRL
		motor_ramp_generation();
		#endif	//VOLTAGE_CTRL
		#if DAVECG_SPEED_CTRL
		motor_ramp_generation();
		#endif	//SPEED_CTRL
		#if DAVECG_SPEED_CURRENT_CTRL
		motor_ramp_generation();
		#endif	//SPEED_CURRENT_CTRL
		//--------------------------------------------------------------------------------------------------
        //blink LED to indicate delay.
		if(g_counter0 > 24000)										// set time elapsed?...
		{              
		SFR_PAGE(_pp0, noSST);
		P3_DATA ^= 0x80;											// ...Then toggle LED...
		g_counter0 = 0x0000;										// ...and reset the s/w time base.
		}
		g_counter0++;												// increment s/w time base.
		//------------------------------------------------------------------------------------------
		#endif
		// Main_4_IAF_CODE_END		


	
	// USER CODE END

// USER CODE BEGIN (MAIN_General,10)


// USER CODE END
