GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource0, GPIO_AF_2); //配置复用功能 PB0 PB1输出PWM
GPIO_PinAFConfig(GPIOB,GPIO_PinSource1, GPIO_AF_2);
#define PRE 200
TIM_TimeBaseStructure.TIM_Period = PRE-1; //5K =200us
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //时钟为48M
TIM_TimeBaseStructure.TIM_Prescaler = 48-1; //预分频为 48,1us 计数一次
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;//TIM_OutputState_Disable;
TIM_OCInitStructure.TIM_Pulse = PRE/2;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;//TIM_OCPolarity_High;
//TIM_OCInitStructure.TIM_OCNIdleState= TIM_OCNIdleState_Reset;
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE);
TIM_Cmd(TIM1, ENABLE);
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>