⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ostimer.c

📁 基本STC4052写的一个小的OS, 完成了任务调度功能,及堆栈空间切换, 及中断处理
💻 C
字号:
//////////////////////////////////////////////////////////////////
//                          
//      Copyright (c) 2006-2010 walnutcy 
//      All Right Reserved.       
//                                 
//      $Author    walnutcy           
//      $Email     walnutcy@163.com   
//      $Version   v1.0        
//      $FileName        
//      $Since       
//      $Log     
//                    
//      DESCRIPTION      
//                      
//	    History:      
//	    <author>          <time>     <version >    <desc>     
//      walnutcy       2006-11-22      0.1          建立
//////////////////////////////////////////////////////////////////

#include "OsTimer.h" 

//////////////////////////////////////////////////////////////////

//定义系统定时器组
#ifdef SET_TIMER
TIMER_LIST gSysTmrLst;
#endif
//////////////////////////////////////////////////////////////////

#ifdef SET_TIMER
///////////////////////////////////////////////
//  Name    : TimerInit (void) 
//  Function: 定时器组初始化 
//  Author  : walnutcy   
//  Date    : 2006-11-22 
///////////////////////////////////////////////
void TimerInit (void)
{
    U8 tmp;
    for (tmp=0; tmp<TIMER_MAX; tmp++)
    {
        gSysTmrLst.tmrItem[tmp].validFlg = C_FALSE;
    }
	gSysTmrLst.validCnt=0;	
}
///////////////////////////////////////////////
//  Name    : SetTimer (PFUNPROC fun,U16 delay)            
//  Function: 定时器的申请     
//  Paras   : PFUNPROC fun -----定时函数指针
//		     U16 delay -------定时时间
//  Return  : C_FALSE: 申请失败 C_TRUE:申请成功 
//  Author  : walnutcy   
//  Date    : 2006-11-22 
///////////////////////////////////////////////
CBOOL SetTimer (PFUNPROC fun,U16 delay)
{
	U8 i;

	if (gSysTmrLst.validCnt >= TIMER_MAX)
    {
        return C_FALSE;
	}

	for (i=0; i<TIMER_MAX; i++)
	{
	    if (C_TRUE == gSysTmrLst.tmrItem[i].validFlg)
	    {
		    if (gSysTmrLst.tmrItem[i].OnTimer == fun)
		    {
			    break;
			}
		}
	}
	
	if (TIMER_MAX>i)
	{
        gSysTmrLst.tmrItem[i].taskTime = delay;
	    gSysTmrLst.tmrItem[i].tmrCnt = 0;
	    gSysTmrLst.tmrItem[i].validFlg = C_TRUE;
	}
	else
	{
    	for (i=0; i<TIMER_MAX; i++)
    	{
    		if (C_FALSE == gSysTmrLst.tmrItem[i].validFlg)
    		{
    			break;
    		}
    	}
        gSysTmrLst.tmrItem[i].taskTime = delay;
    	gSysTmrLst.tmrItem[i].tmrCnt = 0;
    	gSysTmrLst.tmrItem[i].validFlg = C_TRUE;
    	gSysTmrLst.tmrItem[i].OnTimer = fun;
    	gSysTmrLst.validCnt++;
	}
	return C_TRUE;
}

///////////////////////////////////////////////
//  Name    : Timer10msProc (void)       
//  Function: 用户定时组中断处理,检查是否需要运行用户定时器程序。
//  Author  : walnutcy   
//  Date    : 2006-11-22 
///////////////////////////////////////////////
void Timer10msProc (void)
{
	U8 i;
	for (i=0; i<TIMER_MAX;i++)
	{
	    if (C_TRUE == gSysTmrLst.tmrItem[i].validFlg)
	    {
    		gSysTmrLst.tmrItem[i].tmrCnt++;
    		if (gSysTmrLst.tmrItem[i].tmrCnt >= gSysTmrLst.tmrItem[i].taskTime)
    		{
    			gSysTmrLst.tmrItem[i].tmrCnt = 0;
    			gSysTmrLst.tmrItem[i].OnTimer();
    		}
		}
	}

}
#endif

#ifdef TIMER_IS_EXIST
///////////////////////////////////////////////
//  Name    : Timer_Is_Exist (PFUNPROC fun)                  
//  Function: 查询当前定时器是否在队列中          
//  Return  : C_FALSE: 该定时器不在队列中;C_TRUE: 该定时器在队列中
//  Author  : walnutcy   
//  Date    : 2006-11-22 
///////////////////////////////////////////////
CBOOL Timer_Is_Exist (PFUNPROC fun)
{
	U8 i;
	CBOOL tResult = C_FALSE;

	for (i=0; i<TIMER_MAX; i++)
	{
		if(gSysTmrLst.tmrItem[i].OnTimer == fun)
		{
			tResult = C_TRUE;
		}
	}
	return tResult;
}
#endif

#ifdef KILL_TIMER
///////////////////////////////////////////////
//  Name    : KillTimer (PFUNPROC fun)                 
//  Function: 杀死已申请的定时器         
//  Author  : walnutcy   
//  Date    : 2006-11-22 
///////////////////////////////////////////////
CBOOL KillTimer (PFUNPROC fun)
{
	U8 i;

    for (i=0; i<TIMER_MAX; i++)
	{
		if(gSysTmrLst.tmrItem[i].OnTimer == fun)
		{
		    gSysTmrLst.tmrItem[i].validFlg = C_FALSE;
            gSysTmrLst.validCnt--;
		    return C_TRUE;
	    }
	}
    return C_FALSE;
}
#endif

#ifdef CLEAR_TIMER
///////////////////////////////////////////////
//  Name    : ClearTimer (void)               
//  Function: 清除全部的定时器      
//  Author  : walnutcy   
//  Date    : 2006-11-22 
///////////////////////////////////////////////
void ClearTimer (void)
{
    U8 i;
    gSysTmrLst.validCnt = 0;
    for (i=0; i<TIMER_MAX; i++)
    {
        gSysTmrLst.tmrItem[i].validFlg = C_FALSE;
    }
}
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -