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

📄 checktime.c

📁 本人开发的测量水的浊度的仪器
💻 C
字号:
#include "checktime.h"
#include "lcd.h"
#include "common.h"
#include "ds1302.h"
#include "system.h"

#define DISPLAYTIMEX 1
#define DISPLAYTIMEY 12

#define hex2digit(ch) (ch+'0')

idata volatile uchar time[12];
void setcursorpos(uchar pos)
{
	static uchar cursorpos=0;
	ushort addr;
	
	setcursordirection(CURSORRIGHT);
	addr = DISPLAYTIMEY*320+640+cursorpos*2+DISPLAYTIMEX;
	setcursoraddr(addr);
	writecmd(0x42);
	writedat(0x00);
	writedat(0x00);
	
	cursorpos = pos;
	addr = DISPLAYTIMEY*320+640+cursorpos*2+DISPLAYTIMEX;
	setcursoraddr(addr);
	writecmd(0x42);
	writedat(0x3f);
	writedat(0xfc);
}

uchar isbigmonth(uchar month)
{
	if(month < 8)
		return month&0x01;
	else
		return !(month&0x01);
}

#if 0
void increasetime(uchar pos)
{
	uchar tmp,idx,leapyear,month;
	uchar buf[2];

	if(pos == 0 || pos == 1)
		return;

	if((time[0]*10+time[1])/4) 
		leapyear = 0;
	else
		leapyear = 1;
	month = time[2]*10+time[3];

	tmp = (pos-2)/3;
	idx = tmp*2;
	if(!(pos%3)) idx++;

	switch(idx)
	{
	case 0://year10
	case 1://year0
		time[idx]++;
		if(time[idx] > 9)
			time[idx] = 0;
	break;
	case 2://month10
		if(time[2])
		{
			if(time[3])
				time[2] = 0;
		}
		else
		{
			if(time[3] < 3)
				time[2] = 1;
		}
	break;
	case 3://month0
		if(time[2] == 1)
		{
			if(time[3] < 2)
				time[3]++;
			else
				time[3] = 0;
		} 
		else
		{
			time[3]++;
			if(time[3] > 9)
				time[3] = 1;
		}
	break;
	case 4://date10
		if(month == 2)
		{
			if(time[4] < 1)
				time[4]++;
			else
			{
				if(leapyear)
				{
					time[4]++; 
				}
				else
				{
					if(time[5] < 9)
 				    	time[4]++;
				}
			}
		}
		else
		{
			if(time[4] < 2)
				time[4]++;
			else
			{
				if(isbigmonth(month))
				{
					if(time[5] < 2)
						time[4]++;
				}
				else
				{
					if(time[5] < 1)
						time[4]++;
				}
			}
		}
	break;
	case 5://date0
		if(month == 2)
		{
			if(time[4] < 2)
			{
				if(time[5] < 9)
					time[5]++;
			}
			else
			{
				if(leapyear)
				{
					if(time[5] < 9)
						time[5]++;
				}
				else
				{
					if(time[5] < 8)
						time[5]++;
				}
			}
		}
		else
		{
			if(time[4] < 3)
			{
				if(time[5] < 9)
					time[5]++;
			}
			else
			{
				if(isbigmonth(month))
				{
					if(time[5] < 1)
						time[5]++;
				}
			}
		}
	break;
	case 6://hour10
		if(time[6] < 2) 
			time[6]++;
		else
			time[6] = 0;
	break;
	case 7://hour0
		if(time[6] < 2)
		{
			if(time[7] < 9)
				time[7]++;
		}
		else
		{
			if(time[7] < 3)
				time[7]++;
		}
	break;
	case 8://minute10
	case 10://second10
		if(time[idx] < 5)
			time[idx]++;
		else
			time[idx] = 0;
	break;
	case 9://minute0
	case 11://second0
		if(time[idx] < 9)
			time[idx]++;
		else
			time[idx] = 0;
	break;
	}

	buf[0] = hex2digit(time[idx]);
	buf[1] = 0;
	printstring16(0,DISPLAYTIMEX+pos*2,DISPLAYTIMEY,buf);
	setcursorpos(pos);
}

void decreasetime(uchar pos)
{
	uchar tmp,idx;
	uchar buf[2];

	if(pos == 0 || pos == 1)
		return;


	tmp = (pos-2)/3;
	idx = tmp*2;
	if(!(pos%3)) idx++;

	switch(idx)
	{
	case 0://year10
	case 1://year0
		time[idx]++;
		if(time[idx] > 9)
			time[idx] = 0;
	break;
	case 2://month10
	case 4://date10
		if(time[idx+1])
			time[idx]--;
		else
		{
			if(time[idx] > 1)
				time[idx]--;
		}
	break;
	case 3://month0
	case 5://date0
		if(time[idx-1])
			time[idx]--;
		else
		{
			if(time[idx] > 1)
				time[idx]--;
		}
	break;
	case 6://hour10
	case 7://hour0
	case 8://minute10
	case 9://minute0
	case 10://second10
	case 11://second0
		if(time[idx])
			time[idx]--;
	break;
	}

	buf[0] = hex2digit(time[idx]);
	buf[1] = 0;
	printstring16(0,DISPLAYTIMEX+pos*2,DISPLAYTIMEY,buf);
	setcursorpos(pos);
}
#else
void increasetime(uchar pos)
{
	uchar tmp,idx;
	uchar buf[2];

	if(pos == 0 || pos == 1)
		return;

	tmp = (pos-2)/3;
	idx = tmp*2;
	if(!(pos%3)) idx++;

	if(time[idx] == 9)
		time[idx] = 0;
	else
	time[idx]++;

	buf[0] = hex2digit(time[idx]);
	buf[1] = 0;
	printstring16(0,DISPLAYTIMEX+pos*2,DISPLAYTIMEY,buf);
	setcursorpos(pos);
}

void decreasetime(uchar pos)
{
	uchar tmp,idx;
	uchar buf[2];

	if(pos == 0 || pos == 1)
		return;

	tmp = (pos-2)/3;
	idx = tmp*2;
	if(!(pos%3)) idx++;

	if(time[idx] > 0)
		time[idx]--;
	else
		time[idx] = 9;

	buf[0] = hex2digit(time[idx]);
	buf[1] = 0;
	printstring16(0,DISPLAYTIMEX+pos*2,DISPLAYTIMEY,buf);
	setcursorpos(pos);
}
#endif

void checktime()
{
	uchar i,buf[20],key,finished,csrpos,ok,year,month,date,hour,minute,second;

	finished = 0;
	clear(0x0000);
	clear(0x4000);
	printstring16(0,0,0,"现已进入校时程序");
	printstring16(0,0,24,"按1左移");
	printstring16(0,0,26,"按2右移");
	printstring16(0,31,24,"按3加1");
	printstring16(0,31,26,"按4减1");
	printstring16(0,0,28,"按5确认退出");

	year = readyear();
	time[0] = (year>>4)&0x0f;
	time[1] = year&0x0f;
	month = readmonth();
	time[2] = (month>>4)&0x0f;
	time[3] = month&0x0f;
	date = readdate();
	time[4] = (date>>4)&0x0f;
	time[5] = date&0x0f;
	hour = readhour();
	time[6] = (hour>>4)&0x0f;
	time[7] = hour&0x0f;
	minute = readminute();
	time[8] = (minute>>4)&0x0f;
	time[9] = minute&0x0f;
	second = readsecond();
	time[10] = (second>>4)&0x0f;
	time[11] = second&0x0f;

	for(i=0;i<12;i++)
		if(time[i] > 10) time[i] = 0;
	sprintf(buf,"20%02bx-%02bx-%02bx %02bx:%02bx:%02bx",year,month,date,hour,minute,second);
	printstring16(0,DISPLAYTIMEX,DISPLAYTIMEY,buf);

	csrpos = 0;
	setcursorpos(csrpos);

	while(!finished)
	{
		while((key=readkey()) == KEY_NONE) 
			delay(30);
		switch(key)
		{
		case KEY_LEFT:
			if(csrpos)
			{
				csrpos--;
				if((csrpos > 3) && ((csrpos-1)%3 == 0))
					csrpos--;
			}
			else
				csrpos = 18;
			setcursorpos(csrpos);
		break;
		case KEY_RIGHT:
			if(csrpos == 18)
				csrpos = 0;
			else
			{
				csrpos++;
				if((csrpos != 1) && ((csrpos-1)%3 == 0))
					csrpos++;
			}
			setcursorpos(csrpos);
		break;
		case KEY_UP:
			increasetime(csrpos);
		break;
		case KEY_DOWN:
			decreasetime(csrpos);
		break;
		case KEY_ENTER:
			ok = 0;
			year = time[0]*10+time[1];
			month = time[2]*10+time[3];
			date = time[4]*10+time[5];
			hour = time[6]*10+time[7];
			minute = time[8]*10+time[9];
			second = time[10]*10+time[11];

			if(second < 60 &&
				minute < 60 &&
				hour < 24 && 
				month < 13 && 
				month > 0 &&
				date > 0)
			{
				if(month == 2)
				{
					if(year/4)
					{
						if(date<29) ok = 1;
					}
					else
					{
						if(date<30) ok = 1;
					}
				}
				else
				{
					if(isbigmonth(month))
					{
						if(date<32) 
							ok = 1;
					}
					else
					{
						if(date<31)
							ok = 1;
					}
				}
			}

			if(ok)
			{
				writesecond(0x00);
				writeyear((time[0]<<4)|time[1]);
				writemonth((time[2]<<4)|time[3]);
				writedate((time[4]<<4)|time[5]);
				writehour((time[6]<<4)|time[7]);
				writeminute((time[8]<<4)|time[9]);
				writesecond((time[10]<<4)|time[11]);
				printstring16(0,DISPLAYTIMEX+13,DISPLAYTIMEY+4,"时间设置成功");
				finished = 1;
			}
			else
			{
				printstring16(0,DISPLAYTIMEX+13,DISPLAYTIMEY+4,"时间设置错误");
			}
		break;
		}
	}

	delay(500);
}

⌨️ 快捷键说明

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