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

📄 at24c02.c

📁 W3100是WIZnet公司专门为以太网互联和嵌入式设备推出的硬件TCP/IP协议栈芯片
💻 C
字号:
/*
###############################################################################
 Wiznet.
 5F Simmtech Bldg., 228-3, Nonhyun-dong, Kangnam-gu,
 Seoul, Korea

 (c) Copyright 2002, Wiznet, Seoul, Korea

 Filename : at24c02.c
 Created : 2002/01/29
 Modified :
 Description : Implement I2C I/F using Port #1's No 3(SDA) and No 4(SCL) of 89C51 MCU and Read/Write Serail EEPROM, AT24C02.
                    AT24C02 Size : 2K Bits, 256 Byte, 
	       AT24C02 Address Range : 0x00 ~ 0xFF
	       AT24C02 I/F :  I2C using SDA and SCL
Note : AT24C02 has Address A2-A0 to address several,but it's not implemented in this source.
And AT24C02 has PAGE(8 BYTE) Writing, but it's not implemented in this source and it's possible to implement using this functions.
###############################################################################
*/

/*
###############################################################################
Include Part
###############################################################################
*/
#include "i2c.h" 						// i2c definition file
#include "AT24C02.h"						// Header file

/*
###############################################################################
Function Implementation Part
###############################################################################
*/

/*
Description  :  Check operatiton of EEP_ROM(AT24C02)
	 Write 0xEE value into 0xFE address and 0xFF value into 0xFF address.Read and Verify these value.
Argument     :  
Return Value :  success = 1, fail = 0;
Note         :  
*/
char Check_EEPROM()
{
	char c=0;
	if(EEP_Write(0xFE,0xEE)==0) return 0;	     			// write 0xEE's value into 0x00 address
	if(EEP_Write(0xFF,0xFF)==0) return 0;	     			// write 0xFF's value into 0xFF address
	if(EEP_Read(0xFE,&c, NACK)==0) return 0;			// read 0x00's value
	if(c != (char)0xEE)	return 0;	     			// Verify
	if(EEP_Read(0xFF,&c,NACK)==0) return 0;				// read 0xFF's value
	if(c != (char)0xFF) return 0;		     			// Verify
	return 1;
}

/*
Description  :  Write 1 byte data('d') into appropriate address(addr)
Argument     :  
Return Value :  success = 1, fail = 0;
Note         :  
*/
char EEP_Write(char addr, char d)
{
	GEN_START();  							// Generate "START BIT PATTERN" and inform starting of i2C into AT24C02.
	WriteByte(DEVICE_WR);						// Write device's address defined by AT24C02
	if( WAIT_ACK()!=ACK ) 						// if AT24C02 receive DEVICE_WR or not correctly, wait for ACK
		return 0;

	WriteByte(addr);      						// Inform address to write to AT24C02
	if( WAIT_ACK()!= ACK )						// wait for ACK
		return 0;

	WriteByte(d);	      						// write Data

	if( WAIT_ACK()!= ACK )						// wait for ACK
		return 0;
	GEN_STOP();	      						// Send "STOP BIT Pattern" to AT24C02, Informa the end of I2C.
	Delay(10);
	return 1;
}

/*
Description  :  Write len-sized data string into appropriate address(addr)
Argument     :  addr - address to write into AT24C02(INPUT)
	        d    -  starting address of data string's to write(INPUT)
	        len  - length of data string's  to write(INPUT)
Return Value :  success = 1, fail = 0;
Note         :  
*/
char EEP_WriteBytes(char addr, char* d,int len)
{
	int i;
	for(i = 0; i < len ; i++)					// Log per 1 Byte 
		if(EEP_Write(addr+i,d[i])==0) return 0;
	return 1;
}

/*
Description  :Read from AT24C02 of appropriate address(addr) and Return into 'd' value.
Argument     :  addr - address to be read from AT24C02(INPUT)
		d    - variable to store value to be read (OUTPUT)
		IsContinue - to read sequentially, send ACK instead of NACK. Flag's value:ACK-->ACK, NACK-->NACK (INPUT)
Return Value :  success = 1, fail = 0;
Note         :  
*/
char EEP_Read(char addr, char* d,char IsContinue)
{
	GEN_START();
	WriteByte(DEVICE_WR);	 					// Write device's address defined by AT24C02 to write
	if( WAIT_ACK()!= ACK )	
		return 0;
	WriteByte(addr);	 					// Inform address to write to AT24C02 actually
	if( WAIT_ACK()!= ACK )
		return 0;        
	GEN_START();							// Inform starting of Read
	WriteByte(DEVICE_RD);						// Write device's address defined by AT24C02 to read
	if( WAIT_ACK()!= ACK )
		return 0;
	*d = ReadByte(); 
	if(IsContinue == NACK )
	{
		if(WAIT_ACK()!=NACK) return 0;
		GEN_STOP();		      				// If it's read just 1Byte,inform all operations are finished.
	}                           
	else								// If it's read continuously, do without STOP
		SEND_ACK(IsContinue);					// If it's read continuously, then ACK.Else if just 1Byte, then NACK.
	return 1;
}

/*
Description   :  Read each 'len' length from AT24C02 of appropriate address(addr) and Write into 'd' and Return
Argument      :  addr - Address to be read from AT24C02(INPUT)
	    	 d    - starting address variable to be stored data string's value to be Read(INPUT)
		 len  - data string's length to Read(INPUT)
Return Value  :  success = 1, fail = 0;
Note          : It's implemented according to Datasheet,but can't be read in sequential.So read each 1 Byte repeatedly.
*/
char EEP_ReadBytes(char addr, char* d, int len)
{
	char c;
	int i ;

	for(i = 0 ; i < len ; i++)					// Read each 1 byte and store into 'd' sequentially.
	{
		if(EEP_Read(addr+i,&c,NACK)==0)
			return 0;
		d[i] = c;
	}
/*
	if(!EEP_Read(addr,&c,ACK)) return 0;				// 1Byte绰 Read 1Byte according to I2C Spec(ACK instead of NACK, Read 1 Byte without STOP)
	d[0] = c;
	for(i = 1 ; i < len ; i++)					// Read 1 Byte through I2C continuously and wait for ACK
	{
		d[i] = ReadByte();
		PutHTOA(d[i]);PutStringLn("");                          
		if(i == len -1 )					// If read last Byte, don't wait for ACK and send NACK
		{
			if(WAIT_ACK()!=NACK) return 0;
		}
		else	SEND_ACK(ACK);
	}
	GEN_STOP();							// All operations are finished.
*/
	return 1;
}

⌨️ 快捷键说明

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