文章目录
一、简介
本文将基于STM32F407VET6介绍,如何使用RT-Thread Studio开发环境下使用输出PWM波形。主要是使用RTT自带的PWM设备进行编写的驱动函数,更加快捷便利。
二、RTT时钟配置
由于使用RTT生成的工程默认使用的是系统内部时钟,便于我们对时间的控制,所以通常会使用外部时钟,因此需要对工程中的时钟进行更改,更改内容如下:
- 打开RT-Thread Studio软件新建基于芯片的项目,并使用外部时钟系统。
- 在drv_clk.c文件中添加时钟配置函数,并且注释内部时钟的调用。
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 4;
RCC_OscInitStruct.PLL.PLLN = 168;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
void clk_init(char *clk_source, int source_freq, int target_freq)
{
// system_clock_config(target_freq);
SystemClock_Config();
}
三、PWM初始化配置
1.打开PWM驱动框架
在RT-Thread Setting 中借助图形化配置工具打开PWM的驱动框架,如下图所示:
2.定义PWM宏定义
在board.h文件中添加pwm的宏定义,从而保证能够使用pwm的相关驱动函数。
/*-------------------------- PWM CONFIG BEGIN --------------------------*/
/** if you want to use pwm you can use the following instructions.
*
* STEP 1, open pwm driver framework support in the RT-Thread Settings file
*
* STEP 2, define macro related to the pwm
* such as #define BSP_USING_PWM1
*
* STEP 3, copy your pwm timer init function from stm32xxxx_hal_msp.c generated by stm32cubemx to the end if board.c file
* such as void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) and
* void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
*
* STEP 4, modify your stm32xxxx_hal_config.h file to support pwm peripherals. define macro related to the peripherals
* such as #define HAL_TIM_MODULE_ENABLED
*
*/
#define BSP_USING_PWM12
#define BSP_USING_PWM12_CH2
#endif
/*-------------------------- PWM CONFIG END --------------------------*/
3.编写DAC初始化代码
可以使用STM32CubeMx自动生成代码,使能PWM12定时器。
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance==TIM12)
{
/* TIM12 clock enable */
__HAL_RCC_TIM12_CLK_ENABLE();
}
}
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(timHandle->Instance==TIM12)
{
__HAL_RCC_GPIOB_CLK_ENABLE();
/**TIM12 GPIO Configuration
PB15 ------> TIM12_CH2
*/
GPIO_InitStruct.Pin = GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF9_TIM12;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
}
4.打开STM32宏定义
在driver中的stm32f4xx_hal_conf.h中PWM和定时器宏定义打开。
四、驱动代码编写
1.pwm.c
主要包含一个函数,该函数是设置PWM的参数,主要是频率和占空比,可以增加频率和占空比的设置范围
#include "pwm.h"
/**
* @brief 设置PWM的参数
* @param fre 频率 0-999
* @param duty 占空比 0-100
* @return
*/
int set_pwm_param(rt_uint32_t fre, rt_uint32_t duty)
{
rt_uint32_t period, pulse;
if ((fre < 0 && fre > 1000) && duty > 0)
{
rt_kprintf("set param error...\n");
return -RT_ERROR;
}
else
{
if (duty >= 100)
{
duty = 100;
}
}
period = 1000000000 / fre; /* 周期为0.5ms,单位为纳秒ns */
pulse = period * duty / 100; /* PWM脉冲宽度值 */
/* 查找设备 */
pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
if (pwm_dev == RT_NULL)
{
rt_kprintf("pwm sample run failed! can't find %s device!\n", PWM_DEV_NAME);
return RT_ERROR;
}
/* 设置PWM周期和脉冲宽度默认值 */
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
/* 使能设备 */
rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
return 0;
}
2.pwm.h
#ifndef APPLICATIONS_INC_PWM_H_
#define APPLICATIONS_INC_PWM_H_
#include <rtthread.h>
#include <rtdevice.h>
#if 1
#define PWM_DEV_NAME "pwm12" // PWM设备名称
#define PWM_DEV_CHANNEL 2 // PWM通道
#define PWM_GPIO_PORT GPIOB // pwm端口号 -- TIM12_CH2
#define PWM_GPIO_PIN GPIO_PIN_15 // pwm引脚号
#else
#define PWM_DEV_NAME "pwm3" // PWM设备名称
#define PWM_DEV_CHANNEL 2 // PWM通道
#define PWM_GPIO_PORT GPIOA // pwm端口号 -- TIM3_CH2
#define PWM_GPIO_PIN GPIO_PIN_7 // pwm引脚号
#endif
struct rt_device_pwm *pwm_dev; // PWM设备句柄
extern int set_pwm_param(rt_uint32_t fre, rt_uint32_t duty);
#endif /* APPLICATIONS_INC_PWM_H_ */
3.main.c
主要是设置PWM的输出频率和占空比,然后进行PWM的输出。
#include <rtthread.h>
#include <drv_common.h>
#include "pwm.h"
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
#define ID GET_PIN(E, 12)
int main(void)
{
int count = 1;
rt_pin_mode(ID, PIN_MODE_OUTPUT);
set_pwm_param(500, 50);
while (count)
{
rt_pin_write(ID, RESET);
rt_thread_mdelay(1000);
rt_pin_write(ID, SET);
rt_thread_mdelay(1000);
}
return RT_EOK;
}
五、测试验证
通过示波器可以观察到输出的波形,设置的输出频率为:500Hz,输出的占空比为:50%,通过观察波形,可以看到,输出的波形频率为:499.989Hz,输出占空比为50.0%,和测试结果相符,实验合格。测试波形如下: