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

📄 shuzizhong.c

📁 C51 程序练习
💻 C
字号:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar count,s1num;
char miao,shi,fen;
sbit s1=P3^1;
sbit s2=P3^2;
sbit s3=P3^3;
sbit rd=P3^7;
sbit lcden=P2^7;
sbit lcdrs=P2^6;
sbit lcdrw=P2^5;//液晶

uchar code table[]="  2007-7-30 MON";
uchar code table1[]="    00:00:00";
void delay(uint z)
{
	uint x,y;
	for(x=z;x>0;x--)
		for(y=110;y>0;y--);
}

void write_com(uchar com)
{
	lcdrs=0;//写指令
	P0=com;
	delay(5);
	lcden=1;
	delay(5);
	lcden=0;//以上都是根据指令表写出
}
void write_data(uchar date)//写数据
{
	lcdrs=1;//写数据
	P0=date;
	delay(5);
	lcden=1;
	delay(5);
	lcden=0;//以上都是根据指令表写出
}

void init()
{
	uchar num;
	lcdrw=0;//写入为0
	lcden=0;
	write_com(0x38);//开显示
	write_com(0x0c);//不显示光标
	write_com(0x06);
	write_com(0x01);//清屏


	write_com(0x80);
	for(num=0;num<15;num++)
		{
			write_data(table[num]);
			delay(5);
		}

	write_com(0x80+0x40);
	for(num=0;num<12;num++)
		{
			write_data(table1[num]);
			delay(5);
		}
	
	TMOD=0x01;//定时器0的方式1
	TH0=(65536-50000)/256;
	TL0=(65536-50000)%256;//初值
	EA=1;
	ET0=1;
	TR0=1;//启动定时器

}

void write_sfm(uchar add,uchar date)
{
	uchar shi,ge;
	shi=date/10;
	ge=date%10;//秒的
	write_com(0x80+0x40+add);
	write_data(0x30+shi);
	write_data(0x30+ge);
}
void keyscan()//判定功能键是否按下

{
	rd=0;	//行列键盘当独立键盘使用
	if(s1==0)//功能键按下
	{
		delay(5);
		if(s1==0)//确认按下
		{
			s1num++;
			while(!s1);
			if(s1num==1)
			{
				TR0=0;//时间停止
				write_com(0x80+0x40+10);
				write_com(0x0f);//光标闪烁秒的位置
			}
			if(s1num==2)
			{
				write_com(0x80+0x40+7);//分钟的位置
			}
			if(s1num==3)
			{
				write_com(0x80+0x40+4);
			}
			if(s1num==4)
			{
				s1num=0;
				write_com(0x0c);//光标不再闪烁
				TR0=1;//开定时器开始走
			}
		}

	}
	if(s1num!=0)//在s1按下后判断s2调节加时间
		{
			if(s2==0)
			{
				delay(5);//延时去抖动
				if(s2==0)
				{
					while(!s2);//等待松手
					if(s1num==1)//光标在秒处
					{
						miao++;
						if(miao==60)
							miao=0;
						write_sfm(10,miao);
						write_com(0x80+0x40+10);	//数据指针到秒处
					}
					if(s1num==2)//光标在分处
					{
						fen++;
						if(fen==60)
							fen=0;
						write_sfm(7,fen);
						write_com(0x80+0x40+7);	//数据指针到分处					
					}
					if(s1num==3)//光标在时处
					{
						shi++;
						if(shi==24)
							shi=0;
						write_sfm(4,shi);
						write_com(0x80+0x40+4);	//数据指针到shichu	
					}
				}
			}
			if(s3==0)//按s3减
			{
				while(!s3);
				if(s1num==1)
				{
					miao--;
					if(miao==-1)
						miao=59;
					write_sfm(10,miao);
					write_com(0x80+0x40+10);
								
				}
				if(s1num==2)
				{
					fen--;
					if(fen==-1)
						fen=59;
					write_sfm(7,fen);
					write_com(0x80+0x40+7);
								
				}
				if(s1num==3)
				{
					shi--;
					if(shi==-1)
						shi=23;
					write_sfm(4,shi);
					write_com(0x80+0x40+4);
								
				}
			}
		}	
}
void main()
{
	init();
	while(1)
	{
		keyscan();
		if(count==20)//20ms
		{
			count=0;//清零
			miao++;
			if(miao==60)
				{
					miao=0;
					fen++;
					if(fen==60)
						{
							fen=0;
							shi++;
							if(shi==24)
							{
								shi=0;
								
							}
							write_sfm(4,shi);
						}
						write_sfm(7,fen);						
				}
				write_sfm(10,miao);
		}
	}

}

void tiner0() interrupt 1
{
	TH0=(65536-50000)/256;
	TL0=(65536-50000)%256;//初值
	count++;	
}

⌨️ 快捷键说明

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