123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- /********************************************************************************
- * File Name: Timer.c
- *******************************************************************************/
- #include "includes.h"
- #include "Timer.h"
- #define TIM2_COUNTER_CLOCK 1000000 //计数时钟(次/秒)
- #define TIM2_PRESCALER_VALUE (SystemCoreClock/TIM2_COUNTER_CLOCK-1)
- #define TIM3_COUNTER_CLOCK 1000 //计数时钟(次/秒)
- #define TIM3_PRESCALER_VALUE (SystemCoreClock/TIM3_COUNTER_CLOCK-1)
- pt_timer_t PoolTimer;
- /*********************************************************************************
- *Timer3_Init
- arr定时时长
- 因为TIM3_COUNTER_CLOCK=1K/次 因此arr以ms为单位 如arr=65535 定时时间位65535ms=65.35s
- **********************************************************************************/
- void Timer3_Init(unsigned short arr)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- TIM_TimeBaseStructure.TIM_Period = arr; //定是周期到,自动重载值
- TIM_TimeBaseStructure.TIM_Prescaler =TIM3_PRESCALER_VALUE; //预分频值
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //时钟分频因子
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
-
- NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- TIM_ClearFlag(TIM3, TIM_FLAG_Update);
- }
- /*********************************************************************************
- *Timer3_enable
- **********************************************************************************/
- void Timer3_enable( void )
- {
- TIM_ClearFlag(TIM3, TIM_FLAG_Update);
- TIM_SetCounter(TIM3,0x00);
- TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
- TIM_Cmd(TIM3,ENABLE);
- }
- /*********************************************************************************
- *Timer3_disable
- **********************************************************************************/
- void Timer3_disable (void)
- {
- TIM_ITConfig(TIM3, TIM_IT_Update | TIM_IT_Trigger,DISABLE );
- TIM_Cmd(TIM3, DISABLE);
- }
- /*********************************************************************************
- *Timer2_Init
- arr定时时长
- 因为TIM2_COUNTER_CLOCK=1M/次 因此arr以us为单位 如arr=65535 则定时时间为65535us=65.535ms
- **********************************************************************************/
- void Timer2_Init(unsigned short arr)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
- TIM_TimeBaseStructure.TIM_Period = arr;
- TIM_TimeBaseStructure.TIM_Prescaler =TIM2_PRESCALER_VALUE;
- TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1; //时钟分频因子
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
-
- NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- TIM_ClearFlag(TIM2,TIM_FLAG_Update);
- }
- /*********************************************************************************
- *Timer2_enable
- **********************************************************************************/
- void Timer2_enable( void )
- {
- TIM_ClearFlag(TIM2, TIM_FLAG_Update);
- TIM_SetCounter(TIM2,0x00);
- TIM_ITConfig(TIM2,TIM_IT_Update|TIM_IT_Trigger,ENABLE);
- TIM_Cmd(TIM2,ENABLE);
- }
- /*********************************************************************************
- *Timer2_disable
- **********************************************************************************/
- void Timer2_disable (void)
- {
- TIM_ITConfig(TIM2, TIM_IT_Update | TIM_IT_Trigger,DISABLE );
- TIM_Cmd(TIM2, DISABLE);
- }
- /*********************************************************************************
- *TIM3_IRQHandler
- **********************************************************************************/
- //void TIM3_IRQHandler(void)
- //{
- // if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
- // {
- // TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
- // //Timer3_disable();
- // //g_ulTimer3Ct++;
- //
- // }
- //}
- /*********************************************************************************
- *TIM2_IRQHandler
- **********************************************************************************/
- //void TIM2_IRQHandler(void)
- //{
- // if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
- // {
- // PTTimerTick(&PoolTimer);
- // TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
- // }
- //}
- /*********************************************************************************
- SoftTimerInit
- 软定时器初始化
- **********************************************************************************/
- void STimerInit(void)
- {
- PTTimerInitPool(&PoolTimer);//软定时器初始化
- Timer2_Init(1000);//设置定时器2 1ms中断1次
- Timer2_enable();//启动定时器2
- //之后每隔1ms中断1次,在TIM2_IRQHandler中调用PTTimerTick(&PoolTimer);实现软定时器计时。
-
- }
- /*
- 启动一个软定时器.依赖于定时器池PoolTimer
- */
- void STimerStart(pt_timer_t *timer, uint16_t ms)
- {
- PTTimerStart(&PoolTimer, timer, ms);
- }
- /*
- 关闭一个软定时器.从PoolTimer中移除
- */
- void STimerStop(pt_timer_t *timer)
- {
- PTTimerStop(&PoolTimer, timer);
- }
- /*
- 重启一个软定时器。
- */
- void STimerReStart(pt_timer_t *timer, uint16_t ms)
- {
- //先关再开。
- PTTimerStop(&PoolTimer, timer);
- PTTimerStart(&PoolTimer, timer, ms);
- }
- /*
- 用软定时器延时ms毫秒
- */
- void STimerDelayMs(uint16_t ms)
- {
- static timer_t timer;
- PTTimerStart(&PoolTimer,&timer,ms);
- while(1){
- if(PTTimerIsExpired(&timer))break;
- }
- }
- boolean STimerIsExpired(pt_timer_t *timer)
- {
- return PTTimerIsExpired(timer);
- }
|