I2C驱动程序

仅供参考~~

#define  _I2C_C_
#include "include_all.h"

#define  NOPS();{_nop_();_nop_();_nop_();_nop_();_nop_();}

 
sbit SCL = P3^4; //pin16
sbit SDA = P3^5;    //pin17
sbit TLED = P3^6;

unsigned char SystemError;

void Delay(int TIME)
{
   while(TIME--);
}

//---------------------------------
//  Function: void I2C_Start(void)
//  Discrption:
//----------------------------------
void I2C_Start(void)
{
  SDA = 1;
  SCL = 1;
  NOPS(); //Initial
  SDA = 0;
  NOPS();       //START
  SCL = 0;
}

//---------------------------------
//  Function: void I2C_Stop(void)
//  Discrption:
//----------------------------------
void I2C_Stop(void)
{
  SCL = 0;
  SDA = 0;
  NOPS();     //Initial
  SCL = 1;
  NOPS();       //STOP  
  SDA = 1;
}

//---------------------------------
//  Function: void I2C_Start(void)
//  Discrption:
//----------------------------------

WaitAck(void)
{
  unsigned char errtime = 255;
  SDA = 1;
  NOPS();
  SCL = 1;
  NOPS();
  SystemError = 0x10;
  while(SDA)
  {
    errtime--;
    if(!errtime)
    {
      I2C_Stop();
      SystemError = 0x11;
      return;
    }
  }
  SCL = 0;
}

//---------------------------------
//  Function: void SendAck(void)
//  Discrption:
//----------------------------------
void SendAck(void)
{
  SDA =0 ;
  NOPS();
  SCL = 1;
  NOPS();
  SCL = 0;
}

//---------------------------------
//  Function: void SendNotAck(void)
//  Discrption:
//----------------------------------
void SendNotAck(void)
{
  SDA =1 ;
  NOPS();
  SCL = 1;
  NOPS();
  SCL = 0;
}

//-----------------------------------------------------
//  Function: void I2CSendByte(unsigned char send_data)
//  Discrption:
//-----------------------------------------------------
void I2CSendByte(unsigned char send_data)
{
  unsigned char count = 8;
  for(count = 0;count<8;count++)
  {
    SCL = 0;
    _nop_();
   SDA = (bit)(send_data & 0x80);
   send_data <<= 1;
   NOPS();
   SCL = 1;
   NOPS(); 
  }
  SCL = 0;
}

//-----------------------------------------------------
//  Function: unsigned char I2CReceiveByte(void)
//  Discrption:
//-----------------------------------------------------
unsigned char I2CReceiveByte(void)
{
  unsigned char count = 8;
  unsigned char dat = 0;  
  SDA = 1;
  for(count = 0;count<8;count++)
  {  
   dat <<= 1;
   SCL = 0;
   NOPS();
   SCL = 1;
   NOPS();
   dat |= SDA;   
  }
  SCL = 0;
  return(dat);
}

//--------------------------------------------------------------------------
//  Function: void WriteCharToChip(unsigned int address,unsigned char dat)
//  Discrption:
//--------------------------------------------------------------------------
void WriteCharToChip(unsigned int address,unsigned char dat)
{
  I2C_Start();
  I2CSendByte(0xA0);
  WaitAck();
  I2CSendByte((char)(address)>>8);  //????AT24C02,AT24C64,AT24C128,AT24C256??,??????????
  Delay(300);
  WaitAck();
  I2CSendByte((char)(address));
  WaitAck(); 
  I2CSendByte(dat);
  Delay(300);
  WaitAck();
  I2C_Stop(); 
}
//--------------------------------------------------------------------------
//  Function: unsigned char ReadCharFromChip(unsigned int Address)
//  Discrption:
//--------------------------------------------------------------------------
unsigned char ReadCharFromChip(unsigned int Address)
{
   unsigned char recv_data;
   I2C_Start();
   I2CSendByte(0xA0);
   WaitAck();
   I2CSendByte((char)(Address>>8));//????AT24C02,AT24C64,AT24C128,AT24C256??,??????????
   WaitAck();
   I2CSendByte((char)(Address));
   WaitAck();
   I2C_Start();
   I2CSendByte(0xA1);
   WaitAck();
   recv_data = I2CReceiveByte();
   SendNotAck();
   I2C_Stop();
   return(recv_data); 
}

main()
{
  unsigned char Read_data;
  for(;;)
  {
    WriteCharToChip(0x10,0xA3);
    Read_data = ReadCharFromChip(0x10);
  }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值