GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
// 开启复用IO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //使能AHB预分频器到TIM1
GPIO_PinRemapConfig(GPIO_FullRemap_TIM1,ENABLE);
// PE9
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE,&GPIO_InitStruct);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //声明TIM_TimeBaseStructure结构变量
TIM_OCInitTypeDef TIM_OCInitStructure; //声明TIM_OCInitStructure结构变量
TIM_TimeBaseStructure.TIM_Period = 1200; //设置TIM1自动重装周期值
TIM_TimeBaseStructure.TIM_Prescaler = 0; //设置TIM1预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割
TIM_TimeBaseStructure.TIM_CounterMode =TIM_CounterMode_Up; //向上计数
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //按以上参数初始化TIM1
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //PWM2模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_Pulse =500; //通道占空比系数
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出低
TIM_OC1Init(TIM1,&TIM_OCInitStructure); //按以上参数初始化TIM1
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //使能预装载寄存器
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_Cmd(TIM1, ENABLE); //使能TIM1
TIM_CCPreloadControl(TIM1,DISABLE);// 必须加这句话否则没有波形产生
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //使能TIM1在CCR1上的预装载寄存器
TIM_ARRPreloadConfig(TIM1, ENABLE); //使能TIMx在ARR上的预装载寄存器
TIM_Cmd(TIM1, ENABLE); //使能TIM1
TIM_CtrlPWMOutputs(TIM1, ENABLE);//设置PMW主输出
?
?最终发现STM32F103ZCT 多个一个bit.
|