51单片机+74hc595程序求错
#define uint unsigned int
#define uchar unsigned char
uchar code a[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
uchar code d[4]={0x01,0x02,0x04,0x08};
sbit LCK=P0^0;
sbit SDI=P0^1;
sbit SCK=P0^2;
void delay()
{;;;}
void send(uchar x)
{
uchar i;
for(i=0;i<8;i++)
{
x=x<<1;
SCK=0;
delay();
SDI=CY;
SCK=1;
delay();
}
}
void display(uchar d0,uchar d1,uchar d2,uchar d3)
{
LCK=0;
send(a[d0]);
send(d[0]);
LCK=1;
delay();
LCK=0;
send(a[d1]);
send(d[1]);
LCK=1;
delay();
LCK=0;
send(a[d2]);
send(d[2]);
LCK=1;
delay();
LCK=0;
send(a[d3]);
send(d[3]);
LCK=1;
delay();
}
void main()
{
while(1)
{
display(1,2,3,4);
}
}
我位选与段选的顺序弄错了,但是改过来还是不行啊 展开
程序需要更改位选与段选的次序,两个时钟脉冲的引脚,弄混了。
其它基本正常:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
//-----------------------------------------------------
uchar code a[16] = {
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
uchar code d[4] = {0x01, 0x02, 0x04, 0x08};
//-----------------------------------------------------
sbit LCK = P3^7;
sbit SCK = P3^6;
sbit SDI = P3^5;
//-----------------------------------------------------
void delay()
{
;
}
//-----------------------------------------------------
void delay_ms(uint ms)
{
uint a, b;
for(a = ms; a > 0; a--) for(b = 249; b > 0; b--);
}
//-----------------------------------------------------
void send(uchar x)
{
uchar i;
for(i = 0; i < 8; i++) {
x = x << 1;
SCK = 0; delay();
SDI = CY;
SCK = 1; delay();
}
}
//-----------------------------------------------------
void display(uchar d0, uchar d1, uchar d2, uchar d3)
{
LCK = 0; send(d[0]); send(a[d0]); LCK = 1; delay_ms(1);
LCK = 0; send(d[1]); send(a[d1]); LCK = 1; delay_ms(1);
LCK = 0; send(d[2]); send(a[d2]); LCK = 1; delay_ms(1);
LCK = 0; send(d[3]); send(a[d3]); LCK = 1; delay_ms(1);
}
//-----------------------------------------------------
void main()
{
while(1) {
display(1, 2, 3, 4);
}
}
//-----------------------------------------------------
参看:
http://hi.baidu.com/%D7%F6%B6%F8%C2%DB%B5%C0/blog/item/36bd7634baa8ba0691ef390a.html
显示截图如下: