1602液晶数字时钟

/***********************************************************************************************
用1602液晶显示万年历即年月日时分秒
时分秒可以搞定,不过由于年月日涉及的进制问题比较复杂比如闰年闰月等所以此程序只写到一半
据说DS1302可以显示万年历,不过哥硬件知识不行,搞不定,芯片手册英文的,时序图看不懂,悲催~~~~
加键盘设置时钟功能
增加整点闹铃功能
************************************************************************************************/

#include<reg52.h>

//端口设置参考郭天祥TX-1C板子电路图

sbit s0 = P3^0 ;	//键盘
sbit s1 = P3^1 ;
sbit s2 = P3^2 ;
sbit s3 = P3^3 ;
sbit rd = P3^7 ;
sbit lcden = P3^4 ;	//1602使能端	
sbit lcdrs = P3^5 ;	//数据、命令端
sbit lcdwr = P3^6 ;	//读写端
sbit beep = P2^3 ;	//蜂鸣器
uchar t = 0 ;	//蜂鸣器次数
bit beep_flag = 0 ;
uchar t0 = 0 , t1 = 0 ;	//定时器变量
uchar s0num = 0 ;
bit flag_s0 = 0 , flag_s1 = 0 , flag_s2 = 0 ;

uchar qian_nian = 1 , bai_nian = 9 , shi_nian = 9 , nian = 1 , \
shi_yue = 0 , yue = 5 , shi_ri = 1 , ri = 1 , shi_shi = 0 , shi = 0 , shi_fen = 0 , fen = 0 , shi_miao = 0 , miao = 0 ;
bit qian_nian_flag = 0 , bai_nian_flag = 0 , shi_nian_flag = 0 , nian_flag = 0 , shi_yue_flag = 0 , yue_flag = 0 , shi_r\
i_flag = 0 , ri_flag = 0 , shi_shi_flag = 0 , shi_flag = 0 , shi_fen_flag = 0 , fen_flag = 0 , shi_miao_flag = 0 , miao_flag = 0 ;

uchar code *table[] = { "1991-05-11" , "00:00:00" } ;
uchar code table_s0num[] = { 0x80 + 0x4b , 0x80 + 0x4a , 0x80 + 0x48 , 0x80 + 0x47 , 0x80 + 0x45 , 0x80 + 0x44  } ;

void delay( uchar n )	//利用定时器0做延时函数 n ms
{
	TR0 = 1 ;
	while( t0 < n ) ;
	t0 = 0 ;
	TR0 = 0 ;
}

void write_cmd( uchar cmd )	//写指令函数
{
	lcdrs = 0 ;
	P0 = cmd ;
	delay( 1 ) ;
	lcden = 1 ;
	delay( 1 ) ;
	lcden = 0 ;
	delay( 1 ) ;
}
		 
void write_data( uchar date )	//写数据函数
{
	lcdrs = 1 ;
	P0 = date ;
	delay( 1 ) ;
	lcden = 1 ;
	delay( 1 ) ;
	lcden = 0 ;
	delay( 1 ) ;
}

void timer_init()	//定时器初始化函数
{
	TMOD = 0x11 ;
	TH0 = ( 65536 - 921 ) / 256 ;
	TL0 = ( 65536 - 921 ) % 256 ;
	TH1 = ( 65536 - 46080 ) / 256 ;
	TL1 = ( 65536 - 46080 ) % 256 ;
	EA = 1 ;
	ET0 = 1 ;
	ET1 = 1 ;
}

void lcd_init()	//1602液晶初始化
{
	lcdwr = 0 ;
	P0 = 0 ;
	dula = 1 ;
	dula = 0 ;
	P0 = 0xff ;
	wela = 1 ;
	wela = 0 ;
	write_cmd( 0x38 ) ;
	write_cmd( 0x0c ) ;
	write_cmd( 0x06 ) ;
	write_cmd( 0x01 ) ;
}

void point()
{
	write_cmd( 0x0f ) ;
	write_cmd( table_s0num[s0num-1] ) ;
}

void display()	//液晶显示函数
{
	if( !flag_s0 || flag_s1 || flag_s2  )
	{
		write_cmd( 0x80 + 0x4b ) ;
		write_data( miao + 0x30 ) ;
	
		write_cmd( 0x80 + 0x4a ) ;
		write_data( shi_miao + 0x30 ) ;
	
		write_cmd( 0x80 + 0x48 ) ;
		write_data( fen + 0x30 ) ;
	
		write_cmd( 0x80 + 0x47 ) ;
		write_data( shi_fen + 0x30 ) ;
	
		write_cmd( 0x80 + 0x45 ) ;
		write_data( shi + 0x30 ) ;
	
		write_cmd( 0x80 + 0x44 ) ;
		write_data( shi_shi + 0x30 ) ;
	
		write_cmd( 0x80 + 0x0b ) ;
		write_data( shi_ri + 0x30 ) ;
	
		write_cmd( 0x80 + 0x0c ) ;
		write_data( ri + 0x30 ) ;
	}
}

void beep_fun()
{
	t++ ;
	if( t <= 12 )
	{
		beep = !beep ;
	}
	if( t == 13 ) 
	{
		t = 0 ;
		beep_flag = 0 ;
	}
}

void time_count()	//时间函数
{			 
	if( t1 == 20 )
	{
		t1 = 0 ;
		if( ++miao == 10 ) 
		{
			miao = 0 ;
			miao_flag = 1 ;
		}
		if( miao_flag )
		{
			miao_flag = 0 ;
			if( ++shi_miao == 6 )
			{
				shi_miao = 0 ;
				shi_miao_flag = 1 ;
			}
		}	  
		if( shi_miao_flag )
		{
			shi_miao_flag = 0 ;
			if( ++fen == 10 )
			{
				fen = 0 ;
				fen_flag = 1 ;
			}
		}	  
		if( fen_flag )
		{
			fen_flag = 0 ;
			if( ++shi_fen == 6 )
			{
				shi_fen = 0 ;
				shi_fen_flag = 1 ;
			}
		}
		if( shi_fen_flag )
		{
			shi_fen_flag = 0 ;
			beep_flag = 1 ;
			if( ++shi == 10 )
			{
				shi = 0 ;
				shi_flag = 1 ;
			}		 
		}
		if( beep_flag )
		{
			beep_fun() ;
		}
		if( shi_flag )
		{
			shi_flag = 0 ;
			if( ++shi_shi == 2 )
			{
				shi_shi = 0 ;
			}
		}
		if( shi_shi == 2 && shi == 4 )
		{
			shi_shi = 0 ;
			shi = 0 ;
			shi_shi_flag = 1 ;
		}
		if( shi_shi_flag )
		{
			shi_shi_flag = 0 ;
			if( ++ri == 10 )
			{
				ri = 0 ;
				ri_flag = 1 ;
			}
		}
		if( ri_flag )
		{
			ri_flag = 0 ;
			if( ++shi_ri == 3 )
			{
				ri = 0 ;
				ri_flag = 1 ;
			}
		}
	}
}


void keyscan()
{	
	if( !s0 )
	{
		delay(5) ;
		if( !s0 )
		{
			TR1 = 0 ;
			flag_s0 = 1 ;
			flag_s1 = 0 ;
			flag_s2 = 0 ;
			if( 6 == s0num++ )
			{
				s0num = 1 ;
			} 
			switch( s0num )
			{
				case 1 : write_cmd( 0x80 + 0x4b ) ; break ;
				case 2 : write_cmd( 0x80 + 0x4a ) ; break ;
				case 3 : write_cmd( 0x80 + 0x48 ) ; break ;
				case 4 : write_cmd( 0x80 + 0x47 ) ; break ;
				case 5 : write_cmd( 0x80 + 0x45 ) ; break ;
				case 6 : write_cmd( 0x80 + 0x44 ) ; break ;
			}
			while( !s0 ) ;
			write_cmd(0x0f) ;
		}
	}
	if( flag_s0 )
	{
		if( !s1 )
		{
			delay(5) ;
			if( !s1 )
			{
				flag_s1 = 1 ;
				switch( s0num )
				{
					case 1 : write_cmd( 0x80 + 0x4b ) ; miao++ ; 		if( miao == 10 ) 	miao = 0 ; 		break ;
					case 2 : write_cmd( 0x80 + 0x4a ) ; shi_miao++ ; 	if( shi_miao == 6 ) shi_miao = 0 ; 	break ;
					case 3 : write_cmd( 0x80 + 0x48 ) ; fen++ ;			if( fen == 10 ) 	fen = 0 ; 		break ;
					case 4 : write_cmd( 0x80 + 0x47 ) ; shi_fen++ ;		if( shi_fen == 6 )  shi_fen = 0 ; 	break ;
					case 5 : write_cmd( 0x80 + 0x45 ) ; shi++ ;			if( shi == 4 ) 	 	shi = 0 ; 		break ;
					case 6 : write_cmd( 0x80 + 0x44 ) ; shi_shi++ ;		if( shi_shi == 3 )  shi_shi = 0 ; 	break ;	
				}
				display() ;
				point() ;
				while( !s1 ) ;
				point() ;
			}				   
		}
	}
	if( flag_s0 )
	{
		if( !s2 )
		{
			delay(5) ;
			if( !s2 )
			{
				flag_s2 = 1 ;
				switch( s0num )
				{
					case 1 : write_cmd( 0x80 + 0x4b ) ; miao-- ; 		if( miao == -1 ) 	 miao = 9 ; 	break ;
					case 2 : write_cmd( 0x80 + 0x4a ) ; shi_miao-- ; 	if( shi_miao == -1 ) shi_miao = 5 ; break ;
					case 3 : write_cmd( 0x80 + 0x48 ) ; fen-- ;			if( fen == -1 ) 	 fen = 9 ; 		break ;
					case 4 : write_cmd( 0x80 + 0x47 ) ; shi_fen-- ;		if( shi_fen == -1 )  shi_fen = 5 ; 	break ;
					case 5 : write_cmd( 0x80 + 0x45 ) ; shi-- ;			if( shi == -1 ) 	 shi = 3 ; 		break ;
					case 6 : write_cmd( 0x80 + 0x44 ) ; shi_shi-- ;		if( shi_shi == -1 )  shi_shi = 2 ; 	break ;	
				}
				display() ;		 
				point() ;
				while( !s2 ) ;
				point() ;
			}				   
		}
	}	  	  
	if( !s3 )
	{
		delay(5) ;
		if( !s3 )
		{
			TR1 = 1 ;
			flag_s0 = 0 ;
			flag_s1 = 0 ;
			flag_s2 = 0 ;
			s0num = 0 ;
		}
	}	
}

void main()
{
	uchar n ;
	rd = 0 ;
	timer_init() ;
	lcd_init() ;
	write_cmd( 0x83 ) ;		//初始化
	for( n = 0 ; n < 10 ; n++ )
	{
		write_data( table[0][n] ) ;
	}			  
	write_cmd( 0x80 + 0x44 ) ;
	for( n = 0 ; n < 8 ; n++ )
	{
		write_data( table[1][n] ) ;
	}
	TR1 = 1 ;
	while( 1 )
	{
		keyscan() ;
		time_count() ;
		display() ;
	}
}

void timer0() interrupt 1	//延时定时器1ms
{
	TH0 = ( 65536 - 921 ) / 256 ;
	TL0 = ( 65536 - 921 ) % 256 ;
	t0++ ;
}

void timer1() interrupt 3	//时钟用定时器50ms
{
	TH1 = ( 65536 - 46080 ) / 256 ;
	TL1 = ( 65536 - 46080 ) % 256 ;
	t1++ ;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值