51系列数码管显示时间

STC89C52

利用数码管显示时间(非当前时间)

#include<reg52.h>

#define uint unsigned int
#define uchar unsigned char
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
sbit dula=P2^6;
sbit wela=P2^7;
sbit s2=P3^4;

uint miao=0;		//秒
uint fen=0;			//分
uint shi=0;		 //时
uint n=0;			//计数

uint date[6]={0,0,0,0,0,0};	 //定义一个有6个地址的数组,分别存时,分,秒的个位和十位。

void delay(uint z)			 //延时
{
uint x,y;
	 for(x=z;x>0;x--)
	 for(y=110;y>0;y--);
}
void keyscan()
{
if(s2==0)
{
		delay(10);
 	if(s2==0)
	{
 	wela=1;
	P0=0xfe;
	wela=0;

	dula=1;
	P0=0x3f;
	dula=0;
	P0=0xff;
	delay(10);

	wela=1;
	P0=0xfd;
	wela=0;

	dula=1;
	P0=0x4f;
	dula=0;
	P0=0xff;
	delay(10);

	wela=1;
	P0=0xfb;
	wela=0;

	dula=1;
	P0=0x5b;
	dula=0;
	P0=0xff;
	delay(10);

	wela=1;
	P0=0xf7;
	wela=0;

	dula=1;
	P0=0x06;
	dula=0;
	P0=0xff;
	delay(10);

	wela=1;
	P0=0xef;
	wela=0;

	dula=1;
	P0=0x3f;
	dula=0;
	P0=0xff;
	delay(10);

	wela=1;
	P0=0xdf;
	wela=0;

	dula=1;
	P0=0x3f;
	dula=0;
	P0=0xff;
	delay(10);
	while(!s2);
	}

}
}


void write_address(uchar address)	
 
{
	wela=1;
	P0=address;
	wela=0;
}
void write_date(uint n)				 //写数据函数,送入段选信号
{

	dula=1;
	 P0=table[n];
	 dula=0;

}										//显示函数

void display()
{	 	 

	write_date(date[0]);
	P0=0xff;
	write_address(0xdf);
	delay(1);
	write_date(date[1]);
	P0=0xff;
	write_address(0xef);
	delay(1);
	write_date(date[2]);
	P0=0xff;
	write_address(0xf7);
	delay(1);
	write_date(date[3]);
	P0=0xff;
	write_address(0xfb);
	delay(1);
	write_date(date[4]);
	P0=0xff;
	write_address(0xfd);
	delay(1);
	write_date(date[5]);
	P0=0xff;
	write_address(0xfe);
	delay(1);
	 
}

void time_init()						 //定时器初始化函数

{

TMOD=0x01 ;
	TH0=(65536-45872)/256;
	TL0=(65536-45872)%256;
	EA=1;
	ET0=1;
	TR0=1;
							
}

void main()
{
	dula=0;								 //先关闭位选,段选信号。
	wela=0;
	time_init();
	while(1)
	{
	keyscan();
	display();
	}
}

void time() interrupt 1					 //定时器0中断

{
n++;
	 if(n==20)								 //当n=10000时,表示1s到了
	 {	 
	 n=0;
	 miao++;
		 if(miao==60)
		 {
		 miao=0;
		 fen++;
			 if(fen==60)
			 {
			 fen=0;
				 shi++;
				 if(shi==24)
				 {
				 shi=0;
				 }
			 }
		 }
		 date[0]=miao%10;
		 date[1]=miao/10;
		 date[2]=fen%10;
		 date[3]=fen/10;
		 date[4]=shi%10;
		 date[5]=shi/10;
	  }
}

发布了2 篇原创文章 · 获赞 0 · 访问量 14

猜你喜欢

转载自blog.csdn.net/qq_45899288/article/details/105331349