利用51单片机,实现数字0-9每隔一秒显示一个数字。
#include
#include
typedef unsigned char u8;
typedef unsigned int u16;
unsigned char code LedWei[] = {0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE};
unsigned char code LedDuan[10][8] = {
{0x00,0x00,0x3e,0x41,0x41,0x41,0x3e,0x00},
{0x00,0x00,0x02,0x42,0xFE,0x02,0x02,0x00},
{0x00,0x00,0x63,0x85,0x89,0x91,0x61,0x00},
{0x00,0x00,0x22,0x49,0x49,0x36,0x00,0x00},
{0x00,0x00,0x0c,0x14,0x24,0x7f,0x04,0x00},
{0x00,0x00,0x72,0x51,0x51,0x51,0x4e,0x00},
{0x00,0x00,0x3e,0x49,0x49,0x49,0x26,0x00},
{0x00,0x00,0x40,0x40,0x40,0x4f,0x70,0x00},
{0x00,0x00,0x36,0x49,0x49,0x49,0x36,0x00},
{0x00,0x00,0x32,0x49,0x49,0x49,0x3e,0x00},
};
sbit SER = P3^4;
sbit RCLK = P3^5;
sbit SRCLK = P3^6;
void delay(u8 time)
{
u8 i,j;
for(i = 0;i < time;i++);
for(j = 0;j < 121;j++);
}
void Hc595ByteSend(u8 dat)
{
u8 a;
RCLK = 0;
SRCLK = 0;
for(a = 0;a < 8;a++)
{
SER = dat >> 7;
dat <<= 1;
SRCLK = 1;
_nop_();
_nop_();
SRCLK = 0;
}
RCLK = 1;
_nop_();
_nop_();
RCLK = 0;
}
void main()
{
TMOD = 0x01;
TH0 = 0xFC;
TL0 = 0x18;
TR0 = 1;
while(1)
{
while(TF0 == 0);
TF0 = 0xFC;
TL0 = 0x18;
cnt++;
if(cnt >= 1000)
{
cnt = 0;
for(i = 0;i < 50;i++)
{
for(tab = 0;tab < 8;tab++)
{
Hc595ByteSend(0x00);
P0 = LedWei[tab];
Hc595ByteSend(LedDuan[j][tab]);
delay(10);
}
}
j++;
if(j == 10)
{
j = 0;
}
}
}
}