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 退出