/*本程序为基于扫描数码管的时钟程序
1、矩阵键盘可设定时间。
2、矩阵键盘接P2口。
3、数码管段选接P1口。
4、38译码器A,B,C依次为P3.0,P3.1,P3.2。*/
#include<reg51.h>
void delay(int a,int b)
{
int i,j;
for(i = 0;i <= a;i ++)
{
for(j = 0;j <= b;j ++)
{}
}
}
int main()
{
int i,h1,h2,m1,m2,s1,s2;
int Number[]={0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff}; //选择是哪一个数码管发光
int number[]={~0xc0,~0xf9,~0xa4,~0xb0,~0x99,~0x92,~0x82,~0xf8,~0x80,~0x90,0x40};//数码管显示的数字
i = 50;
h1 = 0;h2 = 0;m1 = 0;m2 = 0;s1 = 0;s2 = 0;
while(1)
{
/*按键检测程序*/
P2 = 0Xfe;
if(P2 == 0xee)
{
delay(5,20);
if(P2 == 0xee)
{
h1 ++;
if(h1 == 3)
{
h1 = 0;
}
}
}
if(P2 == 0xde)
{
delay(5,20);
if(P2 == 0xDe)
{
h2 ++;
if(h2 == 5)
{
h2 = 0;
}
}
}
if(P2 == 0xbe)
{
delay(5,20);
if(P2 == 0xbe)
{
m1 ++;
if(m1 == 6)
{
m1 = 0;
}
}
}
if(P2 == 0x7e)
{
delay(5,20);
if(P2 == 0x7e)
{
m2 ++;
if(m2 == 10)
{
m2 = 0;
}
}
}
delay(5,20);
/*时钟显示程序*/
while(i --)
{
P3 = Number[0];P1 = number[h1];delay(13,10);P1 = 0x00;
P3 = Number[1];P1 = number[h2];delay(13,10);P1 = 0x00;
P3 = Number[2];P1 = number[10];delay(13,10);P1 = 0x00;
P3 = Number[3];P1 = number[m1];delay(13,10);P1 = 0x00;
P3 = Number[4];P1 = number[m2];delay(13,10);P1 = 0x00;
P3 = Number[5];P1 = number[10];delay(13,10);P1 = 0x00;
P3 = Number[6];P1 = number[s1];delay(13,10);P1 = 0x00;
P3 = Number[7];P1 = number[s2];delay(13,10);P1 = 0x00;
}
i = 50;//控制数码管保持时间,用于延时控制。
/*进位程序:分钟个位满10进一,分钟十位满6进一;
小时个位满10进一,到达24规零;秒个位满十进一,秒十位满六进一*/
s2 ++;
if(s2 == 10)
{
s2 = 0;
s1 ++;
if((s1 == 6))
{
m2 ++;
s1 = 0;s2 = 0;
if(m2 == 10)
{
m2 = 0;
m1 ++;
if((m1 == 6))
{
h2 ++;
if((h1 == 2)&&(h2 == 4))
{
h1 = 0;h2 = 0;m1 = 0;m2 = 0;s1 = 0;s2 = 0;
}
m1 = 0;m2 = 0;
if((h2 == 10)&&(h1 != 2))
{
h2 = 0;
h1 ++;
if((h1 == 2)&&(h2 == 4))
{
h1 = 0;h2 = 0;m1 = 0;m2 = 0;s1 = 0;s2 = 0;
}
}
}
}
}
}
}
}
基于51单片机的扫描数码管时钟
最新推荐文章于 2023-12-17 13:56:00 发布