【C51】基于C51单片机的LCD电子时钟设计(含代码,电路图,拿去直接用)

学校的一个课设

可以去"链接"直接打包下载

1 LCD电子时钟设计
1、 制作一个基于LCD显示的电子钟,在LCD显示器上显示当前时间。
2、 使用字符型LCD显示器显示时间。
3、 显示格式:“时时:分分:秒秒”。
4、 用4个功能键操作来设置当前时间。功能键K1~K4功能如下:
(1) K1:进入设置现在时间;
(2) K2:修改小时,并显示修改结果;
(3) K3:修改分钟,并显示修改结果;
(4) K4:确认完成设置。
【C51】基于C51单片机的LCD电子时钟设计(含代码,电路图,拿去直接用)_第1张图片

代码

#include
#include
#include
#include
#define uint unsigned int
#define uchar unsigned char
 
uint num,A_num,x,y=0;
uint h,m,s,year=2020,month=7,day=4,lcd_x=0,lcd_y=0;
 
uchar code Zifu[]="0123456789";
 
 
sbit LCD_EN = P3^4;
sbit LCD_RS = P3^5;
 
 
sbit key_A = P1^0;
sbit key_B = P1^1;
sbit key_C = P1^2;
sbit key_D = P1^3;

void delay_1ms(uchar x){
	uchar i,j;
	for(j=0;j<x;j++)
		for(i=0;i<110;i++);
}
 
void write_command(uchar command){
	LCD_RS = 0;
	LCD_EN = 0;
	P0 = command;
	delay_1ms(2);
	LCD_EN = 1;	 		//EN ?1 -- 0 ?? ???? ? ??
	delay_1ms(2);
	LCD_EN = 0;
}
 
void write_data(uchar fuck){
	LCD_RS = 1;
	LCD_EN = 0;
	P0 = fuck;
	delay_1ms(2);
	LCD_EN = 1;
	delay_1ms(2);
	LCD_EN = 0;
}
 
void lcd_post(int X,int Y){			//0,1?? ??? ? ???
	write_command(0x80+X*(0x40)+Y);
}
 
void init(){
	h=m=s=0;
	num=A_num=0;
 
	LCD_EN=0;
	write_command(0x38);
	write_command(0x0c);
	//write_command(0x06); //??????????
	write_command(0x01);
 
	TMOD = 0x02;
	TH0 = 6;
	TL0 = 6;
	EA = 1;
	ET0 = 1;
	TR0 = 1;
 
/*???*/	
	lcd_post(0,0); write_data(Zifu[h/10]);
	lcd_post(0,1); write_data(Zifu[h%10]);
 
	lcd_post(0,2); write_data(':');
 
	lcd_post(0,3); write_data(Zifu[m/10]);
	lcd_post(0,4); write_data(Zifu[m%10]);
 
	lcd_post(0,5); write_data(':');
 
	lcd_post(0,6); write_data(Zifu[s/10]);
	lcd_post(0,7); write_data(Zifu[s%10]);
 
/*???*/
	lcd_post(1,6); write_data(Zifu[year/1000]);
	lcd_post(1,7); write_data(Zifu[(year%1000)/100]);
	lcd_post(1,8); write_data(Zifu[(year%100)/10]);
	lcd_post(1,9); write_data(Zifu[year%10]);
 
	lcd_post(1,10); write_data('-');
 
	lcd_post(1,11); write_data(Zifu[month/10]);
	lcd_post(1,12); write_data(Zifu[month%10]);
 
	lcd_post(1,13); write_data('-');
 
	lcd_post(1,14); write_data(Zifu[day/10]);
	lcd_post(1,15); write_data(Zifu[day%10]);
}
 
 
void keyscan(){

			if(key_A==0){
			delay_1ms(3);
			if(key_A==0){	

					m=(++m)%60;
					lcd_post(0,3); write_data(Zifu[m/10]);
					lcd_post(0,4); write_data(Zifu[m%10]);

			while(!key_A);
		}
	}
	

	

		if(key_B==0){
			delay_1ms(3);
			if(key_B==0){	


					h=(++h)%24;
					lcd_post(0,0); write_data(Zifu[h/10]);
					lcd_post(0,1); write_data(Zifu[h%10]);

			while(!key_B);
		}
	}		
}
 
void main()
{
	init();
	y = 0;
	while(1){
		
		
if(key_D==0){  
		delay_1ms(3);
		if(key_D==0){
			y = 1;
		}
		while(!key_D);
	}
if(key_C==0){  
		delay_1ms(3);
		if(key_C==0){
			y = 0;
		}
		while(!key_C);
}
		


		
		if(num==3686){
			num=0;
			s++;
			if(s==60){
				s=0;
				m++;
				if(m==60){
					m=0;
					h++;
					if(h==24)h=0;
					lcd_post(0,0); write_data(Zifu[h/10]);
					lcd_post(0,1); write_data(Zifu[h%10]);
				}
				lcd_post(0,3); write_data(Zifu[m/10]);
				lcd_post(0,4); write_data(Zifu[m%10]);
			}
			lcd_post(0,6); write_data(Zifu[s/10]);
			lcd_post(0,7); write_data(Zifu[s%10]);
		}
		
		if(y == 1)
		{
		keyscan();
		}
		
		
		
	}
}
 
void T0_time() interrupt 1
{
	num++;
}

//	if(key_A==0){  //??????
//		delay_1ms(3);
//		if(key_A==0){

//		}
//		while(!key_A);
//	}

你可能感兴趣的:(大学时代的C51,单片机)