单片机串口通信让显示在点阵的问题求助
时间:10-02
整理:3721RD
点击:
问题是利用串口通信输送0到9的数字显示在普中的点阵上(16*16)的,这过程我不知道代码哪里错了,在单片机总是仿真不出来,求大虾们帮帮忙谢谢!
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
unsigned char a,flag;
sbit SCK=P3^6;
sbit LCK=P3^5;
sbit SDI=P3^4;
void HC595_Send(uchar dat1,uchar dat0,uchar dat3,uchar dat2);
//void delay(uchar z);
uchar code NUM_HANG[]={0x80,0x00,0x40,0x00,0x20,0x00,0x10,0x00,0x08,0x00,0x04,0x00,0x02,0x00,0x01,0x00,
0x00,0x80,0x00,0x40,0x00,0x20,0x00,0x10,0x00,0x08,0x00,0x04,0x00,0x02,0x00,0x01,};
uchar code NUM[]={0x1F,0xFE,0x1F,0xFE,0x18,0x06,0x18,0x06,0x18,0x06,0x18,0x06,0x18,0x06,0x18,0x06,
0x18,0x06,0x18,0x06,0x18,0x06,0x18,0x06,0x1F,0xFE,0x1F,0xFE,0x00,0x00,0x00,0x00,
0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,
0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,
0x3F,0xFC,0x3F,0xFC,0x00,0x0C,0x00,0x0C,0x00,0x0C,0x00,0x0C,0x1F,0xFC,0x1F,0xFC,
0x18,0x00,0x18,0x00,0x18,0x00,0x18,0x00,0x18,0x00,0x1F,0xFC,0x1F,0xFC,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x80,0x7B,0xC0,0x79,0xC0,0x79,0xC0,0x03,0xC0,
0x1F,0x00,0x03,0xC0,0x01,0xE0,0x01,0xE0,0x79,0xE0,0x79,0xE0,0x7B,0xC0,0x3F,0x80,
0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x80,0x0F,0x80,0x0F,0x80,0x1F,0x80,0x3F,0x80,
0x3F,0x80,0x77,0x80,0x67,0x80,0xFF,0xE0,0x07,0x80,0x07,0x80,0x07,0x80,0x1F,0xC0,
0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xE0,0x70,0x00,0x70,0x00,0x70,0x00,0x70,0x00,
0x7F,0x80,0x7B,0xC0,0x01,0xE0,0x01,0xE0,0x79,0xE0,0x79,0xE0,0x7B,0xC0,0x3F,0x80,
0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xC0,0x3F,0xC0,0x7B,0xC0,0x70,0x00,0x70,0x00,
0xFF,0xC0,0xFD,0xE0,0xF9,0xE0,0xF0,0xE0,0x71,0xE0,0x79,0xE0,0x7D,0xC0,0x1F,0x80,
0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xC0,0x73,0xC0,0x73,0x80,0x07,0x80,0x07,0x00,
0x0F,0x00,0x0E,0x00,0x0E,0x00,0x1E,0x00,0x1E,0x00,0x1E,0x00,0x1E,0x00,0x1E,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x80,0x7B,0xC0,0x71,0xE0,0x71,0xE0,0x79,0xC0,
0x3F,0xC0,0x3F,0x80,0x7B,0xC0,0xF1,0xE0,0xF1,0xE0,0xF1,0xE0,0x7B,0xC0,0x3F,0x80,
0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x80,0x7B,0xC0,0xF1,0xE0,0xF1,0xE0,0xF1,0xE0,
0xF1,0xE0,0x7B,0xE0,0x3F,0xE0,0x01,0xE0,0x01,0xC0,0x7B,0xC0,0x7F,0x80,0x3F,0x00,
};
void mian()
{ int i,j;
SCON=0x40;
TMOD=0X20;
PCON=0;
TH1=0XE6;
TL1=0XE6;
TR1=1;
REN=1;
EA=1;
ES=1;
while(1)
{
if(flag==1)
{flag=0;}
if(a==0x48)
{
HC595_Send(0xff,0xff,0x00,0x00);
for(j=255;j>0;j--) //一个字显示的次数,刷256次然后刷第二个字。
{
for(i=0;i<32;i=i+2) //一共有16行,所以刷16次
{
HC595_Send(~NUM[i],~NUM[i+1],NUM_HANG[i],NUM_HANG[i+1]);
}
}
}
if(a==0x49)
{ HC595_Send(0xff,0xff,0x00,0x00);
for(j=255;j>0;j--) //一个字显示的次数,刷256次然后刷第二个字。
{
for(i=0;i<32;i=i+2) //一共有16行,所以刷16次
{
HC595_Send(~NUM[32+i],~NUM[32+i+1],NUM_HANG[i],NUM_HANG[i+1]);
}
}
}
if(a==0x50)
{
HC595_Send(0xff,0xff,0x00,0x00);
for(j=255;j>0;j--) //一个字显示的次数,刷256次然后刷第二个字。
{
for(i=0;i<32;i=i+2) //一共有16行,所以刷16次
{
HC595_Send(~NUM[32+i],~NUM[32+i+1],NUM_HANG[i],NUM_HANG[i+1]);
}
}
}
if(a==0x51)
{
HC595_Send(0xff,0xff,0x00,0x00);
for(j=255;j>0;j--) //一个字显示的次数,刷256次然后刷第二个字。
{
for(i=0;i<32;i=i+2) //一共有16行,所以刷16次
{
HC595_Send(~NUM[i],~NUM[i+1],NUM_HANG[i],NUM_HANG[i+1]);
}
}
}
if(a==0x52)
{
HC595_Send(0xff,0xff,0x00,0x00);
for(j=255;j>0;j--) //一个字显示的次数,刷256次然后刷第二个字。
{
for(i=0;i<32;i=i+2) //一共有16行,所以刷16次
{
HC595_Send(~NUM[32+i],~NUM[32+i+1],NUM_HANG[i],NUM_HANG[i+1]);
}
}
}
if(a==0x53)
{ HC595_Send(0xff,0xff,0x00,0x00);
for(j=255;j>0;j--) //一个字显示的次数,刷256次然后刷第二个字。
{
for(i=0;i<32;i=i+2) //一共有16行,所以刷16次
{
HC595_Send(~NUM[32+i],~NUM[32+i+1],NUM_HANG[i],NUM_HANG[i+1]);
}
}
}
if(a==0x54)
{ HC595_Send(0xff,0xff,0x00,0x00);
for(j=255;j>0;j--) //一个字显示的次数,刷256次然后刷第二个字。
{
for(i=0;i<32;i=i+2) //一共有16行,所以刷16次
{
HC595_Send(~NUM[32+i],~NUM[32+i+1],NUM_HANG[i],NUM_HANG[i+1]);
}
}
}
if(a==0x55)
{ HC595_Send(0xff,0xff,0x00,0x00);
for(j=255;j>0;j--) //一个字显示的次数,刷256次然后刷第二个字。
{
for(i=0;i<32;i=i+2) //一共有16行,所以刷16次
{
HC595_Send(~NUM[32+i],~NUM[32+i+1],NUM_HANG[i],NUM_HANG[i+1]);
}
}
}
if(a==0x56)
{
HC595_Send(0xff,0xff,0x00,0x00);
for(j=255;j>0;j--) //一个字显示的次数,刷256次然后刷第二个字。
{
for(i=0;i<32;i=i+2) //一共有16行,所以刷16次
{
HC595_Send(~NUM[32+i],~NUM[32+i+1],NUM_HANG[i],NUM_HANG[i+1]);
}
}
}
if(a==0x57)
{
HC595_Send(0xff,0xff,0x00,0x00);
for(j=255;j>0;j--) //一个字显示的次数,刷256次然后刷第二个字。
{
for(i=0;i<32;i=i+2) //一共有16行,所以刷16次
{
HC595_Send(~NUM[32+i],~NUM[32+i+1],NUM_HANG[i],NUM_HANG[i+1]);
}
}
}
}
}
/* void delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
} */
void ser()interrupt 4
{
while(!RI)
RI=0;
a=SBUF;
flag=1;
}
void HC595_Send(uchar dat1,uchar dat0,uchar dat3,uchar dat2)
{
uchar i;
SCK=1;
LCK=1;
for(i=0;i<8;i++)
{
SDI=dat0&0x01; //从高位到低位
dat0>>=1;
SCK=0;
_nop_();
_nop_();
SCK=1;
_nop_();
_nop_();
}
for(i=0;i<8;i++)
{
SDI=dat1&0x01; //从高位到低位
dat1>>=1;
SCK=0;
_nop_();
_nop_();
SCK=1;
_nop_();
_nop_();
}
for(i=0;i<8;i++)
{
SDI=dat2&0x01; //从高位到低位
dat2>>=1;
SCK=0;
_nop_();
_nop_();
SCK=1;
_nop_();
_nop_();
}
for(i=0;i<8;i++)
{
SDI=dat3&0x01; //从高位到低位
dat3>>=1;
SCK=0;
_nop_();
_nop_();
SCK=1;
_nop_();
_nop_();
}
LCK=0; //下面是输出时钟
_nop_();
_nop_();
LCK=1;
_nop_();
_nop_();
}
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
unsigned char a,flag;
sbit SCK=P3^6;
sbit LCK=P3^5;
sbit SDI=P3^4;
void HC595_Send(uchar dat1,uchar dat0,uchar dat3,uchar dat2);
//void delay(uchar z);
uchar code NUM_HANG[]={0x80,0x00,0x40,0x00,0x20,0x00,0x10,0x00,0x08,0x00,0x04,0x00,0x02,0x00,0x01,0x00,
0x00,0x80,0x00,0x40,0x00,0x20,0x00,0x10,0x00,0x08,0x00,0x04,0x00,0x02,0x00,0x01,};
uchar code NUM[]={0x1F,0xFE,0x1F,0xFE,0x18,0x06,0x18,0x06,0x18,0x06,0x18,0x06,0x18,0x06,0x18,0x06,
0x18,0x06,0x18,0x06,0x18,0x06,0x18,0x06,0x1F,0xFE,0x1F,0xFE,0x00,0x00,0x00,0x00,
0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,
0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,
0x3F,0xFC,0x3F,0xFC,0x00,0x0C,0x00,0x0C,0x00,0x0C,0x00,0x0C,0x1F,0xFC,0x1F,0xFC,
0x18,0x00,0x18,0x00,0x18,0x00,0x18,0x00,0x18,0x00,0x1F,0xFC,0x1F,0xFC,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x80,0x7B,0xC0,0x79,0xC0,0x79,0xC0,0x03,0xC0,
0x1F,0x00,0x03,0xC0,0x01,0xE0,0x01,0xE0,0x79,0xE0,0x79,0xE0,0x7B,0xC0,0x3F,0x80,
0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x80,0x0F,0x80,0x0F,0x80,0x1F,0x80,0x3F,0x80,
0x3F,0x80,0x77,0x80,0x67,0x80,0xFF,0xE0,0x07,0x80,0x07,0x80,0x07,0x80,0x1F,0xC0,
0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xE0,0x70,0x00,0x70,0x00,0x70,0x00,0x70,0x00,
0x7F,0x80,0x7B,0xC0,0x01,0xE0,0x01,0xE0,0x79,0xE0,0x79,0xE0,0x7B,0xC0,0x3F,0x80,
0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xC0,0x3F,0xC0,0x7B,0xC0,0x70,0x00,0x70,0x00,
0xFF,0xC0,0xFD,0xE0,0xF9,0xE0,0xF0,0xE0,0x71,0xE0,0x79,0xE0,0x7D,0xC0,0x1F,0x80,
0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xC0,0x73,0xC0,0x73,0x80,0x07,0x80,0x07,0x00,
0x0F,0x00,0x0E,0x00,0x0E,0x00,0x1E,0x00,0x1E,0x00,0x1E,0x00,0x1E,0x00,0x1E,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x80,0x7B,0xC0,0x71,0xE0,0x71,0xE0,0x79,0xC0,
0x3F,0xC0,0x3F,0x80,0x7B,0xC0,0xF1,0xE0,0xF1,0xE0,0xF1,0xE0,0x7B,0xC0,0x3F,0x80,
0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x80,0x7B,0xC0,0xF1,0xE0,0xF1,0xE0,0xF1,0xE0,
0xF1,0xE0,0x7B,0xE0,0x3F,0xE0,0x01,0xE0,0x01,0xC0,0x7B,0xC0,0x7F,0x80,0x3F,0x00,
};
void mian()
{ int i,j;
SCON=0x40;
TMOD=0X20;
PCON=0;
TH1=0XE6;
TL1=0XE6;
TR1=1;
REN=1;
EA=1;
ES=1;
while(1)
{
if(flag==1)
{flag=0;}
if(a==0x48)
{
HC595_Send(0xff,0xff,0x00,0x00);
for(j=255;j>0;j--) //一个字显示的次数,刷256次然后刷第二个字。
{
for(i=0;i<32;i=i+2) //一共有16行,所以刷16次
{
HC595_Send(~NUM[i],~NUM[i+1],NUM_HANG[i],NUM_HANG[i+1]);
}
}
}
if(a==0x49)
{ HC595_Send(0xff,0xff,0x00,0x00);
for(j=255;j>0;j--) //一个字显示的次数,刷256次然后刷第二个字。
{
for(i=0;i<32;i=i+2) //一共有16行,所以刷16次
{
HC595_Send(~NUM[32+i],~NUM[32+i+1],NUM_HANG[i],NUM_HANG[i+1]);
}
}
}
if(a==0x50)
{
HC595_Send(0xff,0xff,0x00,0x00);
for(j=255;j>0;j--) //一个字显示的次数,刷256次然后刷第二个字。
{
for(i=0;i<32;i=i+2) //一共有16行,所以刷16次
{
HC595_Send(~NUM[32+i],~NUM[32+i+1],NUM_HANG[i],NUM_HANG[i+1]);
}
}
}
if(a==0x51)
{
HC595_Send(0xff,0xff,0x00,0x00);
for(j=255;j>0;j--) //一个字显示的次数,刷256次然后刷第二个字。
{
for(i=0;i<32;i=i+2) //一共有16行,所以刷16次
{
HC595_Send(~NUM[i],~NUM[i+1],NUM_HANG[i],NUM_HANG[i+1]);
}
}
}
if(a==0x52)
{
HC595_Send(0xff,0xff,0x00,0x00);
for(j=255;j>0;j--) //一个字显示的次数,刷256次然后刷第二个字。
{
for(i=0;i<32;i=i+2) //一共有16行,所以刷16次
{
HC595_Send(~NUM[32+i],~NUM[32+i+1],NUM_HANG[i],NUM_HANG[i+1]);
}
}
}
if(a==0x53)
{ HC595_Send(0xff,0xff,0x00,0x00);
for(j=255;j>0;j--) //一个字显示的次数,刷256次然后刷第二个字。
{
for(i=0;i<32;i=i+2) //一共有16行,所以刷16次
{
HC595_Send(~NUM[32+i],~NUM[32+i+1],NUM_HANG[i],NUM_HANG[i+1]);
}
}
}
if(a==0x54)
{ HC595_Send(0xff,0xff,0x00,0x00);
for(j=255;j>0;j--) //一个字显示的次数,刷256次然后刷第二个字。
{
for(i=0;i<32;i=i+2) //一共有16行,所以刷16次
{
HC595_Send(~NUM[32+i],~NUM[32+i+1],NUM_HANG[i],NUM_HANG[i+1]);
}
}
}
if(a==0x55)
{ HC595_Send(0xff,0xff,0x00,0x00);
for(j=255;j>0;j--) //一个字显示的次数,刷256次然后刷第二个字。
{
for(i=0;i<32;i=i+2) //一共有16行,所以刷16次
{
HC595_Send(~NUM[32+i],~NUM[32+i+1],NUM_HANG[i],NUM_HANG[i+1]);
}
}
}
if(a==0x56)
{
HC595_Send(0xff,0xff,0x00,0x00);
for(j=255;j>0;j--) //一个字显示的次数,刷256次然后刷第二个字。
{
for(i=0;i<32;i=i+2) //一共有16行,所以刷16次
{
HC595_Send(~NUM[32+i],~NUM[32+i+1],NUM_HANG[i],NUM_HANG[i+1]);
}
}
}
if(a==0x57)
{
HC595_Send(0xff,0xff,0x00,0x00);
for(j=255;j>0;j--) //一个字显示的次数,刷256次然后刷第二个字。
{
for(i=0;i<32;i=i+2) //一共有16行,所以刷16次
{
HC595_Send(~NUM[32+i],~NUM[32+i+1],NUM_HANG[i],NUM_HANG[i+1]);
}
}
}
}
}
/* void delay(uchar z)
{
uchar x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
} */
void ser()interrupt 4
{
while(!RI)
RI=0;
a=SBUF;
flag=1;
}
void HC595_Send(uchar dat1,uchar dat0,uchar dat3,uchar dat2)
{
uchar i;
SCK=1;
LCK=1;
for(i=0;i<8;i++)
{
SDI=dat0&0x01; //从高位到低位
dat0>>=1;
SCK=0;
_nop_();
_nop_();
SCK=1;
_nop_();
_nop_();
}
for(i=0;i<8;i++)
{
SDI=dat1&0x01; //从高位到低位
dat1>>=1;
SCK=0;
_nop_();
_nop_();
SCK=1;
_nop_();
_nop_();
}
for(i=0;i<8;i++)
{
SDI=dat2&0x01; //从高位到低位
dat2>>=1;
SCK=0;
_nop_();
_nop_();
SCK=1;
_nop_();
_nop_();
}
for(i=0;i<8;i++)
{
SDI=dat3&0x01; //从高位到低位
dat3>>=1;
SCK=0;
_nop_();
_nop_();
SCK=1;
_nop_();
_nop_();
}
LCK=0; //下面是输出时钟
_nop_();
_nop_();
LCK=1;
_nop_();
_nop_();
}
好复杂.............。
已解决