
// USER CODE BEGIN (SHARED_INT_General,1)

// USER CODE END


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

#include "SHARED_INT.H"

// USER CODE BEGIN (SHARED_INT_General,2)
// SHARED_INT_General_2_IAF_CODE_BEGIN
#if DAVECG_BLOCK_COMMUTATION
#include "CTRL.H"
#endif
#if	DAVECG_FIELD_ORIENTED_CONTROL	//FOC 
#include "MAIN.H"
#include "CONFIG.H"
#endif
// SHARED_INT_General_2_IAF_CODE_END
// USER CODE END


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

// USER CODE BEGIN (SHARED_INT_General,3)

// USER CODE END


//****************************************************************************
// @Defines
//****************************************************************************

// USER CODE BEGIN (SHARED_INT_General,4)

// USER CODE END


//****************************************************************************
// @Typedefs
//****************************************************************************

// USER CODE BEGIN (SHARED_INT_General,5)

// USER CODE END


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

// USER CODE BEGIN (SHARED_INT_General,6)

// SHARED_INT_General_6_IAF_CODE_BEGIN
#if	DAVECG_FIELD_ORIENTED_CONTROL	//FOC 
extern bit gb_Trap;
extern bit gb_On;
#if DAVECG_CONTROL_PANEL_ENABLE
#if DAVECG_KEIL  //keil
int countMO1 = CAN_MO3_RATE/2;
#endif
#if DAVECG_SDCC //SDCC
xdata int countMO1 = CAN_MO3_RATE/2;
#endif
#if DAVECG_EnGraphDisplay   //Graph Diplay enable / disable option
#if DAVECG_KEIL  //keil
int countMO3 = 0;
#endif
#if DAVECG_SDCC //SDCC
xdata int countMO3 = 0;
#endif
#endif
#endif //end of CP enable
extern unsigned char data ucT2state;
#endif

// SHARED_INT_General_6_IAF_CODE_END

// USER CODE END


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

// USER CODE BEGIN (SHARED_INT_General,7)

// SHARED_INT_General_7_IAF_CODE_BEGIN
#if DAVECG_BLOCK_COMMUTATION
#if DAVECG_BEMF
int l_bemf_aux=0;
unsigned char channel;
#if DAVECG_VOLTAGE_CTRL
int task_counter=0;
#endif
#if DAVECG_SPEED_CTRL
int pi_task_counter;
#endif
#if DAVECG_CURRENT_CTRL
int pi_task_counter;
#endif
#if DAVECG_SPEED_CURRENT_CTRL
int pi_task_counter;
#endif
#endif // BEMF
#endif //BLOCK COMMUTATION
// SHARED_INT_General_7_IAF_CODE_END

// USER CODE END


//****************************************************************************
// @External Prototypes
//****************************************************************************


// USER CODE BEGIN (SHARED_INT_General,8)

// SHARED_INT_General_8_IAF_CODE_BEGIN
#if	DAVECG_FIELD_ORIENTED_CONTROL	//FOC 
#if DAVECG_CONTROL_PANEL_ENABLE
extern void CAN_Transmit_MO1();
#if DAVECG_EnGraphDisplay   //Graph Diplay enable / disable option
extern void CAN_Transmit_MO3();
#endif
#endif
extern void T12_ISR_Handler();
extern void FOC_Scheduler();
extern void MotorStop();
#endif
#if DAVECG_BLOCK_COMMUTATION
#if DAVECG_BEMF
extern void ramp(void);
#endif
#endif
// SHARED_INT_General_8_IAF_CODE_END

// USER CODE END


//****************************************************************************
// @Prototypes Of Local Functions
//****************************************************************************

// USER CODE BEGIN (SHARED_INT_General,9)



// USER CODE END

// USER CODE BEGIN (SHINT_Init,1)

// USER CODE END

// USER CODE BEGIN (SHINT_Init,2)

// USER CODE END

// USER CODE BEGIN (SHINT_Init,3)
// SHINT_XINTR3Isr_IAF_CODE_BEGIN
#if	DAVECG_FIELD_ORIENTED_CONTROL	//FOC
#if DAVECG_CONTROL_PANEL_ENABLE
IEN1         |=  0x19;         // load interrupt enable register 1
#endif 
#endif
// SHINT_XINTR3Isr_IAF_CODE_END

// USER CODE END

// USER CODE BEGIN (SHINT_XINTR5Isr,1)

// USER CODE END

		// USER CODE BEGIN (SHINT_XINTR5Isr,2)

		// USER CODE END

		// USER CODE BEGIN (SHINT_XINTR5Isr,3)

		// SHINT_XINTR5Isr_3_IAF_CODE_BEGIN
		#if	DAVECG_FIELD_ORIENTED_CONTROL	//FOC 
		FOC_Scheduler();
		#endif
		#if DAVECG_BLOCK_COMMUTATION
		#if DAVECG_BEMF			//BEMF

		#if DAVECG_VOLTAGE_CTRL  //VOLTAGE CONTROL

		SFR_PAGE(_ad0, noSST);
		if((ADC_GLOBSTR & 0x01)==0x00)										// if ADC is not busy.....
		{ 
			SFR_PAGE(_ad2, noSST);											// switch to page 2
			if(ADC_RESR3L & 0x10)											// check whether the result is valid
			{
			g_adc_mem		=	(ADC_RESR3L >> 6)&0x0003;					// read ADC result
			g_adc_mem		=	g_adc_mem | (((uword)ADC_RESR3H << 2)); 	// read ADC result
			}
	
			if(ADC_RESR0L & 0x10)											// check whether the result is valid
			{
			ADC_PAGE = 0x02;												// switch to page 2
			channel	=	ADC_RESR0L & 0x07;									// read channel information 
		
			ADC_PAGE = 0x06;												// switch to page 6
				if (channel == 3)		
				{
					#if DAVECG_CURRENT_SENSE
					ADC_QINR0 = 0x04;										// write channel information to be converted
					#endif
					ADC_PAGE = 0x02;										// switch to page 2
					compare_value = (ADC_RESR0L >> 6) & 0x0003;				// read ADC result
					compare_value = compare_value | (((uword)ADC_RESR0H << 2));		// read ADC result
					compare_value = compare_value>>1;
					high_limit		=	compare_value + (compare_value >> 4);
					low_limit		=	compare_value - (compare_value >> 4);
				}	
				#if DAVECG_CURRENT_SENSE
				else if(channel == 4)
				{
					ADC_QINR0	=	0x03;									// write channel information to be converted
					ADC_PAGE = 0x02;
					g_current = (((((uword)ADC_RESR0H << 2)|(ADC_RESR0L>>6))<<5) & 0x7fff);		// read ADC result
					OVER_CURRENT_CHECK();
				} 
				#endif
				else
					ADC_QINR0	=	0x03;									// write channel information to be converted
			}	
		//-----------------------------------------------
		}
		
		SFR_PAGE(_cc3, noSST);         		  		
		l_bemf_aux = (CCU6_MCMOUTL & 0x3F);									// read the present Modulation Pattern....

		if(g_bemf_aux != l_bemf_aux)										// ...compare it with previous Mod Pattern.....
		{																	// if Mod Pattern has changed......
			g_bemf_aux = l_bemf_aux;										// then preserve present Mod Pattern...

			//-------ADC_AUX_BEMF();--------------------					// ....and determine the next ADC channel for Zero crossing sensing.
		
			if((g_bemf_aux == BEMF_AUX_VAL11) || (g_bemf_aux == BEMF_AUX_VAL12)) 
			{																// if phase '1' is Inactive....
				SFR_PAGE(_ad6, noSST);										// switch to page 6
				ADC_CRCR1 = 0x80;											// write channel information to be converted
				ADC_CRMR1 |= 0x40; 											// generate load event	
			} 
			if((g_bemf_aux == BEMF_AUX_VAL21) || (g_bemf_aux == BEMF_AUX_VAL22)) 
			{																// if phase '2' is Inactive.... 
				SFR_PAGE(_ad6, noSST);										// switch to page 6
				ADC_CRCR1 = 0x40;											// write channel information to be converted
				ADC_CRMR1 |= 0x40; 											// generate load event
			} 
			if((g_bemf_aux == BEMF_AUX_VAL31) || (g_bemf_aux == BEMF_AUX_VAL32)) 
			{																// if phase '3' is Inactive.... 
				SFR_PAGE(_ad6, noSST);										// switch to page 6
				ADC_CRCR1 = 0x20;											// write channel information to be converted
				ADC_CRMR1 |= 0x40; 											// generate load event
			}
			//------------------------------------------
		}
		#endif //VOLTAGE CONTROL

		#if DAVECG_SPEED_CTRL   //SPEED CONTROL
		
		SFR_PAGE(_ad0, noSST);
		if((ADC_GLOBSTR & 0x01)==0x00)										// if ADC is not busy.....
		{ 
		//--------------------------------------------------
			SFR_PAGE(_ad2, noSST);
			if(ADC_RESR0L & 0x10)											// check whether the result is valid
			{
				ADC_PAGE = 0x02;
				channel	=	ADC_RESR0L & 0x07;
				ADC_PAGE = 0x06;
				if (channel == 3)
				{
					#if DAVECG_CURRENT_SENSE
					ADC_QINR0 = 0x04;										// write channel information to be converted
					#endif
					ADC_PAGE = 0x02;										// switch to page 2
					compare_value = (ADC_RESR0L >> 6) & 0x0003;				// read ADC result
					compare_value = compare_value | (((uword)ADC_RESR0H << 2));		// read ADC result
					compare_value = compare_value>>1;
					high_limit		=	compare_value + (compare_value >> 4);
					low_limit		=	compare_value - (compare_value >> 4);
				}	
				#if DAVECG_CURRENT_SENSE
				else if(channel == 4)
				{
					ADC_QINR0	=	0x03;									// write channel information to be converted
					ADC_PAGE = 0x02;										// switch to page 2
					g_current = (((((uword)ADC_RESR0H << 2)|(ADC_RESR0L>>6))<<5) & 0x7fff);		// read ADC result
					OVER_CURRENT_CHECK();
										
				} 
				#endif
				else
					ADC_QINR0	=	0x03;									// write channel information to be converted
			}
			//-----------------------------------------------
		}
		SFR_PAGE(_cc3, noSST);         		  								// switch to page 3 
		l_bemf_aux = (CCU6_MCMOUTL & 0x3F);									// read the present Modulation Pattern....

		if(g_bemf_aux != l_bemf_aux)										// ...compare it with previous Mod Pattern.....
		{																	// if Mod Pattern has changed......
			g_bemf_aux = l_bemf_aux;										// then preserve present Mod Pattern...

			//-------ADC_AUX_BEMF();--------------------					// ....and determine the next ADC channel for Zero crossing sensing.
		
			if((g_bemf_aux == BEMF_AUX_VAL11) || (g_bemf_aux == BEMF_AUX_VAL12)) 
			{																// if phase '1' is Inactive....
				SFR_PAGE(_ad6, noSST);										// switch to page 6
				ADC_CRCR1 = 0x80;											// write channel information to be converted
				ADC_CRMR1 |= 0x40; 											// generate load event	
			} 
			if((g_bemf_aux == BEMF_AUX_VAL21) || (g_bemf_aux == BEMF_AUX_VAL22)) 
			{																// if phase '2' is Inactive.... 
				SFR_PAGE(_ad6, noSST);										// switch to page 6
				ADC_CRCR1 = 0x40;											// write channel information to be converted
				ADC_CRMR1 |= 0x40; 											// generate load event
			} 
			if((g_bemf_aux == BEMF_AUX_VAL31) || (g_bemf_aux == BEMF_AUX_VAL32)) 
			{																// if phase '3' is Inactive.... 
				SFR_PAGE(_ad6, noSST);										// switch to page 6
				ADC_CRCR1 = 0x20;											// write channel information to be converted
				ADC_CRMR1 |= 0x40; 											// generate load event
			}
			//------------------------------------------
		}

		#endif // SPEED CONTROL

		#if DAVECG_CURRENT_CTRL   //CURRENT CONTROL

			SFR_PAGE(_cc3, noSST);         		  	
			l_bemf_aux = (CCU6_MCMOUTL & 0x3F);								// read the present Modulation Pattern....
			if(g_bemf_aux != l_bemf_aux)									// ...compare it with previous Mod Pattern.....
			{																// if Mod Pattern has changed......
				g_bemf_aux = l_bemf_aux;									// then preserve present Mod Pattern...
				//-------ADC_AUX_BEMF();--------------------				// ....and determine the next ADC channel for Zero crossing sensing.
				if((g_bemf_aux == BEMF_AUX_VAL11) || (g_bemf_aux == BEMF_AUX_VAL12)) 
				{															// if phase '1' is Inactive....
					SFR_PAGE(_ad6, noSST);									// switch to page 6
					ADC_CRCR1 = 0x80;										// write channel information to be converted
					ADC_CRMR1 |= 0x40; 										// generate load event	
				} 
				if((g_bemf_aux == BEMF_AUX_VAL21) || (g_bemf_aux == BEMF_AUX_VAL22)) 
				{															// if phase '2' is Inactive.... 
					SFR_PAGE(_ad6, noSST);									// switch to page 6
					ADC_CRCR1 = 0x40;										// write channel information to be converted
					ADC_CRMR1 |= 0x40; 										// generate load event
				} 
				if((g_bemf_aux == BEMF_AUX_VAL31) || (g_bemf_aux == BEMF_AUX_VAL32)) 
				{															// if phase '3' is Inactive.... 
					SFR_PAGE(_ad6, noSST);									// switch to page 6
					ADC_CRCR1 = 0x20;										// write channel information to be converted
					ADC_CRMR1 |= 0x40; 										// generate load event
				}
				//------------------------------------------
			}
			
		SFR_PAGE(_ad0, noSST);
		if((ADC_GLOBSTR & 0x01)==0x00)										// if ADC is not busy.....
		{ 
		//--------------------------------------------------
			SFR_PAGE(_ad2, noSST);
			if(ADC_RESR0L & 0x10)											// check whether the result is valid
			{
				ADC_PAGE = 0x02;
				channel	=	ADC_RESR0L & 0x07;
				ADC_PAGE = 0x06;
				if (channel == 3)
				{
					ADC_QINR0 = 0x04;										// write channel information to be converted
					ADC_PAGE = 0x02;										// switch to page 2
					compare_value = (ADC_RESR0L >> 6) & 0x0003;				// read ADC result
					compare_value = compare_value | (((uword)ADC_RESR0H << 2));		// read ADC result
					compare_value = compare_value>>1;
					high_limit		=	compare_value + (compare_value >> 4);
					low_limit		=	compare_value - (compare_value >> 4);
				}	
				else if(channel == 4)
				{
					ADC_QINR0	=	0x03;									// write channel information to be converted
					ADC_PAGE = 0x02;										// switch to page 2	
					g_current = (((((uword)ADC_RESR0H << 2)|(ADC_RESR0L>>6))<<5) & 0x7fff);		// read ADC result
					OVER_CURRENT_CHECK();

				} 
				else
					ADC_QINR0	=	0x03;									// write channel information to be converted
			}
			   //-----------------------------------------------
		}
			
		#endif // CURRENT CONTROL

		#if DAVECG_SPEED_CURRENT_CTRL   //SPEED CURRENT CONTROL
	
			SFR_PAGE(_cc3, noSST);         		  							// switch to page 3
			l_bemf_aux = (CCU6_MCMOUTL & 0x3F);								// read the present Modulation Pattern....
			if(g_bemf_aux != l_bemf_aux)									// ...compare it with previous Mod Pattern.....
			{																// if Mod Pattern has changed......
				g_bemf_aux = l_bemf_aux;									// then preserve present Mod Pattern...
				//-------ADC_AUX_BEMF();--------------------				// ....and determine the next ADC channel for Zero crossing sensing.
				if((g_bemf_aux == BEMF_AUX_VAL11) || (g_bemf_aux == BEMF_AUX_VAL12)) 
				{															// if phase '1' is Inactive....
					SFR_PAGE(_ad6, noSST);									// switch to page 6
					ADC_CRCR1 = 0x80;										// write channel information to be converted
					ADC_CRMR1 |= 0x40; 										// generate load event	
				} 
				if((g_bemf_aux == BEMF_AUX_VAL21) || (g_bemf_aux == BEMF_AUX_VAL22)) 
				{															// if phase '2' is Inactive.... 
					SFR_PAGE(_ad6, noSST);									// switch to page 6
					ADC_CRCR1 = 0x40;										// write channel information to be converted
					ADC_CRMR1 |= 0x40; 										// generate load event
				} 
				if((g_bemf_aux == BEMF_AUX_VAL31) || (g_bemf_aux == BEMF_AUX_VAL32)) 
				{															// if phase '3' is Inactive.... 
					SFR_PAGE(_ad6, noSST);									// switch to page 6
					ADC_CRCR1 = 0x20;										// write channel information to be converted
					ADC_CRMR1 |= 0x40; 										// generate load event
				}
				//------------------------------------------
			}
	
		SFR_PAGE(_ad0, noSST);
		if((ADC_GLOBSTR & 0x01)==0x00)										// if ADC is not busy.....
		{ 
		
			SFR_PAGE(_ad2, noSST);
			if(ADC_RESR0L & 0x10)											// check whether the result is valid
			{
				ADC_PAGE = 0x02;
				channel	=	ADC_RESR0L & 0x07;
				ADC_PAGE = 0x06;
				if (channel == 3)
				{
					ADC_QINR0 = 0x04;										// write channel information to be converted
					ADC_PAGE = 0x02;										// switch to page 2
					compare_value = (ADC_RESR0L >> 6) & 0x0003;				// read ADC result
					compare_value = compare_value | (((uword)ADC_RESR0H << 2));		//	read ADC result
					compare_value = compare_value>>1;
					high_limit		=	compare_value + (compare_value >> 4);
					low_limit		=	compare_value - (compare_value >> 4);
				}	
				else if(channel == 4)
				{
					ADC_QINR0	=	0x03;									// write channel information to be converted
					ADC_PAGE = 0x02;										// switch to page 2
					g_current = (((((uword)ADC_RESR0H << 2)|(ADC_RESR0L>>6))<<5) & 0x7fff);  //	read ADC result
					OVER_CURRENT_CHECK();
					
				} 
				else
					ADC_QINR0	=	0x03;									// write channel information to be converted
			}
			   
		}
			
		#endif // SPEED CURRENT CONTROL
		#endif // BEMF
		#endif // BLOCK COMMUTATION
		// SHINT_XINTR5Isr_3_IAF_CODE_END 
		
		// USER CODE END

// USER CODE BEGIN (SHINT_XINTR5Isr,7)

// USER CODE END

// USER CODE BEGIN (NodeI1,1)

// USER CODE END

// USER CODE BEGIN (NodeI1,2)
	
// USER CODE END

// USER CODE BEGIN (NodeI1,3)

// USER CODE END

		// USER CODE BEGIN (NodeI1,17)

		// NodeI1_17_IAF_CODE_BEGIN
		#if DAVECG_BLOCK_COMMUTATION
		gb_time_out  = 1;									// set the s/w time out flag to indicate		
														    // the detection of 'Time out'
		motor_stop();										// execute motor_stop routine.
		gb_motor_status = 1;								// set the motor_status s/w flag to 
														    // convey to the control panel that 
														    // motor has been stopped.
		#endif
		// NodeI1_17_IAF_CODE_END


		// USER CODE END


		// USER CODE BEGIN (NodeI1,20)

		// NodeI1_20_IAF_CODE_BEGIN
			#if DAVECG_BLOCK_COMMUTATION
			gb_ctrap = 1;
			#endif
			#if	DAVECG_FIELD_ORIENTED_CONTROL	//FOC 

	    	gb_Trap = 1;
			gb_On = 0;
			MotorStop();
			ucT2state = STATEIDLE;
						
			#endif
		// NodeI1_20_IAF_CODE_END

		// USER CODE END

		// USER CODE BEGIN (NodeI1,21)

		// NodeI1_21_IAF_CODE_BEGIN
		#if DAVECG_BLOCK_COMMUTATION
		#if DAVECG_HALL
		#if DAVECG_VOLTAGE_CTRL
		if(g_once==1)
		{
			gb_hs_error = 1;								   // .....then record the event into s/w flag.
			motor_stop();									   // execute motor_stop routine.
			gb_motor_status = 1;							   // set the motor_status s/w flag to 
		}													   // convey to the control panel that 
															   // motor has been stopped.
		#endif // VOLTAGE_CTRL == 1
		#if DAVECG_CURRENT_CTRL
		if(g_once==1)
		{
			gb_hs_error = 1;								   // .....then record the event into s/w flag.
			// Setting the monitor control bit in status control word to enable the monitor program
			motor_stop();									   // execute motor_stop routine.
			gb_motor_status = 1;							   // set the motor_status s/w flag to 
															   // convey to the control panel that 
		}													   // motor has been stopped.
		#endif // CURRENT_CTRL == 1
		#if DAVECG_SPEED_CTRL
		if(g_once==1)
		{
			gb_hs_error = 1;								   // .....then record the event into s/w flag.
			motor_stop();									   // execute motor_stop routine.
			gb_motor_status = 1;							   // set the motor_status s/w flag to 
															   // convey to the control panel that 
		}													   // motor has been stopped.
		#endif // SPEED_CTRL == 1
		#if DAVECG_SPEED_CURRENT_CTRL   //SPEED CURRENT CONTROL
		if(g_once==1)
		{
			gb_hs_error = 1;								   // .....then record the event into s/w flag.
			motor_stop();									   // execute motor_stop routine.
			gb_motor_status = 1;							   // set the motor_status s/w flag to 
															   // convey to the control panel that 
		}													   // motor has been stopped.
		#endif //SPEED_CURRENT_CTRL
		#endif	// HALL = 1
		#endif //BLOCK COMMUTATION
		// NodeI1_21_IAF_CODE_END

		// USER CODE END

// USER CODE BEGIN (NodeI1,5)

// USER CODE END

// USER CODE BEGIN (NodeI2,1)
// NodeI2_1_IAF_CODE_BEGIN
	
// NodeI2_1_IAF_CODE_END
// USER CODE END

// USER CODE BEGIN (NodeI2,2)

// USER CODE END

// USER CODE BEGIN (NodeI2,3)

// USER CODE END

		// USER CODE BEGIN (NodeI2,14)

		// NodeI2_14_IAF_CODE_BEGIN
		#if DAVECG_BLOCK_COMMUTATION
		#if DAVECG_BEMF
			// BEMF slope calculation
			//-------CCU6_BEMF_SLOPE_BLOCK-------------------------------------
				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_SPEED_CALCU
			//-----CCU6_Channel_1_SPEED_CALCULATION_BLOCK----------------------
			#if DAVECG_VOLTAGE_CTRL
				if (g_state == 3)											// if motor is running in steady state( state 3)...
				{
					g_speed_CC61 = speed_calculation(g_time_measure, &g_speed_counter, PP_FACTOR);
				}															// ....then get the present 'speed'.
			#endif //VOLTAGE CONTROL

			#endif	//SPEED_CALCU

			#if DAVECG_CURRENT_CTRL 

			if (g_state == 3)												// if motor is running in steady state( state 3)...
			{
				if(pi_task_counter==1)
				{
				#if DAVECG_SPEED_CALCU
				g_speed_CC61 = speed_calculation(g_time_measure, &g_speed_counter, PP_FACTOR); // get the present 'speed'.
				#endif 
				pi_task_counter=0;
				}
				else
				{
              
				pi_task_counter=1;
				}
			
			}
			#endif //CURRENT CONTROL
			#if DAVECG_SPEED_CTRL

			if (g_state == 3)												// if motor is running in steady state( state 3)...
			{
				
				g_speed_CC61 = speed_calculation(g_time_measure, &g_speed_counter, PP_FACTOR);  // ....then get the present 'speed'.
					
				if (g_speed_counter < (LIMIT_C))							// if motor speed is too small....
				 {
			       g_speed_CC61 = 0;										// ....then reset speed to '0'.
			       pi_task_counter=1;
			       				
				 }  
				pi_task_counter++;	
						
				if(pi_task_counter > 1)
				{
						
					g_pi_controller_out = pi_controller_16bit(&g_pi_array.intg_buffer.S_long,g_speed_setref,g_speed_CC61);  
																			// compute new dutycycle using pi_controller.
					g_duty_cyc = PULSE_PERIOD - g_pi_controller_out;		// map the dutycycle onto the PWM scale....		  			
					g_increment_free = 1;	  
					
					pi_task_counter=0;
				} 	

			}

			#endif  //SPEED CONTROL
			#if DAVECG_SPEED_CURRENT_CTRL 
			if (g_state == 3)												// if motor is running in steady state( state 3)...
			{
								
				if (g_speed_counter < (LIMIT_C))							// if motor speed is too small....
				 {
		        g_speed_CC61 = 0;											// ....then reset speed to '0'.
				 }  

			g_speed_CC61 = speed_calculation(g_time_measure, &g_speed_counter, PP_FACTOR);  // get the present 'speed'.
				
			}
			#endif  //SPEED CURRENT CONTROL
		
			 //---------------------------------------------------------
		#endif	//BEMF == 1
		#endif  //BLOCK COMMUTATION
		// NodeI2_14_IAF_CODE_END

		// USER CODE END

// USER CODE BEGIN (NodeI2,5)

// USER CODE END


// USER CODE BEGIN (NodeI2,17)
	
  // NodeI2_17_IAF_CODE_BEGIN
		#if	DAVECG_FIELD_ORIENTED_CONTROL	//FOC 
		T12_ISR_Handler();					//start of calculation      
    
	#if DAVECG_CONTROL_PANEL_ENABLE
	if( countMO1-- == 0 )
	{
		countMO1 = CAN_MO1_RATE;
		CAN_Transmit_MO1();
	}
	#if DAVECG_EnGraphDisplay   //Graph Diplay enable / disable option
     if( gb_On && countMO3-- == 0) // Send continuous fast data only, when motor is started
	 {
	 	countMO3 = CAN_MO3_RATE;
		CAN_Transmit_MO3();
	 }
	#endif
	#endif
		#endif
	
  // NodeI2_17_IAF_CODE_END    

 // USER CODE END
// USER CODE BEGIN (NodeI3,1)

// USER CODE END

		// USER CODE BEGIN (NodeI3,2)

		  // NodeI3_2_IAF_CODE_BEGIN
	        #if DAVECG_BLOCK_COMMUTATION
			#if DAVECG_HALL
			#if DAVECG_SPEED_CALCU
			unsigned int temp_speed;
			#endif	//SPEED_CALCU
			#endif // HALL == 1
			#endif //BLOCK COMMUTATION	
			// NodeI3_2_IAF_CODE_END

		// USER CODE END

  // USER CODE BEGIN (NodeI3,3)

  // USER CODE END

    // USER CODE BEGIN (NodeI3,14)

    // NodeI3_14_IAF_CODE_BEGIN
	#if DAVECG_BLOCK_COMMUTATION
	#if DAVECG_HALL
	//-------------MCMOUT_UPDATE---------------------------
	 SFR_PAGE(_cc3, noSST);         										// switch to page 3
	 curr_hp	=	((CCU6_MCMOUTH & 0x38)>> 3);

	 SFR_PAGE(_cc0, noSST);         										// switch to page 0
	 CCU6_MCMOUTSL	=	closed_loop[curr_hp + g_direction];

	 curr_hp	=	exp_hall_patt[curr_hp + g_direction];
	 exp_hp		=	exp_hall_patt[curr_hp + g_direction];
	 CCU6_MCMOUTSH	=	((curr_hp << 3) & 0x38) | exp_hp;
	//-----------------------------------------------------
	//-------------CCU6_NI2_SPEED_CAL_CBLOCK---------------
	#if DAVECG_SPEED_CALCU
	SFR_PAGE(_cc1, noSST);													// switch to page 1
	temp_speed = CCU6_CC60SRLH;	
    g_speed_CC60 = speed_calculation(temp_speed, &g_hall_counter, PP_FACTOR);
    if (g_hall_counter < (LIMIT_C)) 										// if motor speed is too small....
    {
        g_speed_CC60 = 0;													// ....then reset speed to '0'.
    }
	
	#if DAVECG_SPEED_CTRL
	g_pi_controller_out = pi_controller_16bit(&g_pi_array.intg_buffer.S_long,g_speed_setref,g_speed_CC60); 
																			// compute new dutycycle using pi_controller.
	g_duty_cyc = PULSE_PERIOD - g_pi_controller_out;						// map the dutycycle onto the PWM scale.... 	
	#endif

	#endif	//SPEED_CALCU
	   													   
	//-----------------------------------------------------
	#if DAVECG_VOLTAGE_CTRL 
	#if DAVECG_CURRENT_SENSE
	//--------------ADC_ERR2_CBLOCK------------------------
	SFR_PAGE(_ad2, noSST);
	g_current = (((((uword)ADC_RESR0H << 2)|(ADC_RESR0L>>6))<<5) & 0x7fff); // Read ADC Result.
	SFR_PAGE(_ad6, noSST);
	ADC_CRCR1 = 0x10;														// select ADC CH 4 for next conversion.
	//-----------------------------------------------------
	OVER_CURRENT_CHECK();
	#endif 
	#if DAVECG_CONTROL_PANEL_ENABLE
	trace();								   
	#endif	// CONTROL_PANEL_ENABLE
	#endif // VOLTAGE_CTRL == 1
	#endif	// HALL = 1
	#endif //BLOCK COMMUTATON
	// NodeI3_14_IAF_CODE_END

    // USER CODE END

    // USER CODE BEGIN (NodeI3,19)

    // NodeI3_19_IAF_CODE_BEGIN

#if	DAVECG_FIELD_ORIENTED_CONTROL	//FOC 
	//**************************************
	//* Request shadow transfer for 
	//* T12 (CCU6)
	//**************************************
	CCU6_PAGE = 0x00;            // switch to page 0 noSST
	CCU6_TCTR4L = 0x40;

	ECCIP2 = 1;        // interrupt node2  enable 
#endif
#if DAVECG_BLOCK_COMMUTATION
#if DAVECG_HALL
#if DAVECG_CURRENT_CTRL 
	//--------------ADC_ERR2_CBLOCK-----------------------
	switch(task_counter)
	{
		case 0:
		SFR_PAGE(_ad2, noSST);
		g_current = (((((uword)ADC_RESR0H << 2)|(ADC_RESR0L>>6))<<5) & 0x7fff);  // Read ADC result
		SFR_PAGE(_ad6, noSST);
		ADC_CRCR1 = 0x10;													// select ADC CH 4 for next conversion.
		OVER_CURRENT_CHECK();
		task_counter = 1;
		break;
	//-----------------------------------------------------
		case 1:
		g_pi_controller_out = pi_controller_16bit(&g_pi_array.intg_buffer.S_long,g_current_rf,g_current);
																			// compute new dutycycle using pi_controller.
		g_duty_cyc = PULSE_PERIOD - g_pi_controller_out;					// map the dutycycle onto the PWM scale....
		task_counter = 2;
		break;
	//------------------------------------------------------
		case 2:
	    SFR_PAGE(_cc0, noSST);							   
		CCU6_CC63SRLH = g_duty_cyc;											// ....load it into the PWM duty cycle reg.
		CCU6_TCTR4H = 0x40;
		CCU6_TCTR4L = 0x00;		
		task_counter = 3;
		break;
	//-----------------------------------------------------
		case 3:
		#if DAVECG_CONTROL_PANEL_ENABLE
		trace();															// buffer the samples of the variable selected for graph plotting.
		#endif
		task_counter = 0;
		break;
	//-----------------------------------------------------
	}
#endif // CURRENT_CTRL == 1
#if DAVECG_SPEED_CTRL 
	//--------------CCU6_NODEI3_CBLOCK();------------------
	switch(task_counter)
	{
		//-------------------------------------------------
		case 0:
		#if DAVECG_CURRENT_SENSE
		SFR_PAGE(_ad2, noSST);
		g_current = (((((uword)ADC_RESR0H << 2)|(ADC_RESR0L>>6))<<5) & 0x7fff);
		SFR_PAGE(_ad6, noSST);
		ADC_CRCR1 = 0x10;													// select ADC CH 4 for next conversion.
		OVER_CURRENT_CHECK();
		#endif
		task_counter = 1;
		break;
		//-------------------------------------------------
		case 1:
	    SFR_PAGE(_cc0, noSST);												// switch to page 0
		CCU6_CC63SRLH = g_duty_cyc;											// ....load it into the PWM duty cycle reg.
		CCU6_TCTR4H = 0x40;
		CCU6_TCTR4L = 0x00;
		task_counter = 2;
		break;
		//-------------------------------------------------
		case 2:
		#if DAVECG_CONTROL_PANEL_ENABLE
		trace();															// buffer the samples of the variable selected for graph plotting.
		#endif
		g_increment_free = 1;												// acknowledge to other routines that Interrupt 
		task_counter = 0;
		break;
		//-------------------------------------------------
	}
	//-----------------------------------------------------
#endif // SPEED_CTRL == 1
#if DAVECG_SPEED_CURRENT_CTRL 
	//--------------CCU6_NODEI3_CBLOCK();------------------
	//++++++++compulsory tasks++++++++++++++++++++++++
	    if(g_duty_cyc < DUTY_CYCLE_MIN)
		g_duty_cyc = DUTY_CYCLE_MIN;
		else if(g_duty_cyc > DUTY_CYCLE_MAX)
		g_duty_cyc = DUTY_CYCLE_MAX;
		
		
		SFR_PAGE(_cc0, noSST);												// switch to page 0
		CCU6_CC63SRLH = g_duty_cyc;											// ....load it into the PWM duty cycle reg.
		CCU6_TCTR4H = 0x40;
		CCU6_TCTR4L = 0x00;		

		g_increment_free = 1;								    
	//++++++++++++++++++++++++++++++++++++++++++++++++
	//++++++Time sharing tasks++++++++++++++++++++++++
		switch(task_counter)
		{
		case 0:																// PI_CURRENT_CONTROL
	    g_pi_speed_out = pi_speed_controller_16bit(&g_pi_speed_array.intg_buffer.S_long, g_speed_setref, g_speed_CC60);		
		task_counter = 1;  
 		break;
		
		case 1:																// PI_SPEED_CONTROL
		g_pi_current_out = pi_current_controller_16bit(&g_pi_current_array.intg_buffer.S_long,g_pi_speed_out,g_current);		
																			// compute new dutycycle using pi_controller.
		g_duty_cyc = PULSE_PERIOD - g_pi_current_out;       
		task_counter = 2;
		break;
		
		case 2:																// Current Measurement from ADC
		SFR_PAGE(_ad2, noSST);
		g_current = (((((uword)ADC_RESR0H << 2)|(ADC_RESR0L>>6))<<5) & 0x7fff);
		SFR_PAGE(_ad6, noSST);
		ADC_CRCR1 = 0x10;													// select ADC CH 4 for next conversion.
		OVER_CURRENT_CHECK();
		task_counter = 3;
		break;

		case 3:																// Graph Variable sampling
		#if DAVECG_CONTROL_PANEL_ENABLE
		trace();															// buffer the samples of the variable selected for graph plotting.
		#endif
		task_counter = 4;
		break;
		
		case 4://Over current checking.....
		task_counter = 0; 
		break;
		//-------------------------------------------------
		}
	//-----------------------------------------------------
#endif // SPEED_CURRENT_CTRL == 1
#endif	// HALL = 1

#if DAVECG_BEMF
#if DAVECG_VOLTAGE_CTRL
	
    if(g_state<3)
	{		
		switch(task_counter)
		{
		case 0:
		commutation();									   
		task_counter = 1;
		break;
		//-----------------------------------------------------
		case 1:
		ramp();
		#if DAVECG_CONTROL_PANEL_ENABLE
		trace();								   
		#endif	// CONTROL_PANEL_ENABLE	
		task_counter = 2;
		break;
		//------------------------------------------------------
		case 2:
		ramp();	
	   	task_counter = 3;
		break;
		//-----------------------------------------------------
		case 3:
		ramp();	
		task_counter = 0;
		break;
		//-----------------------------------------------------
		}		
	}
	else
	{
		if(g_slope==1 || g_slope==0)
		{
		
			SFR_PAGE(_ad6, noSST); 
	  		ADC_CRMR1   = ADC_CRMR1| 0x05;    
		
			commutation();
			#if DAVECG_CONTROL_PANEL_ENABLE
			trace();								   
			#endif	// CONTROL_PANEL_ENABLE
		}
		else
		{
			SFR_PAGE(_ad6, noSST); 
	  		ADC_CRMR1    = ADC_CRMR1& 0xFA;   
		}
	}
	//---------------------------------------------------
	//--------------------------------------------------------------------
	#endif	// VCTRL == 1
	#if DAVECG_CURRENT_CTRL
	
	if(g_duty_cyc < DUTY_CYCLE_MIN)
		g_duty_cyc = DUTY_CYCLE_MIN;
	else if(g_duty_cyc > DUTY_CYCLE_MAX)
		g_duty_cyc = DUTY_CYCLE_MAX;


	SFR_PAGE(_cc0, noSST);									// switch to page 0
	CCU6_CC63SRLH = g_duty_cyc;								// ....load it into the PWM duty cycle reg.
	CCU6_TCTR4H = 0x40;
	CCU6_TCTR4L = 0x00;
	//------------------------------------			
	
	if(g_state<3)
	{
		switch(task_counter)
		{
		case 0:
		ramp();
 		commutation();									  
		task_counter = 1;
		break;
		//-------------------------------------------------
		case 1:
		ramp();
		task_counter = 2;
		break;
		//-------------------------------------------------
		case 2:
		ramp();
		#if DAVECG_CONTROL_PANEL_ENABLE
		trace();								   
		#endif	// CONTROL_PANEL_ENABLE										   
		task_counter = 0;
		break;
		//-------------------------------------------------
		}
		pi_task_counter=0;
	}
	else
	{
		if(g_slope==1 || g_slope==0)
		{
		
			SFR_PAGE(_ad6, noSST); 
	  		ADC_CRMR1   = ADC_CRMR1| 0x05;    
		
			commutation();
			#if DAVECG_CONTROL_PANEL_ENABLE
			trace();								   
			#endif	// CONTROL_PANEL_ENABLE
		}
		else
		{
			SFR_PAGE(_ad6, noSST); 
	  		ADC_CRMR1    = ADC_CRMR1& 0xFA;   
            g_pi_controller_out = pi_controller_16bit(&g_pi_array.intg_buffer.S_long,g_current_rf,g_current); 
																			// compute new dutycycle using pi_controller.
			g_duty_cyc = PULSE_PERIOD - g_pi_controller_out;	
		}
	}
	#endif	// CURRENT_CTRL == 1


	#if DAVECG_SPEED_CTRL

	//---------------------------------------------------------
	if(g_duty_cyc < DUTY_CYCLE_MIN)
		g_duty_cyc = DUTY_CYCLE_MIN;
	else if(g_duty_cyc > DUTY_CYCLE_MAX)
		g_duty_cyc = DUTY_CYCLE_MAX;
	
	SFR_PAGE(_cc0, noSST);										// switch to page 0
	CCU6_CC63SRLH = g_duty_cyc;									// ....load it into the PWM duty cycle reg.
	CCU6_TCTR4H = 0x40;
	CCU6_TCTR4L = 0x00;
	//------------------------------------			
	if(g_state<3)
	{
		switch(task_counter)
		{
		case 0:
		ramp();
 		commutation();									   
		task_counter = 1;
		break;
		//-------------------------------------------------
		case 1:
		ramp();
		task_counter = 2;
		break;
		//-------------------------------------------------
		case 2:
		ramp();
		#if DAVECG_CONTROL_PANEL_ENABLE
		trace();								   
		#endif	// CONTROL_PANEL_ENABLE										   
		task_counter = 0;
		break;
		//-------------------------------------------------
		}
	}
	else
	{
		if(g_slope==1 || g_slope==0)
		{
			commutation();
			#if DAVECG_CONTROL_PANEL_ENABLE
			trace();								   
			#endif	// CONTROL_PANEL_ENABLE
		}
		
	}

	
	//--------------------------------------------------------------------
	#endif	// SPEED_CTRL == 1

	#if DAVECG_SPEED_CURRENT_CTRL
	//---------------------------------------------------------
	
	if(g_duty_cyc < DUTY_CYCLE_MIN)
		g_duty_cyc = DUTY_CYCLE_MIN;
	else if(g_duty_cyc > DUTY_CYCLE_MAX)
		g_duty_cyc = DUTY_CYCLE_MAX;


	SFR_PAGE(_cc0, noSST);										// switch to page 0
	CCU6_CC63SRLH = g_duty_cyc;									// ....load it into the PWM duty cycle reg.
	CCU6_TCTR4H = 0x40;
	CCU6_TCTR4L = 0x00;
	//------------------------------------			
	if(g_state<3)
	{
		switch(task_counter)
		{
		case 0:
		ramp();
		commutation();									   
		task_counter = 1;
		break;
		//-------------------------------------------------
		case 1:
		ramp();
		task_counter = 2;
		break;
		//-------------------------------------------------
		case 2:
		ramp();
	    task_counter = 3;  
		break;
		//-------------------------------------------------
		case 3:
		ramp();
		#if DAVECG_CONTROL_PANEL_ENABLE
		trace();												// buffer the samples of the variable selected for graph plotting.
		#endif								   
		task_counter = 0;
		break;
		//-------------------------------------------------
		}
	}
	
	else
	{
		if(g_slope==1 || g_slope==0)
		{
			commutation();
			#if DAVECG_CONTROL_PANEL_ENABLE
			trace();								   
			#endif	// CONTROL_PANEL_ENABLE
		}
		else
		{
			if(pi_task_counter==2)
			{
				g_pi_speed_out = pi_speed_controller_16bit(&g_pi_speed_array.intg_buffer.S_long, g_speed_setref, g_speed_CC61);
				pi_task_counter=3;
			
			}
			else if(pi_task_counter==4)
			{
				if(g_pi_speed_out > g_pi_speed_array.ymax)
				g_pi_current_array.ymax		=	g_pi_current_out;

				g_pi_current_out = pi_current_controller_16bit(&g_pi_current_array.intg_buffer.S_long,g_pi_speed_out,g_current);
																			// compute new dutycycle using pi_controller.
				g_duty_cyc = PULSE_PERIOD - g_pi_current_out;				// map the dutycycle onto the PWM scale....
				g_increment_free = 1;
				pi_task_counter=0;
			}
			else
				pi_task_counter++;
			
		}
	}

//--------------------------------------------------------------------
	#endif	// SPEED_CURRENT_CTRL == 1
#endif	// BEMF == 1
#endif // BLOCK COMMUTATION
    // NodeI3_19_IAF_CODE_END

    // USER CODE END


    // USER CODE BEGIN (NodeI3,23)
	
    // USER CODE END


  // USER CODE BEGIN (NodeI3,5)

  // USER CODE END


// USER CODE BEGIN (SHARED_INT_General,10)

// USER CODE END


