先配置GPIO:
void GPIO_Configuration() { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//复用推免输出 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOB,&GPIO_InitStructure); } 使用TIM的PWM波不需要中断 所以不需要配置NVIC; void TIM_Configuration(float x) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//设置TIM的结构体 TIM_OCInitTypeDef TIM_OCInitStructure;//设置TIM PWM的结构体 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period=999; TIM_TimeBaseStructure.TIM_Prescaler=0; TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;//使用PWM2功能 TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//跳变后变成高电平 TIM_OCInitStructure.TIM_Pulse=(short)(x*999);//跳变值 TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; TIM_OC3Init(TIM3,&TIM_OCInitStructure); TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Enable); TIM_Cmd(TIM3,ENABLE); TIM_ARRPreloadConfig(TIM3, ENABLE);
|