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

📄 timer.c

📁 GPRS模块通过PPP方式实现TCPIP全过程
💻 C
字号:
/****************************************************************************
uart.c
****************************************************************************/
#ifndef TIMER_C
#define TIMER_C

#include  "config.h"
//#include "IAP.h"
////#include "ffs.h"
#include "uart.h"
#include "timer.h"

/*
   create by skier 2005.01.17
   */
   sys_timeout  g_sys_timer[MAX_SYS_TIMER];
   sys_time system_time;
   
//delete a timer from system
void sys_timer_stop(sys_timeout_handler h, void *arg)
{
      int i;

	//DEBUG_FUNCTION("sys_timer_stop()")

      for(i = 0; i < MAX_SYS_TIMER; i++)
      	{
      	     if(g_sys_timer[i].h  EQ h)
      	     {
      	           g_sys_timer[i].ms_time = 0;
		    g_sys_timer[i].h  = NULL;
		    g_sys_timer[i].arg = NULL;

		   break;
      	     }
      	}

      if(i EQ MAX_SYS_TIMER)
      {
      	    DEBUG_EVENT("Do not find timer, unexpect error!");
      }
}

//add a timer to system
void  sys_timer_start(u32_t msecs, sys_timeout_handler h, void *arg)
{
        int i;

	//DEBUG_FUNCTION("sys_timer_start()")

	sys_timer_stop(h, arg);
		
	 for(i = 0; i < MAX_SYS_TIMER; i++)
      	 {
      	     if(g_sys_timer[i].h   EQ NULL)
      	     {
      	           g_sys_timer[i].ms_time = msecs;
		    g_sys_timer[i].h  = h;
		    g_sys_timer[i].arg = arg;

		   break;
      	     }
      	}

	if(i EQ MAX_SYS_TIMER)
      {
      	    DEBUG_ERR("Old timer do not delete from list, unexpect error!");
      }
}

u32_t  sys_jiffies(void)
{
    return system_time.ms_tick ;
}

/*
*Interrupted Every SYS_TICK ms
*support basic time
*check user timer,eg:PPP/TCP
*/
void sys_time_tick(void)
{
       int i;

	   //updata system time
	   system_time.ms_tick+= SYS_TICK;
	   
	  if((system_time.ms += SYS_TICK) >= 1000)
	  {
	       system_time.ms = 0;
		system_time.sec_tick++;
		 
		if(++system_time.sec EQ 60)
		{
		       system_time.sec = 0;			
			   
			if(++system_time.min EQ 60)
			{
			     system_time.min = 0;
			     if(++system_time.hour  EQ 24)
			     {
			          system_time.hour = 0;
				   system_time.day++;
			     }
			}
		}
	  }

	//check timer
	 for(i = 0; i < MAX_SYS_TIMER; i++)
      	 {           
	     if(g_sys_timer[i].h   NEQ NULL)
      	     {
                  if(g_sys_timer[i].ms_time  > SYS_TICK)/*this timer is untimeout*/
		              g_sys_timer[i].ms_time -= SYS_TICK;
		    else                                                            /* this timer is timeout*/
				g_sys_timer[i].ms_time = 0;
      	     }	   		
      	}
}

//check whether a timer is timeout, if true, run it
void check_sys_timer(void)
{
        int i;
		
	 for(i = 0; i < MAX_SYS_TIMER; i++)
	 {
	      if((g_sys_timer[i].h   NEQ NULL)
			                AND (g_sys_timer[i].ms_time  EQ 0) )
            {
            	       g_sys_timer[i].h (g_sys_timer[i].arg);
            }				
	 }
}
/************************************
 initialize the sytem time
 ***********************************/
 void sys_timer_init(void)
 {
       int i;

      for(i = 0; i < MAX_SYS_TIMER; i++)
      	{
      	      g_sys_timer[i].ms_time = 0;
	      g_sys_timer[i].h  = NULL;
	      g_sys_timer[i].arg = NULL;			
      	}

	//initialize system time
	system_time.sec_tick = 0;
	system_time.ms_tick = 0;
	system_time.day = 0;
	system_time.hour = 0;
	system_time.min = 0;
	system_time.sec = 0;
	system_time.ms = 0;

	lpc2104_timer1_start(SYS_TICK);
}

/*
 * return the time of seconds ,
 *which is ticked every second from system reset
*/
u32_t get_sys_tick(void)
{
   return system_time.sec_tick;
}

/*
* return the time of system
*/
sys_time  get_sys_time(void)
{
    return system_time;
}


/*
*add system run time to the head of DEBUG information
*/
void debug_sys_time(void)
{
       const  CHAR ascTable[11] = {"0123456789"};
	sys_time time = get_sys_time();

       sendString_2_user("\r\n\r\n<");

	//day
	sendByte_2_user(ascTable[time.day/10000]);  
	sendByte_2_user(ascTable[(time.day%10000)/1000]);  
	sendByte_2_user(ascTable[(time.day%1000)/100]);   
	sendByte_2_user(ascTable[(time.day%100)/10]);  
	sendByte_2_user(ascTable[time.day%10]); 
       sendString_2_user("-");

       //hour
	sendByte_2_user(ascTable[time.hour/10]);  
	sendByte_2_user(ascTable[time.hour%10]);
       sendString_2_user(":");
	   
	//minute
	sendByte_2_user(ascTable[time.min/10]);
	sendByte_2_user(ascTable[time.min%10]);
	sendString_2_user(":");
	
	//second
	sendByte_2_user(ascTable[time.sec/10]);
	sendByte_2_user(ascTable[time.sec%10]);
       sendString_2_user(":");
	   
	//ms
	sendByte_2_user(ascTable[time.ms/100]);
	sendByte_2_user(ascTable[(time.ms%100)/10]);
	sendByte_2_user(ascTable[time.ms%10]);
       sendString_2_user(">");
    
}


//int flag = 0;
//#define	  LEDCON	0x00003c00

void __irq IRQ_timer1(void)
{
    sys_time_tick();

   /*if(flag%2 == 0)
		IOCLR = LEDCON;
   else
   	        IOSET = LEDCON;

   flag++;*/

   T1IR = 0x01;
   VICVectAddr = 0x00;
}

void lpc2104_timer1_start(u16_t ms)
{
	   T1PR = 0;
	   T1MCR = 0x03;
	   T1MR0 = 11059*ms;
	   T1TCR = 0x03;
	   T1TCR = 0x01;  

	 VICIntSelect = 0x00;/*IRQ interrupt*/
	 VICVectCntl2 = 0x25;/*Enable interrupt, the source of interrupt is TIMER1*/
	 VICVectAddr2 = (int)IRQ_timer1;
	 VICIntEnable |= 0x00000020;/*Enable timer1 interrupt*/
}


/*****************************
  STOP_TIMER1
 ****************************/
 void lpc2104_timer1_stop(void)
{
      DEBUG_FUNCTION("\r\n-----stop timer1");
	  
      T1TCR = 0x00;
      T1IR = 0x01;					  
}

/*****************************
  STOP_TIMER0
 ****************************/
 void lpc2104_timer0_stop(void)
{
      DEBUG_FUNCTION("\r\n-----stop timer0");
	  
      T0TCR = 0x00;
      T0IR = 0x01;					  
}

#endif

⌨️ 快捷键说明

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