#include "config.h"
#define DATA_OUT RB0
#define DATA_IN  RB1
//发码位数
uchar SendBitNum = 0;
//每位码时间
uchar SendBitDly = 0;
//发码标志位寄存器
uchar SendFlag = 0;
//发码数据寄存器
uint SendData = 0;
bit SendEn =0;
bit D0=0;
uchar SendWord(void)
{
  //是否允许发送
if(SendEn==0)
 {
   DATA_OUT =1;
   if(--SendBitDly>0) return 0 ;
   //重新发送数据
   D0=0;
   SendBitDly =1;
   //8*2+2
   SendBitNum =18;
   SendEn =1;
   //更新发送数据
   return 0;
 }
//发送1位数据 
if(--SendBitDly>0)
  {
   //发送数据位D0=1
   if(D0)
    {
      if(SendBitDly<9)
      {
        DATA_OUT= 0 ;
        return 0;
      } 
      DATA_OUT=1;
      return 0;
    } 
    //发送数据位D0=1
    if(SendBitDly<4)
     {
       DATA_OUT= 0 ;
       return 0;
     } 
    DATA_OUT=1;
    return 0;
   } 
//发送完1位数据
 SendBitDly=11;
//发送下1位 SendData 是一个16位的数据
  if(SendData&0x8000)  D0=1;
  else                 D0=0;
  //移位
  SendData<<=1;     
  //位数减少
  SendBitNum --;    
  //位数没有发完,继续发送 
  if(SendBitNum) return 0;
  //发完固定位数后
  SendBitDly =29;//延时28*125uS=3500us=3.5ms
  SendEn= 0;
  return 0;
}
//读取数据
uchar RHighDly=0;
uchar RLowDly=0;
bit Rold=0;//保存上一次收码口的状态
uchar RCnt=0;
uint WORD0=0;
uint WORD1=0;
uchar ReadWord(void)
{
  //接收到高电平
  if(DATA_IN)
  {
    RHighDly++;
    Rold = 1;
    if(RHighDly<17) return 0;
    //超过3ms重新接收
    RLowDly = 0;
    RCnt=32;
    return 0;
  }
//接收到低电平
REC_L:
  //计数低电平
  RLowDly++;
  if(Rold==0) 
  {
      RHighDly = 0;
      return 0;
  }    
 //REC_H_L : Rold=1;
 //保存本次电平 ROLD=0
 Rold=0;
 //跳转到第一次下降沿处理
 if(RLowDly==1) //goto REC_FST;
    {
       RCnt=32;
       WORD0=0;
       WORD1=0;
       return 0;
    }
  //低电平大于高电平时间为数据1,否则为0;
  //word0低16位 word1高16位
  WORD1<<=1; 
  if(WORD0&0x8000)
    {
      WORD1 |=1;
    }
  WORD0<<=1; 
  if(RLowDly>RHighDly)WORD0 |=1;
  //结束本次比较
  RHighDly=0;
  RLowDly=0;
  //重新计数
  RLowDly++;
  //接收下一位数据
 if(--RCnt!=0)    return 0;
//清零位计数器
 RCnt=0;
 //读取并保存数据
 return 0;
}
串口通信IO口模拟

串口通信IO口模拟

串口通信IO口模拟

串口通信IO口模拟

串口通信IO口模拟

串口通信IO口模拟

 

串口通信IO口模拟串口通信IO口模拟

相关文章:

  • 2021-04-27
  • 2021-09-12
  • 2021-11-18
  • 2021-09-24
  • 2021-11-19
猜你喜欢
  • 2021-09-22
  • 2021-05-14
  • 2021-12-08
  • 2022-12-23
  • 2021-11-28
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案