X5045讀寫一體化C51程序
void X5045SpiOpen(void);//打開(kāi)X5045片選void X5045SpiClose(void);//關(guān)閉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一個(gè)字節(jié)
void X5045WriteWord(unsigned int addr, unsigned int val);//寫X5045一個(gè)字
unsigned char X5045ReadByte(unsigned int addr);//讀X5045一個(gè)字節(jié)
unsigned int X5045ReadWord(unsigned int addr);//讀X5045一個(gè)字
unsigned char X5045ReadStatus(void);//讀X5045狀態(tài)
void X5045WriteStatus(unsigned char val);//寫X5045狀態(tài)
void X5045WriteWait(void);//等待X5045寫入完成
/*--------------------------------------
打開(kāi)X5045片選
--------------------------------------*/
void X5045SpiOpen(void)
{
EEPROMCS= 1;//WDT
EEPROMSCK = 0;
EEPROMSIO = 1;//SO=SI=1,釋放總線
EEPROMCS= 0;//WDT;打開(kāi)X5045片選CS
}
/*--------------------------------------
關(guān)閉X5045片選
--------------------------------------*/
void X5045SpiClose(void)
{
EEPROMCS= 1;//關(guān)閉X5045片選CS
EEPROMSIO = 1;//SO=SI=1,釋放總線
EEPROMSCK = 0;
}
/*--------------------------------------
軟件使能X5045寫操作
--------------------------------------*/
void X5045WriteEnable(void)
{
X5045SpiOpen();//打開(kāi)X5045片選
EEPROMWP = 1;//硬件使能X5045寫操作
X5045SpiSend(WREN);//發(fā)送使能X5045寫命令
X5045SpiClose();//關(guān)閉X5045片選
}
/*--------------------------------------
軟件禁止X5045寫操作
--------------------------------------*/
void X5045WriteDisable(void)
{
X5045SpiOpen();//打開(kāi)X5045片選
X5045SpiSend(WRDI);//發(fā)送禁止X5045寫命令
EEPROMWP = 0;//硬件禁止X5045寫操作
X5045SpiClose();//關(guān)閉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同時(shí)進(jìn)行
EEPROMSI = CY;//送數(shù)據(jù)SI
EEPROMSCK = 1;//上升沿打入數(shù)據(jù)
_nop_();
EEPROMSCK= 0;//下降沿讀入數(shù)據(jù)(首次為假動(dòng)作)
}
return ACC;
}
/*--------------------------------------
寫X5045一個(gè)字節(jié)
--------------------------------------*/
void X5045WriteByte(unsigned int addr, unsigned char val)
{
unsigned char temp;
temp = WRITE;
if ((addr >> 8) & 0x01) temp |= 8;
X5045WriteEnable();//使能X5045寫操作
X5045SpiOpen();//打開(kāi)X5045片選
X5045SpiSend(temp);//發(fā)送X5045寫命令及高位地址
X5045SpiSend(addr & 0xff);//發(fā)送X5045寫低位地址
X5045SpiSend(val);//發(fā)送X5045寫數(shù)據(jù)
X5045SpiClose();//關(guān)閉X5045片選
X5045WriteWait();//等待本次X5045寫入完成
X5045WriteDisable();//禁止X5045寫操作
}
/*--------------------------------------
寫X5045一個(gè)字
--------------------------------------*/
void X5045WriteWord(unsigned int addr, unsigned int val)
{
unsigned char temp;
temp = WRITE;
if ((addr >> 8) & 0x01) temp |= 8;
X5045WriteEnable();//使能X5045寫操作
X5045SpiOpen();//打開(kāi)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();//關(guān)閉X5045片選
X5045WriteWait();//等待本次X5045寫入完成
X5045WriteDisable();//禁止X5045寫操作
}
/*--------------------------------------
讀X5045一個(gè)字節(jié)
--------------------------------------*/
unsigned char X5045ReadByte(unsigned int addr)
{
unsigned char val;
X5045SpiOpen();//打開(kāi)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();//關(guān)閉X5045片選
return val;
}
/*--------------------------------------
讀X5045一個(gè)字
--------------------------------------*/
unsigned int X5045ReadWord(unsigned int addr)
{
unsigned char val;
X5045SpiOpen();//打開(kāi)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();//關(guān)閉X5045片選
addr <<= 8;
addr |= val;
return addr;
}
/*--------------------------------------
讀X5045狀態(tài)
--------------------------------------*/
unsigned char X5045ReadStatus(void)
{
unsigned char val;
X5045SpiOpen();//打開(kāi)X5045片選
X5045SpiSend(RDSR);//發(fā)送讀X5045狀態(tài)命令
val = X5045SpiSend(0xff);//接收X5045狀態(tài)數(shù)據(jù)
X5045SpiClose();//關(guān)閉X5045片選
return val;
}
/*--------------------------------------
寫X5045狀態(tài)
--------------------------------------*/
void X5045WriteStatus(unsigned char val)
{
X5045SpiOpen();//打開(kāi)X5045片選
X5045SpiSend(WRSR);//發(fā)送寫X5045狀態(tài)命令
X5045SpiSend(val);//發(fā)送X5045狀態(tài)數(shù)據(jù)
X5045SpiClose();//關(guān)閉X5045片選
}
/*--------------------------------------
等待X5045寫入完成
--------------------------------------*/
void X5045WriteWait(void)
{
while (X5045ReadStatus() & WIP);//WIP=0 退出
}
void WriteByte(unsigned char Byte, unsigned char ADD) //向地址寫入數(shù)據(jù)這里同樣不做先導(dǎo)字處理,只能寫00-FFH
{
SCK=0;
CS=0;
OUTByte(0x2); //發(fā)送寫指令02H 如要支持000-FFF則要把高位地址左移2位再為02H相或
OUTByte(ADD); //發(fā)送低位地址
OUTByte(Byte); //發(fā)送數(shù)據(jù)
SCK=0;
CS=1;
WIPCHK(); //判斷是否寫入
}
}
void OUTByte(unsigned char Byte) //輸出一個(gè)定節(jié)
{
unsigned char TempCyc;
for(TempCyc=0;TempCyc<8;TempCyc++)
{
SCK = 0;
SI = Byte & 0x80;
Byte = Byte<<1; //右移 -+
SCK = 1;
}
SI=0; //使SI處于確定的狀態(tài)
}
unsigned char INPUTByte(void) //輸入一個(gè)字節(jié)
{
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) //讀地址中的數(shù)據(jù)這里不做先導(dǎo)字處理,只能讀00-FFH
{
unsigned char Temp;
SCK=0;
CS=0;
OUTByte(0x3); //發(fā)送讀指令03H 如要支持000-FFF則要把高位地址左移3位再為03H相或
OUTByte(ADD); //發(fā)送低位地址
Temp = INPUTByte();
SCK=0;
CS=1;
return Temp;
}