|
STM32时钟2不中断的分析
先是怀疑时钟使能问题,但是有
- <STRONG>RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); </STRONG>
复制代码 将按键检查加入,下载后没反应
- <STRONG>void TargetInit(void)
- {
- RCC_Configuration();
- GPIO_Configuration();//加入这一句
- NVIC_Configuration();
- Timer_Configuration();
- OS_CPU_SysTickInit();
- }
- GPIO_Configuration();
- {</STRONG>
复制代码 定时器中断部分
- NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);}
- TIM_TimeBaseStructure.TIM_Period = 200;
- TIM_TimeBaseStructure.TIM_Prescaler = 360-1;
- TIM_TimeBaseStructure.TIM_ClockDivision = 0;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
- TIM_ClearFlag(TIM2,TIM_FLAG_Update);
- TIM_ARRPreloadConfig(TIM2, ENABLE);
- TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
- TIM_Cmd(TIM2,ENABLE);
- void TIM2_IRQHandler(void)
- {
- if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
- {
- TIM_ClearITPendingBit(TIM2, TIM_IT_Update );
- GPIO_WriteBit(led_gpio, led4 , (BitAction)!GPIO_ReadOutputDataBit(led_gpio, led4));
- }
- }
复制代码 修改后,指示灯常亮
再次修改,结果TIM2和TIM3都正常,将程序代码贴出: 中断配置 - NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
复制代码
时钟配置:
- void Timer_Configuration(void)
- { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- //TIM_DeInit(TIM2);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//启动TIM2时钟
- TIM_TimeBaseStructure.TIM_Period = 5000;//200;
- TIM_TimeBaseStructure.TIM_Prescaler = (7200-1);//360-1;
- TIM_TimeBaseStructure.TIM_ClockDivision = 0;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
- TIM_ITConfig( //使能或者失能指定的TIM中断
- TIM2, //TIM2
- TIM_IT_Update | //TIM 中断源
- TIM_IT_Trigger, //TIM 触发中断源
- ENABLE //使能);
- }
复制代码初始化流程:
- void TargetInit(void)
- {
- //RCC_Configuration();
- GPIO_Configuration();//加入
- NVIC_Configuration();
- Timer_Configuration();
- //OS_CPU_SysTickInit();
- }
复制代码
中断服务程序:
- void TIM2_IRQHandler(void)
- {
- if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
- {
- TIM_ClearITPendingBit(TIM2, TIM_IT_Update );
- GPIO_WriteBit(led_gpio, led4 , (BitAction)!GPIO_ReadOutputDataBit(led_gpio, led4));
- }
- }
复制代码注意:我发现网友将该代码写在MAIN中,那需将该程序写在MAIN函数之前,我把它提到MAIN之前 最后实验结果,时钟2中断使LED4不停闪烁,网友设置的值较快,看不到闪烁效果,我改了一下。
|
|