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

CC2640之PWM

通过网上查找资料,自己写了一个CC2640的PWM的输出函数。

/**********配置PWM的IO口***************************/

#define PWM0_IOID IOID_3
PIN_Config timerPinTable[] = { PWM0_IOID | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW| PIN_PUSHPULL | PIN_INPUT_DIS | PIN_DRVSTR_MAX, //PIN_INV_INOUT, PIN_TERMINATE };
PIN_Handle hPin;

PIN_State pinState;

/***************************/

void board_pwm_init()
{

  Power_setDependency(PERIPH_GPT0);

 hPin = PIN_open(&pinState, timerPinTable);    //打开引脚

 PINCC26XX_setMux(hPin, PWM0_IOID, IOC_PORT_MCU_PORT_EVENT0);//引脚与定时器事件映射

 TimerConfigure(GPT0_BASE, TIMER_CFG_SPLIT_PAIR|TIMER_CFG_A_PWM);//配置定时器脉宽调制PWM模式,输出A路PWM波

}

/**********PWM输出函数*********************

duration:PWM占空比

/

void board_pwm_start(unsigned long freq_Hz, uint8 duration)
{ unsigned long PWM_FREQ = freq_Hz;
unsigned long PWM_DIV_FACTOR = 48000000/PWM_FREQ;
unsigned long TIMER_LOADSET = (PWM_DIV_FACTOR-1);
unsigned long TIMER_MATCH;
if(duration > 100)
duration = 100;
if(duration>0)
{
TIMER_MATCH = (PWM_DIV_FACTOR*(100-duration)/100-1);
}
if(duration==0)
TIMER_MATCH=PWM_DIV_FACTOR-2;
Power_setConstraint(Power_SB_DISALLOW); //定时器1电源设置 TimerLoadSet(GPT0_BASE, //设置定时器输出斜面波的波峰 TIMER_LOADSET TIMER_A,
TIMER_LOADSET);

TimerMatchSet(GPT0_BASE, //设置定时器输出斜面波的比较值 TIMER_A,
TIMER_MATCH);

TimerEventControl(GPT0_BASE, TIMER_A, TIMER_EVENT_NEG_EDGE); //设置定时器触发方式

TimerEnable(GPT0_BASE,TIMER_A); //使能定时器:
}

在程序中使用board_pwm_start(1000,70),能够输出70%占空比的波形;使用board_pwm_start(1000,35),可以输出35%占空比的波形。但是将两者一起使用就会出现问题。

board_pwm_start(1000,65);

for(;;)

{

Task_sleep(200000);  

board_pwm_start(1000,40);

}

运行这代码,输出占空比就一直在跳变,大致是在15%-65%不停地变。不知道为什么 ,求解惑

Yue TANG:

用SDK里的PWMLED例子试试

C:\ti\simplelink_cc2640r2_sdk_1_40_00_45\examples\rtos\CC2640R2_LAUNCHXL\drivers\pwmled

 

Example Summary

Sample application to control on-board LEDs with the PWM driver.

Peripherals Exercised

Board_PWM0 – PWM instance used to control Board_GPIO_LED0 brightness
Board_PWM1 – PWM instance used to control Board_GPIO_LED1 brightness

Resources & Jumper Settings

If you’re using an IDE (such as CCS or IAR), please refer to Board.html in your project directory for resources used and board-specific jumper settings. Otherwise, you can find Board.html in the directory <SDK_INSTALL_DIR>/source/ti/boards/<BOARD>.

Example Usage

Run the example.

The onboard LEDs will slowly vary in intensity.

If Board_PWM0 and Board_PWM1 are different (connected to two LEDs), both LEDs will fade-in and fade-out when running the application. Otherwise, only one LED will fade-in and fade-out.

Application Design Details

This application uses one thread, mainThread , which performs the following actions:

Opens and initializes PWM driver objects.

Uses the PWM driver to change the intensity of the LEDs.

The thread sleeps for 50 milliseconds before changing LED intensity again.

TI-RTOS:

When building in Code Composer Studio, the kernel configuration project will be imported along with the example. The kernel configuration project is referenced by the example, so it will be built first. The “release” kernel configuration is the default project used. It has many debug features disabled. These feature include assert checking, logging and runtime stack checks. For a detailed difference between the “release” and “debug” kernel configurations and how to switch between them, please refer to the SimpleLink MCU SDK User’s Guide. The “release” and “debug” kernel configuration projects can be found under <SDK_INSTALL_DIR>/kernel/tirtos/builds/<BOARD>/(release|debug)/(ccs|gcc).

FreeRTOS:

Please view the FreeRTOSConfig.h header file for example configuration information.

TI is a global semiconductor design and manufacturing company. Innovate with 100,000+ analog ICs and embedded processors, along with software, tools and the industry‘s largest sales/support staff.

© Copyright 1995-2017 Texas Instruments Incorporated. All rights reserved.

Yue TANG:

C:\ti\simplelink_cc2640r2_sdk_1_40_00_45\docs\tidrivers\

file:///C:/ti/simplelink_cc2640r2_sdk_1_40_00_45/docs/tidrivers/doxygen/html/_p_w_m_8h.html

 

Detailed Description

PWM driver interface.

============================================================================

To use the PWM driver, ensure that the correct driver library for your device is linked in and include this header file as follows:

#include <ti/drivers/PWM.h>

This module serves as the main interface for applications. Its purpose is to redirect the PWM APIs to specific driver implementations which are specified using a pointer to a PWM_FxnTable.

Overview

The PWM driver in TI-RTOS facilitates the generation of Pulse Width Modulated signals via simple and portable APIs. PWM instances must be opened by calling PWM_open() while passing in a PWM index and a parameters data structure.

The driver APIs serve as an interface to a typical TI-RTOS application. The specific peripheral implementations are responsible for creating all OS specific primitives to allow for thread-safe operation.

When a PWM instance is opened, the period, duty cycle and idle level are configured and the PWM is stopped (waveforms not generated until PWM_start() is called). The maximum period and duty supported is device dependent; refer to the implementation specific documentation for values.

PWM outputs are active-high, meaning the duty will control the duration of high output on the pin (at 0% duty, the output is always low, at 100% duty, the output is always high).

Usage

PWM_Handle pwm;
PWM_Params pwmParams;

// Initialize the PWM driver.
PWM_init();

// Initialize the PWM parameters
PWM_Params_init(&pwmParams);
pwmParams.idleLevel = PWM_IDLE_LOW; // Output low when PWM is not running
pwmParams.periodUnits = PWM_PERIOD_HZ; // Period is in Hz
pwmParams.periodValue = 1e6; // 1MHz
pwmParams.dutyUnits = PWM_DUTY_FRACTION; // Duty is in fractional percentage
pwmParams.dutyValue = 0; // 0% initial duty cycle

// Open the PWM instance
pwm = PWM_open(Board_PWM0, &pwmParams);

if (pwm == NULL) {
// PWM_open() failed
while (1);
}

PWM_start(pwm); // start PWM with 0% duty cycle

PWM_setDuty(pwm,
(PWM_DUTY_FRACTION_MAX / 2)); // set duty cycle to 50%

Details for the example code above are described in the following subsections.

PWM Driver Configuration

In order to use the PWM APIs, the application is required to provide device-specific PWM configuration in the Board.c file. The PWM driver interface defines a configuration data structure:

typedef struct PWM_Config_ {
PWM_FxnTable const *fxnTablePtr;
void *object;
void const *hwAttrs;
} PWM_Config;

The application must declare an array of PWM_Config elements, named PWM_config[]. Each element of PWM_config[] is populated with pointers to a device specific PWM driver implementation's function table, driver object, and hardware attributes. The hardware attributes define properties such as which pin will be driven, and which timer peripheral will be used. Each element in PWM_config[] corresponds to a PWM instance, and none of the elements should have NULL pointers.

Additionally, the PWM driver interface defines a global integer variable 'PWM_count' which is initialized to the number of PWM instances the application has defined in the PWM_Config array.

You will need to check the device-specific PWM driver implementation's header file for example configuration. Please also refer to the Board.c file of any of your examples to see the PWM configuration.

Initializing the PWM Driver

PWM_init() must be called before any other PWM APIs. This function calls the device implementation's PWM initialization function, for each element of PWM_config[].

Opening the PWM Driver

Opening a PWM requires four steps:

Create and initialize a PWM_Params structure.
Fill in the desired parameters.
Call PWM_open(), passing the index of the PWM in the PWM_config structure, and the address of the PWM_Params structure. The PWM instance is specified by the index in the PWM_config structure.
Check that the PWM handle returned by PWM_open() is non-NULL, and save it. The handle will be used to read and write to the PWM you just opened.

Only one PWM index can be used at a time; calling PWM_open() a second time with the same index previously passed to PWM_open() will result in an error. You can, though, re-use the index if the instance is closed via PWM_close(). In the example code, Board_PWM0 is passed to PWM_open(). This macro is defined in the example's Board.h file.

Modes of Operation

A PWM instance can be configured to interpret the period as one of three units:

PWM_PERIOD_US: The period is in microseconds.
PWM_PERIOD_HZ: The period is in (reciprocal) Hertz.
PWM_PERIOD_COUNTS: The period is in timer counts.

A PWM instance can be configured to interpret the duty as one of three units:

PWM_DUTY_US: The duty is in microseconds.
PWM_DUTY_FRACTION: The duty is in a fractional part of the period where 0 is 0% and PWM_DUTY_FRACTION_MAX is 100%.
PWM_DUTY_COUNTS: The period is in timer counts and must be less than the period.

The idle level parameter is used to set the output to high/low when the PWM is not running (stopped or not started). The idle level can be set to:

PWM_IDLE_LOW
PWM_IDLE_HIGH

The default PWM configuration is to set a duty of 0% with a 1MHz frequency. The default period units are in PWM_PERIOD_HZ and the default duty units are in PWM_DUTY_FRACTION. Finally, the default output idle level is PWM_IDLE_LOW. It is the application's responsibility to set the duty for each PWM output used.

Controlling the PWM Duty Cycle

Once the PWM instance has been opened and started, the primary API used by the application will be PWM_setDuty() to control the duty cycle of a PWM pin:

PWM_setDuty(pwm, PWM_DUTY_FRACTION_MAX / 2); // Set 50% duty cycle

Implementation

The PWM driver interface module is joined (at link time) to an array of PWM_Config data structures named PWM_config. PWM_config is implemented in the application with each entry being a PWM instance. Each entry in PWM_configcontains a:

(PWM_FxnTable *) to a set of functions that implement a PWM peripheral
(void *) data object that is associated with the PWM_FxnTable
(void *) hardware attributes that are associated with the PWM_FxnTable

The PWM APIs are redirected to the device specific implementations using the PWM_FxnTable pointer of the PWM_config entry. In order to use device specific functions of the PWM driver directly, link in the correct driver library for your device and include the device specific PWM driver header file (which in turn includes PWM.h). For example, for the MSP432 family of devices, you would include the following header file:

#include <ti/drivers/pwm/PWMTimerMSP432.h>

#include <stdint.h>

Go to the source code of this file.

da qin zheng sheng:

可以直接使用官方cc13系列pwmled工程测试,除了射频部分,其它硬件和cc26是兼容的。

赞(0)
未经允许不得转载:TI中文支持网 » CC2640之PWM
分享到: 更多 (0)