一、简介
本文将基于STM32F407VET6介绍,如何使用RT-Thread Studio开发环境下使用PWM输入捕获。主要是使用RTT自带的定时器设备进行编写的驱动函数,更加快捷便利。
二、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 中借助图形化配置工具打开定时器的驱动框架,如下图所示:
2.定义定时器宏定义
在board.h文件中添加定时器的宏定义,从而保证能够使用定时器的相关驱动函数。本文介绍的是使用定时器3来进行输入捕获的获取。
/*-------------------------- HARDWARE TIMER CONFIG BEGIN --------------------------*/
/** if you want to use hardware timer you can use the following instructions.
*
* STEP 1, open hwtimer driver framework support in the RT-Thread Settings file
*
* STEP 2, define macro related to the hwtimer
* such as #define BSP_USING_TIM and
* #define BSP_USING_TIM1
*
* STEP 3, copy your hardwire timer init function from stm32xxxx_hal_msp.c generated by stm32cubemx to the end of board.c file
* such as void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
*
* STEP 4, modify your stm32xxxx_hal_config.h file to support hardwere timer peripherals. define macro related to the peripherals
* such as #define HAL_TIM_MODULE_ENABLED
*
*/
#define BSP_USING_TIM
#ifdef BSP_USING_TIM
#define BSP_USING_TIM3
#endif
/*-------------------------- HAREWARE TIMER CONFIG END --------------------------*/
3.编写输入捕获初始化代码
可以使用STM32CubeMx自动生成代码,使能TIM3定时器。这里的初始化代码是用于PWM输出控制的定时器初始化,所以本实验是生成一个PWM信号,输入到该引脚进行输入捕获试验。
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中定时器宏定义打开。
5.初始化定时器
如果初始化的定时器没有,需要进行定义,主要是在tim_config.h中定义名称、中断函数名称等内容
#ifdef BSP_USING_TIM3
#ifndef TIM3_CONFIG
#define TIM3_CONFIG \
{ \
.tim_handle.Instance = TIM3, \
.tim_irqn = TIM3_IRQn, \
.name = "timer3", \
}
#endif /* TIM3_CONFIG */
#endif /* BSP_USING_TIM3 */
6.使用STM32CubeMx进行TIM的初始化
- Clock Source:选择内部时钟
- Combined Channels:选择组合输入PWM捕获,PWM Input on CH1
- PSC:设置定时器的预分频值
- Counter Period:设置自动重装载值
- 打开定时器的中断
四、驱动代码编写
1.pwm_input.c
#include "pwm_input.h"
/**
* @brief 定时器3输入引脚初始化
*/
void TIM3_Input_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* Peripheral clock enable */
__HAL_RCC_TIM3_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**TIM3 GPIO Configuration
PA6 ------> TIM3_CH1
*/
GPIO_InitStruct.Pin = GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* TIM3 interrupt Init */
HAL_NVIC_SetPriority(TIM3_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(TIM3_IRQn);
}
/**
* @brief 定时器3初始化
*/
void MX_TIM3_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_SlaveConfigTypeDef sSlaveConfig = {0};
TIM_IC_InitTypeDef sConfigIC = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
htim3.Instance = TIM3;
htim3.Init.Prescaler = 84 - 1;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 0xFFFF;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_IC_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sSlaveConfig.SlaveMode = TIM_SLAVEMODE_RESET;
sSlaveConfig.InputTrigger = TIM_TS_TI1FP1;
sSlaveConfig.TriggerPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
sSlaveConfig.TriggerPrescaler = TIM_ICPSC_DIV1;
sSlaveConfig.TriggerFilter = 0;
if (HAL_TIM_SlaveConfigSynchro(&htim3, &sSlaveConfig) != HAL_OK)
{
Error_Handler();
}
sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
sConfigIC.ICFilter = 0;
if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfigIC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_FALLING;
sConfigIC.ICSelection = TIM_ICSELECTION_INDIRECTTI;
if (HAL_TIM_IC_ConfigChannel(&htim3, &sConfigIC, TIM_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
HAL_TIM_Base_Start(&htim3);
HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_1); /* 使能定时�?2通道1的PWM输入捕获 */
HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_2); /* 使能定时�?2通道2的PWM输入捕获 */
}
/**
* @brief 定时器捕获函数
* @param htim
*/
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == htim3.Instance)
{
switch(htim->Channel)
{
case HAL_TIM_ACTIVE_CHANNEL_1:
g_tim3.PWM_RisingCount = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1); /* 占空比 */
g_tim3.duty = (float) g_tim3.PWM_FallingCount / g_tim3.PWM_RisingCount * 100.00;
g_tim3.fre = (float) (1.0 / ((g_tim3.PWM_RisingCount + 1) * 0.000001));
break;
case HAL_TIM_ACTIVE_CHANNEL_2:
g_tim3.PWM_FallingCount = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);
break;
default:
break;
}
}
}
/**
* @brief
* @param argc
* @param argv
* @return
*/
static int pwm_print(int argc, char *argv[])
{
rt_kprintf(" PWM_Duty:%d.%d% ", (int) (g_tim3.duty * 100) / 100, (int) (g_tim3.duty * 10) % 10);
rt_kprintf(" PWM_Fre:%d.%d Hz", (int) (g_tim3.fre * 100) / 100, (int) (g_tim3.fre * 10) % 10);
rt_kprintf(" FCNT:%d us RCNT=%d us", g_tim3.PWM_FallingCount, g_tim3.PWM_RisingCount);
return 0;
}
MSH_CMD_EXPORT(pwm_print, pwm print);
- 在读取定时器捕获的计数值时, 使用
g_tim3.duty = (float) g_tim3.PWM_FallingCount / g_tim3.PWM_RisingCount * 100.00;
来计算占空比,高电平的时间比上整个周期,即占空比。 - 在读取频率时,使用
g_tim3.fre = (float) (1.0 / ((g_tim3.PWM_RisingCount + 1) * 0.000001));
来计算,其中计数值时采集到的数据,0.000001是定时器定时1us进入一次。
2.pwm_input.h
#ifndef APPLICATIONS_INC_PWM_INPUT_H_
#define APPLICATIONS_INC_PWM_INPUT_H_
#include <rtthread.h>
#include <board.h>
typedef struct m_tim3
{
uint16_t PWM_RisingCount; // 到上升沿时间--周期
uint16_t PWM_FallingCount; // 到下降沿时间--高电平时间
float duty; // 占空比
float fre; // 频率
} TIM3_TypeStruct;
TIM_HandleTypeDef htim3;
TIM3_TypeStruct g_tim3;
extern void TIM3_Input_Init(void);
extern void MX_TIM3_Init(void);
#endif /* APPLICATIONS_INC_PWM_INPUT_H_ */
3.main.c
#include <rtthread.h>
#include <drv_common.h>
#include "pwm.h"
#include "pwm_input.h"
#define DBG_TAG "main"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
int main(void)
{
int count = 1;
set_pwm_param(500, 40);
TIM3_Input_Init();
MX_TIM3_Init();
while (count)
{
rt_thread_mdelay(1000);
}
return RT_EOK;
}
五、测试验证
通过示波器可以观察到输出的波形,设置的输出频率为:500Hz,输出的占空比为:50%,通过观察波形,可以看到读到的数据基本一致,和测试结果相符,实验合格。测试波形和测试数据如下: