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

📄 at24c64.c

📁 AT24C64 Keil C 代码
💻 C
字号:
#include <reg52.h>
#include <absacc.h>

#define	bool		bit
#define 	ByteU 	unsigned char
#define 	WordU	unsigned int

sbit P16 = P1^6;
sbit P17 = P1^7;

#define 	SCL			P16
#define 	SDA			P17


void 		Delay(ByteU Times);
void 		DelayNms(ByteU Times);
bool		IICStart(void);
void 		IICStop(void);
void 		IICAck(void);
void 		IICNack(void);
bool 		IICSendByte(ByteU Data);
ByteU	IICReceiveByte(void);
void 		AT24C64_W(void *mcu_address,WordU AT24C64_address,WordU count);
void 		AT24C64_R(void *mcu_address,WordU AT24C64_address,WordU count);

ByteU	pdata DataIn[10] = {0,1,2,3,4,5,6,7,8,9};
ByteU	idata DataOut[10];
void main(void)
{
//	bool	Flag;
	ByteU Count;
//	ByteU ReadData;
	ByteU ReadData[33];
	ByteU GetRamData[33];
	ByteU testI;
/*
	IICStart();			//Write One Byte Data;
	IICSendByte(0xa0);
	IICSendByte(0x0);
	IICSendByte(0x0);
	IICSendByte(0xaa);
	IICStop();

	Delay(158);
		
	IICStart();			//Read One Byte Data;
	IICSendByte(0xa0);
	IICSendByte(0x0);
	IICSendByte(0x0);

	IICStart();			
	IICSendByte(0xa1);
	ReadData = IICReceiveByte();
	IICStop();
*/
//	Count = 0xff;
	for(Count=0;Count<32;Count++)
		XBYTE[0x0000+Count] = Count;
	for(Count=0;Count<32;Count++)
		GetRamData[Count] = XBYTE[0x0000+Count];
	for(Count=0;Count<32;Count++)
		ReadData[Count] = 0x0;
/*
	IICStart();			//Write One Page Data;
	IICSendByte(0xa0);
	IICSendByte(0x0);
	IICSendByte(0x0);
	Count = 0x20;
	testI = 0x0;
	while(Count--)
		IICSendByte(++testI);
	IICStop();
	
	Delay(250);

	IICStart();			//Read Current Address;
	IICSendByte(0xa1);
	ReadData[32] = IICReceiveByte();
	IICStop();

	Delay(10);
*/
/*	
	IICStart();			//Read One Page Data;
	IICSendByte(0xa0);	//读第一个数据
	IICSendByte(0x0);
	IICSendByte(0x0);
	IICStart();			
	IICSendByte(0xa1);
//	ReadData[0] = IICReceiveByte();
//	IICStop();
	
//	Delay(160);
*/
	IICStart();			//读省下的32个数据
	IICSendByte(0xa1);
	for(Count=0;Count<32;Count++)
	{
		ReadData[Count] = IICReceiveByte();
		Delay(10);
	}//IICNack();
	IICStop();

	Delay(10);
		
	IICStart();			//Read Current Address;
	IICSendByte(0xa1);
	ReadData[32] = IICReceiveByte();
	IICStop();

	Delay(158);

	for(Count=0;Count<32;Count++)
		ReadData[Count] = 0x0;

	for(Count=0;Count<32;Count++)
	{
		IICStart();			//Read One Byte Data;
		IICSendByte(0xa0);
		IICSendByte(0x0);
		IICSendByte(Count);

		IICStart();			
		IICSendByte(0xa1);
		ReadData[Count] = IICReceiveByte();
		IICStop();

		Delay(250);
	}
	while(1) ;
}	

void Delay(ByteU Times)
{
	while(Times--) ;	
}

void DelayNms(ByteU Times)
{
	ByteU i;
	while(Times--)
	{
		i = 37;
		while(i--) ;
	}
}

bool IICStart(void)
{
 	Delay(10);
 	SDA = 1;
 	Delay(10);
 	SCL = 1;
 	Delay(10);
 	if ( SDA == 0) return 0;
 	if ( SCL == 0) return 0;
 	SDA = 0;
 	Delay(10);
 	SCL = 0;
 	Delay(10);
	return 1;
}

void IICStop(void)
{
 	Delay(10);
 	SDA = 0;
 	Delay(10);
 	SCL = 1;
 	Delay(10);
	SDA = 1;
 	Delay(10);
}

void IICAck(void)
{
 	Delay(10);
	SDA = 0;
 	Delay(10);
	SCL = 1;
 	Delay(10);
	SCL = 0;
 	Delay(10);
}

void IICNack(void)
{
 	Delay(10);
 	SDA = 1;
 	Delay(10);
 	SCL = 1;
 	Delay(10);
 	SCL = 0;
 	Delay(10);
}

bool IICSendByte(ByteU Data)
{
	ByteU 	i = 8;
	bool 	bit_ack;
	while(i--)
	{
 		Delay(10);
		 if ( Data &0x80 )   
		 	SDA = 1;
		 else
		 	SDA = 0;
 		Delay(10);
		SCL = 1;
 		Delay(10);
		SCL = 0;
		Data = Data << 1;
	 }
 	Delay(10);
	SDA = 1;
 	Delay(10);
	SCL = 1;
 	Delay(10);
	bit_ack = SDA;
	SCL = 0;
 	Delay(10);
	return bit_ack;
}

ByteU IICReceiveByte(void)
{
	ByteU	i = 8, d;
 	Delay(10);
	SDA = 1;
	while ( i--)
	{
		d = d << 1;
	 	Delay(10);
		SCL = 1;
		if (SDA) d++;
 		Delay(10);
		SCL = 0;
 	}
 	return d;
}

void AT24C64_W(void *mcu_address,WordU AT24C64_address,WordU count)
{
	ByteU	tempData;
	while(count--)
	{
		IICStart();
		IICSendByte(0xa0);
		IICSendByte(AT24C64_address / 256);
		IICSendByte(AT24C64_address % 256);
		IICSendByte(*(ByteU *)mcu_address);
		IICStop();
		DelayNms(10);       /* waiting for write cycle to be completed */
		((ByteU *)mcu_address)++;
		tempData = (ByteU *)mcu_address;
		AT24C64_address++;
	}
}

void AT24C64_R(void *mcu_address,WordU AT24C64_address,WordU count)
{
	ByteU	tempReadData;
	while(count--)
	{
		IICStart();
		/*I2C_Send_Byte( 0xa0 + AT24C64_address / 256 *2 );*/   /* 24C16 USE */
		IICSendByte(0xa0);
		IICSendByte(AT24C64_address / 256);
		IICSendByte(AT24C64_address % 256);
		IICStart();
		/*I2C_Send_Byte( 0xa1 + AT24C64_address /256 *2 );*/
		IICSendByte(0xa1);
		*(ByteU *)mcu_address = IICReceiveByte();
		IICAck();
		IICStop();
		((ByteU *)mcu_address)++;
		tempReadData = *(ByteU *)mcu_address;
		AT24C64_address++;
	}
}

⌨️ 快捷键说明

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