模拟串口的实现--单片机IO口

用EBOX向我们的电力线调试模块发数据,但是要有一个控制接口,但是EBOX没有提供,所有只好用MCU来做伺服器。

一般的单片机都只有一个串口,所以必须模拟一个出来。

 

/*

  sbit TXD1  = P1^4;                  //define p14 as the analog transmit port

  sbit RXD1  = P1^2;                  //define p12 as the analog recieve port

*/

#include "REG52.H"

#define uint unsigned int

#define uchar unsigned char

#include <intrins.h>

 void WaitTF0(void);

 

 void TIMEINI(void)               //counter initial

  {

    TMOD=0x02;

    TH0=0xA0;                           //9600 BPS

       TL0=TH0;

       TR0=1;

       TF0=0;

   }

 

 

   void WByte(uchar input)           //  send function

   {

         uchar i=8;

              TR0=1;

              TXD1=0;                                          //begin bit

              WaitTF0();

                                                 //send the Byte

           while(i--)

            {

                     TXD1=input&0x01;                    //send Byte from low bit to high bit

 

                      WaitTF0();                               

                      input=input>>1;                     //right shift

          }

               TXD1=1;                                         //stop bit

              WaitTF0();

              TR0=0;                                           //stop the counter

   }

   

       uchar RByte()            //the recieve function

   {

         uchar Output=0;

          uchar i=8;

                                      

               while(RXD1);                          

                    i=i;

                     i=i;

                     i=i;

                     i=i;

                     i=i;

                     i=i;

                     i=i;

                     TR0=1;                                 //计数器开始工作

                  WaitTF0();

                                                       

           while(i--)                                                           // 接收8位数据位

            {

                      Output>>=1;

                      if(RXD1)Output=Output|0x80;   //recieve from the high bit to low bit

             WaitTF0();

          }

      

              TR0=0;

               return  Output;

   }

 

  void WaitTF0(void)                         //check the counter

  {

             while(!TF0);

             TF0=0;

  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值