求单片机串行口通信的发送和接收程序
要求用挂仿真器的单片机发送,用keil c51语言写
电脑com1口接收用turbo c2.0写
我自己写了一个,但不知道哪里错了,希望能指点一下,或直接帖一个新的代码上来
/*微机串行口发送接收程序,用TURBO C编译*/
#include<bios.h>
#include<stdio.h>
#define PROTOCAL 0xe3 /*定义RS232通信协议,波特率*/
/*bioscom控制函数的工作命令*/
#define PROT 0
#define TX 1
#define RX 2
#define STATUS 3
int port=0; /*系统通信端口使用COM1*/
void showby(unsigned char c)
{
unsigned int a=0x80;
int i,j;
int b=c;
for(i=8;i>0;i--)
{
j=a&b;
if(j==0)
printf("0");
else
printf("1");
a=a>>1;
}
printf("\n");
}
main()
{
int s;
unsigned char c;
/*设定RS232通信协议*/
showby('1');
bioscom(PROT,PROTOCAL,port);
clrscr();
puts("--------------------");
puts("test.ext PC RS232 COM1 <9,600 N 8 1>");
puts("--------------------");
while(1)
{
s=bioscom(STATUS,0,port)&0x100;
if(s)
{
c=bioscom(RX,0,port);
showby(c);
printf("%c",c); /*显示在屏幕上*/
}
if(kbhit())
{
c=getch(); /*读取按键*/
switch(c)
{
case ' ': /*空白键清屏*/
clrscr();
puts("--------------------");
puts("test.ext PC RS232 COM1 <9,600 N 8 1>");
puts("--------------------");
break;
case 27: /*ESC*/
exit(0);
break;
default:
printf("%c",c);
bioscom(TX,c,port);
break;
}
}
}
}