⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i2c.c

📁 完全按照645协议编写的RS485通讯程序 (REG764单片机)
💻 C
字号:
/********************************************************************
I2C.C
********************************************************************/
#include "Config.h"           
#include "I2C.h"

#define  _Nop()  _nop_()       

sbit SDA=P1^3;            
sbit SCL=P1^2;            

bit ack;
	         

void Start_I2c()
{
  SDA=1;   
  _Nop();
  SCL=1;
  _Nop();    
  _Nop();
  _Nop();
  _Nop();
  _Nop();    
  SDA=0;   
  _Nop();    
  _Nop();
  _Nop();
  _Nop();
  _Nop();       
  SCL=0;   
  _Nop();
  _Nop();
}


void Stop_I2c()
{
  SDA=0;  
  _Nop();   
  SCL=1;  
  _Nop();
  _Nop();
  _Nop();
  _Nop();
  _Nop();
  SDA=1;  
  _Nop();
  _Nop();
  _Nop();
  _Nop();
}


void  SendByte(uchar c)
{
 uchar BitCnt;
 
 for(BitCnt=0;BitCnt<8;BitCnt++)  
    {
    	
      if((c<<BitCnt)&0x80)
     	 SDA=1;   
      else  
     	 SDA=0;                
      _Nop();
      SCL=1;      
      _Nop(); 
      _Nop();             
      _Nop();
      _Nop();
      _Nop();         
      SCL=0; 
    }
    _Nop();
    _Nop();
    SDA=1;               
    _Nop();
    _Nop();   
    SCL=1;
    _Nop();
    _Nop();
    _Nop();
    if(SDA==1)
     ack=0;     
    else 
     ack=1;        
    SCL=0;
    _Nop();
    _Nop();
}


uchar  RcvByte()
{
  uchar retc;
  uchar BitCnt;
  
  retc=0; 
  SDA=1;             
  for(BitCnt=0;BitCnt<8;BitCnt++)
      {
        _Nop();           
        SCL=0;       
        _Nop();
        _Nop();         
        _Nop();
        _Nop();
        _Nop();
        SCL=1;       
        _Nop();
        _Nop();
        retc=retc<<1;
        if(SDA==1)
        retc=retc+1; 
        _Nop();
        _Nop(); 
      }
  SCL=0;    
  _Nop();
  _Nop();
  return(retc);
}

void Ack_I2c(bit a)
{
  if(a==0)
  	SDA=0;    
  else 
  	SDA=1;
  _Nop();
  _Nop();
  _Nop();      
  SCL=1;
    _Nop();
    _Nop();              
    _Nop();
    _Nop();
    _Nop();  
  SCL=0;                
    _Nop();
    _Nop();    
}

#if 0
bit ISendByte(uchar sla,uchar c)
{
   Start_I2c();               
   SendByte(sla);           
   if(ack==0)
   	return(0);
   SendByte(c);              
   if(ack==0)
   	return(0);
   Stop_I2c();                 
   return(1);
}
#endif

bit ISendStr(uchar sla,uchar suba,uchar *s,uchar no)
{
   uchar i;

   Start_I2c();               
   SendByte(sla);            
     if(ack==0)
	 return(0);
   SendByte(suba);            
     if(ack==0)
	 return(0);

   for(i=0;i<no;i++)
    {   
     SendByte(*s);               
       if(ack==0)
	   return(0);
     s++;
    } 
 Stop_I2c();                
  return(1);
}

#if 0
bit IRcvByte(uchar sla,uchar *c)
{
   Start_I2c();               
   SendByte(sla+1);           
     if(ack==0)
	 return(0);
   *c=RcvByte();               
     Ack_I2c(1);              
  Stop_I2c();                 
  return(1);
}
#endif

bit IRcvStr(uchar sla,uchar suba,uchar *s,uchar no)
{
   uchar i;

   Start_I2c();              
   SendByte(sla);           
     if(ack==0)
	 return(0);
   SendByte(suba);            
     if(ack==0)
	 return(0);

   Start_I2c();
   SendByte(sla+1);
      if(ack==0)
	  return(0);

   for(i=0;i<no-1;i++)
    {   
     *s=RcvByte();               
      Ack_I2c(0);               
     s++;
    } 
   *s=RcvByte();
    Ack_I2c(1);                
  Stop_I2c();                    
  return(1);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -