//*****************************************************************************/
// @Module         PI Controller
// @Filename       PI_CTRL.C
// @Controller     XE164 Series
//
//
// @Description    This file contains functions for PI Control algorithms
//
//****************************************************************************

//****************************************************************************
// @Function  
// 
// int pi_controller64(int reference,int actual) 
//
//----------------------------------------------------------------------------
// @Description  
//
//  PI-Controller
//      derived from transfer function G = kp + 1/(p*Ti)
//
//      e(k)   = reference - actual          T0 = 
//  yn(k+1)= yn(k)   + ki * e(k)             ki = T0/Ti
//  y(k+1) = yn(k+1) + kp * e(k) * 64
//----------------------------------------------------------------------------
//****************************************************************************
// @Project Includes
//****************************************************************************

//****************************************************************************
// @Macros
//****************************************************************************
 
//****************************************************************************
// @Defines
//****************************************************************************
  
//****************************************************************************
// @Typedefs
//****************************************************************************

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


//****************************************************************************
// @External Prototypes
//****************************************************************************
#if DAVECG_ControlPanelKBS.POT_ENABLE
unsigned int MUL_MAC(unsigned int a, unsigned int b);
#endif
//****************************************************************************
// @Prototypes Of Local Functions
//****************************************************************************

//****************************************************************************
#if DAVECG_ControlAlgoKBS.SPEED_CTRL
#if DAVECG_ControlAlgoKBS.KEIL
unsigned int  PIControllerSpeed(unsigned int *PI_Parameter,unsigned int  Actual, unsigned int Ref)
{
	int temp;

	#pragma asm
    MOV     R12,MCW         ;Save MCW register
    MOV     MCW,#1536       ;Set saturation and shift left
    MOV     R11,ZEROS       ;Load zero in R11
     
	CoLOAD  R11,R10         ;Load Accumulator (High) with R10 (reference)
    CoSUB   R11,R9        	;error = reference - actual
    CoSTORE R9,MAS   	    ;Load error in R9

	MOV  	R1,[R8+]	 	;Load Kp value to R1
	MOV  	R2,[R8+]	 	;Load Ki value to R2
	MOV  	R3,[R8+]	 	;Load Ymin value to R3
	MOV  	R4,[R8+]	 	;Load Ymax value to R4

    MOV     R5,[R8+]        ;Load integral buffer (Low) value to R5   
    CoLOAD  R5,[R8]         ;Load integral buffer in accumulator 
     
	CoMAC   R2,R9           ;yn = Ki * error + yn

    CoMIN   R11,R4         	;Limit max yn
    CoMAX   R11,R3          ;Limit min yn
    CoSTORE R6,MAH          ;Store yn-high in R6 
    CoSTORE R5,MAL          ;Store yn-low  in R5    
    
	MOV     [R8],R6        	;Store R6 in integral buffer(High)  
    MOV     [-R8],R5        ;Store R5 in integral buffer(Low)   
    CoMUL   R1,R9           ;Kp * error 
    CoSHL   #6              ;64 * Kp * error    
    CoADD   R5,R6           ;y = yn + (64 * Kp * error) 
    CoMIN   R11,R4          ;Limit max y    
    CoMAX   R11,R3          ;Limit min y    
    CoSTORE R4,MAS          ;Store y-high in R4 (return register)   
    MOV     MCW,R12		    ;Restore MCW register value
		    
#pragma endasm
   
   temp=*PI_Parameter;
   temp=Actual;
   temp=Ref;

}
#endif //End of Keil
#if DAVECG_ControlAlgoKBS.Tasking
unsigned int  PIControllerSpeed(unsigned int *PI_Parameter,unsigned int  Actual, unsigned int Ref)
{
 unsigned int PIval;

 #pragma asm (@w1 = PI_Parameter,@w2 = Actual,@w3 = Ref ,@4)

    MOV     R7,MCW         ;Save MCW register
    MOV     MCW,#1536       ;Set saturation and shift left
    MOV     R0,ZEROS       ;Load zero in R11
     
	CoLOAD  R0,@w3         ;Load Accumulator (High) with R10 (reference)
    CoSUB   R0,@w2        	;error = reference - actual
    CoSTORE R9,MAS   	    ;Load error in R9

	MOV  	R1,[@w1+]	 	;Load Kp value to R1
	MOV  	R2,[@w1+]	 	;Load Ki value to R2
	MOV  	R3,[@w1+]	 	;Load Ymin value to R3
	MOV  	R4,[@w1+]	 	;Load Ymax value to R4

    MOV     R5,[@w1+]        ;Load integral buffer (Low) value to R5   
    CoLOAD  R5,[@w1]         ;Load integral buffer in accumulator 
     
	CoMAC   R2,R9           ;yn = Ki * error + yn

    CoMIN   R0,R4         	;Limit max yn
    CoMAX   R0,R3          ;Limit min yn
    CoSTORE R6,MAH          ;Store yn-high in R6 
    CoSTORE R5,MAL          ;Store yn-low  in R5    
    
	MOV     [@w1],R6        	;Store R6 in integral buffer(High)  
    MOV     [-@w1],R5        ;Store R5 in integral buffer(Low)   
    CoMUL   R1,R9           ;Kp * error 
    CoSHL   #6              ;64 * Kp * error    
    CoADD   R5,R6           ;y = yn + (64 * Kp * error) 
    CoMIN   R0,R4          ;Limit max y    
    CoMAX   R0,R3          ;Limit min y    
    CoSTORE @4,MAS          ;Store y-high in R4 (return register)   
    MOV     MCW,R7	    ;Restore MCW register value
		    
#pragma endasm	(PIval = @4)
   
   	return(PIval);
}


#endif //End of TASKING
#endif //End of SC

#if DAVECG_ControlAlgoKBS.CURRENT_CTRL
#if DAVECG_ControlAlgoKBS.KEIL

unsigned int PIControllerCurrent(unsigned int *PI_Parameter,unsigned int Actual, unsigned int Ref)
{
int temp;

	#pragma asm
    MOV     R12,MCW         ;Save MCW register
    MOV     MCW,#1536       ;Set saturation and shift left
    MOV     R11,ZEROS       ;Load zero in R11
     
	CoLOAD  R11,R10         ;Load Accumulator (High) with R10 (reference)
    CoSUB   R11,R9        	;error = reference - actual
    CoSTORE R9,MAS   	    ;Load error in R9

	MOV  	R1,[R8+]	 	;Load Kp value to R1
	MOV  	R2,[R8+]	 	;Load Ki value to R2
	MOV  	R3,[R8+]	 	;Load Ymin value to R3
	MOV  	R4,[R8+]	 	;Load Ymax value to R4

    MOV     R5,[R8+]        ;Load integral buffer (Low) value to R5   
    CoLOAD  R5,[R8]         ;Load integral buffer in accumulator 
     
	CoMAC   R2,R9           ;yn = Ki * error + yn

    CoMIN   R11,R4         	;Limit max yn
    CoMAX   R11,R3          ;Limit min yn
    CoSTORE R6,MAH          ;Store yn-high in R6 
    CoSTORE R5,MAL          ;Store yn-low  in R5    
    
	MOV     [-R8],R6        	;Store R6 in integral buffer(High)  
 
    CoMUL   R1,R9           ;Kp * error 
    CoSHL   #6              ;64 * Kp * error    
    CoADD   R5,R6           ;y = yn + (64 * Kp * error) 
    CoMIN   R11,R4          ;Limit max y    
    CoMAX   R11,R3          ;Limit min y    
    CoSTORE R4,MAS          ;Store y-high in R4 (return register)   
    MOV     MCW,R12		    ;Restore MCW register value
		    
#pragma endasm
   
   temp=*PI_Parameter;
   temp=Actual;
   temp=Ref;

}
#endif //End of Keil
#if DAVECG_ControlAlgoKBS.Tasking

unsigned int PIControllerCurrent(unsigned int *PI_Parameter,unsigned int Actual, unsigned int Ref)
{
	 unsigned int PIval;

 #pragma asm (@w1 = PI_Parameter,@w2 = Actual,@w3 = Ref ,@4)

    MOV     R7,MCW         ;Save MCW register
    MOV     MCW,#1536       ;Set saturation and shift left
    MOV     R0,ZEROS       ;Load zero in R11
     
	CoLOAD  R0,@w3         ;Load Accumulator (High) with R10 (reference)
    CoSUB   R0,@w2        	;error = reference - actual
    CoSTORE R9,MAS   	    ;Load error in R9

	MOV  	R1,[@w1+]	 	;Load Kp value to R1
	MOV  	R2,[@w1+]	 	;Load Ki value to R2
	MOV  	R3,[@w1+]	 	;Load Ymin value to R3
	MOV  	R4,[@w1+]	 	;Load Ymax value to R4

    MOV     R5,[@w1+]        ;Load integral buffer (Low) value to R5   
    CoLOAD  R5,[@w1]         ;Load integral buffer in accumulator 
     
	CoMAC   R2,R9           ;yn = Ki * error + yn

    CoMIN   R0,R4         	;Limit max yn
    CoMAX   R0,R3          	;Limit min yn
    CoSTORE R6,MAH          ;Store yn-high in R6 
    CoSTORE R5,MAL          ;Store yn-low  in R5    
    
	MOV     [-@w1],R6        ;Store R6 in integral buffer(High)  

    CoMUL   R1,R9           ;Kp * error 
    CoSHL   #6              ;64 * Kp * error    
    CoADD   R5,R6           ;y = yn + (64 * Kp * error) 
    CoMIN   R0,R4           ;Limit max y    
    CoMAX   R0,R3           ;Limit min y    
    CoSTORE @4,MAS          ;Store y-high in R4 (return register)   
    MOV     MCW,R7	     	;Restore MCW register value
		    
#pragma endasm	(PIval = @4)
   
   	return(PIval);
}
#endif //End of TASKING
#endif //End of CC

#if DAVECG_ControlAlgoKBS.SPEED_CURRENT_CTRL
#if DAVECG_ControlAlgoKBS.KEIL

unsigned int PIControllerSpeed(unsigned int *PI_Parameter,unsigned int Actual, unsigned int Ref)
{
	int temp;

	#pragma asm
    MOV     R12,MCW         ;Save MCW register
    MOV     MCW,#1536       ;Set saturation and shift left
    MOV     R11,ZEROS       ;Load zero in R11
     
	CoLOAD  R11,R10         ;Load Accumulator (High) with R10 (reference)
    CoSUB   R11,R9        	;error = reference - actual
    CoSTORE R9,MAS   	    ;Load error in R9

	MOV  	R1,[R8+]	 	;Load Kp value to R1
	MOV  	R2,[R8+]	 	;Load Ki value to R2
	MOV  	R3,[R8+]	 	;Load Ymin value to R3
	MOV  	R4,[R8+]	 	;Load Ymax value to R4

    MOV     R5,[R8+]        ;Load integral buffer (Low) value to R5   
    CoLOAD  R5,[R8]         ;Load integral buffer in accumulator 
     
	CoMAC   R2,R9           ;yn = Ki * error + yn

    CoMIN   R11,R4         	;Limit max yn
    CoMAX   R11,R3          ;Limit min yn
    CoSTORE R6,MAH          ;Store yn-high in R6 
    CoSTORE R5,MAL          ;Store yn-low  in R5    
    
	MOV     [R8],R6        	;Store R6 in integral buffer(High)  
    MOV     [-R8],R5        ;Store R5 in integral buffer(Low)   
    CoMUL   R1,R9           ;Kp * error 
    CoSHL   #6              ;64 * Kp * error    
    CoADD   R5,R6           ;y = yn + (64 * Kp * error) 
    CoMIN   R11,R4          ;Limit max y    
    CoMAX   R11,R3          ;Limit min y    
    CoSTORE R4,MAS          ;Store y-high in R4 (return register)   
    MOV     MCW,R12		    ;Restore MCW register value
		    
#pragma endasm
   
   temp=*PI_Parameter;
   temp=Actual;
   temp=Ref;
}
unsigned int PIControllerCurrent(unsigned int *PI_Parameter,unsigned int Actual, unsigned int Ref)
{
int temp;

	#pragma asm
    MOV     R12,MCW         ;Save MCW register
    MOV     MCW,#1536       ;Set saturation and shift left
    MOV     R11,ZEROS       ;Load zero in R11
     
	CoLOAD  R11,R10         ;Load Accumulator (High) with R10 (reference)
    CoSUB   R11,R9        	;error = reference - actual
    CoSTORE R9,MAS   	    ;Load error in R9

	MOV  	R1,[R8+]	 	;Load Kp value to R1
	MOV  	R2,[R8+]	 	;Load Ki value to R2
	MOV  	R3,[R8+]	 	;Load Ymin value to R3
	MOV  	R4,[R8+]	 	;Load Ymax value to R4

    MOV     R5,[R8+]        ;Load integral buffer (Low) value to R5   
    CoLOAD  R5,[R8]         ;Load integral buffer in accumulator 
     
	CoMAC   R2,R9           ;yn = Ki * error + yn

    CoMIN   R11,R4         	;Limit max yn
    CoMAX   R11,R3          ;Limit min yn
    CoSTORE R6,MAH          ;Store yn-high in R6 
    CoSTORE R5,MAL          ;Store yn-low  in R5    
    
	MOV     [-R8],R6        	;Store R6 in integral buffer(High)  
 
    CoMUL   R1,R9           ;Kp * error 
    CoSHL   #6              ;64 * Kp * error    
    CoADD   R5,R6           ;y = yn + (64 * Kp * error) 
    CoMIN   R11,R4          ;Limit max y    
    CoMAX   R11,R3          ;Limit min y    
    CoSTORE R4,MAS          ;Store y-high in R4 (return register)   
    MOV     MCW,R12		    ;Restore MCW register value
		    
#pragma endasm
   
   temp=*PI_Parameter;
   temp=Actual;
   temp=Ref;

}
#endif //End of Keil
#if DAVECG_ControlAlgoKBS.Tasking

unsigned int  PIControllerSpeed(unsigned int *PI_Parameter,unsigned int  Actual, unsigned int Ref)
{
 unsigned int PIval;

 #pragma asm (@w1 = PI_Parameter,@w2 = Actual,@w3 = Ref ,@4)

    MOV     R7,MCW         ;Save MCW register
    MOV     MCW,#1536       ;Set saturation and shift left
    MOV     R0,ZEROS       ;Load zero in R11
     
	CoLOAD  R0,@w3         ;Load Accumulator (High) with R10 (reference)
    CoSUB   R0,@w2        	;error = reference - actual
    CoSTORE R9,MAS   	    ;Load error in R9

	MOV  	R1,[@w1+]	 	;Load Kp value to R1
	MOV  	R2,[@w1+]	 	;Load Ki value to R2
	MOV  	R3,[@w1+]	 	;Load Ymin value to R3
	MOV  	R4,[@w1+]	 	;Load Ymax value to R4

    MOV     R5,[@w1+]        ;Load integral buffer (Low) value to R5   
    CoLOAD  R5,[@w1]         ;Load integral buffer in accumulator 
     
	CoMAC   R2,R9           ;yn = Ki * error + yn

    CoMIN   R0,R4         	;Limit max yn
    CoMAX   R0,R3          ;Limit min yn
    CoSTORE R6,MAH          ;Store yn-high in R6 
    CoSTORE R5,MAL          ;Store yn-low  in R5    
    
	MOV     [@w1],R6        	;Store R6 in integral buffer(High)  
    MOV     [-@w1],R5        ;Store R5 in integral buffer(Low)   
    CoMUL   R1,R9           ;Kp * error 
    CoSHL   #6              ;64 * Kp * error    
    CoADD   R5,R6           ;y = yn + (64 * Kp * error) 
    CoMIN   R0,R4          ;Limit max y    
    CoMAX   R0,R3          ;Limit min y    
    CoSTORE @4,MAS          ;Store y-high in R4 (return register)   
    MOV     MCW,R7	    ;Restore MCW register value
		    
#pragma endasm	(PIval = @4)
   
   	return(PIval);

}

unsigned int PIControllerCurrent(unsigned int *PI_Parameter,unsigned int Actual, unsigned int Ref)
{
	 unsigned int PIval;

 #pragma asm (@w1 = PI_Parameter,@w2 = Actual,@w3 = Ref ,@4)

    MOV     R7,MCW         ;Save MCW register
    MOV     MCW,#1536       ;Set saturation and shift left
    MOV     R0,ZEROS       ;Load zero in R11
     
	CoLOAD  R0,@w3         ;Load Accumulator (High) with R10 (reference)
    CoSUB   R0,@w2        	;error = reference - actual
    CoSTORE R9,MAS   	    ;Load error in R9

	MOV  	R1,[@w1+]	 	;Load Kp value to R1
	MOV  	R2,[@w1+]	 	;Load Ki value to R2
	MOV  	R3,[@w1+]	 	;Load Ymin value to R3
	MOV  	R4,[@w1+]	 	;Load Ymax value to R4

    MOV     R5,[@w1+]        ;Load integral buffer (Low) value to R5   
    CoLOAD  R5,[@w1]         ;Load integral buffer in accumulator 
     
	CoMAC   R2,R9           ;yn = Ki * error + yn

    CoMIN   R0,R4         	;Limit max yn
    CoMAX   R0,R3          	;Limit min yn
    CoSTORE R6,MAH          ;Store yn-high in R6 
    CoSTORE R5,MAL          ;Store yn-low  in R5    
    
	MOV     [-@w1],R6        ;Store R6 in integral buffer(High)  

    CoMUL   R1,R9           ;Kp * error 
    CoSHL   #6              ;64 * Kp * error    
    CoADD   R5,R6           ;y = yn + (64 * Kp * error) 
    CoMIN   R0,R4           ;Limit max y    
    CoMAX   R0,R3           ;Limit min y    
    CoSTORE @4,MAS          ;Store y-high in R4 (return register)   
    MOV     MCW,R7	     	;Restore MCW register value
		    
#pragma endasm	(PIval = @4)
   
   	return(PIval);
}	
#endif //End of TASKING
#endif //End of SCC
#if DAVECG_ControlPanelKBS.POT_ENABLE
#if DAVECG_ControlAlgoKBS.KEIL
unsigned int MUL_MAC(unsigned int a, unsigned int b)
{
   //Output =a*b/2^11
   int temp;
   #pragma asm
   CoMUL   R8,R9 		   ; a*b
   CoSHL   #4          	   ; a*b*2^5
   CoSTORE R4,MAH          ; Store high in R4 
   #pragma endasm
   temp=a;
   temp=b;
}
#endif
#if DAVECG_ControlAlgoKBS.Tasking
unsigned int MUL_MAC(unsigned int a, unsigned int b)
{
   //Output =a*b/2^15
    int retval;
    #pragma asm (@w1 = a,@w2 = b,@3)
    CoMUL  @w1,@w2           ;a*b
    CoSHL   #4          	   ; a*b*2^5
    CoSTORE @w3,MAH          ;Store yn-high in R6 
	#pragma endasm	(retval = @3)
	return retval;
   
}

#endif
#endif

