Home > other >  Small white questions stm32f407 development board to study, use the timer 1 PWM mode frequency squar
Small white questions stm32f407 development board to study, use the timer 1 PWM mode frequency squar

Time:10-14

 void TIM2_PWM_config (u32 arr) 
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;

RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM2, ENABLE);
RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_PinAFConfig (GPIOA, GPIO_PinSource15 GPIO_AF_TIM2);
GPIO_PinAFConfig (GPIOB, GPIO_PinSource3 GPIO_AF_TIM2);

GPIO_InitStructure. GPIO_Pin=GPIO_Pin_15;
GPIO_InitStructure. GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_InitStructure. GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_Init (GPIOA, & amp; GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;
GPIO_Init (GPIOB, & amp; GPIO_InitStructure);

TIM_ITConfig (TIM2, TIM_IT_Update, ENABLE);

NVIC_InitStructure. NVIC_IRQChannel=TIM2_IRQn;
NVIC_InitStructure. NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure. NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure. NVIC_IRQChannelCmd=ENABLE;
NVIC_Init (& amp; NVIC_InitStructure);

TIM_TimeBaseStructure. TIM_Period=arr;
TIM_TimeBaseStructure. TIM_Prescaler=0;
TIM_TimeBaseStructure. TIM_ClockDivision=0;
TIM_TimeBaseStructure. TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit (TIM2, & amp; TIM_TimeBaseStructure);


TIM_OCInitStructure. TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure. TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure. TIM_OCPolarity=TIM_OCPolarity_Low;
TIM_OC1Init (TIM2, & amp; TIM_OCInitStructure);
TIM_OC1PreloadConfig (TIM2, TIM_OCPreload_Enable);
//TIM_ARRPreloadConfig (TIM2, ENABLE);


TIM_OCInitStructure. TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure. TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure. TIM_OCPolarity=TIM_OCPolarity_Low;
TIM_OC2Init (TIM2, & amp; TIM_OCInitStructure);
TIM_OC2PreloadConfig (TIM2, TIM_OCPreload_Enable);

TIM2 - & gt; CCR1=arr/2;
TIM2 - & gt; CCR2=arr/2;

TIM_Cmd (TIM2, ENABLE);
}

CodePudding user response:

Say the wrong is the timer 2
  • Related