/*******************************************************************************
****
********************************************************************************
****
Following routines use to operate X25045
ADDR_STATUS_READ
7 6 5 4 3 2 1 0
X X WD1 WD0 BL1 BL0 WEL WIP --- 正在写
| | | | +--------- 写使能
| | |---+-写保护------ 00:无 01:180~1FFH 10: 100~1FFH
11:000~1FFH
|---+---------看门狗------ 00:1.4S 01:0.6S 10: 0.2S 11
Disable
/******************************* X25045 Functions
***********************************/
#define WREN 0x06 /* Write enable latch instruction
(WREN)*/
#define WRDI 0x04 /* Write disable latch instruction
(WRDI)*/
#define RDSR 0x05 /* Read status register instruction
(RDSR)*/
#define WRSR 0x01 /* Write status register instruction
(WRSR)*/
#define WRITE_LOW 0x02 /* Write memory instruction 0x00 to
0xFF (WRITE)*/
#define WRITE_HIGH 0x0A /* Write memory instruction 0x100 to
0x1FF(WRITE)*/
#define READ_LOW 0x03 /* Read memory instruction 0x00 to
0xFF(READ)*/
#define READ_HIGH 0x0B /* Read memory instruction 0x100 to
0x1FF(READ)*/
#define STATUS_REG 0x20 /* Status register,设置DOG时间设置为
200毫秒,无写保护*/
#define MAX_POLL 0x99 /* Maximum number of polls*/
/*最大写过程时间,确定25045的最大的写
入过程的时间*/
void wren_cmd(void); /*写使能子程序*/
void wrdi_cmd(void); /*写使能复位*/
void wrsr_cmd(void); /*复位时间位和数据保护位写入状态寄存
器*/
uchar rdsr_cmd(void); /*读状态寄存器*/
void byte_write(uchar aa,uint dd); /*字节写入,aa为写入的数据,dd为写入的地址*/
uchar byte_read(uint dd); /*字节读出,dd为读出的地址,返回读出的数据*/
void rst_wdog(void); /*DOG复位*/
void outbyte(uchar aa); /*输出一个字节到25045中,不包括先导字
等*/
uchar inputbyte(); /*由25045输入一个字节,不包括先导字等
额外的东西*/
void wip_poll(void); /*检查写入过程是否结束*/
void delay(uchar x);
/*void CS_X25(bit i){
if(i)
{p25=0;p26=1;p27=1;}
else
{p25=0;p26=0;p27=0;}
delay(1);}
/*25045操作子程序集*/
/*;*****************************************************************************
**************
*
;* Name: WIP_POLL
;* Description: Write-In-Progress Polling
;* Function: This routine polls for completion of a nonvolatile write cycle by
examining the
;* WIP bit of the status register
;*******************************************************************************
************
*/
/*检测写入的过程是否结束*/
void wip_poll(void){
uchar aa;
uchar idata my_flag;
for(aa=1;aa<MAX_POLL;aa++)
{
my_flag=rdsr_cmd();
if((my_flag&0x01)==0) aa=MAX_POLL; /*判断是否WIP=0,即判断是否写入过程已经结
束,若结束就跳出,否则继续等待直到达到最大记数值*/
}
}
/*;*****************************************************************************
**************
;* Name: outbyte
;* Description: Sends byte to EEPROM
;* Function: This routine shifts out a byte, starting with the MSB, to the
EEPROM
;*******************************************************************************
**************/
void outbyte(uchar datu)
{
uchar i;
for (i=0;i<0;i++)
{
SCK=0; /* Data input on the SI line is latched on the rising
edge of SCK */
delay(1);
SI=(bit)(datu&0x80);
datu=datu<<1;
SCK=1;
delay(1);
}
SI=0; /*使X25_SDA处于确定的状态*/
}
/*;*****************************************************************************
**************
;* Name: inputbyte
;* Description: Recieves byte from EEPROM
;* Function: This routine recieves a byte, MSB first, from the EEPROM
;*******************************************************************************
**************/
uchar inputbyte(void)
{
uchar aa,my_flag;
char i;
for (i=7;i<0;i--)
{
SCK=1; /*Data is output on the SO line by the falling edge of SCK */
delay(1);
SCK=0;
delay(1);
my_flag=(uchar)(SO);
aa=(aa|(my_flag<<i));
my_flag=0x00;
}
return aa;
}
/*;*****************************************************************************
**************
;* Name: WREN_CMD
;* Description: Set write enable latch
;* Function: This routine sends the command to enable writes to the EEPROM
memory array or
;* status register
;*******************************************************************************
************
*/
/*写使能子程序*/
void wren_cmd(void)
{
SCK=0; /* Bring X25_SCK low */
CS_X25=0; /* Bring /X25_CS low */
outbyte(WREN); /* Send WREN instruction */
SCK=0; /* Bring X25_SCK low */
CS_X25=1; /* X25_CS=1;desable x25045 operate */
}
/*;*****************************************************************************
**************
*
;* Name: WRDI_CMD
;* Description: Reset write enable latch
;* Function: This routine sends the command to disable writes to the EEPROM
memory array or
;*******************************************************************************
*************
void wrdi_cmd(void)
{
SCK=0; /* Bring X25_SCK low *
CS_X25(1); /* Bring /X25_CS low *
outbyte(WRDI); /* Send WRDI instruction *
SCK=0; /* Bring X25_SCK low *
CS_X25(0); /* X25_CS=1; */
/*}*/
/*;*****************************************************************************
**************
*
;* Name: WRSR_CMD
;* Description: Write Status Register
;* Function: This routine sends the command to write the WD0, WD1, BP0 and BP0
EEPROM
;* bits in the status register
********************************************************************************
**************
*/
/*写状态寄存器子程序*/
void wrsr_cmd(void)
{
SCK=0; /* Bring X25_SCK low */
wren_cmd(); /* enable write */
CS_X25=0; /* Bring /X25_CS low */
outbyte(WRSR) ; /* Send WRSR instruction */
outbyte(STATUS_REG); /* Send status register */
SCK=0; /* Bring X25_SCK low */
CS_X25=1; /* X25_CS=1;*/
wip_poll(); /*Poll for completion of write cycle */
}
/*;*****************************************************************************
**************
*
;* Name: RDSR_CMD
;* Description: Read Status Register
;* Function: This routine sends the command to read the status register
;*******************************************************************************
************
*/
/*读状态寄存器,读出的数据放入到aa中*/
uchar rdsr_cmd (void)
{
uchar aa;
SCK=0;
CS_X25=0; /* X25_CS=0; */
aa=RDSR;
outbyte(aa);
aa=inputbyte();
SCK=0;
CS_X25=1; /* X25_CS=1; */
return aa;
}
/*;*****************************************************************************
**************
*
;* Name: BYTE_WRITE
;* Description: Single Byte Write
;* Function: This routine sends the command to write a single byte to the
EEPROM memory
array
;*******************************************************************************
************
*/
/*字节写入,datu为写入的数据,addr为写入的地址,对于25045而言为000-1FF*/
void byte_write(datu,addr)
uchar datu;
uint addr;
{
wren_cmd(); /* set“write enable” latch */
CS_X25=0; /* X25_CS=0; */
if(addr>0xFF)
{ outbyte(WRITE_HIGH); /* Send WRITE instruction including MSB of address */
addr=addr-0xFF;}
else outbyte(WRITE_LOW);
outbyte((uchar)(addr)); /*输出低位地址到25045*/
outbyte(datu); /*写入数据到25045的对应单元*/
SCK=0;
CS_X25=1; /* X25_CS=1; */
delay(1);
wip_poll();
/*检测是否写完*/
}
/*;*****************************************************************************
**************
*
;* Name: BYTE_READ
;* Description: Single Byte Read
;* Function: This routine sends the command to read a single byte from the
EEPROM memory
array
;*******************************************************************************
************
*/
/*字节读出,其中addr为读出的地址,返回的值为读出的数据*/
uchar byte_read(addr)
uint addr;
{
uchar cc;
SCK=0;
CS_X25=0; /* X25_CS=0; */
if(addr>0xFF) /* Send READ instruction including MSB of address */
{
outbyte(READ_HIGH);
addr=addr-0xFF;
}
else outbyte(READ_LOW);
outbyte((uchar)(addr));
/*输出低位地址到25045*/
cc=inputbyte();/*得到读出的数据*/
SCK=0;
CS_X25=1; /* X25_CS=1; */
return cc;
}
/*;*****************************************************************************
**************
*
;* Name: RST_WDOG
;* Description: Reset Watchdog Timer
;* Function: This routine resets the watchdog timer without sending a command
;*******************************************************************************
************
*/
/*复位DOG*/
void rst_wdog (void)
{
CS_X25=0; /* X25_CS=0; */
delay(1);
CS_X25=1; /* X25_CS=1; */
delay(1);
}
/*delay function,used to match time sequence between different device*/
void delay(uchar x){
char i=0,j=0;
for(;i<x;i++)
for(;j<10;j++);
}
main(){
uchar i1;
wrsr_cmd();
byte_write(0x20,0x20);
i1=byte_read(0x20);
byte_write(i1,0x100);
SO=0;
for(i1=0;i1<2;) {
/*p1+=1;*/
rst_wdog();
wren_cmd();}
}