首页 > 代码库 > stm32L(stm32f)的pwm输出-GPIO设置
stm32L(stm32f)的pwm输出-GPIO设置
STM32L用到GPIO的的复用功能时,需要配置寄存器GPIOX_AFRL与GPIOX_AFRH
stm32F中与stm32l此处不同
今天把调试了一个PWM输出的程序,怎么也出不来波形
就是缺了GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_TIM3); //重点没有就不出波形
void PWM_Init()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_TIM3); //重点没有就不出波形
TIM_DeInit(TIM3);
TIM_TimeBaseStructure.TIM_Prescaler = 5; ////预分频(时钟分频)6M/(6+1)=1000K
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 5; //计数器 1000k/5=200k
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_SelectOCxM(TIM3, TIM_Channel_2, TIM_OCMode_PWM2);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 2;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_Cmd(TIM3, ENABLE);
}
stm32L(stm32f)的pwm输出-GPIO设置