使用的是STM32F407的Discovery板,用的库是官方的库。只是简单的配置定时器2,然后通过定时器中断实现LED的闪烁,结果定时器中断进不去,查看断点执行的时候,中断服务程序中的flag数值的变化也很混乱,不知道什么原因,麻烦大家帮忙看看是不是配置有问题。。。- #include"stm32f4_discovery.h"
- #include"stm32f4xx.h"
- void led(int n);
- void tim_config(void);
- int flag=0;
- void main()
- {
- SystemInit();
-
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_14;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; /*端口模式为输出 */
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; /*输出类型 推挽输出 */
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- tim_config();
- }
- void tim_config(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
- NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级为 0
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //响应优先级为 0
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //通道中断使能
- NVIC_Init(&NVIC_InitStructure);
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseStructure.TIM_Period = 9999;
- TIM_TimeBaseStructure.TIM_Prescaler = 7199;//定时1s
- TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
- TIM_ClearFlag(TIM2,TIM_FLAG_Update);
- TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
- TIM_Cmd(TIM2,ENABLE);
-
-
- }
- void led(int n)
- {
- if(n==0){
- GPIO_SetBits(GPIOD,GPIO_Pin_12);
- GPIO_SetBits(GPIOD,GPIO_Pin_14);
- }
- else if(n==1){
- GPIO_ResetBits(GPIOD,GPIO_Pin_12);
- GPIO_ResetBits(GPIOD,GPIO_Pin_14);
-
- }
- }
- void TIM2_IRQHandler(void)
- {
- if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET)
- {
- TIM_ClearFlag(TIM2,TIM_FLAG_Update);
- led(flag);
- flag=!flag;
- }
- }
- void Delay(uint32_t nCount)
- {
- while (nCount--);
- }
- #ifdef USE_FULL_ASSERT
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- while (1)
- {}
- }
- #endif
- /**
- * @}
- */
- /**
- * @}
- */
- /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
|