通用定时器产生pwm信号失败
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_tim.h"
TIM_TimeBaseInitTypeDef TIM3_TimeBaseStructure;
TIM_OCInitTypeDef TIM3_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
void rcc_configuration(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOB,ENABLE);
}
void time_init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);
TIM3_TimeBaseStructure.TIM_Prescaler=2;
TIM3_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM3_TimeBaseStructure.TIM_Period =24000;
TIM3_TimeBaseStructure.TIM_ClockDivision=0x0;
TIM3_TimeBaseStructure.TIM_RepetitionCounter=0x0;
TIM_TimeBaseInit(TIM3,&TIM3_TimeBaseStructure);
TIM3_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM3_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM3_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM3_OCInitStructure.TIM_Pulse=18000;
TIM_OC2Init(TIM3,&TIM3_OCInitStructure);
TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);
TIM_Cmd(TIM3,ENABLE);
}
int main(void)
{
rcc_configuration();
time_init();
while(1)
{
TIM3_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM3_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM3_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM3_OCInitStructure.TIM_Pulse=18000;
TIM_OC2Init(TIM3,&TIM3_OCInitStructure);
}
}