单片机控制74hc595驱动4个单数码管静态显示
- 演示效果:
*材料 AT89C52 、74HC595 共阳数码管 - 采用Proteus仿真
实例程序
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar count,second,min;
sbit lclk=P1^0;
sbit sclk=P1^1;
sbit Data=P1^2;
uchar dis[4];
uchar code displayhex[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
void send595(uchar *pdat)
{
uchar i,j;
j=0x80;
for(i=0;i<8;i++)
{
if((*pdat)&j) Data=1;
else Data=0;
sclk=1;
sclk=0;
j>>=1;
}
}
void display595( uchar *pdat)
{
uchar i;
for(i=0;i<4;i++)
{
send595(&displayhex[*(pdat+i)]);
}
lclk=0;
lclk=1;
}
void init()
{
TMOD=0X01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void main()
{
init();
while(1)
{
if(count==20)
{
count=0;
second++;
if(second==60)
{
second=0;
min++;
if(min==24)
{
min=0;
}
}
dis[0]=second%10;
dis[1]=second/10;
dis[2]=min%10;
dis[3]=min/10;
}
display595(dis);
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
}
|