51单片机数字钟

这个星期是实训周,很是无聊,回宿舍内卷,提前把代码写完了,上课打王者

我们实训用的是金沙滩开发板,我之前学的是普中开发板,但是看懂原理图基本是一样的。

我们要实现的现象是显示一个数字钟,可以通过按键停止,启动,调节时间

用到的有数码管,定时器,138译码器,矩阵按键.

这是我写的代码嘻嘻

#include"reg52.h"
typedef unsigned char uchar;
typedef unsigned int uint;
sbit LSA = P1^0;     //38译码器
sbit LSB = P1^1;
sbit LSC = P1^2;
sbit ADDR3 = P1^3;   //使能引脚
sbit ENLED =P1^4;

sbit KeyInt1 = P2^4;  //按键
sbit KeyInt2 = P2^5;
sbit KeyInt3 = P2^6;
sbit KeyInt4 = P2^7;
uchar code smgduan[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};  // 共阴极数码管段选
uint  hour=0,min=0,second=0;  //定义小时分钟秒
void delay(uint i)		  //延时函数i= 1;延时10us
{
while(i--);
}


void Timer0Int()		//定时器1
{
TMOD = 0x01;
TH0 = (65535-50000)/256;
TL0 = (65535-50000)%256;
ET0 = 1;
EA = 1;
TR0 = 1;
}
void display()			//38译码扫描显示
{
uchar i;
	for(i=0;i<6;i++)
	{
switch(i)
{

	case 5: LSA = 0;LSB = 0;LSC = 0;P0 = smgduan[second%10];delay(10);P0 = 0xff;	break;
	case 4: LSA = 1;LSB = 0;LSC = 0;P0 = smgduan[second/10];delay(10);P0 = 0xff;	break;
	case 3: LSA = 0;LSB = 1;LSC = 0;P0 = smgduan[min%10]&0x7f;delay(10);P0 = 0xff;	break;
	case 2: LSA = 1;LSB = 1;LSC = 0;P0 = smgduan[min/10];delay(10);P0 = 0xff;	break;
	case 1: LSA = 0;LSB = 0;LSC = 1;P0 = smgduan[hour%10]&0x7f;delay(10);P0 = 0xff;break;
	case 0: LSA = 1;LSB = 0;LSC = 1;P0 = smgduan[hour/10];delay(10);P0 = 0xff;break;
}
}
}
void keypress()
{
		P2 = 0xf7;    //1111/0111    先第一行全置一,第一列置0
if(KeyInt1==0)   //KeyInt1被按下   0111/0111
{
	delay(10);  //消抖
if(KeyInt1==0)  //再检测
{
	while(!KeyInt1); //松手
	EA = 0;  //关中断
}
}
P2 = 0xf7;
if(KeyInt2==0)
{
	delay(10);
if(KeyInt2==0)
{
	while(!KeyInt2);
	EA = 1;
}
}
P2 = 0xf7;
if(KeyInt3==0)
{
	delay(10);
if(KeyInt3==0)
{
	while(!KeyInt3);
	EA = 1;
	hour++;
	if(hour==24)
	{
		hour=0;
}
}
}
if(KeyInt4==0)  
{
	delay(10);
if(KeyInt4==0)
{
	while(!KeyInt4);  //检测松手
	EA = 1;    //打开中断总开关
	min++;  //分钟累加
	if(min==0)
	{
		min=0;
}
}	
}	
}	
	
	
	
void main()			   //ִ主函数
{
ENLED=0;         
	ADDR3=1;   
Timer0Int();   //定时器

while(1)
{
	display();   //显示

	keypress();  //按键
}
}
void Timer0()interrupt 1 	//进入定时器1
{
uint num;
num++;       //1秒
if(num==20)
{
second++;     //秒累加
num = 0;      
if(second==60)  //如果秒等于60置0
{
second=0;
min++;

if(min==60)   //如果分等于60置0
{
min=0;
hour++;

if(hour==24)  //如果小时等于12置0
{
hour=0;
min =0;
second=0;
}
}
}
}
}

陌生的你支持一下,希望得到大佬的指点.

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

焊五弟:

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

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

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

打赏作者

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

抵扣说明:

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

余额充值