搞了半天了,没发现哪里有问题,帮忙看看这是怎么回事
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); //使能PORTA PORTB时钟
RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM8,ENABLE); //TIM2和TIM4时钟使能
TIM_DeInit(TIM8); //重新初始化TIM8为默认状态
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9; //PBC 6 7 8 9
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF; //复用功能
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //速度100MHz
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP; //推挽复用输出
// GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOC,&GPIO_InitStructure); //初始化PC 6 7 8 9
TIM_TimeBaseStructure.TIM_Period=arr; //自动重装载值
TIM_TimeBaseStructure.TIM_Prescaler=psc; //定时器分频
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; //时钟分频
// TIM_TimeBaseStructure.TIM_RepetitionCounter=0; //重复计数次数
TIM_TimeBaseInit(TIM8,&TIM_TimeBaseStructure); //初始化TIM4
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1; //PWM模式1
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; //使能输出
TIM_OCInitStructure.TIM_Pulse=4200; //CCRx
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High; //高电平有效
// TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Reset; //空闲高电平
TIM_OC1Init(TIM8, &TIM_OCInitStructure); //初始化TIM8 CH1输出比较
TIM_OC2Init(TIM8, &TIM_OCInitStructure); //初始化TIM8 CH2输出比较
TIM_OC3Init(TIM8, &TIM_OCInitStructure); //初始化TIM8 CH3输出比较
TIM_OC4Init(TIM8, &TIM_OCInitStructure); //初始化TIM8 CH4输出比较
TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable); //使能TIM8在CCR1上的预装载寄存器
TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable); //使能TIM8在CCR2上的预装载寄存器
TIM_OC3PreloadConfig(TIM8, TIM_OCPreload_Enable); //使能TIM8在CCR3上的预装载寄存器
TIM_OC4PreloadConfig(TIM8, TIM_OCPreload_Enable); //使能TIM8在CCR4上的预装载寄存器
TIM_ARRPreloadConfig(TIM8,ENABLE); //TIM8 ARPE使能
TIM_CtrlPWMOutputs(TIM8, ENABLE);
TIM_Cmd(TIM8, ENABLE); //使能TIM8
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>