24c02.c

来自「24c02是一种E2PROM」· C语言 代码 · 共 90 行

C
90
字号
/***********************************************************
*   source file for operation at24c02
*   write by : lp.xiao
*   Rev 1.0  06.9.29                                                           
***********************************************************/

#include "24C02.h"
#include "i2c.h"
void DelayEEPROM()
{
	unsigned int sq0;
	for(sq0=0;sq0<20000;sq0++);
}
/***********************************************************
*   write one byte in at24c02
*   input parameter:1:address 2:data buffer be wrote
*   output parameter: 1:success 0:fail                                                           
***********************************************************/
unsigned char WriteByte(unsigned int addr,unsigned char *pbuf)
{ 
       START;
       if(!SendByte(DEVICE_W)) return 0;
       if(!SendByte(addr)) return 0;
       if(!SendByte(*pbuf)) return 0;
       STOP;
       return 1;
}
/***********************************************************
*   write more than one byte in at24c02 from random beginning address
*   input parameter:1:address 2:data buffer be wrote 3:size be wrote
*   output parameter: 1:success 0:fail                                                            
***********************************************************/
unsigned char WritePage(unsigned int addr,unsigned char *pbuf,unsigned int num)
{  
       START;
       if(!SendByte(DEVICE_W)) return 0;
       if(!SendByte(addr)) return 0;
       for(;num>0;num--)
       {
          if(!SendByte(*pbuf))
            return 0;
          else
            pbuf++;  
       }
       STOP;
	   DelayEEPROM();
       return 1;
}
/***********************************************************
*   Read one byte in at24c02 from random address
*   input parameter:1:address 2:point to data be read
*   output parameter: 1:success 0:fail                                                           
***********************************************************/
unsigned char ReadByte(unsigned int addr,unsigned char *pbuf)
{
       START;
       if(!SendByte(DEVICE_W)) return 0;
       if(!SendByte(addr))  return 0;
       START;
       if(!SendByte(DEVICE_R)) return 0;
       *pbuf = RecByte();
       NOACK;
       STOP;
       return 1;
}
/***********************************************************
*   Read more than one byte in at24c02 from random beginning address
*   input parameter:1:address 2:point to data be read 3:size be read
*   output parameter: 1:success 0:fail                                                          
***********************************************************/
unsigned char ReadSeq(unsigned int addr,unsigned char *pbuf,unsigned int num)
{
       START;
       if(!SendByte(DEVICE_W)) return 0;
       if(!SendByte(addr))  return 0;
       START;
       if(!SendByte(DEVICE_R)) return 0;
       for(;num>0;num--)
       {
          *pbuf = RecByte();
          pbuf++;
          if(num !=1)
          ACK;
          else
          NOACK;
       }
       STOP;
       return 1;
}

⌨️ 快捷键说明

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