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

📄 20051107-24c02.c

📁 用51单片机的io口模拟iic操作.不是采用中断的方式
💻 C
字号:
//////////////////////////////////////////////////////////////////////
//
//this file shows how to use 24c02 in a 89C51 system
//
//the commom-used function is ReadByte(),WriteByte,Clear()
//ReadPage() and WritePage() is in vacancy now
//
//							- HateMath  2005.11.7
//////////////////////////////////////////////////////////////////////

#include<At89X51.h>
#include <intrins.h>

//////////////////////////////////////////////////////////////////////

sbit SDA = 0xB0^5;	//P3_5 connect the SDA footprint
sbit SCL = 0xB0^4;	//P3_4 connect the CLK footprint



//////////////////////////////////////////////////////////////////////
//delay some time
void Delay()
{
	_nop_();
	_nop_();

}

//////////////////////////////////////////////////////////////////////
// get ACK signal from 24c02
bit GetACK()
{
	bit bACK = 1;

	SDA = 1;
	Delay();
	SCL = 1;
	Delay();
	bACK = SDA;
	SCL = 0;

	return bACK;
} 

//////////////////////////////////////////////////////////////////////
//tell 24c02 to stop
void Stop()
{
	SDA = 0;
	Delay();
	SCL = 1;
	Delay();
	SDA = 1;
	Delay();

}
//////////////////////////////////////////////////////////////////////
//tell 24c02 to start
void Start()
{
	SDA = 1;
	SCL = 1;
	Delay();
	SDA = 0;
	Delay();
	SCL = 0;	//钳住I2C总线准备发送或接受数据
}

//////////////////////////////////////////////////////////////////////
//send one byte(word adress or device adress) to 24c02
//
//Parameters:	
//		nData:	data to be write to 24c02, 8 bit by default
void SendByte(char nData)
{

	//test each bit of nData,then send to 24c02 bit by bit
	//MSB of nData first, LSB last
	unsigned char nTestBit = 0x80;	//mask bit
	unsigned char i = 0;
	for(i = 0; i < 8; i++)
	{
		SDA = nData & nTestBit;
		Delay();
		SCL = 1;
		Delay();
		SCL = 0;
		SDA = !SDA;		//(? necessarry)

		nTestBit >>= 1;
	}
}

//////////////////////////////////////////////////////////////////////
//random write one byte to 24c02
//
//Parameters:	
//		nData:	data to be write to 24c02, 8 bit by default
//		nAdress:adress in 24c02, from which write the nData
void WriteByte(char nData, const unsigned char nAdress)
{
	unsigned int i = 0;		//for loop


	//1.start signal
	Start();

	//2.send device adress(0xA0 when write)
	SendByte(0xA0);
	while(GetACK());

	//3.send word adress
	SendByte(nAdress);	
	while(GetACK());

	//4.send nData
	SendByte(nData);
	while(GetACK());

	//5.send stop signal
	Stop();

	//6.delay for 10ms, (! necessary)see "write cycle timing" in the data-sheet for 24c02
	for(i = 0; i < 2000; i++) //at 12MHz condition, 2000 is OK
	{
		Delay();
	}	
}

//////////////////////////////////////////////////////////////////////
//read one byte from 24c02
//
//Parameters:	
//		nAddress:	adress of memory in 24c02,range:(0 - 256)
char ReadByte(const unsigned char nAddress)
{
	char i = 0;	//not the unsigned char
	char nBuff = 0;
	
	//1.start signal
	Start();

	//2.send device adress(write)
	SendByte(0xA0);		
	while(GetACK());

	//3.send word adress
	SendByte(nAddress);	
	while(GetACK());

	//4.start signal again
	Start();

	//5.send device adress(read)
	SendByte(0xA1);		
	while(GetACK());

	//6.get each bit from 24c02,MSB first,LSB last.
	for(i = 7; i >= 0; i--)
	{
		SCL = 1;
		Delay();
		nBuff |= (unsigned char)SDA << i;//(?)to be changed
		SCL = 0;
		Delay();
	}

	//stop
	Stop();	

	return nBuff;	
}


//////////////////////////////////////////////////////////////////////
//write 8 BYTE data into 24c02 from nStartAddress
//
//Parameters:	
//		nStartAddress:	adress of memory in 24c02,range:(0 - 256)
void WritePage(const unsigned char nStartAddress)
{

}


//////////////////////////////////////////////////////////////////////
//clear all data in 24c02
//the memory will be FF all.
//
void ClearAll()
{
	unsigned short i = 0;

	for(i = 0; i <= 255; i++)	
	{
		WriteByte(0xFF, i);		
	}

}



⌨️ 快捷键说明

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