今天编写了一个单脉冲输出的代码,我看在野火论坛里很少讨论单脉冲的问题,但在原子里
有一大堆的相关话题和提问,我把自己用HAL库编写的单脉冲输出的代码分享给大家供参考和讨论。
还有一个话题就是到底单脉冲输出有啥实际用处?还望大家多提点实际使用的用处。
以下是TIMSinglePWM.c文件内容:
#include "TIMSinglePWM.h"TIM_HandleTypeDef TIM_HandleStructure;
TIM_OnePulse_InitTypeDef TIM_OnePulse_InitStructure;void TIMSinglePWM_Init(uint16_t arr,uint16_t psc)
{
TIM_HandleStructure.Instance = TIMSinglePWM_TIMx;
TIM_HandleStructure.Init.Prescaler = psc;
TIM_HandleStructure.Init.Period = arr;
TIM_HandleStructure.Init.CounterMode = TIM_COUNTERMODE_UP;
TIM_HandleStructure.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;HAL_TIM_OnePulse_Init(&TIM_HandleStructure,TIM_OPMODE_SINGLE);TIM_OnePulse_InitStructure.OCMode = TIM_OCMODE_PWM1;
TIM_OnePulse_InitStructure.Pulse = 100;
TIM_OnePulse_InitStructure.ICFilter = 0x0;
TIM_OnePulse_InitStructure.ICPolarity = TIM_ICPOLARITY_RISING;
TIM_OnePulse_InitStructure.ICSelection = TIM_ICSELECTION_DIRECTTI;
TIM_OnePulse_InitStructure.OCPolarity = TIM_OCPOLARITY_LOW;
TIM_OnePulse_InitStructure.OCIdleState = TIM_OCIDLESTATE_RESET;HAL_TIM_OnePulse_ConfigChannel(&TIM_HandleStructure,&TIM_OnePulse_InitStructure,TIM_CHANNEL_1,TIM_CHANNEL_2);HAL_TIM_OnePulse_Start(&TIM_HandleStructure,TIM_CHANNEL_1);
}void HAL_TIM_OnePulse_MspInit(TIM_HandleTypeDef *htim)
{
GPIO_InitTypeDef GPIO_InitStructure;TIMSinglePWM_TIM_CLK_ENABLE();
TIMSinglePWM_TIM_GPIO_CLK_ENABLE();GPIO_InitStructure.Pin = TIMSinglePWM_OCPWM_CHANNEL1_GPIO_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
GPIO_InitStructure.Pull = GPIO_PULLUP;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;HAL_GPIO_Init(TIMSinglePWM_OCPWM_CHANNEL1_GPIO_PORT,&GPIO_InitStructure);GPIO_InitStructure.Pin = TIMSinglePWM_ICPWM_CHANNEL2_GPIO_PIN;
GPIO_InitStructure.Mode = GPIO_MODE_AF_INPUT;
GPIO_InitStructure.Pull = GPIO_PULLDOWN;
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;HAL_GPIO_Init(TIMSinglePWM_ICPWM_CHANNEL2_GPIO_PORT,&GPIO_InitStructure);}
以下是TIMSingelPWM.h头文件
#ifndef __TIMSINGLEPWM_H
#define __TIMSINGLEPWM_H#include "sys.h"
#include "led.h"//定时器
#define TIMSinglePWM_TIMx TIM4
#define TIMSinglePWM_TIM_CLK_ENABLE() __HAL_RCC_TIM4_CLK_ENABLE()
#define TIMSinglePWM_TIM_CLK_DISABLE() __HAL_RCC_TIM4_CLK_DISABLE()
#define TIMSinglePWM_TIM_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()//TIM3通道1输出引脚
#define TIMSinglePWM_OCPWM_CHANNEL1_GPIO_PIN GPIO_PIN_6
#define TIMSinglePWM_OCPWM_CHANNEL1_GPIO_PORT GPIOB//TIM3通道2输出引脚
#define TIMSinglePWM_ICPWM_CHANNEL2_GPIO_PIN GPIO_PIN_7
#define TIMSinglePWM_ICPWM_CHANNEL2_GPIO_PORT GPIOBvoid TIMSinglePWM_Init(uint16_t arr,uint16_t psc);#endif /__TIMSINGLEPWM_H/
1.main.c中只需要包含这个头文件,在任何实验板上用示波器、逻辑分析仪观察PB6的单脉冲输出;
2.在PB7上用杜邦线连接实验板的PB7口与3.3V电源端以产生一个正脉冲,或者用开关实现更加方便;
3.观察到的单脉冲输出波形如下图所示(第三个通道的波形):
注明:设计的ARR=1000-1,PSC=72-1,也就是产生的PWM波周期是1ms。设计CCR的值为100,产生的单脉冲宽度=1000-100=0.9ms。
到底单脉冲有啥用处呢?
我查了网上的资料,目前发现2个:
1.用来可控硅的过零检测后的通断控制;
2.用来液晶显示屏上需要精确控制PWM波形数量。
那么到底单脉冲输出有哪些实际应用呢?望大家提供。 |