CC26xx PWM

CC26xx PWM使用定时做的,请查看下面代码分析。

#ifndef EXCLUDE_IO
/* -----------------------------------------------------------------------------
*  Includes
* ------------------------------------------------------------------------------
*/

// TI RTOS drivers
#include <ti/drivers/Power.h>
#include <ti/drivers/power/PowerCC26XX.h>

// Temporary PWM solution directly on DriverLib
// (until a Timer RTOS driver is in place)
#include <ti/drivers/pin/PINCC26XX.h>
#include <driverLib/timer.h>

#include "sensortag_buzzer.h"

/* -----------------------------------------------------------------------------
*  Local variables
* ------------------------------------------------------------------------------
*/
static PIN_Handle hPin = NULL;

/* -----------------------------------------------------------------------------
*  Public Functions
* ------------------------------------------------------------------------------
*/

/*******************************************************************************
 * @fn          SensorTagBuzzer_open
 *
 * @brief       Initialize the Buzzer
 *
 * @descr       Initializes pin and PWM
 *
 * @return      -
 */
void SensorTagBuzzer_open(PIN_Handle hGpioPin)
{
    hPin = hGpioPin;

    // Turn on PERIPH power domain and clock for GPT0 and GPIO
    Power_setDependency(PowerCC26XX_PERIPH_GPT0);
    Power_setConstraint(PowerCC26XX_SB_DISALLOW);

    // Assign GPT0
    TimerConfigure(GPT0_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_A_PWM);

    // Configure pin for PWM output
    PINCC26XX_setMux(hPin, Board_BUZZER, IOC_PORT_MCU_PORT_EVENT0);
}


/*******************************************************************************
 * @fn          SensorTagBuzzer_setFrequency
 *
 * @brief       Set the frequency (3Hz - 8 KHz)
 *
 * @return      return true if the frequency is within range
 */
bool SensorTagBuzzer_setFrequency(uint16_t freq)
{
    uint32_t ticks;
    uint32_t loadLow;
    uint32_t loadHigh;
    uint32_t matchLow;
    uint32_t matchHigh;

    if (hPin == NULL)
    {
        // Make sure the pin is not used when not open
        return false;
    }

    if (freq < BUZZER_FREQ_MIN && freq > BUZZER_FREQ_MAX)
    {
        return false;
    }

    // Stop timer during reconfiguration
    TimerDisable(GPT0_BASE, TIMER_A);

    // Calculate timer load and match values
    ticks = 48000000 / freq;
    loadLow = ticks & 0x0000FFFF;
    loadHigh = (ticks & 0x00FF0000) >> 16;
    matchLow = (ticks / 2) & 0x0000FFFF;
    matchHigh = ((ticks / 2) & 0x00FF0000) >> 16;

    // Set timer load
    TimerLoadSet(GPT0_BASE, TIMER_A, loadLow);
    TimerPrescaleSet(GPT0_BASE, TIMER_A, loadHigh);

    // Set timer match
    TimerMatchSet(GPT0_BASE, TIMER_BOTH, matchLow);
    TimerPrescaleMatchSet(GPT0_BASE, TIMER_A, matchHigh);

    // Start timer
    TimerEnable(GPT0_BASE, TIMER_A);

    return true;
}

/*******************************************************************************
 * @fn          SensorTagBuzzer_close
 *
 * @brief       Closes the buzzer interface
 *
 * @return      -
 */
void SensorTagBuzzer_close(void)
{
    if (hPin != NULL)
    {
        // Configure pin as GPIO
        PINCC26XX_setMux(hPin, Board_BUZZER, IOC_PORT_GPIO);

        // Turn off PERIPH power domain and clock for GPT0
        Power_releaseDependency(PowerCC26XX_PERIPH_GPT0);
        Power_releaseConstraint(PowerCC26XX_SB_DISALLOW);

        hPin = NULL;
    }
}

#endif // EXCLUDE_IO


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值