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

📄 at24xx.c

📁 仿真AT24CXX的KEIL C51源程序
💻 C
字号:
//#include "acval.h"
//nclude "acpro.h"
/*
void main(void)
{    
        ddr=0x0;
	addr1=0x03;
	TMPL=0XA3;
 	while(1)
 	{
		writebyte(addr,TMPL);
		TMPH=readbyte(addr1);
	};
}
void ACK()
{
    PDIRAT24XX=PDIRAT24XXIN;
    P1PUEN=P124PU1;
    _nop_();
    _nop_();
    SCL=0;
    _nop_();
    _nop_();
    _nop_();
    SCL=1;//SDA=1;
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    SCL=0;
    _nop_();
}

bit startbit()
{
    PDIRAT24XX=PDIRAT24XXOUT;
    P1PUEN=P124PU0;
    SDA=1;
    SCL=1;
	_nop_();
   	_nop_();
	_nop_();
	_nop_();
	_nop_();
	SDA=0;
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	SCL=0;
	return 1;
}

void stopbit()
{
    PDIRAT24XX=PDIRAT24XXOUT;
    P1PUEN=P124PU0;
    SDA=0;
    _nop_();
    _nop_();
    SCL=1;
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    SDA=1;
}

void write8bit(unsigned char ch)
{
	unsigned char i;
    PDIRAT24XX=PDIRAT24XXOUT;
    P1PUEN=P124PU0;
    for (i=1;i<=8;i++)
    {
        SCL=0;
        _nop_();
			if((ch&0x80))
			{
				SDA=1;
			}
         else
         {
         	SDA=0;
         }
        _nop_();
        SCL=1;
        ch<<=1;
    }
    _nop_();
    _nop_();
	SCL=0;    
}


void writebyte(unsigned char address,unsigned char ddata)
{    
	if (startbit())
    {
        write8bit(0xa0);        
        ACK();                    
        write8bit(address&0x00ff);
        ACK();
        write8bit(ddata);
        ACK();
        stopbit();
    }
}

unsigned char readbyte(unsigned char address)
{    
	unsigned char i,ddata;
    startbit();
    write8bit(0xa0);           
    ACK();                    
    write8bit(address&0x00FF);
    ACK();
		startbit();
    
    write8bit(0xa1);
    ACK();
    _nop_();
    PDIRAT24XX=PDIRAT24XXIN;
    P1PUEN=P124PU1;
    _nop_();
   i=8;
    while (i--)
    {
    	SCL=0;
    	ddata<<=1;                
    	_nop_();
    	_nop_();
    	_nop_();
    	_nop_();
    	_nop_();
    	SCL=1;
	    _nop_();
	    _nop_();
	    _nop_();
	    _nop_();
	    _nop_();
	    _nop_();
	    _nop_();
	    _nop_();
	    _nop_();
    	if (SDA)
    	{ 
    		ddata|=0x01;    
    	}
    }
    _nop_();
    PDIRAT24XX=PDIRAT24XXOUT;
    P1PUEN=P124PU0;
    _nop_();
	 _nop_();
	SCL=1;
    _nop_();
	 _nop_();
	SCL=0;
    _nop_();
	 _nop_();
    stopbit();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    return ddata;
} 

  */
//#include "reg52.h"
#include <AT89X51.H> 
//#include "pin_io.h"
#include "types.h"
#include "i2c.h"
#include "misc.h"

#include "bd_customer.h"

#define i2cSlaveAddr(deviceID, addr)	deviceID|((addr>>8)<<1)
#define i2cWordAddr(addr)		addr&0xFF
#define I2cErrTolerance 20

int     addr,addr1,TMPL;
char wtval[10],rdval[10];

#define AT24C01 0XA0
 
void main(void)
{                                       
        addr=0x0;
	addr1=0x03;
	TMPL=0XA3;
        wtval[0]=1;
 	while(1)
 	{
                i2c_WriteTBL(AT24C01, addr,wtval, 1);
                i2c_ReadTBL(AT24C01, addr, rdval, 1);
	//	writebyte(addr,TMPL);
	//	TMPH=readbyte(addr1);
	};
}

//============================================
// Setup i2c Start condition
Bool i2c_Start(void)
{ Set_i2c_SDA();
  Delay4us();
  Set_i2c_SCL();
  Delay4us();
  if (i2c_SDALo() || i2c_SCLLo())
    return FALSE;
  Clr_i2c_SDA();
  Delay4us();
  Clr_i2c_SCL();
  return TRUE;
}
//=============================================
// Setup i2c Stop condition
void i2c_Stop(void)
{ // SCL=L, SDA=L, Stop condition.
  Clr_i2c_SCL();
  Delay4us();
  Clr_i2c_SDA();
  Delay4us();
  Set_i2c_SCL();
  Delay4us();
  Set_i2c_SDA();
}

//============================================
Bool i2c_SendByte(BYTE value)
{ BYTE i;
  Bool result;

  for (i=0; i<8; i++) // Send data via i2c pin
    { if (value&BIT7)
        Set_i2c_SDA();
      else
      	Clr_i2c_SDA();
      Delay4us();
      Set_i2c_SCL();
      value<<=1;
      Clr_i2c_SCL();
    }
  Set_i2c_SDA();
  Delay4us();
  Set_i2c_SCL();
  result=i2c_SDALo(); // see if ACK
  Clr_i2c_SCL();
  Delay4us();
  Clr_i2c_SDA();

  return result;
}

//============================================
BYTE i2c_ReceiveByte(const Bool ack)
{ BYTE i;
  BYTE value;

  for (i=0; i<8; i++)
    { value<<=1;
      Set_i2c_SDA();
      Set_i2c_SCL();
      Delay4us();
      if (i2c_SDAHi())
      	value|=BIT0;
      Clr_i2c_SCL();
    }
  if (ack)
    Clr_i2c_SDA();
  else
    Set_i2c_SDA();
  Delay4us();
  Set_i2c_SCL();
  Delay4us();
  Clr_i2c_SCL();

  return value;
}

Bool i2c_BurstWrite(BYTE count, BYTE *buffer)
{ while (count--)
    { if (i2c_SendByte(*(buffer++))==FALSE)
        return FALSE;
    }
 return TRUE;
}
Bool i2c_BurstRead(BYTE count, BYTE * buffer)
{ BYTE i;

  for (i=0; i<count-1; i++)
    *(buffer+i)=i2c_ReceiveByte(1);
  *(buffer+i)=i2c_ReceiveByte(0);
  i2c_Stop();
  return TRUE;
}

Bool i2c_MasterStart(I2C_Direction direct, BYTE addr)
{ BYTE retry=5;

  if (direct==I2C_READ) // Set I2C direction bit.
    addr|=BIT0;
  else
    addr&=~BIT0;

  while (retry--)
    { if (i2c_Start()==FALSE)
        continue;

      if (i2c_SendByte(addr)==TRUE) // send address success
        return TRUE;
      i2c_Stop();
      ForceDelay1ms(1);
    }
  return FALSE;
}


void i2c_WriteTBL(BYTE deviceID, WORD addr, BYTE *buffer, BYTE count)
{ BYTE slaveAddr;
  BYTE wordAddr;
  BYTE offset=0;
  BYTE tempSize;
  Bool succ=TRUE;
  BYTE ErrCount=0;

  while (count)
    { slaveAddr=i2cSlaveAddr(deviceID, addr+offset);
      wordAddr=i2cWordAddr(addr+offset);
      if (i2c_MasterStart(I2C_WRITE, slaveAddr)==FALSE)
        {
        if(ErrCount <I2cErrTolerance)
          {
          ErrCount++;
          //ForceDelay1ms(1);
          i2c_Stop();
          continue;
          }
      	break;
      	}
      if (i2c_BurstWrite(1, &wordAddr)==FALSE)
      	break;
      tempSize=0x10-(wordAddr&0xF);
      if (count>tempSize)
      	{ if (i2c_BurstWrite(tempSize, (BYTE*)(buffer+offset))==FALSE)
      	    break;
      	  i2c_Stop();
      	  count-=tempSize;
      	  offset+=tempSize;
      	}
      else if (count>0x10)
      	{ if (i2c_BurstWrite(0x10, (BYTE*)(buffer+offset))==FALSE)
            break;
      	  i2c_Stop();
      	  count-=0x10;
      	  offset+=0x10;
      	}
      else
      	{ if (i2c_BurstWrite(count, (BYTE*)(buffer+offset))==FALSE)
      	    break;
      	  i2c_Stop();
          count=0;
      	}
      ErrCount = 0;
      //ForceDelay1ms(1);
    }
  i2c_Stop();
}

void i2c_ReadTBL(BYTE deviceID, WORD addr, BYTE *buffer, BYTE count)
{ WORD retry=10;
  BYTE slaveAddr;
  BYTE wordAddr;

  slaveAddr=i2cSlaveAddr(deviceID, addr);
  wordAddr=i2cWordAddr(addr);
  while (retry--)
    { if (i2c_MasterStart(I2C_WRITE, slaveAddr)==FALSE)
        {
        //ForceDelay1ms(2);
        i2c_Stop();
        continue;
        }
      if (i2c_BurstWrite(1, &wordAddr)==FALSE)
      	continue;
      if (i2c_MasterStart(I2C_READ, slaveAddr)==FALSE)
        continue;
      if (i2c_BurstRead(count, buffer)==FALSE)
      	continue;
      return;
    }
}

#if 0
void i2c_WriteByte(BYTE deviceID, WORD addr, BYTE *value)
{ i2c_WriteTBL(deviceID, addr, value, 1);
}
void i2c_ReadByte(BYTE deviceID, WORD addr, BYTE *value)
{ i2c_ReadTBL(deviceID, addr, value, 1);
}
#endif

void Delay4us(void)
{
#if CPU_CLOCK_MHZ>=20
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();
#endif
}

#define DelayPeriod	((WORD)CPU_CLOCK_MHZ*78/12)

void ForceDelay1ms(WORD msNums)
{ WORD t;

  if (msNums>0)
    { while (msNums--)
        { t=DelayPeriod;
          while (t--);
        }
    }
}

⌨️ 快捷键说明

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