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

本文介绍了一款基于51单片机的2LCD定时闹钟设计,详细展示了时钟和定时器的功能实现,包括时间显示、闹钟设置、计时结束报警等功能。通过按键进行模式切换和时间设置,利用蜂鸣器实现报警。代码中包含了定时器初始化、LCD初始化、时间显示、按键扫描等关键部分。
摘要由CSDN通过智能技术生成

学校的一个课设

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

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

在这里插入图片描述
在这里插入图片描述

#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 <REGX51.H>
}

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


评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搞IC的那些年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值