TI中文支持网
TI专业的中文技术问题搜集分享网站

msp432 Timer_A0不能使用

我准备用msp432的driver例程中pwmled的例程编写一个马达控制程序,再修改测试过程中发现无法使用Timer_A0这个硬件资源,我在board.h,msp432p401r.h,msp432p401r.c中做了相应的修改,但依然无法打开Timer_A0。Timer_A1-A3都可以用。程序如下。

“Board.h”

#define Board_TIMER0                MSP_EXP432P401R_TIMER_T32_0
#define Board_TIMER1                MSP_EXP432P401R_TIMER_T32_1
#define Board_TIMER2                MSP_EXP432P401R_TIMER_TA_0
#define Board_TIMER3                MSP_EXP432P401R_TIMER_TA_1
#define Board_TIMER4                MSP_EXP432P401R_TIMER_TA_2
#define Board_TIMER5                MSP_EXP432P401R_TIMER_TA_3

“msp432p401r.h”

/*!
 *  @def    MSP_EXP432P401R_TimerName
 *  @brief  Enum of Timer names on the MSP_EXP432P401R dev board
 */
typedef enum MSP_EXP432P401R_TimerName {
    MSP_EXP432P401R_TIMER_T32_0 = 0,
    MSP_EXP432P401R_TIMER_T32_1,
    MSP_EXP432P401R_TIMER_TA_0,
    MSP_EXP432P401R_TIMER_TA_1,
    MSP_EXP432P401R_TIMER_TA_2,
    MSP_EXP432P401R_TIMER_TA_3,

    MSP_EXP432P401R_TIMERCOUNT
} MSP_EXP432P401R_TimerName;

“msp432p401r.c”

/*
 *  =============================== Timer ===============================
 */
#include <ti/drivers/Timer.h>
#include <ti/drivers/timer/TimerMSP432.h>

TimerMSP432_Object timerMSP432Objects[MSP_EXP432P401R_TIMERCOUNT];

const TimerMSP432_HWAttrs timerMSP432HWAttrs[MSP_EXP432P401R_TIMERCOUNT] = {
    /* Timer32_0 */
    {
        .timerBaseAddress = TIMER32_0_BASE,
        .clockSource = TIMER_A_CLOCKSOURCE_SMCLK,
        .intNum = INT_T32_INT1,
        .intPriority = ~0
    },
    {
        .timerBaseAddress = TIMER32_1_BASE,
        .clockSource = TIMER_A_CLOCKSOURCE_SMCLK,
        .intNum = INT_T32_INT2,
        .intPriority = ~0
    },
    /* Timer_A0 */
    {
        .timerBaseAddress = TIMER_A0_BASE,
        .clockSource = TIMER_A_CLOCKSOURCE_ACLK,
        .intNum = INT_TA0_N,
        .intPriority = ~0
    },
    /* Timer_A1 */
    {
        .timerBaseAddress = TIMER_A1_BASE,
        .clockSource = TIMER_A_CLOCKSOURCE_ACLK,
        .intNum = INT_TA1_0,
        .intPriority = ~0
    },
    /* Timer_A2 */
    {
        .timerBaseAddress = TIMER_A2_BASE,
        .clockSource = TIMER_A_CLOCKSOURCE_ACLK,
        .intNum = INT_TA2_0,
        .intPriority = ~0
    },
    /* Timer_A3 */
    {
        .timerBaseAddress = TIMER_A3_BASE,
        .clockSource = TIMER_A_CLOCKSOURCE_ACLK,
        .intNum = INT_TA3_0,
        .intPriority = ~0
    }
};

const Timer_Config Timer_config[MSP_EXP432P401R_TIMERCOUNT] = {
    {
        .fxnTablePtr = &TimerMSP432_Timer32_fxnTable,
        .object = &timerMSP432Objects[MSP_EXP432P401R_TIMER_T32_0],
        .hwAttrs = &timerMSP432HWAttrs[MSP_EXP432P401R_TIMER_T32_0]
    },
    {
        .fxnTablePtr = &TimerMSP432_Timer32_fxnTable,
        .object = &timerMSP432Objects[MSP_EXP432P401R_TIMER_T32_1],
        .hwAttrs = &timerMSP432HWAttrs[MSP_EXP432P401R_TIMER_T32_1]
    },
    {
        .fxnTablePtr = &TimerMSP432_Timer_A_fxnTable,
        .object = &timerMSP432Objects[MSP_EXP432P401R_TIMER_TA_0],
        .hwAttrs = &timerMSP432HWAttrs[MSP_EXP432P401R_TIMER_TA_0]
    },
    {
        .fxnTablePtr = &TimerMSP432_Timer_A_fxnTable,
        .object = &timerMSP432Objects[MSP_EXP432P401R_TIMER_TA_1],
        .hwAttrs = &timerMSP432HWAttrs[MSP_EXP432P401R_TIMER_TA_1]
    },
    {
        .fxnTablePtr = &TimerMSP432_Timer_A_fxnTable,
        .object = &timerMSP432Objects[MSP_EXP432P401R_TIMER_TA_2],
        .hwAttrs = &timerMSP432HWAttrs[MSP_EXP432P401R_TIMER_TA_2]
    },
    {
        .fxnTablePtr = &TimerMSP432_Timer_A_fxnTable,
        .object = &timerMSP432Objects[MSP_EXP432P401R_TIMER_TA_3],
        .hwAttrs = &timerMSP432HWAttrs[MSP_EXP432P401R_TIMER_TA_3]
    }
};

"pwmled.c"

/*
 *  ======== pwmled.c ========
 */
/* For usleep() */
#include <unistd.h>
#include <stddef.h>
#include <stdbool.h>

/* Driver Header files */
#include <ti/drivers/PWM.h>
#include <ti/drivers/Timer.h>

/* Example/Board Header files */
#include "Board.h"

PWM_Handle pwm1 = NULL;
PWM_Handle pwm2 = NULL;
PWM_Handle pwm3 = NULL;
PWM_Handle pwm4 = NULL;

/* Period and duty in microseconds */
uint32_t   pwmPeriod = 5000;
uint32_t   duty = 3000;

/* N steps */
uint32_t   n = 4096;

/* motor start/stop flag */
bool motorflag = true;
/*******************************************************************************
 *                                  LOCAL FUNCTIONS
 ******************************************************************************/
static void motor_forward(uint32_t value);
static void motor_reversal(uint32_t value);
static void timer0Callback(Timer_Handle myHandle);
/*
 *  ======== mainThread ========
 *  Task periodically increments the PWM duty for the on board LED.
 */
void *mainThread(void *arg0)
{
    PWM_Params params;
    Timer_Params timeparams;

    Timer_Handle timer0;

    /* Call driver init functions. */
    PWM_init();
    Timer_init();

    PWM_Params_init(&params);
    params.dutyUnits = PWM_DUTY_US;
    params.dutyValue = 0;
    params.periodUnits = PWM_PERIOD_US;
    params.periodValue = pwmPeriod;
    pwm1 = PWM_open(Board_PWM0, &params);
    pwm2 = PWM_open(Board_PWM1, &params);
    pwm3 = PWM_open(Board_PWM2, &params);
    pwm4 = PWM_open(Board_PWM3, &params);
    if (pwm1 == NULL) {
        /* Board_PWM0 did not open */
        while (1);
    }
    if (pwm2 == NULL) {
        /* Board_PWM0 did not open */
        while (1);
    }
    if (pwm3 == NULL) {
        /* Board_PWM0 did not open */
        while (1);
    }
    if (pwm4 == NULL) {
        /* Board_PWM0 did not open */
        while (1);
    }
    PWM_start(pwm1);
    PWM_start(pwm2);
    PWM_start(pwm3);
    PWM_start(pwm4);

    Timer_Params_init(&timeparams);
    timeparams.period = 5000000;
    timeparams.periodUnits = Timer_PERIOD_US;
    timeparams.timerMode = Timer_ONESHOT_CALLBACK;
    timeparams.timerCallback = timer0Callback;

    timer0 = Timer_open(Board_TIMER2, &timeparams);  

    if (timer0 == NULL) {
            /* Failed to initialized timer */
            while (1);                                     程序会在这里进入死循环,因为Board_TIMER2(Timer_A0)没有打开.
        }

    Timer_start(timer0);

    motor_forward(n);
    sleep(1);
    motor_reversal(n);
    sleep(1);

    return NULL;
}

/*Motor forward*/
static void motor_forward(uint32_t value)
{
    uint32_t i;
    uint8_t  j;
    uint8_t  step;
    for(i=value;i>8;i=i-8)
    {
        if(motorflag == false)
        {
            i = 0;
            break;
        }

        for(step=0;step<8;step++)
        {
            if(motorflag == false)
            {
                break;
            }
            switch(step)
            {
            case 0:
                 /*A*/
                 PWM_setDuty(pwm1, duty);
                 usleep(pwmPeriod);
                 break;
            case 1:
                 /*AB*/
                 PWM_setDuty(pwm2, duty);
                 usleep(pwmPeriod);
                 PWM_setDuty(pwm1, 0);
                 break;
            case 2:
                 /*B*/
                 usleep(pwmPeriod);
                 break;
            case 3:
                 /*BC*/
                 PWM_setDuty(pwm3, duty);
                 usleep(pwmPeriod);
                 PWM_setDuty(pwm2, 0);
                 break;
            case 4:
                 /*C*/
                 usleep(pwmPeriod);
                 break;
            case 5:
                 /*CD*/
                 PWM_setDuty(pwm4, duty);
                 usleep(pwmPeriod);
                 PWM_setDuty(pwm3, 0);
                 break;
            case 6:
                 /*D*/
                 usleep(pwmPeriod);
                 break;
            case 7:
                 /*DA*/
                 PWM_setDuty(pwm1, duty);
                 usleep(pwmPeriod);
                 PWM_setDuty(pwm4, 0);
                 PWM_setDuty(pwm1, 0);
                 break;
            default:
                 break;
            }
        }
    }

    for(j=0;j<i;j++)
    {
        if(motorflag == false)
        {
            break;
        }
        switch(j)
        {
        case 0:
             /*A*/
             PWM_setDuty(pwm1, duty);
             usleep(pwmPeriod);
             break;
        case 1:
             /*AB*/
             PWM_setDuty(pwm2, duty);
             usleep(pwmPeriod);
             PWM_setDuty(pwm1, 0);
             break;
        case 2:
             /*B*/
             usleep(pwmPeriod);
             break;
        case 3:
             /*BC*/
             PWM_setDuty(pwm3, duty);
             usleep(pwmPeriod);
             PWM_setDuty(pwm2, 0);
             break;
        case 4:
             /*C*/
             usleep(pwmPeriod);
             break;
        case 5:
             /*CD*/
             PWM_setDuty(pwm4, duty);
             usleep(pwmPeriod);
             PWM_setDuty(pwm3, 0);
             break;
        case 6:
             /*D*/
             usleep(pwmPeriod);
             break;
        case 7:
             /*DA*/
             PWM_setDuty(pwm1, duty);
             usleep(pwmPeriod);
             PWM_setDuty(pwm4, 0);
             PWM_setDuty(pwm1, 0);
             break;
        default:
             break;
        }
    }

    PWM_setDuty(pwm1, 0);
    PWM_setDuty(pwm2, 0);
    PWM_setDuty(pwm3, 0);
    PWM_setDuty(pwm4, 0);

}

/*Motor reversal*/
static void motor_reversal(uint32_t value)
{
    uint32_t i;
    uint8_t  j;
    uint8_t  step;
    for(i=value;i>8;i=i-8)
    {
        if(motorflag == false)
        {
            i = 0;
            break;
        }
        for(step=0;step<8;step++)
        {
            if(motorflag == false)
            {
                break;
            }
            switch(step)
            {
            case 0:
                 /*D*/
                 PWM_setDuty(pwm4, duty);
                 usleep(pwmPeriod);
                 break;
            case 1:
                 /*DC*/
                 PWM_setDuty(pwm3, duty);
                 usleep(pwmPeriod);
                 PWM_setDuty(pwm4, 0);
                 break;
            case 2:
                 /*C*/
                 usleep(pwmPeriod);
                 break;
            case 3:
                 /*CB*/
                 PWM_setDuty(pwm2, duty);
                 usleep(pwmPeriod);
                 PWM_setDuty(pwm3, 0);
                 break;
            case 4:
                 /*B*/
                 usleep(pwmPeriod);
                 break;
            case 5:
                 /*BA*/
                 PWM_setDuty(pwm1, duty);
                 usleep(pwmPeriod);
                 PWM_setDuty(pwm2, 0);
                 break;
            case 6:
                 /*A*/
                 usleep(pwmPeriod);
                 break;
            case 7:
                 /*AD*/
                 PWM_setDuty(pwm4, duty);
                 usleep(pwmPeriod);
                 PWM_setDuty(pwm1, 0);
                 PWM_setDuty(pwm4, 0);
                 break;
            default:
                 break;
            }
        }
    }

    for(j=0;j<i;j++)
    {
        if(motorflag == false)
        {
            break;
        }
        switch(j)
        {
        case 0:
             /*D*/
             PWM_setDuty(pwm4, duty);
             usleep(pwmPeriod);
             break;
        case 1:
             /*DC*/
             PWM_setDuty(pwm3, duty);
             usleep(pwmPeriod);
             PWM_setDuty(pwm4, 0);
             break;
        case 2:
             /*C*/
             usleep(pwmPeriod);
             break;
        case 3:
             /*CB*/
             PWM_setDuty(pwm2, duty);
             usleep(pwmPeriod);
             PWM_setDuty(pwm3, 0);
             break;
        case 4:
             /*B*/
             usleep(pwmPeriod);
             break;
        case 5:
             /*BA*/
             PWM_setDuty(pwm1, duty);
             usleep(pwmPeriod);
             PWM_setDuty(pwm2, 0);
             break;
        case 6:
             /*A*/
             usleep(pwmPeriod);
             break;
        case 7:
             /*AD*/
             PWM_setDuty(pwm4, duty);
             usleep(pwmPeriod);
             PWM_setDuty(pwm1, 0);
             PWM_setDuty(pwm4, 0);
             break;
        default:
             break;
        }
    }

    PWM_setDuty(pwm1, 0);
    PWM_setDuty(pwm2, 0);
    PWM_setDuty(pwm3, 0);
    PWM_setDuty(pwm4, 0);

}

static void timer0Callback(Timer_Handle myHandle)
{
    motorflag = false;
}

gaoyang9992006:

dev.ti.com/…/app.html
在这个图形化工具里配置试试,这么多行,手工搞容易遗漏。。

user5196561:

回复 gaoyang9992006:

有关timer的配置就那么几行,其他timerA都可以打开,只有timerA0打不开,无法使用。

user5196561:

回复 user5196561:

Timer_A0是不是已经被其他资源占用了,所以打不开。因为我目前位置看到的例程中timer driver使用的timerA都是timerA1,A2,A3。为社么不使用A0呢。

user5196561:

回复 user5196561:

TimerA0用作定时的话也不用配置输出口,

user5196561:

回复 user5196561:

TimerA配置成定时的话也不用配置输出口

赞(0)
未经允许不得转载:TI中文支持网 » msp432 Timer_A0不能使用
分享到: 更多 (0)