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

📄 time.c

📁 单片机程序 接上一个header.rar文件的
💻 C
字号:
//-------------------------------------------------------------
// ID Code      : 
// Description	:
// Update Note  : 
//
//-------------------------------------------------------------

#define _TIME_C
#include "header\include.h"

//--------------------------------------------------
// Global Variables
//--------------------------------------------------
sbit LED0 = P1^3;
/*
typedef struct{
	UINT8 hour;
	UINT8 minute;
	UINT8 second;
	UINT8 TimeArray[8];
}StTime;
*/
StTime stCurrTime = {
					16,  0,	0, 
					{0,0,10,0,0,10,0,0}};	//initial time :12:57:30
StTime stAlarmTime = {
					16, 1,0,
					{0,0,11,0,0,11,0,0}};  //alarm 1 

StTimerEvent TimerEventTab[MaxTimerEventAmount];
//--------------------------------------------------
// Description  :
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void TimeHandler(void)
{
	UINT8 uceventcnt;


	for(uceventcnt=0; uceventcnt< MaxTimerEventAmount; uceventcnt++)
	{
		if(TimerEventTab[uceventcnt].uctime == 0)
		{
			SET_TIMER0_CRITICAL();
			TimerEventTab[uceventcnt].uctime = InactiveEventTime;
			CLR_TIMER0_CRITICAL();
			
			(*TimerEventTab[uceventcnt].event)();
		}
	}

	if(GET_TIMERUN())
	{
		CLR_TIMERUN();
		TimeRun();
	}
	
	if(GET_ALARM_ONOFF())
	{
		AlarmRun();
	//	LedFlash();
		if(stCurrTime.second >= 30)
			CLR_ALARM_ONOFF();
	}

} 

void AlarmRun(void)
{
	ActiveTimerEvent(20, LedFlash);   //flash per 200 ms
}

//--------------------------------------------------
// Description  :
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void UpdataTime(StTime * ptime)
{
	ptime->TimeArray[0] = ptime->hour/10;
	ptime->TimeArray[1] = ptime->hour%10;

	ptime->TimeArray[3] = ptime->minute/10;
	ptime->TimeArray[4] = ptime->minute%10;

	ptime->TimeArray[6] = ptime->second/10;
	ptime->TimeArray[7] = ptime->second%10;
	
}

//--------------------------------------------------
// Description  :
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void MdfTimeProc(StTime * pTime)
{
	switch (mdfitem)
	{
		case mdfhour:	
			if(pTime->hour == 23)
				pTime->hour= 0;
			else
				pTime->hour++;
			break;
	
		case mdfminute:							 
			if(pTime->minute == 59)
				pTime->minute = 0;
			else
				pTime->minute++;
			break;					
			

		#if (_Use_Second_Modify == _ENABLE)
		case mdfsecond:	
			if(pTime->second == 59)
				pTime->second = 0;
			else
				pTime->second++;
			break;
		#endif
		
		default :
			break;
	}
}


//--------------------------------------------------
// Description  :
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void TimeRun(void)
{
	stCurrTime.second++;
	if(stCurrTime.second == 60)
	{
		stCurrTime.second = 0;
		stCurrTime.minute++;
		if(stCurrTime.minute == 60)
		{
			stCurrTime.minute = 0;
			stCurrTime.hour++;
			if(stCurrTime.hour == 24)	
					stCurrTime.hour = 0;

			stCurrTime.TimeArray[0] = stCurrTime.hour/10;
			stCurrTime.TimeArray[1] = stCurrTime.hour%10;
		}
		stCurrTime.TimeArray[3] = stCurrTime.minute/10;
		stCurrTime.TimeArray[4] = stCurrTime.minute%10;
	}
	stCurrTime.TimeArray[6]=stCurrTime.second/10;
	stCurrTime.TimeArray[7]=stCurrTime.second%10;

	if((!GET_ALARM_ONOFF()) && (stCurrTime.hour == stAlarmTime.hour) && (stCurrTime.minute == stAlarmTime.minute))
		SET_ALARM_ONOFF();
}


//--------------------------------------------------
// Description  :  0xd8ef == 55535    0xb1df == 45535
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void Timer0Init(void)//Timer0Init
{
	//interrupt configuration
	EA=0;
	TR0=0;
	TL0=0xef;
	TH0=0xd8;
	TMOD=0x01;
	ET0=1;
	EA=1;
	TR0=1;
}

//--------------------------------------------------
// Description  : 12MHZ ==10ms per intetrrupt
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void Timer0Isr(void) interrupt 1  //interrupt
{
	static UINT8 cnt4clk ;
	static UINT8 cnt4timerevent;
	TL0=0xef;
	TH0=0xd8;
	cnt4clk++;
	cnt4timerevent++;
	if(cnt4clk == 100)
	{
		cnt4clk = 0;
		SET_TIMERUN();
	}
	if(cnt4timerevent == 50)//decrease timer cnt by 10x10ms = 100ms
	{	
		cnt4timerevent = 0;
		if(!GET_TIMER0_CRITICAL())
			DecreaseTimerCnt();
	}
}

//timer event functions 
//--------------------------------------------------
// Description  :
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void TimerEventInit(void)
{
	UINT8 uceventcnt;
	for(uceventcnt=0; uceventcnt< MaxTimerEventAmount; uceventcnt++)
	{
		TimerEventTab[uceventcnt].uctime = InactiveEventTime;		
		TimerEventTab[uceventcnt].event = NULL;
	}
}

//--------------------------------------------------
// Description  :
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void ActiveTimerEvent(UINT8 usTime, void (*Event)(void))
{
	UINT8 uceventcnt;
	for(uceventcnt=0; uceventcnt< MaxTimerEventAmount; uceventcnt++)
	{
		if((TimerEventTab[uceventcnt].event == Event) && (TimerEventTab[uceventcnt].uctime != InactiveEventTime) )
			return ;
	}
		
	for(uceventcnt=0; uceventcnt< MaxTimerEventAmount; uceventcnt++)
	{
		if(TimerEventTab[uceventcnt].uctime == InactiveEventTime)
		{
			SET_TIMER0_CRITICAL();
			TimerEventTab[uceventcnt].uctime = usTime;
			CLR_TIMER0_CRITICAL();
			
			TimerEventTab[uceventcnt].event = Event;
			break;
		}
	}	
}


//--------------------------------------------------
// Description  : period :100ms 
// Input Value  : None
// Output Value : None
//--------------------------------------------------
void DecreaseTimerCnt(void)  
{
	UINT8 uceventcnt;
	for(uceventcnt=0; uceventcnt< MaxTimerEventAmount; uceventcnt++)
	{
		if( (TimerEventTab[uceventcnt].uctime != InactiveEventTime) && (TimerEventTab[uceventcnt].uctime != 0) )
		{
			TimerEventTab[uceventcnt].uctime--;
		}
	}
}

void LedFlash(void)
{
	LED0 = ~LED0 ;
}

⌨️ 快捷键说明

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