一片595驱动C程序:
#include
#define uchar unsigned char
#define uint unsigned int
sbit shp=P2^0;
sbit ds=P2^1;
sbit stp=P2^2;
uchar code table[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void delay(uint t)
{
uchar i;
for(;t>0;t--)for(i=248;i>0;i--);
}
void writedata(uchar kk)
{
uchar temp,nn;
temp=table[kk];
for(nn=0;nn<8;nn++)
{ shp=0;
ds=temp&0x80;
temp=temp<<1;
shp=1;
}
}
void send595()
{
stp=0;
delay(1);
stp=1;
}
void main()
{uchar i;
while(1)
{ for(i=0;i<8;i++)
{writedata(i);
send595();
delay(200);
}
}
}
两片595让八位数码管依次点亮1-8;
#include
#define uchar unsigned char
#define uint unsigned int
sbit shp=P2^1;
sbit ds=P2^0;
sbit stp=P2^2;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x76,
0x73,0x3e,0x00};
void delay(uint t)
{
uint n;
for(;t>0;t--)for(n=20;n>0;n--);
}
void writeled(uchar i,uchar j)//点亮一个七段数码管;
{
uchar temp,k;
temp=j;
for(k=0;k<8;k++)
{
ds=temp&0x80;//第一位位码输出;
shp=0;shp=1;shp=0;//位同步脉冲;
temp=temp<<1;
}
temp=table[i];
for(k=0;k<8;k++)
{
ds=temp&0x80;//第一位断码输出;
shp=0;shp=1;shp=0;//位同步脉冲;
temp=temp<<1;
}
stp=0;
//delay(2);
stp=1;
stp=0;
}
void main()
{
uchar i;
uchar j=0;
while(1)
{
writeled(i,j);
i++;
j=j<<1;
delay(2000);
if(i==8)
{
i=0;
j=0;
}
}
}
以上程序经测试通过。