X5045读写一体化C51程序
扫描二维码
随时随地手机看文章
void X5045SpiOpen(void);//打开X5045片选void X5045SpiClose(void);//关闭X5045片选
void X5045WriteEnable(void);//软件使能X5045写操作
void X5045WriteDisable(void);//软件禁止X5045写操作
unsigned char X5045SpiSend(unsigned char val);//X5045收发SPI协议
void X5045WriteByte(unsigned int addr, unsigned char val);//写X5045一个字节
void X5045WriteWord(unsigned int addr, unsigned int val);//写X5045一个字
unsigned char X5045ReadByte(unsigned int addr);//读X5045一个字节
unsigned int X5045ReadWord(unsigned int addr);//读X5045一个字
unsigned char X5045ReadStatus(void);//读X5045状态
void X5045WriteStatus(unsigned char val);//写X5045状态
void X5045WriteWait(void);//等待X5045写入完成
/*--------------------------------------
打开X5045片选
--------------------------------------*/
void X5045SpiOpen(void)
{
EEPROMCS= 1;//WDT
EEPROMSCK = 0;
EEPROMSIO = 1;//SO=SI=1,释放总线
EEPROMCS= 0;//WDT;打开X5045片选CS
}
/*--------------------------------------
关闭X5045片选
--------------------------------------*/
void X5045SpiClose(void)
{
EEPROMCS= 1;//关闭X5045片选CS
EEPROMSIO = 1;//SO=SI=1,释放总线
EEPROMSCK = 0;
}
/*--------------------------------------
软件使能X5045写操作
--------------------------------------*/
void X5045WriteEnable(void)
{
X5045SpiOpen();//打开X5045片选
EEPROMWP = 1;//硬件使能X5045写操作
X5045SpiSend(WREN);//发送使能X5045写命令
X5045SpiClose();//关闭X5045片选
}
/*--------------------------------------
软件禁止X5045写操作
--------------------------------------*/
void X5045WriteDisable(void)
{
X5045SpiOpen();//打开X5045片选
X5045SpiSend(WRDI);//发送禁止X5045写命令
EEPROMWP = 0;//硬件禁止X5045写操作
X5045SpiClose();//关闭X5045片选
}
/*--------------------------------------
X5045收发SPI协议
--------------------------------------*/
unsigned char X5045SpiSend(unsigned char val)
{
unsigned char i;
WDTRST = 0x1e;//89s52内狗
WDTRST = 0xe1;//89s52内狗
ACC = val;
for (i = 8; i > 0; i --)
{
CY = EEPROMSO;//取数据SO
_rlca_();//存数据ACC.0读数据ACC.7同时进行
EEPROMSI = CY;//送数据SI
EEPROMSCK = 1;//上升沿打入数据
_nop_();
EEPROMSCK= 0;//下降沿读入数据(首次为假动作)
}
return ACC;
}
/*--------------------------------------
写X5045一个字节
--------------------------------------*/
void X5045WriteByte(unsigned int addr, unsigned char val)
{
unsigned char temp;
temp = WRITE;
if ((addr >> 8) & 0x01) temp |= 8;
X5045WriteEnable();//使能X5045写操作
X5045SpiOpen();//打开X5045片选
X5045SpiSend(temp);//发送X5045写命令及高位地址
X5045SpiSend(addr & 0xff);//发送X5045写低位地址
X5045SpiSend(val);//发送X5045写数据
X5045SpiClose();//关闭X5045片选
X5045WriteWait();//等待本次X5045写入完成
X5045WriteDisable();//禁止X5045写操作
}
/*--------------------------------------
写X5045一个字
--------------------------------------*/
void X5045WriteWord(unsigned int addr, unsigned int val)
{
unsigned char temp;
temp = WRITE;
if ((addr >> 8) & 0x01) temp |= 8;
X5045WriteEnable();//使能X5045写操作
X5045SpiOpen();//打开X5045片选
X5045SpiSend(temp);//发送X5045写命令及高位地址
X5045SpiSend(addr & 0xff);//发送X5045写低位地址
X5045SpiSend(val & 0xff);//发送X5045写低位数据
X5045SpiSend(val >> 8);//发送X5045写高位数据
X5045SpiClose();//关闭X5045片选
X5045WriteWait();//等待本次X5045写入完成
X5045WriteDisable();//禁止X5045写操作
}
/*--------------------------------------
读X5045一个字节
--------------------------------------*/
unsigned char X5045ReadByte(unsigned int addr)
{
unsigned char val;
X5045SpiOpen();//打开X5045片选
val = READ;
if ((addr >> 8) & 0x01) val |= 8;
X5045SpiSend(val);//发送X5045读命令及高位地址
X5045SpiSend(addr & 0xff);//发送X5045读低位地址
val = X5045SpiSend(0xff);//接收X5045读数据
X5045SpiClose();//关闭X5045片选
return val;
}
/*--------------------------------------
读X5045一个字
--------------------------------------*/
unsigned int X5045ReadWord(unsigned int addr)
{
unsigned char val;
X5045SpiOpen();//打开X5045片选
val = READ;
if ((addr >> 8) & 0x01) val |= 8;
X5045SpiSend(val);//发送X5045读命令及高位地址
X5045SpiSend(addr & 0xff);//发送X5045读低位地址
val = X5045SpiSend(0xff);//接收X5045读低位数据
addr = X5045SpiSend(0xff);//接收X5045读高位数据
X5045SpiClose();//关闭X5045片选
addr <<= 8;
addr |= val;
return addr;
}
/*--------------------------------------
读X5045状态
--------------------------------------*/
unsigned char X5045ReadStatus(void)
{
unsigned char val;
X5045SpiOpen();//打开X5045片选
X5045SpiSend(RDSR);//发送读X5045状态命令
val = X5045SpiSend(0xff);//接收X5045状态数据
X5045SpiClose();//关闭X5045片选
return val;
}
/*--------------------------------------
写X5045状态
--------------------------------------*/
void X5045WriteStatus(unsigned char val)
{
X5045SpiOpen();//打开X5045片选
X5045SpiSend(WRSR);//发送写X5045状态命令
X5045SpiSend(val);//发送X5045状态数据
X5045SpiClose();//关闭X5045片选
}
/*--------------------------------------
等待X5045写入完成
--------------------------------------*/
void X5045WriteWait(void)
{
while (X5045ReadStatus() & WIP);//WIP=0 退出
}
void WriteByte(unsigned char Byte, unsigned char ADD) //向地址写入数据这里同样不做先导字处理,只能写00-FFH
{
SCK=0;
CS=0;
OUTByte(0x2); //发送写指令02H 如要支持000-FFF则要把高位地址左移2位再为02H相或
OUTByte(ADD); //发送低位地址
OUTByte(Byte); //发送数据
SCK=0;
CS=1;
WIPCHK(); //判断是否写入
}
}
void OUTByte(unsigned char Byte) //输出一个定节
{
unsigned char TempCyc;
for(TempCyc=0;TempCyc<8;TempCyc++)
{
SCK = 0;
SI = Byte & 0x80;
Byte = Byte<<1; //右移 -+
SCK = 1;
}
SI=0; //使SI处于确定的状态
}
unsigned char INPUTByte(void) //输入一个字节
{
unsigned char Temp=0, TempCyc;
for(TempCyc=0;TempCyc<8;TempCyc++)
{
Temp = Temp<<1; //右移
SCK = 0;
SCK = 0;
SCK = 1;
SCK = 1;
if (SO)
Temp = Temp|0x01; //SO为1,则最低位为1
}
return Temp;
}
unsigned char ReadByte(unsignedchar ADD) //读地址中的数据这里不做先导字处理,只能读00-FFH
{
unsigned char Temp;
SCK=0;
CS=0;
OUTByte(0x3); //发送读指令03H 如要支持000-FFF则要把高位地址左移3位再为03H相或
OUTByte(ADD); //发送低位地址
Temp = INPUTByte();
SCK=0;
CS=1;
return Temp;
}