stm32 tim5输出PWM程序
时间: 2023-08-04 22:05:48 浏览: 179
STM32 TIM+DMA输出PWM工程源码
5星 · 资源好评率100%
以下是一个简单的基于STM32 TIM5的PWM输出程序:
```c
#include "stm32f4xx.h"
#include "stm32f4xx_tim.h"
#include "stm32f4xx_rcc.h"
int main(void)
{
//初始化TIM5
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
TIM_TimeBaseInitStruct.TIM_Prescaler = 1000;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period = 100;
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseInitStruct);
//初始化PWM模式
TIM_OCInitTypeDef TIM_OCInitStruct;
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStruct.TIM_Pulse = 50;
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStruct.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM_OCInitStruct.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStruct.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OC1Init(TIM5, &TIM_OCInitStruct);
//启动TIM5
TIM_Cmd(TIM5, ENABLE);
while(1){
//do something
}
}
```
这个程序会在PA0引脚上输出一个50%的占空比的PWM波形,具体的占空比和频率可以通过修改TIM_OCInitStruct中的参数来实现。
阅读全文