想要利用定时器输出PWM波,初始化函数如下
void test_init(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); TIM_InternalClockConfig(TIM1);//选择内部时钟源 //初始化GPIO口 GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = LED_3_GPIOpin|LED_4_GPIOpin; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); //始化驱动LED的PWM波,使用定时器TIM1 TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; TIM_TimeBaseStructInit(&TIM_TimeBaseInitStructure); TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInitStructure.TIM_Period = 100 - 1; //ARR TIM_TimeBaseInitStructure.TIM_Prescaler = 9 - 1; //PSC 8M Hz TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStructure); TIM_OCInitTypeDef TIM_OCInitStructure; TIM_OCStructInit(&TIM_OCInitStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; //CCR TIM_OC3Init(TIM1, &TIM_OCInitStructure);//CH3通道 TIM_OC4Init(TIM1, &TIM_OCInitStructure);//CH3通道 TIM_Cmd(TIM1, ENABLE); } void test_pwm_set(uint16_t Compare) { TIM_SetCompare3(TIM1, Compare); TIM_SetCompare4(TIM1, Compare); }
在主函数调用init初始化后,在while循环中更改占空比,利用示波器测相应管脚均为低电平,LED也未能实现呼吸闪烁
利用相同初始化流程,利用TIM2在PC0(ch3通道)能正常输出PWM
不知道为啥这个TIM1总是无法输出PWM。
尝试过更换板子,但没有改善(莫非是风枪温度高了把TIM1外设给烤没了?,但两块板子都是TIM1有问题TIM2正常
求助!