STM32输出不同频率的占空比为50%的PWM信号,控制蜂鸣器音调
(1)主控芯片 F405RGT6
(2)蜂鸣器 低电平触发
STM32F405RGT6 | 蜂鸣器 | JLINK下载器 |
---|---|---|
3.3V | VCC(或不接) | 3.3V |
GND | GND | GND |
PA11(输出PWM信号) | I/O(或蜂鸣器正极) | |
PA14 | SWCK | |
PA13 | SWIO |
(1)Rt-Thread Studio
(2) STM32CubeMX
(1)依次选择文件-------新建----------RT-Thread 项目
(2)按要求选择自己的芯片型号,这里使用STM32F405RG,其余默认
(3)在applications下鼠标右键新建文件夹,命名为pwm_output,并选中文件夹鼠标右键新增pwm_output.c文件
(4) 在pwm_output.c文件中插入以下源码
/*
* Copyright (c) 2006-2020, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-11-12 BROSY the first version
*/
#include
#include
#include
// #define DBG_ENABLE
#define DBG_TAG "pwm"
#define DBG_LEVEL DBG_LOG
#define DBG_COLOR
#include
#define T (1000000000) /* 输出周期(1s), 频率1 */
#define PWM_DEV_NAME "pwm1" /* PWM设备名称 */
#define PWM_DEV_CHANNEL 4 /* PWM通道 */
#define LED GET_PIN(C, 9)
struct rt_device_pwm *pwm_dev; /* PWM设备句柄 */
static rt_thread_t tid1 = RT_NULL; /* PWM输出线程 */
/**
* @brief 初始化PWM
*/
static void pwm_init()
{
rt_pin_mode(LED, PIN_MODE_OUTPUT);
/* 查找设备 */
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);
}
rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
}
/**
* @brief 循环输出1.11kHz、1.25kHz、1.67kHz、5kHz脉冲信号
* @brief 周期为400ms,每个频率的信号的持续时间为100ms
*/
static void pwm_output_entry(void *parameter)
{
rt_uint32_t freq1 = 1111;
rt_uint32_t freq2 = 1250;
rt_uint32_t freq3 = 1666;
rt_uint32_t freq4 = 5000;
rt_uint32_t period = 0;
rt_uint32_t pulse = 0;
pwm_init();
while(1)
{
period = T/freq1; /* 周期 */
pulse = period*0.5; /* 占空比 */
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse); /* 输出PWM */
rt_thread_mdelay(100);
period = T/freq2;
pulse = period*0.5;
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
rt_thread_mdelay(100);
period = T/freq3;
pulse = period*0.5;
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
rt_thread_mdelay(100);
period = T/freq4;
pulse = period*0.5;
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
rt_thread_mdelay(100);
}
}
static int pwm_output_sample(void)
{
tid1 = rt_thread_create("pwm_out",
pwm_output_entry, RT_NULL,
1024, 25, 10);
if (tid1 != RT_NULL)
rt_thread_startup(tid1);
return 0;
}
/* 初始化 */
INIT_APP_EXPORT(pwm_output_sample);
(5)打开CubeMX新建工程,配置TIM1 CH4 输出PWM,并配置输出工程属性,生成代码。
(6)打开stm32f4xx_hal_msp.c,复制下面几个函数
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* htim_pwm)
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef* htim_pwm)
打开Rt-Thread studio drivers文件夹下面的drv_pwm.c文件,将函数粘贴在extern下面
(7)在board.h中加入如下代码
#define BSP_USING_PWM1
#define BSP_USING_PWM1_CH4
(8)在pwm_config.h中加入如下代码
#ifdef BSP_USING_PWM1
#ifndef PWM1_CONFIG
#define PWM1_CONFIG \
{ \
.tim_handle.Instance = TIM1, \
.name = "pwm1", \
.channel = 0 \
}
#endif /* PWM1_CONFIG */
#endif /* BSP_USING_PWM1 */
(9)启用PWM设备驱动,勾选使用PWM设备驱动后ctrl+s保存
(10)打开stm32f4xx_hal_conf.h文件,取消HAL_TIM_MODULE_ENABLED注释
蜂鸣器循环发出四种不同音调
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
period控制信号周期(频率),周期单位为纳秒(ns)
pulse为每个周期输出高电平的时间,控制PWM占空比,大小为0~period