51单片机实例9——简易时钟

该博客介绍了一个简易的时钟设计,通过C语言编程实现。使用了8段LED数码管显示时间,包括小时、分钟和秒,并通过定时器T0进行时间更新。程序中包含了数码管显示、时间拆分、延时等关键功能,以及中断服务子程序来实现1秒定时器。
摘要由CSDN通过智能技术生成

简易时钟

1、设计目的

        简易时钟

2、仿真电路

3、程序设计(C语言)

#include<reg51.h>
#include<math.h>
sbit U_74HC138_A=P3^0;
sbit U_74HC138_B=P3^1;
sbit U_74HC138_C=P3^2;
int t0_num=0;  //定义T0中断响应次数
int number_hour=23;
int number_hour1=0;	//定义时间小时十位
int number_hour2=0;	//定义时间小时个位
int number_minute=59;
int number_minute1=0;	//定义时间分钟十位
int number_minute2=0;	//定义时间分钟个位
int number_second=50;
int number_second1=0;	//定义时间秒十位
int number_second2=0;	//定义时间秒个位
/****************************************
函数功能:两位十进制数拆分为十位(place=0)与个位(place=1)
*****************************************/
int double_split(int number,int place)
{
	int num=0;
	switch(place)
	{
		case 0: num=number/10;
		break;
		case 1: num=number%10;
		break;
	}
	return num;
}
/****************************************
函数功能:数码管初始值设置
*****************************************/
void init_led_8CC()
{
	P2=0x00;
	U_74HC138_A=0;
	U_74HC138_B=0;
	U_74HC138_C=0;
}
/****************************************
函数功能:8段共阴极led显示数字
		a								 —
	f		b							| |
		g								 —
	e		c							| |
		d		 h					 — .
*****************************************/
int led_8CC_number(int number)
{
	int led;
	int num[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};	//8段共阳极led数字显示段码编码
	switch(number)
	{
		case 0:led=num[0];
		break;
		case 1:led=num[1];
		break;
		case 2:led=num[2];
		break;
		case 3:led=num[3];
		break;
		case 4:led=num[4];
		break;
		case 5:led=num[5];
		break;
		case 6:led=num[6];
		break;
		case 7:led=num[7];
		break;
		case 8:led=num[8];
		break;
		case 9:led=num[9];
		break;
		default:led=0x00;
		break;
	}
	return led;
}
/****************************************
函数功能:延时
*****************************************/
void delay(unsigned int num)
{
	int i=0;
	if(num>0)
	{
		for(num;num>0;num--)
			for(i=250;i>0;i--);
	}
}
/****************************************
函数功能:时间显示
*****************************************/
void time_show()
{
	number_hour1=double_split(number_hour,0);
	number_hour2=double_split(number_hour,1);
	number_minute1=double_split(number_minute,0);
	number_minute2=double_split(number_minute,1);
	number_second1=double_split(number_second,0);
	number_second2=double_split(number_second,1);
	switch(0)
	{
		case 0:
		{
			U_74HC138_A=0;
			U_74HC138_B=0;
			U_74HC138_C=0;
			P2=led_8CC_number(number_hour1);
			delay(1);
		}
		case 1:
		{
			U_74HC138_A=1;
			U_74HC138_B=0;
			U_74HC138_C=0;
			P2=led_8CC_number(number_hour2);
			delay(1);
		}
		case 2:
		{
			U_74HC138_A=0;
			U_74HC138_B=1;
			U_74HC138_C=0;
			P2=0x40;
			delay(1);
		}	
		case 3:
		{
			U_74HC138_A=1;
			U_74HC138_B=1;
			U_74HC138_C=0;
			P2=led_8CC_number(number_minute1);
			delay(1);
		}	
		case 4:
		{
			U_74HC138_A=0;
			U_74HC138_B=0;
			U_74HC138_C=1;
			P2=led_8CC_number(number_minute2);
			delay(1);
		}	
		case 5:
		{
			U_74HC138_A=1;
			U_74HC138_B=0;
			U_74HC138_C=1;
			P2=0x40;
			delay(1);
		}	
		case 6:
		{
			U_74HC138_A=0;
			U_74HC138_B=1;
			U_74HC138_C=1;
			P2=led_8CC_number(number_second1);
			delay(1);
		}	
		case 7:
		{
			U_74HC138_A=1;
			U_74HC138_B=1;
			U_74HC138_C=1;
			P2=led_8CC_number(number_second2);
			delay(1);
		}	
	}
}
/****************************************
函数功能:定时器T0的初始值设置
*****************************************/
void t0_init_value(int t)
{
	TH0=((int) (pow(2,16)-t*pow(10,-3)*12*pow(10,6)/12))/256; 
	TL0=((int) (pow(2,16)-t*pow(10,-3)*12*pow(10,6)/12))%256;  //设置定时器初值
}
/****************************************
函数功能:定时器T0的定时初始化
*****************************************/
void t0_init(int t)
{
	TMOD=0x01;  //设置T0工作于定时工作方式1
	t0_init_value(t);
	IE=0x00;	//禁止中断
	ET0=1;	//开启T0溢出中断
	EA=1;	//开启总中断
	TR0=1;	//启动T0
}
/****************************************
函数功能:定时器T0的中断服务子程序,1s定时闪烁
*****************************************/
void second_1s() interrupt 1
{
	TH0=15536/256; 
	TL0=15536%256;  //设置定时器初值
	if(t0_num==20)
	{
		t0_num=0;
		number_second++;
		if(number_second==60)
		{
			number_second=0;
			number_minute++;
			if(number_minute==60)
			{
				number_minute=0;
				number_hour++;
				if(number_hour==24)
					number_hour=0;
			}
		}
	}
	else
		t0_num++;		//中断次数计数
}
/****************************************
函数功能:主函数
*****************************************/
void main()
{
	init_led_8CC();
	t0_init(50);	//调用定时初始化函数
	while(1)	//持续运行
	{
		time_show();
	}
}

  • 4
    点赞
  • 69
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

LAI-BF

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

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

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

打赏作者

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

抵扣说明:

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

余额充值