MSP432P401R TI Drivers 库函数学习笔记(五)PWM
生活随笔
收集整理的這篇文章主要介紹了
MSP432P401R TI Drivers 库函数学习笔记(五)PWM
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
- API (機翻)
- 函數
- 上機實戰
- 配置引腳
- PWM初始化,實現簡易呼吸燈的效果
- 實驗結果
- 完整代碼
- myPWM.c
- myPWM.h
- myTask.c
- myTask.h
- main.c
- main.h
平臺:Code Composer Studio 10.4.0
MSP432P401R SimpleLink? 微控制器 LaunchPad? 開發套件
(MSP-EXP432P401R)
API (機翻)
PWM 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 parametersPWM_Params_init(&pwmParams);pwmParams.idleLevel = PWM_IDLE_LOW; // Output low when PWM is not runningpwmParams.periodUnits = PWM_PERIOD_HZ; // Period is in HzpwmParams.periodValue = Frequency; // Frequency HzpwmParams.dutyUnits = PWM_DUTY_FRACTION; // Duty is in fractional percentagepwmParams.dutyValue = 0; // 0% initial duty cycle// Open the PWM instance*hpwm = PWM_open(index, &pwmParams);if (*hpwm == NULL){// PWM_open() failedwhile (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_ */總結
以上是生活随笔為你收集整理的MSP432P401R TI Drivers 库函数学习笔记(五)PWM的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java虚拟机学习-JVM调优总结-新一
- 下一篇: CleanMyMac X4.11.1中文