平台:Code Composer Studio 10.4.0
MSP432P401R SimpleLink™ 微控制器 LaunchPad™ 开发套件
(MSP-EXP432P401R)
API (机翻)
函数
void PWM_close (PWM_Handle handle)
函数关闭由PWM句柄指定的PWM实例
int_fast16_t PWM_control (PWM_Handle handle, uint_fast16_t cmd, void *arg)
函数在给定的PWM_Handle上执行特定的实现特性
void PWM_init (void)
这个函数初始化PWM模块
PWM_Handle PWM_open (uint_least8_t index, PWM_Params *params)
这个函数打开一个给定的PWM实例,并将周期、负载和空闲电平设置为params参数中指定的值
void PWM_Params_init (PWM_Params *params)
函数将PWM_Params结构初始化为默认值
int_fast16_t PWM_setDuty (PWM_Handle handle, uint32_t duty)
命令功能设置指定PWM句柄的占空比。PWM实例在主动高电平输出模式(非开漏)下运行;
0%代表低电平,100%代表高电平。这个API可以被调用,而PWM正在运行&负载必须总是
低于或等于周期。如果调用该函数时发生错误,PWM占空比将保持不变
int_fast16_t PWM_setPeriod (PWM_Handle handle, uint32_t period)
设置指定PWM句柄的周期。这个API可以在PWM运行时调用。周期必须总是大于等于占空比。
如果调用该函数时发生错误,PWM周期将保持不变
int_fast16_t PWM_setDutyAndPeriod (PWM_Handle handle, uint32_t duty, uint32_t period)
设置指定PWM句柄的周期和占空比。这个API必须在PWM运行时调用。周期必须总是大于占空比。
如果在调用函数时发生错误,周期和占空比将保持不变
void PWM_start (PWM_Handle handle)
以当前设置启动指定的PWM句柄
void PWM_stop (PWM_Handle handle)
能停止指定的PWM句柄。输出将被设置为PWM_open()中的参数指定的空闲级别
上机实战
配置引脚
LED1,用于指示单片机正常工作
PWM输出引脚
PWM初始化,实现简易呼吸灯的效果
/*
* ======== mainThread ========
*/
void *mainThread(void *arg0)
{
float LED2_G_Duty = 0;
int8_t LED2_DIR = 1;
My_Task_Init(LED_Task, 1, 1024);
My_PWM_Hz_Init(&hpwm1, PWM_1, 1000);
while(1)
{
if(LED2_G_Duty >= 100)
LED2_DIR = -1;
else if(LED2_G_Duty <= 0)
LED2_DIR = 1;
LED2_G_Duty += LED2_DIR * 0.5;
if(LED2_G_Duty < 0)
LED2_G_Duty = 0;
else if(LED2_G_Duty > 100)
LED2_G_Duty = 100;
My_PWM_setDuty(&hpwm1, LED2_G_Duty);
usleep(1000);
}
}
实验结果
完整代码
myPWM.c
/*
* myPWM.c
*
* Created on: 2021年8月2日
* Author: Royic
*/
// Import PWM Driver definitions
#include "./inc/myPWM.h"
PWM_Handle hpwm1;
void My_PWM_Hz_Init(PWM_Handle *hpwm, uint_least8_t index, uint32_t Frequency)
{
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 = Frequency; // Frequency Hz
pwmParams.dutyUnits = PWM_DUTY_FRACTION; // Duty is in fractional percentage
pwmParams.dutyValue = 0; // 0% initial duty cycle
// Open the PWM instance
*hpwm = PWM_open(index, &pwmParams);
if (*hpwm == NULL)
{
// PWM_open() failed
while (1);
}
PWM_start(*hpwm); // start PWM with 0% duty cycle
}
inline void My_PWM_setDuty(PWM_Handle *hpwm, float Percentage)
{
PWM_setDuty(*hpwm, (uint32_t) (PWM_DUTY_FRACTION_MAX / 100. * Percentage)); // set duty cycle to Duty_Cycle%
}
myPWM.h
/*
* myTask.h
*
* Created on: 2021年8月2日
* Author: Royic
*/
#ifndef INC_MYTASK_H_
#define INC_MYTASK_H_
#include "./inc/main.h"
void *mainThread(void *arg0);
void My_Task_Init(void *(*startroutine)(void *), int priority, size_t stacksize);
void *LED_Task(void *arg0);
#endif /* INC_MYTASK_H_ */
myTask.c
/*
* myTask.c
*
* Created on: 2021年8月2日
* Author: Royic
*/
/* POSIX Header files */
#include <pthread.h>
/* RTOS header files */
#include <ti/sysbios/BIOS.h>
#include "./inc/myTask.h"
/* Driver Header files */
#include <ti/drivers/GPIO.h>
void My_Task_Init(void *(*startroutine)(void *), int priority, size_t stacksize)
{
pthread_t thread;
pthread_attr_t attrs;
struct sched_param priParam;
int retc;
/* Initialize the attributes structure with default values */
pthread_attr_init(&attrs);
/* Set priority, detach state, and stack size attributes */
priParam.sched_priority = priority;
retc = pthread_attr_setschedparam(&attrs, &priParam);
retc |= pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);
retc |= pthread_attr_setstacksize(&attrs, stacksize);
if (retc != 0)
{
/* failed to set attributes */
while (1)
{
}
}
retc = pthread_create(&thread, &attrs, startroutine, NULL);
if (retc != 0)
{
/* pthread_create() failed */
while (1)
{
}
}
}
void *LED_Task(void *arg0)
{
while(1)
{
GPIO_toggle(LED1);
sleep(1);
}
}
myTask.h
/*
* myTask.h
*
* Created on: 2021年8月2日
* Author: Royic
*/
#ifndef INC_MYTASK_H_
#define INC_MYTASK_H_
#include "./inc/main.h"
void *mainThread(void *arg0);
void My_Task_Init(void *(*startroutine)(void *), int priority, size_t stacksize);
void *LED_Task(void *arg0);
#endif /* INC_MYTASK_H_ */
main.c
/*
* ======== main_tirtos.c ========
*/
#include "./inc/main.h"
/* POSIX Header files */
#include <pthread.h>
/* RTOS header files */
#include <ti/sysbios/BIOS.h>
/* Driver configuration */
#include <ti/drivers/Board.h>
#include <ti/drivers/GPIO.h>
#include "./inc/myTask.h"
#include "./inc/myPWM.h"
/*
* ======== main ========
*/
int main(void)
{
/* Call driver init functions */
Board_init();
GPIO_init();
My_Task_Init(mainThread, 1, 1024);
BIOS_start();
return (0);
}
/*
* ======== mainThread ========
*/
void *mainThread(void *arg0)
{
float LED2_G_Duty = 0;
int8_t LED2_DIR = 1;
My_Task_Init(LED_Task, 1, 1024);
My_PWM_Hz_Init(&hpwm1, PWM_1, 1000);
while(1)
{
if(LED2_G_Duty >= 100)
LED2_DIR = -1;
else if(LED2_G_Duty <= 0)
LED2_DIR = 1;
LED2_G_Duty += LED2_DIR * 0.5;
if(LED2_G_Duty < 0)
LED2_G_Duty = 0;
else if(LED2_G_Duty > 100)
LED2_G_Duty = 100;
My_PWM_setDuty(&hpwm1, LED2_G_Duty);
usleep(1000);
}
}
main.h
/*
* main.h
*
* Created on: 2021年8月2日
* Author: Royic
*/
#ifndef INC_MAIN_H_
#define INC_MAIN_H_
/* For usleep() */
#include <unistd.h>
#include <stdint.h>
#include <stddef.h>
/* Driver configuration */
#include "ti_drivers_config.h"
#endif /* INC_MAIN_H_ */