#define       GLOBALS_MOTOR
#include "../App_Headers/includes.h"


//-----------------------------------------------------------------------------
// 函数名称   :    Api_Motor_Init
// 函数功能   :    初始化电机驱动
// 输入       :    
//                 *Motor    ->   电机结构指针
// 输出       :    无
// 返回值     :    运行状态
// 其他       :    无    1280周期   中断频率25000
//-----------------------------------------------------------------------------
void  Api_Motor_Init(void)
{
    INT16U         *w;
    INT8U          Nub;

    MCCTL0_MCPRE        = Fbus;            //时钟为总线的1分频
    MCCTL0_MCSWAI       = Enable;          //停止模式停止工作 
    MCCTL0_FAST         = Disable;         //11bit分辨率
    MCCTL0_DITH         = Disable;         //PER-> P10-P0   0:包含P0
    MCCTL0_MCTOIF       = ClearFlag;       //清零中断标志
    MCCTL1_RECIRC       = True;            //低续流
    MCCTL1_MCTOIE       = Enable;          //使能中断
    MCPER               = InitMCPER;       //PWM周期
    //
    MCCC0_MCOM          = FHBM;            //双全H桥模式
    MCCC0_MCAM          = Enable;          //左对齐
    MCCC0_CD            = Disable;         //禁止延时
    MCCC1_MCOM          = FHBM;            //全H桥模式
    MCCC1_MCAM          = Enable;          //左对齐
    MCCC1_CD            = Disable;         //禁止延时
    //
    MCCC2_MCOM          = FHBM;;            //双全H桥模式
    MCCC2_MCAM          = Enable;          //左对齐
    MCCC2_CD            = Disable;         //禁止延时
    MCCC3_MCOM          = FHBM;             //全H桥模式
    MCCC3_MCAM          = Enable;          //左对齐
    MCCC3_CD            = Disable;         //禁止延时

    
    MCCC4_MCOM          = FHBM;         
    MCCC4_MCAM          = Enable;         
    MCCC4_CD            = Disable;         
    MCCC5_MCOM          = FHBM;          
    MCCC5_MCAM          = Enable;       
    MCCC5_CD            = Disable;       
    
    
    MCCC6_MCOM          = FHBM;            //双全H桥模式
    MCCC6_MCAM          = Enable;          //左对齐
    MCCC6_CD            = Disable;         //禁止延时
    MCCC7_MCOM          = FHBM;            //全H桥模式
    MCCC7_MCAM          = Enable;          //左对齐
    MCCC7_CD            = Disable;         //禁止延时
    
    w = &MCDC0;                            //初始化MCOC 0-7
         
    for(Nub = 0;Nub < 8 ;Nub++)
    {
         *(w + Nub) = 0;         
    }
}
//-----------------------------------------------------------------------------
// 函数名称   :    Api_Motor_Drive
// 函数功能   :    
// 输入       :    
// 输出       :    无
// 返回值     :    无
// 其他       :    无
//-----------------------------------------------------------------------------
void Api_Motor_Drive(MeterPos *M,INT8U M_sel)
{    
    switch(M_sel)
    {
        case EngineMotorNub:
            if(M->Time > null)
                M->Time --;
            if(M->Time == null)
            {
                if(M->CurPosition > M->DestPosition)
                {
                    M->Time = 2000/(M->CurPosition - M->DestPosition);                    
                    M->Dir = widdershins; 
                }
                else if(M->CurPosition < M->DestPosition)
                {
                    M->Time = 2000/(M->DestPosition - M->CurPosition);
                    M->Dir = clockwise;
                }
                else
                {
                    M->Dir = keep_local;
                }
                if(M->MotorReset == Enable)
                {
                    M->Dir = widdershins;
                }
                if((M->Time <= 7)||(Curkeyinput == KeyOFF)||(M->MotorReset == Enable))
                    M->Time = 7; 
                
                if(M->Time >= 170)
                    M->Time = 170; 
                
                if(M->Dir == clockwise)
                {
                    if(M->CurPosition < M->DestPosition)
                    {
                        M->CurPosition++; 
                    }                         
                    M->bPhase ++;
                    if(M->bPhase >= TabNub)
                       M->bPhase = null;                                                 
                }
                else
                {
                    if(M->Dir == widdershins)
                    {
                        if(RevZero != reset)
                        {
                            if(M->bPhase == null)
                                M->bPhase = TabNub;
                            if(M->bPhase > null)
                                M->bPhase --;  
                        } 
                        if(M->CurPosition > M->DestPosition)
                            M->CurPosition--;                   
                    }                                                       
                }
                MCDC1 = Sin[M->bPhase];
                MCDC0 = Cos[M->bPhase];
             }
             break;
        case VehMotorNub:
            if(M->Time > null)
                M->Time --;
            if(M->Time == null)
            {
                if(M->CurPosition > M->DestPosition)
                {                                                            
                    if(M->CurBoardVal < 40)
                    {
                        M->Time = 2300/(M->CurPosition - M->DestPosition);
                    } 
                    else
                    {
                        M->Time = 3200/(M->CurPosition - M->DestPosition);
                    }
                    M->Dir = widdershins; 
                }
                else if(M->CurPosition < M->DestPosition)
                {
                    M->Time = 3800/(M->DestPosition - M->CurPosition);
                    M->Dir = clockwise;
                }
                else
                {
                    M->Dir = keep_local;
                }
                if(M->MotorReset == Enable)
                {
                    M->Dir = widdershins;
                }
              
                if((M->Time <= 7)||(Curkeyinput == KeyOFF)||(M->MotorReset == Enable))
                    M->Time = 7; 

                if(M->Time >= 185)
                    M->Time = 185;               

                if(M->Dir == clockwise)
                {
                    if(M->CurPosition < M->DestPosition)
                    {
                        M->CurPosition++; 
                    } 
                    M->bPhase ++;
                    if(M->bPhase >=TabNub)
                       M->bPhase = null;                                                 
                }
                else
                {
                    if(M->Dir == widdershins)
                    {
                        if(VehZero != reset)
                        {                        
                            if(M->bPhase == null)
                                M->bPhase = TabNub;
                            if(M->bPhase > null)
                                M->bPhase --;
                        }
                        if(M->CurPosition > M->DestPosition)
                            M->CurPosition--;                   
                    }                                                      
                }
                MCDC7 = Sin[M->bPhase];
                MCDC6 = Cos[M->bPhase];
             }
             break;

        case FuelMotorNub:
            if(M->Time > null)
                M->Time --;
            if(M->Time == null)
            {
                if(M->CurPosition > M->DestPosition)
                {
                    //M->Time = 9000/(M->CurPosition - M->DestPosition);
                    //M->Time = FuleSpeed;                    
                    M->Dir = widdershins; 
                }
                else if(M->CurPosition < M->DestPosition)
                {
                    //M->Time = 9000/(M->DestPosition - M->CurPosition);
                    //M->Time = FuleSpeed;
                    M->Dir = clockwise;
                }
                else
                {
                    M->Dir = keep_local;
                }
                if(M->MotorReset == Enable)
                {
                    M->Dir = widdershins;
                }
                if((M->Time <= 7)||(Curkeyinput == KeyOFF)||(M->MotorReset == Enable))
                    M->Time = 7; 
                
                //if(M->Time >= 170)
                    M->Time = FuleSpeed; 
                
                if(M->Dir == clockwise)
                {
                    if(M->CurPosition < M->DestPosition)
                    {
                        M->CurPosition++; 
                    }                         
                    M->bPhase ++;
                    if(M->bPhase >= TabNub)
                       M->bPhase = null;                                                 
                }
                else
                {
                    if(M->Dir == widdershins)
                    {
                        if(YB_ZERO_IN != reset)
                        {
                            if(M->bPhase == null)
                                M->bPhase = TabNub;
                            if(M->bPhase > null)
                                M->bPhase --;  
                        } 
                        if(M->CurPosition > M->DestPosition)
                            M->CurPosition--;                   
                    }                                                       
                }
                MCDC5 = Sin[M->bPhase];
                MCDC4 = Cos[M->bPhase];
             }
              break;
        case TempMotorNub:
            if(M->Time > null)
                M->Time --;
            if(M->Time == null)
            {
                if(M->CurPosition > M->DestPosition)
                {
                    //M->Time = 9000/(M->CurPosition - M->DestPosition); 
                    //M->Time = 170;                    
                    M->Dir = widdershins; 
                }
                else if(M->CurPosition < M->DestPosition)
                {
                    //M->Time = 9000/(M->DestPosition - M->CurPosition);
                    //M->Time = 170;
                    M->Dir = clockwise;
                }
                else
                {
                    M->Dir = keep_local;
                }
                if(M->MotorReset == Enable)
                {
                    M->Dir = widdershins;
                }
                if((M->Time <= 7)||(Curkeyinput == KeyOFF)||(M->MotorReset == Enable))
                    M->Time = 7; 
                
                //if(M->Time >= 170)
                    M->Time = TempSpeed; 
                
                if(M->Dir == clockwise)
                {
                    if(M->CurPosition < M->DestPosition)
                    {
                        M->CurPosition++; 
                    }                         
                    M->bPhase ++;
                    if(M->bPhase >= TabNub)
                       M->bPhase = null;                                                 
                }
                else
                {
                    if(M->Dir == widdershins)
                    {
                        if(Tem_ZERO_IN != reset)
                        {
                            if(M->bPhase == null)
                                M->bPhase = TabNub;
                            if(M->bPhase > null)
                                M->bPhase --;  
                        } 
                        if(M->CurPosition > M->DestPosition)
                            M->CurPosition--;                   
                    }                                                       
                }
                MCDC3 = Sin[M->bPhase];
                MCDC2 = Cos[M->bPhase];
             } 
              break;                                
        default:
             ;
    }
}
//-----------------------------------------------------------------------------
// 函数名称   :    Api_Motor_isr
// 函数功能   :    PWM溢出中断
// 输入       :    无
// 输出       :    无
// 返回值     :    无
// 其他       :    无
//-----------------------------------------------------------------------------
#pragma CODE_SEG NON_BANKED 
interrupt void Api_Motor_isr(void)
{
    static    INT8U     bState;
    
    if(bState >  TempMotorNub)
    {
         bState = null;
    }
    
    switch(bState)
    {
                
         case VehMotorNub:
         {
              Api_Motor_Drive((MeterPos*)&VehMeter,VehMotorNub);
              break;
         }
              

         case EngineMotorNub:
         {
              Api_Motor_Drive((MeterPos*)&RevMeter,EngineMotorNub);
              break;
         }
         case FuelMotorNub:
         {
              Api_Motor_Drive((MeterPos*)&FuelMeter,FuelMotorNub);
              break;
         }
        case TempMotorNub:
         {
              Api_Motor_Drive((MeterPos*)&TempMeter,TempMotorNub);
              break;
         }
         default:
         ;
    }

    bState++;
    MCCTL0_MCTOIF       = ClearFlag;       //清零中断标志
}
#pragma CODE_SEG DEFAULT 
