串行通信实验

一、   实验题目

甲乙两个单片机进行串行通信。采用12MHZ时钟频率晶振频率和方式1进行通信。甲机上有4*4键盘、一个七段数码管,乙机上有两个七段数码管;甲机发出按键显示内容,乙机接收后在数码管上交替显示。

二、  实验目的

理解单片机串行口实现通信的各种工作方式

掌握单片机串行通信程序设计、调试方法

三、  实验设备

DELL台式机 + proteus版本6 + keilC51版本7

四、  实验电路图(可打印)

 

 

五、  程序流程图

 

 

六、  程序源码(可打印)

甲机:

#include<reg51.h>

#define uchar unsigned char

#define uint unsigned int

uchar dspcode[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,

0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

void dlms()

{

uchar i;

for(i=125;i>0;i--);

}

uchar kbscan()

{

uchar sccode,recode,ret;

P1=0xf0;

if((P1&0xf0)!=0xf0)

{

dlms();

if((P1&0xf0)!=0xf0)

{

sccode=0xfe;

while((sccode&0x10)!=0)

{

P1=sccode;

if((P1&0xf0)!=0xf0)

{

recode=P1&0xf0;

sccode=sccode&0x0f;

switch(sccode)

{

case 0x0e: ret=0;break;

case 0x0d: ret=4;break;

case 0x0b: ret=8;break;

case 0x07: ret=12;break;

}

switch(recode)

{

case 0xe0: ret=ret+1;break;

case 0xd0: ret=ret+2;break;

case 0xb0: ret=ret+3;break;

case 0x70: ret=ret+4;break;

}

return ret;

}

else

sccode=(sccode<<1)|0x01;

}

}

}

return 0;

}

void main()

{

uchar c;

while(1)

{

P1=0xf0;

if((P1&0xf0)!=0xf0)

{

c=kbscan();

if(c!=0)

{

P0=~dspcode[c-1];

TMOD=0x20;

TH1=0xe6;

TL1=0xe6;

TR1=1;

SCON=0x40;

SBUF=c;

while(!TI);

TI=0;

TR1=0;

P1=0xf0;

while((P1&0xf0)!=0xf0);

}

}

}

}

乙机

#include<reg51.h>

#define uchar unsigned char

#define uint unsigned int

uchar c1=0,c2=0;

uchar i=0;

uchar dspcode[17]={0x00,0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,

0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

void interrupt4() interrupt 4

{

RI=0;

c2=c1;

c1=SBUF;

}

void main()

{

EA=1;

ES=1;

TMOD=0x20;

TH1=0xe6;

TL1=0xe6;

TR1=1;

SCON=0x50;

while(1)

{

P0=~dspcode[c1];

P2=~dspcode[c2];

}

}

 

posted @ 2015-12-22 13:31  hamburger  阅读(1194)  评论(0编辑  收藏  举报