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

📄 time.c

📁 好东西与大家分享……关于单片机驱动微型打印机和液晶屏的程序!!已经测试过!绝对没有问题
💻 C
字号:
#include <head.h> // SFR declarations
void AssignPcf8563Time(void)
//	屏蔽无效位
{
	Pcf8563.PCFTime.Status1.Byte 		&= 0x00;
	Pcf8563.PCFTime.Status2.Byte 		&= 0x00;
	if(Pcf8563.PCFTime.Seconds.Byte 	& 0x80)
	// VF = Seconds & 0x80
	// VF = 1,indicate the Vdd drops blow Vlow ,& the time may be corrupted
	{
		RWflag = I2CWRITE;
		Pcf8563.PCFTime.Seconds.Byte 	&= 0x7f;
	}//write
	Pcf8563.PCFTime.Minutes.Byte 		&= 0x7f;
	Pcf8563.PCFTime.Hours.Byte			&= 0x3f;
	Pcf8563.PCFTime.Days.Byte			&= 0x3f;
	Pcf8563.PCFTime.Weeks.Byte			&= 0x07;
	Pcf8563.PCFTime.Months.Byte			&= 0x1f;
	Pcf8563.PCFTime.Years.Byte			&= 0xff;
//	Pcf8563.PCFTime.MinuteAlarm.Byte	&= 0x80;
//	Pcf8563.PCFTime.HourAlarm.Byte		&= 0x80;
//	Pcf8563.PCFTime.DayAlarm.Byte		&= 0x80;
//	Pcf8563.PCFTime.WeekdayAlarm.Byte	&= 0x80;
	Pcf8563.PCFTime.ClkoutFreq.Byte		&= 0x83;
	Pcf8563.PCFTime.TimerControl.Byte	&= 0x83;
}
// if RWflag = I2CWRITE, so the operation is writing
// if RWflag = I2CREAD, so the operation is Reading
void ReadCurrentTime(void)
{
	STA = 1;
	delay(20000);	// 90ms/10
	if(Readoverflag)	// 时间帧已经读完
	{
		Readoverflag = 0;
	}
	AssignPcf8563Time();
}
void DisplayCurrentTime(void)
{
#define	TimerStart	0x36
#define YLine		3
	unsigned char i=0;
/// 2003-11-23 //
//	write_chinese(0x30+i*4,YLine,0x02,snum2);i++;
//	write_chinese(0x30+i*4,YLine,0x02,snum0);i++;
	write_chinese(TimerStart+i*4,YLine,0x02,
		SNumber(Pcf8563.PCFTime.Years.HByte.Msb));i++;
	write_chinese(TimerStart+i*4,YLine,0x02,
		SNumber(Pcf8563.PCFTime.Years.HByte.Lsb));i++;
	write_chinese(TimerStart+i*4,YLine,0x02,
		Pozhehao);i++;
	write_chinese(TimerStart+i*4,YLine,0x02,
		SNumber(Pcf8563.PCFTime.Months.HByte.Msb));i++;
	write_chinese(TimerStart+i*4,YLine,0x02,
		SNumber(Pcf8563.PCFTime.Months.HByte.Lsb));i++;
	write_chinese(TimerStart+i*4,YLine,0x02,
		Pozhehao);i++;
	write_chinese(TimerStart+i*4,YLine,0x02,
		SNumber(Pcf8563.PCFTime.Days.HByte.Msb));i++;
	write_chinese(TimerStart+i*4,YLine,0x02,
		SNumber(Pcf8563.PCFTime.Days.HByte.Lsb));i++;
// 11:59:36   //
	i++;
	write_chinese(TimerStart+i*4,YLine,0x02,
		SNumber(Pcf8563.PCFTime.Hours.HByte.Msb));i++;
	write_chinese(TimerStart+i*4,YLine,0x02,
		SNumber(Pcf8563.PCFTime.Hours.HByte.Lsb));i++;
	write_chinese(TimerStart+i*4,YLine,0x02,
		Maohao);i++;
	write_chinese(TimerStart+i*4,YLine,0x02,
		SNumber(Pcf8563.PCFTime.Minutes.HByte.Msb));i++;
	write_chinese(TimerStart+i*4,YLine,0x02,
		SNumber(Pcf8563.PCFTime.Minutes.HByte.Lsb));i++;
	write_chinese(TimerStart+i*4,YLine,0x02,
		Maohao);i++;
	write_chinese(TimerStart+i*4,YLine,0x02,
		SNumber(Pcf8563.PCFTime.Seconds.HByte.Msb));i++;
	write_chinese(TimerStart+i*4,YLine,0x02,
		SNumber(Pcf8563.PCFTime.Seconds.HByte.Lsb));i++;
}
void DisplayCurModValue(unsigned char i)
{
#define	TimerStart2	0x3a
	ClearModTime();
	write_chinese(TimerStart2+i*12,2,0x02,sdot);
}
// 
void ModifyTime(void)
{
	ReadCurrentTime();
	DisplayCurrentTime();
	CurSelModValue = 4;		// Minute
	DisplayCurModValue(CurSelModValue);
	while(1)
	{
		READFLAG = ZEROFLAG = 1;
		if((READFLAG==1) && (ZEROFLAG==1))
		{
			CHANGEFLAG = 1;
			break;
		}
	}
	while(1)
	{
		if(CHANGEFLAG == 0) 
		{
			if(CurSelModValue > 3)	CurSelModValue = 0;
			else	CurSelModValue++;
			while(!CHANGEFLAG)
			{
				CHANGEFLAG = 1;
			}
			DisplayCurModValue(CurSelModValue);
		}
		if((ADDFLAG == 0) && (SUBFLAG == 0))
		{
			ClearModTime();
			RWflag = I2CWRITE;
			ADDFLAG = SUBFLAG = CHANGEFLAG = 1;
			break;
		}
		else if(ADDFLAG == 0)
		{
			SetTime(CurSelModValue,1);
			while(!ADDFLAG)
			{
				ADDFLAG = 1;
			}
		}
		else if(SUBFLAG == 0)
		{
			SetTime(CurSelModValue,0);
			while(!SUBFLAG)
			{
				SUBFLAG = 1;
			}
		}
		DisplayCurrentTime();
	}
}
void SetTime(unsigned char i,bit flag)
{
	switch(i)
	{
		case 4:
			SetMinute(flag);
			break;
		case 3:
			SetHour(flag);
			break;
		case 2:
			SetDay(flag);
			break;
		case 1:
			SetMonth(flag);
			break;
		case 0:
			SetYear(flag);
			break;
		default:
			break;
	}
}
// if flag = 1,value++,else value--
void SetMinute(bit flag)
{
	if(flag)
	{
		if((Pcf8563.PCFTime.Minutes.Byte & 0x0f) < 0x09)
		{
			Pcf8563.PCFTime.Minutes.Byte++;
		}
		else if((Pcf8563.PCFTime.Minutes.Byte & 0xf0) < 0x50)
		{
			Pcf8563.PCFTime.Minutes.Byte += 0x07;
		}  
		else	Pcf8563.PCFTime.Minutes.Byte = 0x00;
	}
	else
	{
		if((Pcf8563.PCFTime.Minutes.Byte & 0x0f) > 0)
		{
			Pcf8563.PCFTime.Minutes.Byte--;
		}
		else if((Pcf8563.PCFTime.Minutes.Byte & 0xf0) > 0)
		{
			Pcf8563.PCFTime.Minutes.Byte -= 0x07;
		}  
		else	Pcf8563.PCFTime.Minutes.Byte = 0x59;
	}
}
void SetHour(bit flag)
{
	if(flag)
	{
		if((Pcf8563.PCFTime.Hours.Byte & 0xf0) < 0x20)
		{
			if((Pcf8563.PCFTime.Hours.Byte & 0x0f) < 0x09)	
			{
				Pcf8563.PCFTime.Hours.Byte++;
			}
			else
			{
				Pcf8563.PCFTime.Hours.Byte += 0x07;
			}
		}
		else if((Pcf8563.PCFTime.Hours.Byte & 0x0f) < 0x03)
		{
			Pcf8563.PCFTime.Hours.Byte++;
		}
		else
		{
			Pcf8563.PCFTime.Hours.Byte = 0x00;
		}
	}
	else
	{
		if((Pcf8563.PCFTime.Hours.Byte & 0x0f) > 0x00)
		{
			Pcf8563.PCFTime.Hours.Byte--;
		}
		else if((Pcf8563.PCFTime.Hours.Byte & 0xf0) > 0x00)
		{
			Pcf8563.PCFTime.Hours.Byte -= 0x07;
		}
		else
		{
			Pcf8563.PCFTime.Hours.Byte = 0x23;
		}
	}
}
void SetDay(bit flag)
{
	if(flag)
	{
		if((Pcf8563.PCFTime.Days.Byte & 0xf0) < 0x03)
		{
			if((Pcf8563.PCFTime.Days.Byte & 0x0f) < 0x09)
			{
				Pcf8563.PCFTime.Days.Byte++;
			}
			else
			{
				Pcf8563.PCFTime.Days.Byte += 0x07;
			}
		}
		else
		{
			if(Pcf8563.PCFTime.Days.Byte == 0x30)
			{
				Pcf8563.PCFTime.Days.Byte = 0x31;
			}
			else
			{
				Pcf8563.PCFTime.Days.Byte = 0x00;
			}
		}			
	}
	else
	{
		if((Pcf8563.PCFTime.Days.Byte & 0x0f) > 0x00)
		{
			Pcf8563.PCFTime.Days.Byte--;
		}
		else if((Pcf8563.PCFTime.Days.Byte & 0xf0) > 0x00)
		{
			Pcf8563.PCFTime.Days.Byte -= 0x07;
		}
		else
		{
			Pcf8563.PCFTime.Days.Byte = 0x31;
		}
	}
}
void SetMonth(bit flag)
{
	if(flag)
	{
		if((Pcf8563.PCFTime.Months.Byte & 0xf0) < 0x01)
		{
			if((Pcf8563.PCFTime.Months.Byte & 0x0f) < 0x09)
			{
				Pcf8563.PCFTime.Months.Byte++;
			}
			else
			{
				Pcf8563.PCFTime.Months.Byte += 0x07;
			}
		}
		else
		{
			if((Pcf8563.PCFTime.Months.Byte & 0x0f) < 0x02)
			{
				Pcf8563.PCFTime.Months.Byte++;
			}
			else
			{
				Pcf8563.PCFTime.Months.Byte = 0x00;
			}
		}			
	}
	else
	{
		if((Pcf8563.PCFTime.Months.Byte & 0x0f) > 0x00)
		{
			Pcf8563.PCFTime.Months.Byte--;
		}
		else if(Pcf8563.PCFTime.Months.Byte == 0x10)
		{
			Pcf8563.PCFTime.Months.Byte = 0x09;
		}
		else
		{
			Pcf8563.PCFTime.Months.Byte = 0x12;
		}
	}
}
void SetYear(bit flag)
{
	if(flag)
	{
		if((Pcf8563.PCFTime.Years.Byte & 0x0f) < 0x09)
		{
			Pcf8563.PCFTime.Years.Byte++;
		}
		else if((Pcf8563.PCFTime.Years.Byte & 0xf0) < 0x90)
		{
			Pcf8563.PCFTime.Years.Byte += 0x07;
		}  
		else	Pcf8563.PCFTime.Years.Byte = 0x00;
	}
	else
	{
		if((Pcf8563.PCFTime.Years.Byte & 0x0f) > 0)
		{
			Pcf8563.PCFTime.Years.Byte--;
		}
		else if((Pcf8563.PCFTime.Years.Byte & 0xf0) > 0)
		{
			Pcf8563.PCFTime.Years.Byte -= 0x07;
		}  
		else	Pcf8563.PCFTime.Years.Byte = 0x99;
	}
}

⌨️ 快捷键说明

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