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

📄 i2c_0.c

📁 这是一个实用的I2C应用实例
💻 C
字号:


#include<reg51.h>
#include<math.h>
#include<stdio.h>
#include<intrins.h>
#include<string.h>

#define uint unsigned int
#define uchar unsigned char

#define _Nop() _nop_()

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

bit ACK;

void I2C_Start();
void I2C_Stop();
void SendByte(uchar);
void ACK_I2C(bit);
bit ISendByte(uchar,uchar);
bit IRcvByte(uchar,uchar *);
bit SendStr(uchar,uchar,uchar *,uchar);
bit IRcvStr(uchar,uchar,uchar *,uchar);
uchar RcvByte();

void main()
{
      I2C_Start();
	  I2C_Stop();
}
//*****************************************************************
void I2C_Start()
{
      SDA=1;              //send the star data singal
	 _Nop();

	  SCL=1;              //the star time delay>=4.7us
	 _Nop();
     _Nop();
	 _Nop();
	 _Nop();
	 _Nop();

	  SDA=0;              //send the star singal
	 _Nop();             //the star lock time>>4us
     _Nop();
	 _Nop();
	 _Nop();
	 _Nop();

	  SCL=0;             //lock the I2C bus,star to send or receive
	 _Nop();
	 _Nop();

}
//******************************************************************
void I2C_Stop()
{
      SDA=0;            //send the stop singal
	 _Nop();

	  SCL=1;            //send the stop time>>4us
	 _Nop();
     _Nop();
	 _Nop();
	 _Nop();
	 _Nop();

	  SDA=1;           //send I2C stop bus singal
     _Nop();
	 _Nop();
	 _Nop();
	 _Nop();
}
//******************************************************************
void SendByte(uchar C)
{
     uchar BitCount;
	 
	 for(BitCount=0;BitCount<8;BitCount++)
	 {
	      if((C<<BitCount)&0x80)
		       SDA=1;
		  else
		       SDA=0;
		 _Nop();

		  SCL=1;         //set the SCL high>>4us,call the beikong to receice
		 _Nop();
         _Nop();
	     _Nop();
	     _Nop();
	     _Nop();

		  SCL=0;
	 }
	 _Nop();
	 _Nop();

	  SDA=1;             //8 bit is send finish,wati for the ack
	 _Nop();
	 _Nop();

	  SCL=1;
	 _Nop();
	 _Nop();
	 _Nop();

	  if(SDA==1)
	       ACK=0;
	  else
	       ACK=1;

	  SCL=0;
	 _Nop();
	 _Nop();
}
//******************************************************************
uchar RcvByte()
{
     uchar retc;
	 uchar BitCount;

	 retc=0;

	 SDA=1;             //set to the input 
	 
	 for(BitCount=0;BitCount<8;BitCount++)
	 {
	      _Nop();

		   SCL=0;        //SCL low>>4.7us ,wang to receive data
		  _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
	      SCL=1;
	 _Nop();
	 _Nop();
	 _Nop();

	  SCL=1;            //SCL=high>=4us
	 _Nop();
	 _Nop();
	 _Nop();
	 _Nop();
	 _Nop();

	  SCL=0;
	 _Nop();
	 _Nop();
}
//***************************************************************
bit ISendByte(uchar sla,uchar c)
{
     I2C_Start();
	 SendByte(sla);
	 
	 if(ACK==0)
	 return(0);

	 SendByte(c);

	 if(ACK==0)
	 return(0);

	 I2C_Stop();

	 return(1);
}
//************************************************************
bit IRcvByte(uchar sla,uchar *c)
{
     I2C_Start();
     SendByte(sla+1);

	 if(ACK==0)
	 return(0);

	 *c=RcvByte();
	 ACK_I2C(1);
	 I2C_Stop();

	 return(1);
}
//***********************************************************
bit SendStr(uchar sla,uchar suba,uchar *s,uchar n0)
{
     uchar i;

	 I2C_Start();
	 SendByte(sla);

	 if(ACK==0)
	 return(0);

	 SendByte(suba);

	 if(ACK==0)
	 return(0);

	 for(i=0;i<n0;i++)
	 {
	      SendByte(*s);

		  if(ACK==0);
          return(0);

		  s++;
	 }
	 I2C_Stop();
	 return(1);
}
//**************************************************************
bit IRcvStr(uchar sla,uchar suba,uchar *s,uchar n0)
{
     uchar i;

	 I2C_Start();
	 SendByte(sla);

	 if(ACK==0);
	 return(0);

	 SendByte(suba);

	 if(ACK==0)
	 return(0);

	 I2C_Start();
	 SendByte(sla+1);

	 if(ACK==0)
	 return(0);

	 for(i=0;i<n0-1;i++)
	 {
          *s=RcvByte();
		  ACK_I2C(0);
	 }
     *s=RcvByte();
	 ACK_I2C(1);
	 I2C_Stop();
	 return(1);

}

⌨️ 快捷键说明

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