我想使用TIM4的PWM功能,不知道为什么仿真得不到结果呢?
代码如下,求大神看看问题出在哪里:
#include "tim4pwm.h"
uint32_t CCR2VAL = 500;
static void TIM4PWM_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* TIM4 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
/* GPIOD clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
/*GPIOD Configuration: TIM4 channel 2 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
static void TIM4PWM_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseStructure.TIM_Period = 999; //当定时器从0计数到999,即为1000次,为一个定时周期
TIM_TimeBaseStructure.TIM_Prescaler = 0; //设置预分频:不预分频,即为72MHz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //设置时钟分频系数:不分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2VAL; //设置通道2的电平跳变值,输出另外一个占空比的PWM
TIM_OC2Init(TIM4, &TIM_OCInitStructure); //使能通道2
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM4, ENABLE); // 使能TIM4重载寄存器ARR
/* TIM4 enable counter */
TIM_Cmd(TIM4, ENABLE); //使能定时器4
}
void TIM4PWMInit(void)
{
TIM4PWM_GPIO_Config();
TIM4PWM_Mode_Config();
} |