⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clock.c

📁 基于51单片机实现的数字钟 用keil编译
💻 C
字号:
/*
数字时钟
时钟频率24MHz
*/

#include<reg51.h>
#include<intrins.h>

unsigned char code LED_CODES[]={0xc0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};

unsigned char hhh,hhl,mmh,mml,ssh,ssl;
unsigned char hhls,hhhs,mmhs,mmls;
unsigned char display_array[6]={0xc0,0xc0,0xc0,0xc0,0xc0,0xc0};
unsigned char display_set[6]={0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char tt1=0,tt2=0,tt3=8;
unsigned char display_flag=0;
unsigned char flash=0;

void init();
void delay(int d);
void display0(unsigned char *p);
void display1();
void display2();

void main()
{
	TMOD=0x11;
	TH0=0x3C;
	TL0=0xAF;
	TH1=0x00;
	TL1=0x00;
	IT0=1;
	IT1=1;
	EA=1;
	ET0=1;
	ET1=1;
	TR0=1;
	TR1=1;
	EX0=1;
	EX1=1;
	init();
	while(1)
	{
		display_array[0]=LED_CODES[ssl];
		display_array[1]=LED_CODES[ssh];
		display_array[2]=LED_CODES[mml];
		display_array[3]=LED_CODES[mmh];
		display_array[4]=LED_CODES[hhl];
		display_array[5]=LED_CODES[hhh];
		if(display_flag==0)
			display0(display_array);
		else if(display_flag==1)
			display1();
		else if(display_flag==2)
			display2();
	}
}

void timer1() interrupt 1		   //秒计时器
{
	TH0=0x3C;
	TL0=0xAF;
	tt1++;
	if(tt1==5)	//40为一秒
	{
		ssl++;
		if(ssl==10)
		{
			ssh++;
			ssl=0;
		}
		if(ssh==6)
		{
			mml++;
			ssh=0;
		}
		if(mml==10)
		{
			mmh++;
			mml=0;
		}
		if(mmh==6)
		{
			hhl++;
			mmh=0;
		}
		if(hhl==10)
		{
			hhh++;
			hhl=0;	
		}
		if(hhl==4&&hhh==2)
		{
			hhh=0;
			hhl=0;
		}
		tt1=0;
	}
}

void timer2() interrupt 3  //闪烁延时
{
	TH1=0x00;
	TL1=0x00;
	tt2++;
	if(tt2==tt3)
	{
		if(flash==0)
		{
			flash=1;
			tt3=8;
		}
		else
		{
			flash=0;
			tt3=12;
		}
		tt2=0;
	}
}

void display0(unsigned char *p)		//显示6个数码管
{
	int k;
	P2=0x80;
	for(k=5;k>=0;k--)
	{
		P2=_crol_(P2,1);
		P1=p[k];
		delay(1);
	}
} 

void display1()				//设置小时时的显示
{ 
	if(flash==0)
	{
		display_set[5]=LED_CODES[hhhs];
		display_set[4]=LED_CODES[hhls];
		display_set[3]=0xff;
		display_set[2]=0xff;
		display_set[1]=0xff;
		display_set[0]=0xff;
		display0(display_set);
	}
	else
	{
		display_set[5]=0xff;
		display_set[4]=0xff;
		display_set[3]=0xff;
		display_set[2]=0xff;
		display_set[1]=0xff;
		display_set[0]=0xff;
		display0(display_set);	
	}	 
}

void display2()				//设置分钟时的显示
{
	if(flash==0)
	{
		display_set[5]=0xff;
		display_set[4]=0xff;
		display_set[3]=LED_CODES[mmhs];
		display_set[2]=LED_CODES[mmls];
		display_set[1]=0xff;
		display_set[0]=0xff;
		display0(display_set);
	}
	else
	{
		display_set[5]=0xff;
		display_set[4]=0xff;
		display_set[3]=0xff;
		display_set[2]=0xff;
		display_set[1]=0xff;
		display_set[0]=0xff;
		display0(display_set);	
	}
}

void delay(int d)		//普通延时
{
	unsigned char i,j;
	for(i=d;i>0;i--)
		for(j=50;j>0;j--);
}

void init()			   //初始化
{
	P1=0x00;
	hhh=hhl=mmh=mml=ssh=ssl=0x00;
}

void mint0() interrupt 0	//外部中断0,set1键
{
	display_flag++;
	if(display_flag==1)
	{
		hhhs=hhh;
		hhls=hhl;
	}
	if(display_flag==2)
	{
		mmhs=mmh;
		mmls=mml;
	}
	if(display_flag==3)
	{
		display_flag=0;
		if(hhh!=hhhs||hhl!=hhls||mmh!=mmhs||mml!=mmls)
		{
			ssl=0;
			ssh=0;
		}
		hhh=hhhs;
		hhl=hhls;
		mmh=mmhs;
		mml=mmls;
	}
	delay(1);
}

void mint1() interrupt 2	//外部中断1,set2键
{
	if(display_flag==1)
	{	//设定时
		hhls++;
		if(hhls==10)
		{
			hhhs++;
			hhls=0;
		}
		if(hhls==4&&hhhs==2)
		{
			hhhs=0;
			hhls=0;
		}
	}
	if(display_flag==2)
	{	//设定分
		mmls++;
		if(mmls==10)
		{
			mmhs++;
			mmls=0;
		}
		if(mmhs==6)		
			mmhs=0;				
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -