pc机和单片机的串行通信怎样实现!

如题所述

pc机是上位机,单片机是下位机,首先确定单片机同pc连接号232
接口的或者usb转232 视你的单片机类型而定下载个软件串口调试助手或者windows自带的超级终端调试,写好的程序将字符串发送给pc机或者用软件将字符串写如单片机通过数码管或者液晶显示,其中牵涉通讯协议和串口设置,程序生活如歌如上已经说了,可以借鉴下。是在没有明了,找些串口调试的资料在单片机开发版里面,许多都带了!
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-11-01
看这个例子,PC用个串口助手或者超级终端。
/*************** writer:shopping.w ******************/
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar Receive_Buffer[101];
uchar Buf_Index = 0;
uchar code DSY_CODE[]=
{
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00
};

void Delay(uint x)
{
uchar i;
while(x--)
{
for(i=0;i<120;i++);
}
}

void main()
{
uchar i;
P0 = 0x00;
Receive_Buffer[0]=i;
SCON = 0x50;
TMOD = 0x20;
PCON = 0x00;
TH1 = 0xfd;
TL1 = 0xfd;
EA = 1;
EX0 = 1;
IT0 = 1;
ES = 1;
IP = 0x01;
TR1 = 1;
while(1)
{
for(i=0;i<100;i++)
{
if(Receive_Buffer[i]==-1)
break;
P0 = DSY_CODE[Receive_Buffer[i]];
Delay(200);
}
Delay(200);
}
}

void Serial_INT() interrupt 4
{
uchar c;
if(RI==0)
return;
ES = 0;
RI = 0;
c = SBUF;
if(c>='0' && c<='9')
{
Receive_Buffer[Buf_Index]=c-'0';
Receive_Buffer[Buf_Index+1]=-1;
Buf_Index = (Buf_Index+1)%100;
}
ES = 1;
}

void EX_INT0() interrupt 0
{
uchar *s = ("Receiving From 8051...\r\n");
uchar i = 0;
while(s[i]!='\0')
{
SBUF = s[i];
while(TI == 0);
TI = 0;
i++;
}
}
第2个回答  2011-11-01
一般的单片机的串口输出的都是TTL电平,需要外接RS232通讯芯片,将其转换为CMOS电平,才能和PC机进行通讯。本回答被提问者采纳
相似回答