我在板子上使用TIM1第一通道产生PWM,配置如下:void TIM1_PWM_CH1_Init(void){ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; 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_DOWN; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1); TIM_DeInit(TIM1); TIM_TimeBaseStructure.TIM_Period = 2000; TIM_TimeBaseStructure.TIM_Prescaler = 84-1; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; TIM_OC1Init(TIM1, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); TIM_ARRPreloadConfig(TIM1, ENABLE); TIM_Cmd(TIM1, ENABLE); TIM_CtrlPWMOutputs(TIM1, ENABLE); TIM_SetCompare1(TIM1, 0);// TIM_Cmd(TIM1, DISABLE);} 如果我在这个之前启动了TIM5的计数器中断,TIM1的输出就会变成高电平,且不受控。 TIM5的配置如下void TIM5_INT_Config(u32 Period){ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; uint16_t prescalerValue = 0; //Enable the TIM2 gloabal Interrupt NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); PValue = ((SystemCoreClock /2) / 1000000) - 1; //Time base configuration TIM_TimeBaseStructure.TIM_Period = Period; TIM_TimeBaseStructure.TIM_Prescaler = PValue; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); TIM_ARRPreloadConfig(TIM5, ENABLE); //TIM Interrupts enable //TIM2 enable counter TIM_Cmd(TIM5, ENABLE); TIM_ClearITPendingBit(TIM5, TIM_IT_Update); TIM_ITConfig(TIM5, TIM_IT_Update, ENABLE); } 后来定位到TIM_ClearITPendingBit(TIM5, TIM_IT_Update);当时为了防止定时器初始化就进中断,清除了标志位,去除这段代码就不会出现这种诡异的现象,理论上不应该啊。求赐教,看TIM1的寄存器都会发生改变。 |
用仿真器查看TIM1和TIM5的寄存器值,看看配置哪边有问题,逐行代码进行仿真。
加油!光看你贴的代码,(没格式,比较乱),未看到什么异常。