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

📄 24c256.c

📁 读写串行存储器24C256底层驱动程序,在51系列单片机成功应用.
💻 C
字号:
#include "Cabe.H"

/*---------------------------------------------
读写24c256标准程序段

作者:dasheng
Modified by:rgh111,for FM24C256.  04-8-16 14:08
I have deleted the page read &write subroutine.
-------------------------------------------*/

#define SCL   P16
#define SDA   P17


/*-----------------------------------------------
调用方式:void start_bit(void)
函数说明:开始位
-----------------------------------------------*/
void start_bit(void)
{
	SCL = 1;
	SDA = 1;
	_nop_(); 
	SDA = 0;
	_nop_(); 
	SCL = 0;
	_nop_(); 
}


/*-----------------------------------------------
调用方式:void stop_bit(void)
函数说明:停止位
-----------------------------------------------*/
void stop_bit(void)
{
	SDA = 0;
	_nop_(); 
	SCL = 1;
	_nop_(); 
	SDA = 1;
	_nop_(); 
	SCL = 0;
}


/*-----------------------------------------------
调用方式:void mast_ack(void)
函数说明:主答函数
-----------------------------------------------*/
/*
void mast_ack(void)
{
	SCL=0;
	_nop_(); 
	SDA=0;
	_nop_();
	SCL=1;
	_nop_();
	SCL=0;
	_nop_();
	SDA=1;
	_nop_();
}
*/

/*-----------------------------------------------
调用方式:void ack(void)
函数说明:应答函数
-----------------------------------------------*/
void ack(void)
{
	SDA=0;
	_nop_();  
	SCL = 1;
	_nop_();  
	SCL = 0;
	//while(SDA){;}   //This may make the CPU crash. 04-3-18 17:16
}


/*-----------------------------------------------
调用方式:void no_ack(void)
函数说明:无需应答位,在读程序中用到
-----------------------------------------------*/
void no_ack(void)
{
	SDA=1;
	_nop_(); 
	SCL=1;
	_nop_(); 
	//while(SDA){;}  //This may make the CPU crash. 04-3-18 17:16
	SCL=0;
}
/*-----------------------------------------------
调用方式:Bit WaitAck(void)
函数说明:需应答位,在写程序中用到
-----------------------------------------------*/
Bit WaitAck(void)
{
	 Byte errtime=255;//因故障接收方无ACK,超时值为255。
	 SDA=1;
	 _nop_();
	 SCL=1;
	 _nop_();
	 while(SDA)
	 {
	     errtime--;
	     if (!errtime)
	     {
	        return false;
	     }
	 }
	 SCL=0;
	 return true;
}

/*-----------------------------------------------
调用方式:void write_8bit(uchar ch)
函数说明:写一个字节(8位)数据
-----------------------------------------------*/
void write_8bit(Byte ch)
{
	Byte i=8;
	EA = 0;
	SCL=0;
	_nop_();
	while(i--)
	{
		SDA=(bit)(ch&0x80);
		_nop_(); 
		ch<<=1;
		SCL=1;
		_nop_();
		SCL=0;
		_nop_();		
	}
	EA = 1;
}
/*-----------------------------------------------
调用方式:void Read_8bit(uchar ch)
函数说明:读一个字节(8位)数据
-----------------------------------------------*/
Byte Read_8bit(void)
{
	Byte data i = 8,rdata = 0;;
	SDA = 1;
	_nop_();
	EA = 0;
	while(i--)
	{
		rdata<<=1;
		SCL=0;
		_nop_(); 
		SCL=1;
		if(SDA) rdata|=0x01;
	}
	SCL = 0;
	EA = 1;
	return rdata;
}

/*----------------------------------------------
调用方式:Byte read24c256(uint address)
函数说明:读24c256指定地址数据(字节读)
-----------------------------------------------*/
Byte ReadCharFm24c256(Word address)
{
	Byte data rdata,Addr;
	Byte i = 8;

	//EA=0;//避免与串口通讯等中断冲突
	start_bit();
	if(address < 0x8000)
	{
		write_8bit(0xA0);
		Addr = 0xA0 + 1;
	}
	else
	{
		write_8bit(0xA2);
		Addr = 0xA2 + 1;
	}
	WaitAck();
	write_8bit(address/256);
	WaitAck();						//伪写
	write_8bit((Byte)address);
	WaitAck();
	start_bit();
	write_8bit(Addr);
	WaitAck();
	while(i--)
	{
		rdata<<=1;
		SCL=0;
		_nop_(); 
		SCL=1;
		if(SDA) rdata|=0x01;
	}
	no_ack();
	stop_bit();

//	EA=1;
	return rdata;
}
/*--------------------------------------------------------------
调用方式:void ReadFm24c256(Word Addr,Word Lenth,Byte *buffer)
函数说明:从指定的地址读出Lenth长度的数据放入buffer中

⌨️ 快捷键说明

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