Timer.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /********************************************************************************
  2. * File Name: Timer.c
  3. *******************************************************************************/
  4. #include "includes.h"
  5. #include "Timer.h"
  6. #define TIM2_COUNTER_CLOCK 1000000 //计数时钟(次/秒)
  7. #define TIM2_PRESCALER_VALUE (SystemCoreClock/TIM2_COUNTER_CLOCK-1)
  8. #define TIM3_COUNTER_CLOCK 1000 //计数时钟(次/秒)
  9. #define TIM3_PRESCALER_VALUE (SystemCoreClock/TIM3_COUNTER_CLOCK-1)
  10. pt_timer_t PoolTimer;
  11. /*********************************************************************************
  12. *Timer3_Init
  13. arr定时时长
  14. 因为TIM3_COUNTER_CLOCK=1K/次 因此arr以ms为单位 如arr=65535 定时时间位65535ms=65.35s
  15. **********************************************************************************/
  16. void Timer3_Init(unsigned short arr)
  17. {
  18. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  19. NVIC_InitTypeDef NVIC_InitStructure;
  20. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  21. TIM_TimeBaseStructure.TIM_Period = arr; //定是周期到,自动重载值
  22. TIM_TimeBaseStructure.TIM_Prescaler =TIM3_PRESCALER_VALUE; //预分频值
  23. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //时钟分频因子
  24. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  25. TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  26. NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  27. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
  28. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  29. NVIC_Init(&NVIC_InitStructure);
  30. TIM_ClearFlag(TIM3, TIM_FLAG_Update);
  31. }
  32. /*********************************************************************************
  33. *Timer3_enable
  34. **********************************************************************************/
  35. void Timer3_enable( void )
  36. {
  37. TIM_ClearFlag(TIM3, TIM_FLAG_Update);
  38. TIM_SetCounter(TIM3,0x00);
  39. TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
  40. TIM_Cmd(TIM3,ENABLE);
  41. }
  42. /*********************************************************************************
  43. *Timer3_disable
  44. **********************************************************************************/
  45. void Timer3_disable (void)
  46. {
  47. TIM_ITConfig(TIM3, TIM_IT_Update | TIM_IT_Trigger,DISABLE );
  48. TIM_Cmd(TIM3, DISABLE);
  49. }
  50. /*********************************************************************************
  51. *Timer2_Init
  52. arr定时时长
  53. 因为TIM2_COUNTER_CLOCK=1M/次 因此arr以us为单位 如arr=65535 则定时时间为65535us=65.535ms
  54. **********************************************************************************/
  55. void Timer2_Init(unsigned short arr)
  56. {
  57. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  58. NVIC_InitTypeDef NVIC_InitStructure;
  59. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  60. TIM_TimeBaseStructure.TIM_Period = arr;
  61. TIM_TimeBaseStructure.TIM_Prescaler =TIM2_PRESCALER_VALUE;
  62. TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1; //时钟分频因子
  63. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  64. TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  65. NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  66. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
  67. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  68. NVIC_Init(&NVIC_InitStructure);
  69. TIM_ClearFlag(TIM2,TIM_FLAG_Update);
  70. }
  71. /*********************************************************************************
  72. *Timer2_enable
  73. **********************************************************************************/
  74. void Timer2_enable( void )
  75. {
  76. TIM_ClearFlag(TIM2, TIM_FLAG_Update);
  77. TIM_SetCounter(TIM2,0x00);
  78. TIM_ITConfig(TIM2,TIM_IT_Update|TIM_IT_Trigger,ENABLE);
  79. TIM_Cmd(TIM2,ENABLE);
  80. }
  81. /*********************************************************************************
  82. *Timer2_disable
  83. **********************************************************************************/
  84. void Timer2_disable (void)
  85. {
  86. TIM_ITConfig(TIM2, TIM_IT_Update | TIM_IT_Trigger,DISABLE );
  87. TIM_Cmd(TIM2, DISABLE);
  88. }
  89. /*********************************************************************************
  90. *TIM3_IRQHandler
  91. **********************************************************************************/
  92. //void TIM3_IRQHandler(void)
  93. //{
  94. // if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
  95. // {
  96. // TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
  97. // //Timer3_disable();
  98. // //g_ulTimer3Ct++;
  99. //
  100. // }
  101. //}
  102. /*********************************************************************************
  103. *TIM2_IRQHandler
  104. **********************************************************************************/
  105. //void TIM2_IRQHandler(void)
  106. //{
  107. // if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
  108. // {
  109. // PTTimerTick(&PoolTimer);
  110. // TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
  111. // }
  112. //}
  113. /*********************************************************************************
  114. SoftTimerInit
  115. 软定时器初始化
  116. **********************************************************************************/
  117. void STimerInit(void)
  118. {
  119. PTTimerInitPool(&PoolTimer);//软定时器初始化
  120. Timer2_Init(1000);//设置定时器2 1ms中断1次
  121. Timer2_enable();//启动定时器2
  122. //之后每隔1ms中断1次,在TIM2_IRQHandler中调用PTTimerTick(&PoolTimer);实现软定时器计时。
  123. }
  124. /*
  125. 启动一个软定时器.依赖于定时器池PoolTimer
  126. */
  127. void STimerStart(pt_timer_t *timer, uint16_t ms)
  128. {
  129. PTTimerStart(&PoolTimer, timer, ms);
  130. }
  131. /*
  132. 关闭一个软定时器.从PoolTimer中移除
  133. */
  134. void STimerStop(pt_timer_t *timer)
  135. {
  136. PTTimerStop(&PoolTimer, timer);
  137. }
  138. /*
  139. 重启一个软定时器。
  140. */
  141. void STimerReStart(pt_timer_t *timer, uint16_t ms)
  142. {
  143. //先关再开。
  144. PTTimerStop(&PoolTimer, timer);
  145. PTTimerStart(&PoolTimer, timer, ms);
  146. }
  147. /*
  148. 用软定时器延时ms毫秒
  149. */
  150. void STimerDelayMs(uint16_t ms)
  151. {
  152. static timer_t timer;
  153. PTTimerStart(&PoolTimer,&timer,ms);
  154. while(1){
  155. if(PTTimerIsExpired(&timer))break;
  156. }
  157. }
  158. boolean STimerIsExpired(pt_timer_t *timer)
  159. {
  160. return PTTimerIsExpired(timer);
  161. }