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

📄 bldc_w.c

📁 无刷直流电机的无传感器控制TI程序
💻 C
字号:
/*-----------------------------------------------------------------------------
BLDC_W.C       Primary system file for the implementation of current
               controller for a three phase brushless DC Motor.
-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------
    Get the compilation target setting.
    This target is defined by TARGET.H. To change the target, or to find out
    the present target, see that file.
-----------------------------------------------------------------------------*/
#include <TARGET.H>
/*-----------------------------------------------------------------------------
    Include header information for this file.
-----------------------------------------------------------------------------*/
#include <bldc.h>


/*-----------------------------------------------------------------------------
    System settings
-----------------------------------------------------------------------------*/
#if (TARGET==F243)
#define WAIT_STATES     0x0040
#endif /* (TARGET==F243) */

#if (TARGET==F2407)
#define WAIT_STATES     0x00C0
#endif /* (TARGET==F243) */

/*-----------------------------------------------------------------------------
    Global Declarations

/*-----------------------------------------------------------------------------
    Instance the EVMDAC Interface.
-----------------------------------------------------------------------------*/
        EVMDAC dac = EVMDAC_DEFAULTS;

/*-----------------------------------------------------------------------------
    Instance the PWM Generator (Driver) Interface.
    Also initalize the PWMGEN object.
    This pre-initializer takes on the nature depending on the TARGET device.
    An IMPORTANT NOTE :
        This pre-initalization initializes the PWMGEN data structure in
        memory. This WILL NOT initialize the PWM Generator timers and
        so on. This is accomplished by calling the init method in the
        PWMGEN object. This applies to most drivers that supply an init
        method.
-----------------------------------------------------------------------------*/
        PWMGEN pwm = PWMGEN_DEFAULTS;

/*-----------------------------------------------------------------------------
    Instance the ADC (Driver) Interface.
    Also initalize the ADCVALS object.
    This pre-initializer takes on the nature depending on the TARGET device.
    An IMPORTANT NOTE :
        This pre-initalization initializes the ADCVALS data structure in
        memory. This WILL NOT initialize the ADC CONTROLS and
        so on. This is accomplished by calling the init method in the
        ADCVALS object.
-----------------------------------------------------------------------------*/
        ADCVALS adc = ADC_DEFAULTS;

/*-----------------------------------------------------------------------------
    Instance a single BLDC_TI object.
-----------------------------------------------------------------------------*/
        BLDC_TI bldc = BLDC_TI_INITVALS;

/*-----------------------------------------------------------------------------
    Instance the WATCHDOG Interface.
-----------------------------------------------------------------------------*/
        WATCHDOG wdog = WATCHDOG_DEFAULTS;

void main()
{
/*-----------------------------------------------------------------------------
    Return system to a sane condition
-----------------------------------------------------------------------------*/
        RstSystem();

/*-----------------------------------------------------------------------------
    Initialize PWM generator
-----------------------------------------------------------------------------*/
        (*pwm.init) (&pwm);      /* pwm driver initialization */

/*-----------------------------------------------------------------------------
    Initialize ADC driver
-----------------------------------------------------------------------------*/
        (*adc.init) (&adc);      /* adc driver initialization */


/*-----------------------------------------------------------------------------
    Initialize time base generator
-----------------------------------------------------------------------------*/
        time_base_init();

/*-----------------------------------------------------------------------------
    Initialise the Real time monitor
-----------------------------------------------------------------------------*/

#if (REAL_TIME == TRUE)
        rtmon_init();      /* Call the monitor init function  */
#endif /* REAL_TIME==TRUE */

/*-----------------------------------------------------------------------------
    Hardware/Board Specific Initialization
-----------------------------------------------------------------------------*/
        evm_pwm_init();

/*-----------------------------------------------------------------------------
    ADC channel select
    Channels 6,5,4,3 for 243EVM with DMC1000
    Channels 12,11,10,5 for 2407EVM with DMC1000
-----------------------------------------------------------------------------*/

        adc.a4_ch_sel = CHANNEL_SETTINGS;

/*-----------------------------------------------------------------------------
    Intialize the BLDC_TI object. This is a call to the init method within
    the BLDC_TI object bldc
-----------------------------------------------------------------------------*/
        BLDC_TI_Init(&bldc);

        pwm.d_func = ALIGN_DUTY;


/*-----------------------------------------------------------------------------
    ADC gain & offset adjustment
-----------------------------------------------------------------------------*/

        adc.c1_gain = 0x0800;    /* gain = 0.125 (Q13)              */
        adc.c2_gain = 0x0800;    /* gain = 0.125 (Q13)              */
        adc.c3_gain = 0x0800;    /* gain = 0.125 (Q13)              */
        adc.c4_gain = 0x1fff;    /* gain = 1.0 (Q13)                */

        bldc.current_set = 0x0065;


 enable_ints();     /* set off the system running */



/*---------------------------------------------------------------------------*/

        while(1)             /* Nothing running in the background at present */
        {

        } /* Main system background loop */

} /* End: main() */


void interrupt c_int03()
{
        asm("      CLRC     XF ");
/*---------------------------------------------------------------------------*/
#if     TARGET==F243
        EVIFRB=0x0ffff;            /* Clear all Group A EV interrupt flags    */

#elif   TARGET==F2407
        EVAIFRB=0x0ffff;          /* Clear all EV1 Group A EV interrupt flags*/
#endif/* #if TARGET */

        if (bldc.align_flag != FALSE ) {
            bldc.mod6.cntr = 0;
            pwm.cmtn_ptr_bd = 0;
            (*pwm.update)(&pwm);


             /* Wait for a "while" */

            if (bldc.cmtn.v_timer < V_TIMER_THRESHOLD){
                   update_v_timer();
                   dac.update(&dac);
                   asm("      SETC     XF ");
                   return;
            }/* if (bldc.cmtn.v_timer < V_TIMER_THRESHOLD)*/

            bldc.align_flag = 0;
        }/*if (bldc.align_flag != FALSE ) */

        bldc.cmtn.va = adc.c1_out;
        bldc.cmtn.vb = adc.c2_out;
        bldc.cmtn.vc = adc.c3_out;

        bldc.pid2.fb_reg2  = adc.c4_out;

        BLDC_TI_Run(&bldc);

        if(FALSE == bldc.I_loop_flg)
            pwm.d_func = bldc.rmp2.out;
        else
            pwm.d_func = bldc.pid2.out_reg2;

        pwm.cmtn_ptr_bd = bldc.mod6.cntr;  /* Input to PWM driver */
        (*pwm.update)(&pwm);
        (*adc.update)(&adc);

        update_v_timer();
        dac.update(&dac);

        asm("      SETC     XF ");


}  /* interrupt c_int03() */


void RstSystem(void)
{
/*-----------------------------------------------------------------------------
    First execute the initialization for the Wait Stage Genrator,
    Global interrupt disable, Shut off the Watchdog,
    and set up the Interupt Mask Register
-----------------------------------------------------------------------------*/
#if (TARGET==F243)
        disable_ints();           /* Make sure the interrupts are disabled   */
        IMR = 0x00;               /* Mask all interrupts                     */
        IFR = 0x00ff;             /* Clear any pending interrupts, if any    */
        PIRQR0 = PIRQR0 & 0x0fffe; /* Clear pending PDP flag */
        EVIFRA = EVIFRA | 0x0001;   /* Clear PDP int flag */

        asm("  CLRC   SXM ");         /* Clear signextension mode */
        asm("  CLRC   OVM ");         /* Reset overflow mode     */
        asm("  CLRC   CNF ");         /* Config block B0 to data memory */
        asm("  SPM    0   ");         /*  Set product mode at 0   */

        WSGR=WAIT_STATES;         /* Initialize Wait State Generator         */
        SCSR=0x40c0;              /* Init SCSR */
        wdog.disable();           /* Vccp/Wddis pin/bit must be high         */
        wdog.reset();             /* reset watchdog counter */

        EVIMRB=0x0004;        /* Enable the timer2 underflow interrupt */

        EVIFRA = 0xFFFF;      /* Clear all Group A interrupt flags */
        EVIFRB = 0xFFFF;      /* Clear all Group B interrupt flags */
        EVIFRC = 0xFFFF;      /* Clear all Group C interrupt flags */

        #if (REAL_TIME==TRUE)
        IMR = 0x0044;                /* En Int lvl 3 & 7 (T2 ISR) */
        #endif /* (REAL_TIME==TRUE) */

        #if (REAL_TIME==FALSE)
        IMR = 0x0004;                /* En Int lvl 3   (T2 ISR) */
        #endif /* (REAL_TIME==TRUE)*/

#endif /* (TARGET==F243) */

#if (TARGET==F2407)
        disable_ints();           /* Make sure the interrupts are disabled   */
        IMR = 0x00;               /* Mask all interrupts                     */
        IFR = 0x00ff;             /* Clear any pending interrupts, if any    */
        PIRQR0 = PIRQR0 & 0x0fffe; /* Clear pending PDP flag */
        PIRQR2 = PIRQR2 & 0x0fffe; /* Clear pending PDP flag */

        EVAIFRA = EVAIFRA | 0x0001; /* Clear PDPINTA flag */
        EVBIFRA = EVBIFRA | 0x0001; /* Clear PDPINTB flag */

        asm("  CLRC   SXM ");         /* Clear signextension mode */
        asm("  CLRC   OVM ");         /* Reset overflow mode     */
        asm("  CLRC   CNF ");         /* Config block B0 to data memory */
        asm("  SPM    0   ");         /*  Set product mode at 0   */

        WSGR=WAIT_STATES;         /* Initialize Wait State Generator */
        SCSR1=0x0085;             /* Init SCSR1 */
        wdog.disable();           /* Vccp/Wddis pin/bit must be high */
        wdog.reset();             /* reset watchdog counter */

        EVAIMRB=0x0004;        /* Enable the timer underflow interrupt */

        EVAIFRA = 0XFFFF;      /* Clear all Group A interrupt flags */
        EVAIFRB = 0XFFFF;      /* Clear all Group B interrupt flags */
        EVAIFRC = 0XFFFF;      /* Clear all Group C interrupt flags */

        #if (REAL_TIME==TRUE)
        IMR = 0X0044;                /* En Int lvl 3 & 7 (T2 ISR) */
        #endif /* (REAL_TIME==TRUE) */

        #if (REAL_TIME==FALSE)
        IMR = 0X0004;                /* En Int lvl 3   (T2 ISR) */
        #endif /* (REAL_TIME==TRUE)*/

#endif /* (TARGET==F2407) */

}   /* RstSystem(void) */


void interrupt phantom(void)
{

  static int phantom_count;
  phantom_count ++;


/*  Empty function: Used to handle any unwanted interrupts or events.
    All unused interrupt vectors are pointed to this function so that
    if any un-handled interrupts do get enabled, they are handled in a
    benign manner, preventing un-intended branches, calls or execution
    into garbage.
    Note that this function is an ISR, not a ordinary function.
*/
} /* phantom() */

/*------------------------------------------------------------------------------
    This function just provides a c-interface to the asm RTMON init function.
------------------------------------------------------------------------------*/

void rtmon_init(void)
{
    asm("       CALL    MON_RT_CNFG ");
}  /* rtmon_init() */


/*------------------------------------------------------------------------------
    This function derives time base from T2 underflow interrupt (i.e. period)
------------------------------------------------------------------------------*/

void time_base_init(void)
{
        T2PER = SYSTEM_INT_PERIOD;  /* Initialize period register */
/*
 * T2CON = 1001000001000000b
 * Operation is not affected by emulation suspend
 * Continuous up count mode
 * Enable timer operations
 */
        T2CON = 0x9040;

} /* time_base_init() */

void evm_pwm_init(void)
{
        OCRA     = OCRA & 0XBFFF;        /* Select Secondary function IOPB6 */
        PBDATDIR = PBDATDIR |0X4000;     /* Set IOPB6 as output        */
        PBDATDIR = PBDATDIR & EVM_IOPB6; /* Set IOPB6 low/high, Enable/disable PWM  */
} /* evm_pwm_init */

void update_v_timer(void)
{
   bldc.cmtn.v_timer++;                  /* Inc virtual timer */
   bldc.cmtn.v_timer = bldc.cmtn.v_timer & 0x7fff;   /* Force 15 bit wrap around and save */
}/* update_v_timer */

⌨️ 快捷键说明

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