⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bldczc.c

📁 菲斯卡尔无传感器无刷控制方案。具体说明文档和程序都在压缩包内。
💻 C
📖 第 1 页 / 共 3 页
字号:
/*************************************************************************
* Freescale Inc.
* (c) Copyright 2005 Freescale, Inc.
* ALL RIGHTS RESERVED.
*************************************************************************/

/*************************************************************************
* FILE NAME         :   BldcZC.c
*
* DESCRIPTION       :   Brushless DC Zero Crossing algorithms
*
* MODULES INCLUDED  :   types.h
                        arch.h
                        periph.h
                        pwm.h
                        bldcadczcconfig.h
                        bldcdrv.h
                        BldcZC.h
*************************************************************************/
                        
/* Include Header Files */

/* include intristic functions */
#include "types.h"
#include "arch.h"
#include "periph.h"
#include "pwm.h"

#include "bldcadczcconfig.h"
#include "bldcdrv.h"
#include "BldcZC.h"

#ifdef __cplusplus
extern "C" {
#endif

/* BldcZCAlgo.c Internal Avialibility Functions Declarations */
static inline Result PresetCalculation  ( bldczc_sStateComput *pState_Comput, bldczc_sTimes *pTimes);
static inline Result SetCalculation     ( bldczc_sStateComput *pState_Comput, bldczc_sTimes *pTimes);
static inline Result CmtTimeout         ( bldczc_sStates *pStates, bldczc_sTimes *pTimes , UWord16 T_Actual );
static inline Result ZCToffTimeout      ( bldczc_sStates *pStates, bldczc_sTimes *pTimes, Frac16 U_ZCPhaseX );
static inline Result PrepareToffTiming  ( bldczc_uCmdGeneral *pCmd_General, UWord16 *T_Next, UWord16 T_Cmt0, UWord16 Per_Toff);
/****************************************************************************************************

/****************************************************************************************************
*
* MODULE        : bldczcTimeoutIntAlg ( bldczc_sStates *pStates,
*                                       bldczc_sTimes *pTimes,
*                                       UWord16 T_Actual,
*                                       Frac16 U_ZCPhaseX)
*
* DESCRIPTION  : Function bldczcTimeoutIntAlg is intended to be called from Commutation Timer
*                 interrupt. According to the required commutatino event (commutation or Toff
*                 finishing) is calls required bldc function. It works with cooperation with
*                 bldczc_sStates and bldczc_sTimes structures and passes these structures to
*                 called function.
*
* RETURNS       : The function returns the PASS (0)      
*
* SUBMODULES    : CmtTimeout( pStates, pTimes, T_Actual )
*                 ZCToffTimeout ( bldczc_sStates *pStates )
*
* ARGUMENTS     : in/out - pointer to bldczc_sStates structure that contains the status
*                          variables for all BLDC motor BEMF Zero Crossing algorithms
*                 in/out - pointer to bldczc_sTimes structure that contains the time
*                          variables for all BLDC motor BEMF Zero Crossing algorithms
*                 in     - T_Actual, 16bit range Actual Commmutatrion Time
*                                               
* RANGE ISSUES  : All the time variables and components T_x in bldczc_sTimes structure are 
*                 computed as 16 bit rollower registers. It mean that if their results overflows
*                 16 bits, they are not saturated, just the overflow bit is ignored and low
*                 16 bits word is taken as a result. Then the T_x variables can be used as outputs
*                 and inputs from a 16 bit past compare timer used as a system clock base!
*
* SPECIAL ISSUES: The function calculates correct results if the saturation mode is set or not .
*
****************************************************************************************************/
/*--------------------[ bldczcTimeoutIntAlg - Begin ]----------------------*/
Result bldczcTimeoutIntAlg ( bldczc_sStates *pStates, bldczc_sTimes *pTimes , UWord16 T_Actual, Frac16 U_ZCPhaseX )
{
	/* Because this function is called from commutation timer, first, it is needed to determine
	   the source of interrupt.
	   a. Commutation time is expired (time for commutation)
	   b. ZeroCross Toff time is expired.
	*/

	/* Apply only if commutation timing is in progress */
	if ( pStates->State_General.Cmd_General.Cmt_TimedFlag )
	{
		/* Prepare commutation */
		CmtTimeout( pStates, pTimes, T_Actual);
	}

    else
    
    /* Apply, only if ZeroCrossing timing is in progress */
    if ( pStates->State_General.Cmd_General.ZCToff_TimedFlag )
    {
        ZCToffTimeout ( pStates, pTimes, U_ZCPhaseX );
    }

	return (PASS);
}
/*--------------------[ bldczcTimeoutIntAlg - End ]------------------------*/

/*--------------------[ CmtTimeout - Begin ]-------------------------------*/
inline static Result CmtTimeout (bldczc_sStates *pStates, bldczc_sTimes *pTimes , UWord16 T_Actual)
{
	/* Clear Cmt_TimerFlag, which is caused program to come here, from bldczcTimeoutIntAlg function */
	pStates->State_General.Cmd_General.Cmt_TimedFlag = 0;
 
	/* Because commutation is going to be performed, set ToffFlag, so ZeroCross inputs from
	   ADC are ignored, until ToffFlag is cleared in ZCToffTimeout function */
	pStates->State_ZCros.Cmd_ZCros.ZC_ToffFlag = 1;

	/* disable Zero crossing Interrupts, until magnetization to zero and BEMF stabilizes */
	pStates->State_ZCros.Cmd_ZCros.ZCrosInt_EnblFlag = 0;

	/* Copy actual time, which is the value of Commutation Timer, to T_Cmt0. This is the commutation time */
	/* T_Cmt0 must be set before PrepareProcTiming function */
	pTimes->T_Cmt0 = T_Actual;
	PrepareToffTiming (&pStates->State_General.Cmd_General, &pTimes->T_Next, pTimes->T_Cmt0, pTimes->Per_Toff);

    pStates->State_Cmt.Cmd_Cmt.CmtDone_CmtServ_RqFlag = 1;	
    bldczcCmtServ(&pStates->State_Cmt);

    pStates->State_ZCros.Index_ZC_Phase = ZC_Phase_Tab [ pStates->State_Cmt.Step_Cmt ]; 
    pStates->State_ZCros.Cmd_ZCros.Expect_ZCInp_PositivFlag = \
    Expect_ZCInpFlag_Tab [ pStates->State_Cmt.Step_Cmt] [ pStates->State_Cmt.Cmd_Cmt.DIRFlag ];

    /* Enable Event Timer update in next possible oppourtunity */
	pStates->State_Cmt.Cmd_Cmt.Cmt_DrvRqFlag = 1;
	
	/* Enable to change the Zero Cross input channel, because commutation is going to be performed */
	pStates->State_ZCros.Cmd_ZCros.ZCInpSet_DrvRqFlag = 1;
	
    /* If no zero crossing get between previous commutations */
    if (!pStates->State_ZCros.Cmd_ZCros.ZC_GetFlag)
    {
        pStates->State_ZCros.Cntr_ZCrosOK = 0;
        pStates->State_ZCros.Cntr_ZCrosErr--;
    }
		
    pStates->State_ZCros.Cmd_ZCros.ZC_GetFlag = 0;

    /* Compare detected ZeroCrossOK count with the limit specified to leave Start sequence */
    if (pStates->State_ZCros.Cntr_ZCrosOK >= pStates->State_ZCros.Min_ZCrosOKStart)
    {
        /* Apply only if Start mode is in progress and ZeroCross serve is performed */
        if ( pStates->State_General.Cmd_General.StartMode_HndlrFlag )
        {
			/* Because Start sequence was active and it enough ZeroCrossings are detected to leave
			   Start sequence, mark that the Start sequence may be finished. This bit will be checked
			   inside CommutationControl and algorithm will switch from Start to Run */
            pStates->State_General.Cmd_General.EndStart_HndlrCmdFlag = 1;
        };
    };

    /* Compare ZeroCross missings with limit */
    if (pStates->State_ZCros.Cntr_ZCrosErr <= 0)
    {
        /* Because ZeroCross error is occoured, enable ZeroCross handler.
           This handler bit will be checked inside CommutationControl */
        pStates->State_General.Cmd_General.MaxZCErr_HndlrCmdFlag = 1;
    };

    /* Computation request for commutation */
    if (!pStates->State_Comput.Cmd_Comput.ZC_ComputFlag)
    {
        /* This is corrective action 1 */
        pTimes->T_ZCros = pTimes->T_Cmt0;
        SetCalculation (&pStates->State_Comput, pTimes);
    };
    PresetCalculation (&pStates->State_Comput, pTimes);
  
	return (PASS);
}
/*--------------------[ CmtTimeout - End ]----------------------------------*/

/******************** ZCToffTimeout - Begin ********************/
inline static Result ZCToffTimeout ( bldczc_sStates *pStates, bldczc_sTimes *pTimes, Frac16 U_ZCPhaseX )
{
	/* Clear ZC_ToffFlag, because Toff time expired.
       This will enable receiving zero cross inputs from ADCLimit function */
	pStates->State_ZCros.Cmd_ZCros.ZC_ToffFlag = 0;

	/* Mark that the ZeroCrossing timing is no more in progress */
	pStates->State_General.Cmd_General.ZCToff_TimedFlag = 0;

    /* pTimes->T_Next = pTimes->T_Cmt0 + pTimes->Per_CmtPreset; */
    pTimes->T_Next = L_add( (Word32) pTimes->T_Cmt0, (Word32) pTimes->Per_CmtPreset);
        
    /* Enable Event Timer update in next possible oppourtunity */
    pStates->State_General.Cmd_General.Timer_DrvRqFlag = 1;
		
    pStates->State_General.Cmd_General.Cmt_TimedFlag = 1;

    pStates->State_ZCros.Cmd_ZCros.ZCrosInt_EnblFlag = 1;
    
    if ( ( pStates->State_ZCros.Cmd_ZCros.Expect_ZCInp_PositivFlag == 1 ) & ( 0 <= U_ZCPhaseX ) ||
         ( pStates->State_ZCros.Cmd_ZCros.Expect_ZCInp_PositivFlag == 0 ) & ( 0 > U_ZCPhaseX  ) )
    {
        /* This is corrective action 2 */
        if (!pStates->State_ZCros.Cmd_ZCros.ZC_GetFlag)
        {
            pStates->State_ZCros.Cmd_ZCros.ZC_GetFlag = 1;

            /* Because ZeroCross missed, reset conCntr_ZCrosOK variable to 0 */
            pStates->State_ZCros.Cntr_ZCrosOK = 0;

            /* Because ZeroCross missed, decrement Cntr_ZCrosErr variable by 1 */
            pStates->State_ZCros.Cntr_ZCrosErr--;
            pStates->State_ZCros.Cmd_ZCros.ZCrosInt_EnblFlag = 0;

            pTimes->T_ZCros = L_add ( (Word32) pTimes->T_Cmt0, (Word32) pTimes->Per_Toff);
	    	SetCalculation (&pStates->State_Comput, pTimes);
            pTimes->T_Next = L_add( (Word32) pTimes->T_ZCros, (Word32) pTimes->Per_HlfCmt);
        }
    }

	return (PASS);
}
/********************** ZCToffTimeout - End  ***********************/

/******************** PrepareToffTiming - Begin  *******************/
/*  Prepare Toff Timing for timer
    T_Next = T_Cmt0 + Per_Toff
    pCmd_General->B.ZCToff_TimedFlag = 1;
    pCmd_General->B.Timer_RqFlag = 1;                              */
inline static Result PrepareToffTiming (bldczc_uCmdGeneral *pCmd_General,
                                        UWord16 *T_Next,
                                        UWord16 T_Cmt0,
                                        UWord16 Per_Toff)
{
	/* *T_Next = T_Cmt0 + Per_Toff; */        
    *T_Next = L_add ( (Word32) T_Cmt0, (Word32) Per_Toff);
    
	/* Mark that the Toff timing is in progress */
	pCmd_General->ZCToff_TimedFlag = 1;

    /* Enable Event Timer update in next possible oppourtunity */
    pCmd_General->Timer_DrvRqFlag = 1;

	return (PASS);
}
/********************  PrepareToffTiming - End   *******************/

/****************************************************************************************************
*
* MODULE        : bldczcHndlrInit ( bldczc_sStates *pStates,
*                                   bldczc_sTimes *pTimes,
*                                   UWord16 T_Actual,
*                                   UWord16 Start_PerProcCmt,
*                                   bldczc_eStartingMode Starting_Mode )
*
* DESCRIPTION   : Function bldczcHndlrInit() is initialization of the command interface
*`                bldczcHndlr()
*
* RETURNS       : The function returns the PASS (0)      
*
* SUBMODULES    : PrepareProcTiming ( bldczc_uCmdGeneral *pCmd_General,
*                                     UWord16 *T_Next,
*                                     UWord16 T_Cmt0,
*                                     UWord16 Const_PerProcCmt )
*

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -