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

学校的一个课设

可以去“我的下载”直接打包下载

2 LCD定时闹钟设计
1、 分别显示时钟/定时时间,格式“时时:分分:秒秒”;
2、 两屏显示区分:均分两行显示,第一行分别为“时钟”,“计时”;
3、 计时结束报警,由蜂鸣器实现;
4、 时钟/闹钟时间设置功能键如下:
(1) 模式键K1,实现时钟/计时显示切换;
(2) 时分秒时间设置键:K2,K3,K4,按相应键实现相应时间加一;
(3) 计时开始/停止键K5,实现计时开始/停止切换

【C51】基于C51单片机的LCD定时闹钟设计(含代码,电路,直接用)_第1张图片
【C51】基于C51单片机的LCD定时闹钟设计(含代码,电路,直接用)_第2张图片

#include "reg52.h"
#include "intrins.h"
#include "string.h"

sbit RS = P1^0;
sbit RW = P1^1;
sbit E = P1^2;
sbit BEEP = P1^3;
char count;
long second = 0;

void T0_init(void)	 
{
    TMOD = 0x01;
    TH0 = 0x3C;
    TL0 = 0x0B0;
    EA = 1;
    ET0 = 1;
    TR0 = 0;

	TR1 = 0;
}

void T1_init(void) 
{
    TMOD = 0x10;
    TH1 = 0x3C;
    TL1 = 0x0B0;
    EA = 1;
    ET1 = 1;
    TR1 = 0;

	TR0 = 0;
}

void delay(int x)	
{
	unsigned int i,j;
	for(i=x;i>0;i--)
		for(j=120;j>0;j--);
}

void write_command(char dat)   
{
	E = 0;
	RS = 0;
	_nop_();
	RW = 0;
	P2 = dat;
	delay(5);
	E = 1;
}

void write_char(char dat)
{
	E = 0;
	RS = 1;
	_nop_();
	RW = 0;
	P2 = dat;
	delay(5);
	E = 1;
}

void init(void)	   
{
	char i = 0;
	write_command(0x38);
	i = 0;
	while(P2^7 == 0&&i>5)
	{
		delay(1);
		i++;
	}

	write_command(0x08);
	i = 0;
	while(P2^7 == 0&&i>5)
	{
		delay(1);
		i++;
	}

	write_command(0x01);
	i = 0;
	while(P2^7 == 0&&i>5)
	{
		delay(1);
		i++;
	}

	write_command(0x06);
	i = 0;
	while(P2^7 == 0&&i>5)
	{
		delay(1);
		i++;
	}

	write_command(0x0c);
	i = 0;
	while(P2^7 == 0&&i>5)
	{
		delay(1);
		i++;
	}	
}

void display(char add,char c)	 
{
	write_command(0x80+add);
	switch(c)
	{
		case(0):write_char('0');break;
		case(1):write_char('1');break;
		case(2):write_char('2');break;
		case(3):write_char('3');break;
		case(4):write_char('4');break;
		case(5):write_char('5');break;
		case(6):write_char('6');break;
		case(7):write_char('7');break;
		case(8):write_char('8');break;
		case(9):write_char('9');break;
	}
}

void time_display(void)	  
{	
	char temp;

	temp = second/36000; 
	display(0x04,temp);

	temp = (second/3600)%10; 
	display(0x05,temp);

	write_command(0x80+0x06);write_char(':');

	temp = (second%3600)/600;
	display(0x07,temp);

	temp = ((second%3600)/60)%10;  
	display(0x08,temp);
	write_command(0x80+0x09);write_char(':');

	temp = (second%60)/10;  
	display(0x0a,temp);

	temp = (second%60)%10; 
	display(0x0b,temp);
}

void count_down_display(void)	
{
	char temp;

	temp = second/36000; 
	display(0x44,temp);

	temp = (second/3600)%10; 
	display(0x45,temp);

	write_command(0x80+0x46);write_char(':');

	temp = (second%3600)/600;
	display(0x47,temp);

	temp = ((second%3600)/60)%10;  
	display(0x48,temp);
	write_command(0x80+0x49);write_char(':');

	temp = (second%60)/10; 
	display(0x4a,temp);

	temp = (second%60)%10; 
	display(0x4b,temp);	
}

char scan_key(void)	
{
	char key = 6,a = 0;
	P3 = 0x07;
	if(P3 != 0x07)
	{
		delay(5);
		if(P3 != 0x07)
		{
			switch(P3)
			{
				case(0x06):key = 1;break;
				case(0x05):key = 2;break;
				case(0x03):key = 3;break;
			}
			P3 = 0x18;
			switch(P3)
			{
				case(0x10):key += 0;break;
				case(0x08):key += 3;break;
			}
		}
		while((a<130)&&(P3 != 0x18))
		{
			delay(1);
			a++;
		}
	}
	return key;
}

void alarm(void)   
{
	char i;
	for(i=0;i<8;i++)
	{
		BEEP = ~BEEP;
		delay(400);
	}
}

void main(void)
{
	char k;
	BEEP = 0;
	init();
	flag1:
	T0_init();
	while(1)	
	{
		k = scan_key();
		if(k != 6)
		{
			switch(k)
			{
				case(1):second += 1;break;
				case(2):second += 60;break;
				case(3):second += 3600;break;
				case(4):TR0 = ~TR0;goto flag3;break;
				case(5):write_command(0x01);second = 0;goto flag2;break;
			}	
		}
		if(second >= 86400)
		{
			second = 0;
		}
		time_display();
	}
	flag2:	
	T1_init();
	while(1)
	{
		k = scan_key();
		if(k != 6)
		{
			switch(k)
			{
				case(1):second += 1;break;
				case(2):second += 60;break;
				case(3):second += 3600;break;
				case(4):TR1 = ~TR1;goto flag4;break;
				case(5):write_command(0x01);second = 0;count = 0;goto flag1;break;
			}	
		}
		if(second >= 86400)
		{
			second = 0;
		}
		count_down_display();
	}
	flag3:	
	while(1)
	{
		k = scan_key();
		switch(k)
		{
			case(4):TR0 = ~TR0;break;
			case(5):write_command(0x01);second = 0;count = 0;goto flag2;break;
		}
		if(second >= 86400)
		{
			second = 0;
		}
		time_display();	
	}
	flag4: 
	while(1)
	{
		k = scan_key();
		switch(k)
		{
			case(4):TR0 = ~TR0;break;
			case(5):write_command(0x01);second = 0;count = 0;goto flag1;break;	
		}
		count_down_display();
		if(second < 0)
		{
			second = 0;
			count = 0;
			TR1 = 0;
			alarm();
			goto flag2;
		}	
	}

}

void Timer0Interrupt(void) interrupt 1	
{
    TH0 = 0x3C;
    TL0 = 0x0B0;
    count++;
	if(count >= 20)
	{
		second++;
		count = 0;
		if(second>=86400)
		{
			second = 0;
			count = 0;
		}
	}
#include 
}

void Timer1Interrupt(void) interrupt 3	 
{
    TH1 = 0x3C;
    TL1 = 0x0B0;
    count++;
	if(count >= 20)
	{
		second--;
		count = 0;
	}
}


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