//实验未实现,主函数的for循环只在内层,导致无法选在行
#include <reg51.h>
#include <intrins.h>
sbit ser=P3^4;//595芯片数据位
sbit rck=P3^5;//上升沿发送
sbit sck=P3^6;//上升沿移位
char h[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};//行,共阴极
char l[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};//列
void delay(int i)
{
while(i--);
}
void fs (unsigned char dat)
{
unsigned char a;
sck=0;
rck=0;
for(a=0;a<8;a++)
{
ser=dat>>7;
dat<<=1;
sck=1;
_nop_();
_nop_();
sck=0;
}
rck=1;
_nop_();
_nop_();
rck=0;
}
int main()
{
int i,j;
P0=0XFF;//消隐
while(1)
{
for(i=0;i<8;i++)//外层,行选择
{
for(j=0;j<8;j++)//内层,列选择
{
fs(h[i]);
P0=l[j];
delay(5000);//实验现象只在内层
}
}
if(i>=8)
{
i=0;
}
}
return 0;
}
解决方案:
#include <reg51.h>
#include <intrins.h>
#define ground P0
sbit ser=P3^4;
sbit rck=P3^5;
sbit sck=P3^6;
unsigned char code l[8]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};//加入code是为了分配内存
unsigned char code h[8][8]={
{0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80}, // 0 1000 0000
{0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40}, // 1
{0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20}, // 2
{0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10}, // 3
{0x08,0x08,0x08,0x08,0x08,0x08,0x08,0x08}, // 4
{0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04}, // 5
{0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02}, // 6
{0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01}, //7
} ;
void delay(int i)
{
while(i--);
}
void fs (unsigned char dat)
{
unsigned char a;
sck=0;
rck=0;
for(a=0;a<8;a++)
{
ser=dat>>7;
dat<<=1;
sck=1;
_nop_();
_nop_();
sck=0;
}
rck=1;
_nop_();
_nop_();
rck=0;
}
int main()
{
int i,j,k=0;
while(1)
{
for(j=0;j<8;j++)
{
fs(0x00); //消隐
ground=l[j]; //0x7f 0xbf
fs(h[k][j]);
delay(50000);
}
k++;
if(k==8)
{
k=0;
}
}
return 0;
}
分析上下程序:1.第一个程序已经修改正确,可以完成任务需求,错因为将595芯片管脚定义错误
2.附上595芯片图纸
![](https://img.haomeiwen.com/i7058785/8a75ceb93441d2be.png)
网友评论