You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
1.6 KiB
73 lines
1.6 KiB
|
3 weeks ago
|
/*
|
||
|
|
* Timer.c
|
||
|
|
*
|
||
|
|
* Created on: Feb 28, 2025
|
||
|
|
* Author: bihon
|
||
|
|
*/
|
||
|
|
|
||
|
|
#include "Timer.h"
|
||
|
|
|
||
|
|
uint16_t Timer_Value; //用于存储计时时间
|
||
|
|
static uint16_t Time;//静态变量,用于实际计时
|
||
|
|
volatile uint8_t Timer_Stop_Permission;//计时中断允许标志
|
||
|
|
volatile uint8_t Timer_Stop_Mark; //计时器停止标志
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief 设置定时器的计时时间
|
||
|
|
* @param set_time 要设置的计时时间(单位:ms)
|
||
|
|
* @param stop_perm 计时中断允许标志,0 表示不允许中断,1 表示允许中断
|
||
|
|
* @return 设置成功返回 1,参数错误设置失败返回 0
|
||
|
|
* @note 由于定时器周期为 2ms,实际计时时间为 set_time / 2
|
||
|
|
*/
|
||
|
|
uint8_t Timer_Set(uint16_t set_time, uint8_t stop_perm)
|
||
|
|
{
|
||
|
|
if(set_time >= 2)
|
||
|
|
{
|
||
|
|
Timer_Value = set_time / 2;
|
||
|
|
Time = Timer_Value;//为定时器赋计数值
|
||
|
|
Timer_Stop_Permission = stop_perm;//为定时器允许中断标志赋值
|
||
|
|
Timer_Stop_Mark = 0;//定时器停止标志恢复默认值
|
||
|
|
return 1;//设置成功
|
||
|
|
}else
|
||
|
|
{
|
||
|
|
return 0;//参数错误,设置失败
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief 开始倒计时,在定时器中断中调用
|
||
|
|
* @return 计时结束返回 1,计时未结束或被停止返回 0,计时过程被打断返回2
|
||
|
|
*/
|
||
|
|
uint8_t Timer_Start_Count()
|
||
|
|
{
|
||
|
|
//定时器周期为2ms,实际计时数据为设定时间除2
|
||
|
|
|
||
|
|
if(!Timer_Stop_Mark)
|
||
|
|
{
|
||
|
|
if(Time >= 0)
|
||
|
|
{
|
||
|
|
if(Time == 0)
|
||
|
|
{
|
||
|
|
return 1;//计时结束
|
||
|
|
}
|
||
|
|
Time--;
|
||
|
|
return 0;//仍在计时
|
||
|
|
}
|
||
|
|
}else
|
||
|
|
{
|
||
|
|
Time = 0;//计数器清零
|
||
|
|
return 2;//停止计时
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief 停止定时器计时
|
||
|
|
*/
|
||
|
|
void Timer_Stop_Count()
|
||
|
|
{
|
||
|
|
if(Timer_Stop_Permission == 1)
|
||
|
|
{
|
||
|
|
Timer_Stop_Mark = 1;
|
||
|
|
}
|
||
|
|
}
|