Timer.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "stm32f10x.h" // Device header
  2. #include "Timer_Module.h"
  3. void Timer_Init(void)
  4. {
  5. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  6. TIM_InternalClockConfig(TIM2);
  7. TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
  8. TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  9. TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
  10. TIM_TimeBaseInitStructure.TIM_Period = 10-1;
  11. //TIM_TimeBaseInitStructure.TIM_Prescaler = 7200-1;
  12. TIM_TimeBaseInitStructure.TIM_Prescaler = 800-1;
  13. TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
  14. TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
  15. TIM_ClearFlag(TIM2, TIM_FLAG_Update);
  16. TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
  17. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  18. NVIC_InitTypeDef NVIC_InitStructure;
  19. NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  20. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  21. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  22. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  23. NVIC_Init(&NVIC_InitStructure);
  24. TIM_Cmd(TIM2, ENABLE);
  25. }
  26. void TIM2_IRQHandler(void)
  27. {
  28. if(TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
  29. {
  30. my_timer();
  31. TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
  32. }
  33. }