stm32输出的pwm有什么用

stm32输出的pwm有什么用,第1张

stm32单片机的PWM模块就是产生方波的,一般式通过设置其相关定时器来实现产生不同频率,不同占空比的方波信号;同时PWM可以是脉冲宽度调制器,通过改变输入PWM的占空比与频率控制开关管的开关状态来改变输出电压,如常用的开关电源适配器,PWM可调风扇等大都都是利用PWM来实现的。电力电子,电机调速领域都有广泛应用,可以调节脉冲宽度的脉冲.很多地方都有有、在开关电源里面用来驱动开关管.通过改变PWM的宽度就可以实现对输出的控制。在单片机领域使用就更加广泛。电机调速,灯光调亮,电功率调节可能回用到。

#include "stm32f10x.h"

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure

TIM_OCInitTypeDef TIM_OCInitStructure

uint16_t TimerPeriod = 0

uint16_t Channel1Pulse = 0, Channel2Pulse = 0, Channel3Pulse = 0, Channel4Pulse = 0

int main(void)

{

/* System Clocks Configuration */

RCC_Configuration()

/* GPIO Configuration */

GPIO_Configuration()

/* TIM1 Configuration ---------------------------------------------------

Generate 7 PWM signals with 4 different duty cycles:

TIM1CLK = SystemCoreClock, Prescaler = 0, TIM1 counter clock = SystemCoreClock

SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density

and Connectivity line devices and to 24 MHz for Low-Density Value line and

Medium-Density Value line devices

The objective is to generate 7 PWM signal at 17.57 KHz:

- TIM1_Period = (SystemCoreClock / 17570) - 1

The channel 1 and channel 1N duty cycle is set to 50%

The channel 2 and channel 2N duty cycle is set to 37.5%

The channel 3 and channel 3N duty cycle is set to 25%

The channel 4 duty cycle is set to 12.5%

The Timer pulse is calculated as follows:

- ChannelxPulse = DutyCycle * (TIM1_Period - 1) / 100

----------------------------------------------------------------------- */

/* Compute the value to be set in ARR regiter to generate signal frequency at 17.57 Khz */

TimerPeriod = (SystemCoreClock / 17570 ) - 1

/* Compute CCR1 value to generate a duty cycle at 50% for channel 1 and 1N */

Channel1Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10)

/* Compute CCR2 value to generate a duty cycle at 37.5% for channel 2 and 2N */

Channel2Pulse = (uint16_t) (((uint32_t) 375 * (TimerPeriod - 1)) / 1000)

/* Compute CCR3 value to generate a duty cycle at 25% for channel 3 and 3N */

Channel3Pulse = (uint16_t) (((uint32_t) 25 * (TimerPeriod - 1)) / 100)

/* Compute CCR4 value to generate a duty cycle at 12.5% for channel 4 */

Channel4Pulse = (uint16_t) (((uint32_t) 125 * (TimerPeriod- 1)) / 1000)

/* Time Base configuration */

TIM_TimeBaseStructure.TIM_Prescaler = 0

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up

TIM_TimeBaseStructure.TIM_Period = TimerPeriod

TIM_TimeBaseStructure.TIM_ClockDivision = 0

TIM_TimeBaseStructure.TIM_RepetitionCounter = 0

TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure)

/* Channel 1, 2,3 and 4 Configuration in PWM mode */

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable

TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable

TIM_OCInitStructure.TIM_Pulse = Channel1Pulse

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low

TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High

TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set

TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset

TIM_OC1Init(TIM1, &TIM_OCInitStructure)

TIM_OCInitStructure.TIM_Pulse = Channel2Pulse

TIM_OC2Init(TIM1, &TIM_OCInitStructure)

TIM_OCInitStructure.TIM_Pulse = Channel3Pulse

TIM_OC3Init(TIM1, &TIM_OCInitStructure)

TIM_OCInitStructure.TIM_Pulse = Channel4Pulse

TIM_OC4Init(TIM1, &TIM_OCInitStructure)

/* TIM1 counter enable */

TIM_Cmd(TIM1, ENABLE)

/* TIM1 Main Output Enable */

TIM_CtrlPWMOutputs(TIM1, ENABLE)

while (1)

{}

}

/**

* @brief Configures the different system clocks.

* @param None

* @retval None

*/

void RCC_Configuration(void)

{

/* TIM1, GPIOA, GPIOB, GPIOE and AFIO clocks enable */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOE|

RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO, ENABLE)

}

/**

* @brief Configure the TIM1 Pins.

* @param None

* @retval None

*/

void GPIO_Configuration(void)

{

GPIO_InitTypeDef GPIO_InitStructure

#ifdef STM32F10X_CL

/* GPIOE Configuration: Channel 1/1N, 2/2N, 3/3N and 4 as alternate function push-pull */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_11|GPIO_Pin_13|GPIO_Pin_14|

GPIO_Pin_8|GPIO_Pin_10|GPIO_Pin_12

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz

GPIO_Init(GPIOE, &GPIO_InitStructure)

/* TIM1 Full remapping pins */

GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE)

#else

/* GPIOA Configuration: Channel 1, 2 and 3 as alternate function push-pull */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz

GPIO_Init(GPIOA, &GPIO_InitStructure)

/* GPIOB Configuration: Channel 1N, 2N and 3N as alternate function push-pull */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15

GPIO_Init(GPIOB, &GPIO_InitStructure)

#endif

}

可以的, 定时器PWM模式可以输出nS级别的PWM波形,软件IO输出只能到达us级。

STM32F4_TIM输出PWM波形(可调频率、占空比):

http://blog.csdn.net/ybhuangfugui/article/details/51867044


欢迎分享,转载请注明来源:夏雨云

原文地址:https://www.xiayuyun.com/zonghe/367145.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-12
下一篇2023-05-12

发表评论

登录后才能评论

评论列表(0条)

    保存