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

//****************************************************************************
// @Function  
// 
// int pi_controller64(long *pi_parameter,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
//----------------------------------------------------------------------------
// Computing time  42 CPU-cycle 
//
//----------------------------------------------------------------------------
// @Returnvalue   
//
//----------------------------------------------------------------------------
// @Parameters    
//
//--------------------------- Arguments -------------------------------------
//  reference : reference value
//  actual    : actual value
//
//  struct pi_parameter
//      { 
//      int yn;   Integral buffer
//      int kp;   Proportional Constant
//      int ki;   Integral Constant
//      int ymin; Limit value min
//      int ymax; Limit value max
//      };
//---------------------------------------------------------------------------
//  Register modified:
//
//  R1,R3,R4,R5,R6,R7,R8,R9,R10
//      
//----------------------------------------------------------------------------
//
//****************************************************************************
#include "CONFIG.H"

#if DAVECG_TASKING
int  pi_controller64(long *pi_parameter,int reference,int actual)
{
long a;
int retvalue;
#pragma asm (@w1 = pi_parameter,@w2 = reference,@w3 = actual ,@4)

        mov     R8,MCW          ;Save MCW register                            
        mov     MCW,#1536       ;Set saturation and shift left                
        mov     R9,ZEROS        ;Load zero in R9                             
        CoLOAD  R9,@w2          ;Load Accumulator (High) with @w2(reference)  
        CoSUB   R9,@w3          ;error = reference - actual                   
        CoSTORE R10 ,MAS        ;Load error in R10                             
        mov     R3,[@w1+]                
        CoLOAD  R3,[@w1+]       ;Load yn (integral buffer) in accumulator
        mov     R1,@w1          ;Save parameters addres in R1            
        mov     R5,[R1+]        ;Load Kp (proportional Constant) in R5         
        mov     R6,[R1+]        ;Load Ki = T0/Ti (integral Constant) in R6    
        CoMAC   R6,R10          ;yn = Ki * error + yn                    
        mov     R6,[R1+]        ;Load ymax (limit value max)             
        mov     R7,[R1+]        ;Load ymin (limit value min)              
        CoMIN   R9,R6           ;Limit max yn                                                       
        CoMAX   R9,R7           ;Limit min yn                                                           
        CoSTORE R4,MAH          ;Store yn-high in R4                    
        CoSTORE R3,MAL          ;Store yn-low  in R3                        
        mov     [-@w1],R4       ;Store R4 in integral buffer(High)          
        mov     [-@w1],R3       ;Store R3 in integral buffer(Low)       
        CoMUL   R5,R10          ;Kp * error                             
        CoSHL   #6              ;64 * Kp * error                            
        CoADD   R3,R4           ;y = yn + (64 * Kp * error)                 
        CoMIN   R9,R6           ;Limit max y                                    
        CoMAX   R9,R7           ;Limit min y                                    
        CoSTORE @4,MAS          ;Store y-high in @4 (return register)           
        mov     MCW,R8          ;Restore MCW register                           

#pragma endasm (retvalue = @4)

a = reference;
a = actual;
a = *pi_parameter;
return retvalue;
}
#elif DAVECG_KEIL
sfr  MCW      = 0xFFDC;       //MAC Control Word

int pi_controller64(long *pi_parameter,int reference,int actual)
{
long a;
#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,R9          ;Load Accumulator (High) with R9 (reference)
        CoSUB   R11,R10         ;error = reference - actual
        CoSTORE R9 ,MAS         ;Load error in R9
        mov     R3,[R8+]        ;        
        CoLOAD  R3,[R8+]        ;Load yn (integral buffer) in accumulator 
        mov     R1,R8           ;Save parameters addres in R1
        mov     R5,[R1+]        ;Load Kp (proportional Constant) in R5
        mov     R6,[R1+]        ;Load Ki = T0/Ti (integral Constant) in R6
        CoMAC   R6,R9           ;yn = Ki * error + yn
        mov     R6,[R1+]        ;Load ymax (limit value max)
        mov     R7,[R1+]        ;Load ymin (limit value min)
        CoMIN   R11,R6          ;Limit max yn
        CoMAX   R11,R7          ;Limit min yn
        CoSTORE R4,MAH          ;Store yn-high in R4 
        CoSTORE R3,MAL          ;Store yn-low  in R3    
        mov     [-R8],R4        ;Store R4 in integral buffer(High)  
        mov     [-R8],R3        ;Store R3 in integral buffer(Low)   
        CoMUL   R5,R9           ;Kp * error 
        CoSHL   #6              ;64 * Kp * error    
        CoADD   R3,R4           ;y = yn + (64 * Kp * error) 
        CoMIN   R11,R6          ;Limit max y    
        CoMAX   R11,R7          ;Limit min y    
        CoSTORE R4,MAS          ;Store y-high in R4 (return register)   
        mov     MCW,R12         ;Restore MCW register       

#pragma endasm

a = reference;
a = actual;
a = *pi_parameter;

}
#endif

