新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > 单片机与pc串口通信-----下位机程序

单片机与pc串口通信-----下位机程序

作者:时间:2016-08-03来源:网络收藏

  //MCS-51通过中断方式接收pc机发来的字符,并回送给主机

本文引用地址:http://www.eepw.com.cn/article/201608/294951.htm

  #include

  #include

  #define uchar unsigned char

  uchar xdata rt_buf[32];

  uchar r_in,t_out;

  bit r_full,t_empty;

  serial() interrupt 4 //中断程序

  {

  if(RI && r_full)

  {

  rt_buf[r_in]=SBUF;

  RI=0;

  if(rt_buf[r_in]==0x24)

  {

  r_full=1;

  SBUF=rt_buf[t_out];

  t_empty=0;

  }

  /*接收字符为$,则接收结束;设置接收结束标志,开始发送*/

  r_in=++r_in;

  }

  else if(TI && t_empty)

  {

  TI=0;

  t_out=++t_out;

  SBUF=rt_buf[t_out];

  if(t_out==r_in)

  t_empty=1;

  /*t_out=r_in则发送完,设发送完标志t_empty*/

  }

  }

  main()

  {

  /*设置定时器T1工作于方式2,计数常数为0xfdH */

  TMOD=0x20; //TMOD:GATE C/T1 M1 M0 GATE C/T0 M1 M0 0010 0000 以TR启动定时

  TL1=0xfd; //定时3us

  TH1=0xfd;

  SCON=0x50; //在11.0592MHz下,设置串行口波特率为9600,方式1,允许接收 01010000

  PCON=0xD0; // 11010000 波特率加倍

  IE=0x10; //00010000 允许串行中断ES=1

  TR1=1;

  EA=1;

  r_in=t_out=0;

  t_empty =1;

  r_full=0;

  for(;;)

  {

  printf ("%s","serial communicationn"); //添加自己的程序处理代码

  }

  }

  附上: proteus下和pc机的连接图

  



关键词: 单片机 串口

评论


相关推荐

技术专区

关闭