1. 程式人生 > >微控制器軟定時器

微控制器軟定時器

一直想在微控制器上實現一個像MFC和WPF一樣的軟定時器,花了點時間終於寫出來了,使用時需把timer_run函式放到定時器中斷服務函式裡面

typedef struct _TIMER
{
	unsigned char isEnable;
	unsigned int  count;
	unsigned int  countNeed;
	void (*func)();	
}TIMER;



void timer_Run(TIMER *timer)
{
	if(timer->isEnable)
	{
		timer->count ++;
		if(timer->count == timer->countNeed)
		{
			timer->count = 0;
			timer->func();
		}
	}	
}

void startTimer(TIMER *timer)
{
	timer->count = 0;
	timer->isEnable = 1;
}

void stopTimer(TIMER *timer)
{
	timer->isEnable = 0;
	timer->count =0;
}