码迷,mamicode.com
首页 > 其他好文 > 详细

单片机第15课:时钟

时间:2014-10-20 15:08:47      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:单片机   时钟   

#include<reg51.h>
/*
¹¦ÄÜ£ºLCDÏÔʾʱÖÓ¡¢ÈÕÆÚ¡¢ºÍÐÇÆÚ
×÷Õߣºluluc
*/
#define uint unsigned int
#define uchar unsigned char

uchar code table_row1[] = " 2014-10-20 MON ";
uchar code table_row2[] = "    00-00-00 ";

sbit lcd_E = P2^7;
sbit lcd_RS = P2^6;
sbit lcd_RW = P2^5;
sbit key1 = P1^0;
sbit key2 = P1^1;
sbit key3 = P1^2;
sbit key4 = P1^3;

char flag_t0,hour,minute,second,count_key1;

void write_lcd_comm(uchar comm);
void write_lcd_data(uchar lcd_data);
void delay(uchar z);
void init_lcd();
void init_t0();
void init_v();
void display_on_lcd(uchar where, uchar what);
void clock_work_modle();
void key_scan();

void main()
{
	init_lcd();
	init_v();
	init_t0();
	while(1)
	{
	 	//ʱÖӵŤ×÷Ô­Àí
		clock_work_modle();
		//ɨÃèÊÇ·ñÓа´¼ü°´ÏÂ
		key_scan();		
	}
}
/*
¹¦  ÄÜ£º°´¼üɨÃè
²Î  Êý£º
·µ»ØÖµ£ºÎÞ
*/
void key_scan()
{
	//key1---Í£Ö¹ÅÜ±í¡¢¶¨Î»
	if(key1 == 0)
	{
		//ÑÓʱһÏÂ
		delay(10);
		//Ôٴμì²â
		if(key1 == 0)
		{
		 	
			//ȷʵÊÇ°´ÏÂÁË
			//µÈ´ýËÉÊÖ£¬ËÉÊÖÖ®ºó¶¨Ê±Æ÷Í£Ö¹¹¤×÷
			while(!key1);
			delay(10);
			while(!key1);
			count_key1++;
			switch(count_key1)
			{
			 	case 1:
					TR0 = 0;
					//ͬʱ¶¨Î»µ½Ãë
					//Êý¾ÝÖ¸ÕëÉèÖÃ
					write_lcd_comm(0x80 + 0x40 + 11);
					break;
				case 2:
					//ͬʱ¶¨Î»µ½·Ö
					//Êý¾ÝÖ¸ÕëÉèÖÃ
					write_lcd_comm(0x80 + 0x40 + 8);
					break;
				case 3:
					//ͬʱ¶¨Î»µ½Ê±
					//Êý¾ÝÖ¸ÕëÉèÖÃ
					write_lcd_comm(0x80 + 0x40 + 5);
					break;
				case 4:
					TR0 = 1;
					count_key1 = 0;
					break;
			}
		}
		
	}
	//°´¼ü2±»°´ÏÂ
	if(count_key1 != 0)
	{
		if(key2 == 0)
		{
		 	//ÑÓʱһÏÂ
			delay(10);
			//Ôٴμì²â
			if(key2 == 0)
			{
			 	
				//ȷʵÊÇ°´ÏÂÁË
				//µÈ´ýËÉÊÖ£¬ËÉÊÖÖ®ºó¶¨Ê±Æ÷Í£Ö¹¹¤×÷
				while(!key2);
				delay(10);
				while(!key2);
				switch(count_key1)
				{
				 	case 1:
						second++;
						if(second == 60)
						{
						  	second = 0;
						}
						display_on_lcd(0x80+0x40+10, second);
						//Êý¾ÝÖ¸ÕëÉèÖÃ
						write_lcd_comm(0x80 + 0x40 + 11);
						break;
					case 2:
						minute++;
						if(minute == 60)
						{
						  	minute = 0;
						}
						display_on_lcd(0x80+0x40+7, minute);
						//Êý¾ÝÖ¸ÕëÉèÖÃ
						write_lcd_comm(0x80 + 0x40 + 8);
						break;
					case 3:
						hour++;
						if(hour == 24)
						{
						  	hour = 0;
						}
						display_on_lcd(0x80+0x40+4, hour);
						//Êý¾ÝÖ¸ÕëÉèÖÃ
						write_lcd_comm(0x80 + 0x40 + 5);
						break;
				}
			}
		}
	}
	
	//°´¼ü3±»°´ÏÂ
	if(count_key1 != 0)
	{
		if(key3 == 0)
		{
		 	//ÑÓʱһÏÂ
			delay(10);
			//Ôٴμì²â
			if(key3 == 0)
			{
			 	
				//ȷʵÊÇ°´ÏÂÁË
				//µÈ´ýËÉÊÖ£¬ËÉÊÖÖ®ºó¶¨Ê±Æ÷Í£Ö¹¹¤×÷
				while(!key3);
				delay(10);
				while(!key3);
				switch(count_key1)
				{
				 	case 1:
						second--;
						if(second == -1)
						{
						  	second = 59;
						}
						display_on_lcd(0x80+0x40+10, second);
						//Êý¾ÝÖ¸ÕëÉèÖÃ
						write_lcd_comm(0x80 + 0x40 + 11);
						break;
					case 2:
						minute--;
						if(minute == -1)
						{
						  	minute = 59;
						}
						display_on_lcd(0x80+0x40+7, minute);
						//Êý¾ÝÖ¸ÕëÉèÖÃ
						write_lcd_comm(0x80 + 0x40 + 8);
						break;
					case 3:
						hour--;
						if(hour == -1)
						{
						  	hour = 23;
						}
						display_on_lcd(0x80+0x40+4, hour);
						//Êý¾ÝÖ¸ÕëÉèÖÃ
						write_lcd_comm(0x80 + 0x40 + 5);
						break;
				}
			}
		}
	}
}
/*
¹¦  ÄÜ£ºÊ±ÖÓ¹¤×÷Ô­Àí
²Î  Êý£º
·µ»ØÖµ£ºÎÞ
*/
void clock_work_modle()
{
//ÈÃÃëÿ¸ôÒ»Ãë¼Ó1
	if(flag_t0 == 20)
	{
		flag_t0 = 0;
		second++;
		if(second == 60)
		{
			second = 0;
			//·ÖÖÓ¼Ó1
			minute++;
			if(minute == 60)
			{
			 	minute = 0;
				//·ÖÖÓ¼Ó1
				hour++;
				if(hour == 24)
				{
				 	hour = 0;
					
				}
				//½«·ÖÔÚLCDÉÏÏÔʾ³öÀ´---LCDÉÏÃæÊÇÁ½Î»Êý
				display_on_lcd(0x80+0x40+4, hour);
			}
		   	//½«·ÖÔÚLCDÉÏÏÔʾ³öÀ´---LCDÉÏÃæÊÇÁ½Î»Êý
			display_on_lcd(0x80+0x40+7, minute);
		}
		//½«ÃëÔÚLCDÉÏÏÔʾ³öÀ´---LCDÉÏÃæÊÇÁ½Î»Êý
		display_on_lcd(0x80+0x40+10, second);
	}
}
/*
¹¦  ÄÜ£º½«Êý×ÖÔÚLCDÉÏÃæÏÔʾ³öÀ´
²Î  Êý£ºuchar where, uchar what
·µ»ØÖµ£ºÎÞ
*/
void display_on_lcd(uchar where, uchar what)
{
	//»ñÈ¡Á½¸öÊý×Ö£¬Ê®Î»ºÍ¸öλ
	uchar left_num,right_num;
	//ÏÔʾµÄλÖÃ
	left_num = what/10;
	right_num = what%10;
	//Êý¾ÝÖ¸ÕëÉèÖÃ
	write_lcd_comm(where);
	write_lcd_data(0x30+left_num);
	write_lcd_data(0x30+right_num);
	
}
/*
¹¦  ÄÜ£º±äÁ¿³õʼ»¯
²Î  Êý£ºÎÞ
·µ»ØÖµ£ºÎÞ
*/
void init_v()
{
	//ʱ·ÖÃë
	second = 0;
	minute = 0;
	hour = 0;
	//KEY1±»°´ÏµĴÎÊý
	count_key1 = 0;
}
/*
¹¦  ÄÜ£º¶¨Ê±Æ÷t0³õʼ»¯
²Î  Êý£ºÎÞ
·µ»ØÖµ£ºÎÞ
*/
void init_t0()
{
	//ÖØÐÂ×°³õÖµ,¶¨Ê±50ms
	TH0 = (65536-50000)/256;
	TH0 = (65536-50000)/256;
	//È·¶¨¶¨Ê±Æ÷µÄ¹¤×÷·½Ê½1
	TMOD = 0x01;
	//¿ªÖжÏ
	ET0 = 1;
	EA = 1;
	//Æô¶¯¶¨Ê±Æ÷
	TR0 = 1;
}
/*
¹¦  ÄÜ£º¶¨Ê±Æ÷ÖжϷþÎñ³ÌÐò
²Î  Êý£ºÎÞ
·µ»ØÖµ£ºÎÞ
*/
void timer0() interrupt 1
{
	//ÖØÐÂ×°³õÖµ,¶¨Ê±50ms
	TH0 = (65536-50000)/256;
	TH0 = (65536-50000)/256;
	flag_t0++;
}
/*
¹¦  ÄÜ£ºLCD³õʼ»¯
²Î  Êý£ºÎÞ
·µ»ØÖµ£ºÎÞ
*/
void init_lcd()	   
{
	uchar flag;
	
	//ÏÔʾģʽÉèÖÃ
	write_lcd_comm(0x38);
	//ÉèÖÿª¹ØºÍ¹â±êÉèÖÃ
	write_lcd_comm(0x0f);
	write_lcd_comm(0x06);
	//ÇåÆÁ
	write_lcd_comm(0x01);
	//Êý¾ÝÖ¸ÕëÉèÖÃ
	write_lcd_comm(0x80);

	//³õʼ»¯Êý¾Ý
	for(flag=0; flag<15; flag++)
	{
	 	write_lcd_data(table_row1[flag]);
	}
	write_lcd_comm(0x80 + 0x40);
	//³õʼ»¯Êý¾Ý
	for(flag=0; flag<12; flag++)
	{
	 	write_lcd_data(table_row2[flag]);
	}

}
/*
¹¦  ÄÜ£ºLCDдÃüÁî
²Î  Êý£ºuchar comm
·µ»ØÖµ£ºÎÞ
*/
void write_lcd_comm(uchar comm)
{
	void delay(uchar z);
	//×¢ÒâʱÐò
	lcd_RS = 0;
	lcd_RW = 0;
	P0 = comm;
	lcd_E = 1;
	delay(5);
	lcd_E = 0;
}

/*
¹¦  ÄÜ£ºLCDдÊý¾Ý
²Î  Êý£ºuchar lcd_data
·µ»ØÖµ£ºÎÞ
*/
void write_lcd_data(uchar lcd_data)
{
	void delay(uchar z);
	//×¢ÒâʱÐò
	lcd_RS = 1;
	lcd_RW = 0;
	P0 = lcd_data;
	lcd_E = 1;
	delay(5);
	lcd_E = 0;
}

/*
¹¦  ÄÜ£ºÑÓʱº¯Êý
²Î  Êý£ºuchar z
·µ»ØÖµ£ºÎÞ
*/
void delay(uchar z)
{
	uint i,j;
	for(i=110; i>0; i--)
		for(j=0;j<z; j++);
}

单片机第15课:时钟

标签:单片机   时钟   

原文地址:http://blog.csdn.net/kotei_88_luluc_66/article/details/40300517

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!