void X5045SpiOpen(void);//打開X5045片選
void X5045SpiClose(void);//關閉X5045片選
void X5045WriteEnable(void);//軟件使能X5045寫操作
void X5045WriteDisable(void);//軟件禁止X5045寫操作
unsigned char X5045SpiSend(unsigned char val);//X5045收發(fā)SPI協(xié)議
void X5045WriteByte(unsigned int addr unsigned char val);//寫X5045一個字節(jié)
void X5045WriteWord(unsigned int addr unsigned int val);//寫X5045一個字
unsigned char X5045ReadByte(unsigned int addr);//讀X5045一個字節(jié)
unsigned int X5045ReadWord(unsigned int addr);//讀X5045一個字
unsigned char X5045ReadStatus(void);//讀X5045狀態(tài)
void X5045WriteStatus(unsigned char val);//寫X5045狀態(tài)
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);//發(fā)送使能X5045寫命令
X5045SpiClose();//關閉X5045片選
}
/*--------------------------------------
軟件禁止X5045寫操作
--------------------------------------*/
void X5045WriteDisable(void)
{
X5045SpiOpen();//打開X5045片選
X5045SpiSend(WRDI);//發(fā)送禁止X5045寫命令
EEPROMWP = 0;//硬件禁止X5045寫操作
X5045SpiClose();//關閉X5045片選
}
/*--------------------------------------
X5045收發(fā)SPI協(xié)議
--------------------------------------*/
unsigned char X5045SpiSend(unsigned char val)
{
unsigned char i;
WDTRST = 0x1e;//89s52內(nèi)狗
WDTRST = 0xe1;//89s52內(nèi)狗
ACC = val;
for (i = 8; i > 0; i --)
{
CY = EEPROMSO;//取數(shù)據(jù)SO
_rlca_();//存數(shù)據(jù)ACC.0讀數(shù)據(jù)ACC.7同時進行
EEPROMSI = CY;//送數(shù)據(jù)SI
EEPROMSCK = 1;//上升沿打入數(shù)據(jù)
_nop_();
EEPROMSCK = 0;//下降沿讀入數(shù)據(jù)(首次為假動作)
}
return ACC;
}
/*--------------------------------------
寫X5045一個字節(jié)
--------------------------------------*/
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);//發(fā)送X5045寫命令及高位地址
X5045SpiSend(addr & 0xff);//發(fā)送X5045寫低位地址
X5045SpiSend(val);//發(fā)送X5045寫數(shù)據(jù)
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);//發(fā)送X5045寫命令及高位地址
X5045SpiSend(addr & 0xff);//發(fā)送X5045寫低位地址
X5045SpiSend(val & 0xff);//發(fā)送X5045寫低位數(shù)據(jù)
X5045SpiSend(val >> 8);//發(fā)送X5045寫高位數(shù)據(jù)
X5045SpiClose();//關閉X5045片選
X5045WriteWait();//等待本次X5045寫入完成
X5045WriteDisable();//禁止X5045寫操作
}
/*--------------------------------------
讀X5045一個字節(jié)
--------------------------------------*/
unsigned char X5045ReadByte(unsigned int addr)
{
unsigned char val;
X5045SpiOpen();//打開X5045片選
val = READ;
if ((addr >> 8) & 0x01) val |= 8;
X5045SpiSend(val);//發(fā)送X5045讀命令及高位地址
X5045SpiSend(addr & 0xff);//發(fā)送X5045讀低位地址
val = X5045SpiSend(0xff);//接收X5045讀數(shù)據(jù)
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);//發(fā)送X5045讀命令及高位地址
X5045SpiSend(addr & 0xff);//發(fā)送X5045讀低位地址
val = X5045SpiSend(0xff);//接收X5045讀低位數(shù)據(jù)
addr = X5045SpiSend(0xff);//接收X5045讀高位數(shù)據(jù)
X5045SpiClose();//關閉X5045片選
addr <<= 8;
addr |= val;
return addr;
}
/*--------------------------------------
讀X5045狀態(tài)
--------------------------------------*/
unsigned char X5045ReadStatus(void)
{
unsigned char val;
X5045SpiOpen();//打開X5045片選
X5045SpiSend(RDSR);//發(fā)送讀X5045狀態(tài)命令
val = X5045SpiSend(0xff);//接收X5045狀態(tài)數(shù)據(jù)
X5045SpiClose();//關閉X5045片選
return val;
}
/*--------------------------------------
寫X5045狀態(tài)
--------------------------------------*/
void X5045WriteStatus(unsigned char val)
{
X5045SpiOpen();//打開X5045片選
X5045SpiSend(WRSR);//發(fā)送寫X5045狀態(tài)命令
X5045SpiSend(val);//發(fā)送X5045狀態(tài)數(shù)據(jù)
X5045SpiClose();//關閉X5045片選
}
/*--------------------------------------
等待X5045寫入完成
--------------------------------------*/
void X5045WriteWait(void)
{
while (X5045ReadStatus() & WIP);//WIP=0 退出