//共阴数码管74HC164串行驱动 ,实现数码管每隔1s闪烁。
#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
sbit DAT=P3^7; //模拟串口数据发送端
sbit CLK=P3^4;//模拟时钟控制端
void init();
uint count,flag;
uint a,b,m,i;
uint t=0;
//uchar code tab[]={0xfd,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xff,0xf6,0xef};//0-9,-,全灭
uchar code tab[]={0xdf,0x45,0xaf,0xed,0x75,0xf9,0xfb,
0x4d,0xff,0xfd,0x7f,0x3b,0xd7,0xcd,0x9b,0x71,0x27,0x01};
void sendbyte(uchar byte)
{
uchar num,c;
num=tab[byte];
for(c=0;c<8;c++)
{
CLK=0;
DAT=num&0x01;
CLK=1;
num>>=1;
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void display()
{
m=12;
a=m/10;
b=m%10;
sendbyte(a);
delay(58);
sendbyte(b);
}
/*闪烁程序*/
void shanshou()
{
while(i<=20)
{
if(flag!=1)
{
display();
}
else
{
sendbyte(tab[10]);
delay(58);
sendbyte(tab[10]);
flag=0;
delay(1000);
}
i++;
}
}
void main()
{
init();
shanshou();
if(i==20)
{
TR0=0;
display();
}
}
void init()
{
TMOD=0x01;//设置定时器0为工作方式1
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;//开总中断
ET0=1;//开定时器0中断
TR0=1;//启动定时器0
}
void time0() interrupt 1 // 定时器0中断
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==20)
{
flag=1;
count=0;
}
} |