本文所讲内容的开发坏境基于keil5,rt-thread-4.0.3,env,STM32CubeMX等,硬件为德飞莱尼莫m3s开发板。开发环境搭建及安装请自行百度。内容简单粗暴,不要太计较,主要是实现了功能。
本文不讲理论知识,rttheard PWM设备驱动相关知识请移步官网https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/device/pwm/pwm
本文也不讲rtthread移植相关及env环境搭建,要想了解相关知识请移步官网https://www.rt-thread.org/document/site/#/development-tools/env/env
德飞莱尼莫m3s开发板采用STM32F103ZET6作为主控芯片,共有两个红色用户功能led灯,如下图:
本文选用led3 作为被控led灯,由图可知要想控制led3的亮度,就是控制PB5引脚的低电平持续时间,持续时间越长越亮,越短越暗。
查询STM32F103ZET6相关手册可知PB5对应TIM3_CH2通道,所以我们就是要配置此引脚的PWM功能。方法如下:
1.通过env配置相关功能,进入工程目录下,rt-thread-4.0.3\bsp\stm32\stm32f103-dofly-M3S,右击鼠标,弹出如下画面
选择ConEmu Here后弹出如下所示画面
输入menuconfig后按回车键打开配置画面,如下:
按键盘上下箭头选择第二项,RT-Thread Components --->,如下图
按回车进入,进入后按键盘上下箭头选择Device Drivers --->,如下图
按回车进入,进入后按键盘上下箭头选择[ ] Using hardware timer device drivers,如下图
按下Y键使能此项配置,选项前面的方挂号内会多出*,使能后如下图所示
继续往下找到 [ ] Using PWM device drivers选项,并使能它,方法如上。如下图:
使能之前需要修改一下Kconfig配置文件,此文件在rt-thread-4.0.3\components\drivers目录下,修改内容如下:
config RT_USING_PWM
bool "Using PWM device drivers"
default n
if RT_USING_PWM
config BSP_USING_PWM1
bool "Using TIM1_PWM"
default n
if BSP_USING_PWM1
config BSP_USING_PWM1_CH1
bool "Using TIM1 PWM channel1"
default n
config BSP_USING_PWM1_CH2
bool "Using TIM1 PWM channel1"
default n
config BSP_USING_PWM1_CH3
bool "Using TIM1 PWM channel1"
default n
config BSP_USING_PWM1_CH4
bool "Using TIM1 PWM channel1"
default n
endif
config BSP_USING_PWM2
bool "Using TIM2_PWM"
default n
if BSP_USING_PWM2
config BSP_USING_PWM2_CH1
bool "Using TIM2 PWM channel1"
default n
config BSP_USING_PWM2_CH2
bool "Using TIM2 PWM channel2"
default n
config BSP_USING_PWM2_CH3
bool "Using TIM2 PWM channel3"
default n
config BSP_USING_PWM2_CH4
bool "Using TIM2 PWM channel4"
default n
endif
config BSP_USING_PWM3
bool "Using TIM3_PWM"
default n
if BSP_USING_PWM3
config BSP_USING_PWM3_CH1
bool "Using TIM3 PWM channel1"
default n
config BSP_USING_PWM3_CH2
bool "Using TIM3 PWM channel2"
default n
config BSP_USING_PWM3_CH3
bool "Using TIM3 PWM channel3"
default n
config BSP_USING_PWM3_CH4
bool "Using TIM3 PWM channel4"
default n
endif
config BSP_USING_PWM4
bool "Using TIM4_PWM"
default n
if BSP_USING_PWM4
config BSP_USING_PWM4_CH1
bool "Using TIM4 PWM channel1"
default n
config BSP_USING_PWM4_CH2
bool "Using TIM4 PWM channel2"
default n
config BSP_USING_PWM4_CH3
bool "Using TIM4 PWM channel3"
default n
config BSP_USING_PWM4_CH4
bool "Using TIM4 PWM channel4"
default n
endif
config BSP_USING_PWM5
bool "Using TIM5_PWM"
default n
if BSP_USING_PWM5
config BSP_USING_PWM5_CH1
bool "Using TIM5 PWM channel1"
default n
config BSP_USING_PWM5_CH2
bool "Using TIM5 PWM channel2"
default n
config BSP_USING_PWM5_CH3
bool "Using TIM5 PWM channel3"
default n
config BSP_USING_PWM5_CH4
bool "Using TIM5 PWM channel4"
default n
endif
endif
该段修改的内容具体是啥意思这里不作讲解,想了解的可以百度搜索相关资料研究。这段代码主要用来实现PWM功能的配置。使能后如下图所示:
从图中可以看出选择了 TIM3的PWM_CH2功能。配置好后连续按ESC键退至如下所示画面
按下回车键退出配置画面。
输入scons --target=mdk5按回车键重新生成keil5工程。
打开工程,在工程中stm32f1xx_hal_msp.c最下方添加如下代码:
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* tim_pwmHandle)
{
if(tim_pwmHandle->Instance==TIM3)
{
/* USER CODE BEGIN TIM3_MspInit 0 */
/* USER CODE END TIM3_MspInit 0 */
/* TIM3 clock enable */
__HAL_RCC_TIM3_CLK_ENABLE();
/* USER CODE BEGIN TIM3_MspInit 1 */
/* USER CODE END TIM3_MspInit 1 */
}
}
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(timHandle->Instance==TIM3)
{
/* USER CODE BEGIN TIM3_MspPostInit 0 */
/* USER CODE END TIM3_MspPostInit 0 */
__HAL_RCC_GPIOB_CLK_ENABLE();
/**TIM3 GPIO Configuration
PB5 ------> TIM3_CH2
*/
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
__HAL_AFIO_REMAP_TIM3_PARTIAL();
/* USER CODE BEGIN TIM3_MspPostInit 1 */
/* USER CODE END TIM3_MspPostInit 1 */
}
}
void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef* tim_pwmHandle)
{
if(tim_pwmHandle->Instance==TIM3)
{
/* USER CODE BEGIN TIM3_MspDeInit 0 */
/* USER CODE END TIM3_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM3_CLK_DISABLE();
/* USER CODE BEGIN TIM3_MspDeInit 1 */
/* USER CODE END TIM3_MspDeInit 1 */
}
}
该段代码主要用于初始化PB5引脚的PWM硬件功能,此段代码可以通过STM32CubeMX生成,生成方法自己百度搜索解决。
编译工程测试设备驱动添加是否成功,如果编译无错表示添加成功,编译有误根据实际错误排查问题,如有疑问可以联系我一起帮忙解决。
在主程序中添加测试代码,如下:
int main(void)
{
struct rt_device_pwm *pwm_dev; /* PWM设备句柄 */
static rt_uint32_t period, pulse;
#define PWM_DEV_NAME "pwm3" /* PWM设备名称 */
#define PWM_DEV_CHANNEL 2 /* PWM通道 */
period = 500000; /* 周期为0.5ms,单位为纳秒ns */
pulse = 0; /* PWM脉冲宽度值,单位为纳秒ns */
/* 查找设备 */
pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
/* 设置PWM周期和脉冲宽度 */
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
/* 使能设备 */
rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
// /* 关闭设备通道 */
// rt_pwm_disable(pwm_dev,PWM_DEV_CHANNEL);
while (count++)
{
pulse = pulse + 2000;
if(pulse >= period) pulse = 0;
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
rt_thread_mdelay(10);
}
return RT_EOK;
}
编译后下载至开发板中观察led3调节亮度效果,如下:
<iframe allowfullscreen="true" data-mediaembed="tencent" id="hXW4yWtL-1627266229184" src="https://v.qq.com/txp/iframe/player.html?vid=n3263z7js0k"></iframe>rtthread pwm调节led亮度
https://v.qq.com/x/page/n3263z7js0k.html