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

📄 eeprom.c

📁 使用picc开发的基于pic16f87x的操作eeprom的源程序
💻 C
字号:
/*

[from http://www.htsoft.com/ and PIC C online forum, 24 Feb 2000]
[cezarym@apator.torun.pl]

Hello,

There are some routines could you help follows. I have written it for 12CE673,
but it should work for 16CE674. You must change Delay() functions if you work
with no 4MHz oscilator. There is not possibility to preset EEPROM during
programing the device. You must write initialization procedure in PIC program.

Regards, Cezary.

*/

/*
12CE67x EEPROM procedures

//***
//write one byte to EEPROM
void Write_Byte(char address,char data)

Input:    address data ( char type)
Output:   e2_err=1 if EEPROM is busy (eg. writing process)
	  e2_err=0 if OK

//***
//current addresss reading, addresss counter is incremented; the function could be
//use for sequential reading (after first Read_Random())

char Read_Current(void)
Output:   data (char type)
	  e2_err=1 if EEPROM is busy (eg. writing process)
	  e2_err=0 if OK

//***
//read data from Address
char Read_Random(char address)
Input:    address (type char)
Output:   data from address (char)
	  e2_err=1 if EEPROM is busy (eg. writing process)
	  e2_err=0 if OK

*/

//*****
//definitions

#define i2c_port GPIO
#define Start_bit() SDA=0

static bit e2_err;

char PC_offset;

void Delay_5(void)
{
}

void Delay_6m(void)
{
	char licz=0;
	do
	{
		Delay_5();
		Delay_5();
		Delay_5();
	} while (--licz);
}

void Stop_bit()
{
	SDA=0;
	SCL=1;
	Delay_5();
	SDA=1;
}

void Transfer_Byte(char eebyte)
{
	char count=8;
	e2_err=0;
	do
	{
		NOP();
		SCL=0;
		SDA=0;
		if (testbit(eebyte,7)) SDA=1;
		eebyte<<=1;
		SCL=1;
	} while (--count);
	NOP();
	SCL=0;
	Delay_5();
	SDA=1;
	SCL=1;
	NOP();
	if (SDA) e2_err=1;
	SCL=0;
	if (e2_err) Stop_bit();
	else NOP(); // because compiler bug
}

char Read_Byte(void)
{
	char count;
	char eedata;
	SDA=1;
	NOP();
	SCL=1;
	count=8;
	do
	{
		SCL=1;
		eedata=(eedata<<1) | SDA;
		SCL=0;
		SDA=1;
	} while (--count);
	NOP();
	SCL=1;
	Delay_5();
	SCL=0;
	Stop_bit();
	return eedata;
}

char Read_Current(void)
{
	SCL=1;
	SDA=1;
	Delay_5();
	Start_bit();
	Transfer_Byte(0xA1);
	if (!e2_err) return Read_Byte();
	else
	{
		Delay_6m();
		return 0;
	}
}

char Read_Random(char address)
{
	Start_bit();
	Transfer_Byte(0xA0);
	if (!e2_err) Transfer_Byte(address);
	if (!e2_err) return Read_Current();
	else
	{
		Delay_6m();
		return 0;
	}
}

void Write_Byte(char address, char data)
{
	Start_bit();
	Transfer_Byte(0xA0);
	if (!e2_err) Transfer_Byte(address);
	if (!e2_err) Transfer_Byte(data);
	if (!e2_err) Stop_bit();
	else Delay_6m();
}

⌨️ 快捷键说明

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