代码如下
#include <reg51.h>
sbit Hc38A=P2^2;
sbit Hc38B=P2^3;
sbit Hc38C=P2^4;
unsigned char sec=0,min=0,hou=0;
unsigned char code SMG[]={0X3F,0X06,0X5B,0X4F,0X66,
0X6D,0X7D,0X07,0X7F,0X6F};
void delay(unsigned char t)
{
while(t--)
;
}
void HC38(unsigned char n)
{
switch (n)
{
case 0:Hc38C=0;Hc38B=0;Hc38A=0;break;
case 1:Hc38C=0;Hc38B=0;Hc38A=1;break;
case 2:Hc38C=0;Hc38B=1;Hc38A=0;break;
case 3:Hc38C=0;Hc38B=1;Hc38A=1;break;
case 4:Hc38C=1;Hc38B=0;Hc38A=0;break;
case 5:Hc38C=1;Hc38B=0;Hc38A=1;break;
case 6:Hc38C=1;Hc38B=1;Hc38A=0;break;
case 7:Hc38C=1;Hc38B=1;Hc38A=1;break;
}
}
void display()
{
HC38(0);
P0=SMG[hou/10];
delay(100);
HC38(1);
P0=SMG[hou%10];
delay(100);
HC38(3);
P0=SMG[min/10];
delay(100);
HC38(4);
P0=SMG[min%10];
delay(100);
HC38(6);
P0=SMG[sec/10];
delay(100);
HC38(7);
P0=SMG[sec%10];
delay(100);
}
void shuaxin(unsigned char k)
{
while(k--)
display();
}
void main()
{
while(1)
{
shuaxin(50);
sec++;
if(sec>=59)
{
sec=0;
min++;
if(min>=59)
{
min=0;
hou++;
if(hou>23)
hou=0;
}
}
}
}
接线如下