C8051F12X操作FLASH存儲(chǔ)器程序
DBOUT 為輸出模式
DBus 為數(shù)據(jù)端口 Px
/--------FLASH命令傳送------//
void Comm_Latch_Cycle(uchar COMMAND)
{
uchar SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
DBMODE = DBOUT;
ALE=0;
ALE=0;
CLE=1;
RE=1;
WE=1;
DBus=COMMAND; //WRITE COMMAND
WE=0;
WE=1;
CLE=0;
ALE=0;
SFRPAGE = SFRPAGE_SAVE;
}
//------------------ 傳送地址------------------------------
void Add_Latch_Cycle(uint ADDR ) //傳送地址,ADDR為頁(yè)地址,從addr頁(yè)第0字節(jié)開始
{
uchar SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
DBMODE= DBOUT;
CLE=0;
ALE=1;
RE=1;
WE=1;
DBus=0; //A0-A7,column address
WE=0;
WE=1;
DBus=(uchar)ADDR; //A9-A16 ,Row Address page address A12-A29
WE=0;
WE=1;
DBus=(uchar)(ADDR>>8); //A17-A24,Row Address
WE=0;
WE=1;
ALE=0;
CLE=0;
SFRPAGE = SFRPAGE_SAVE;
}
//讀狀態(tài),狀態(tài)正確時(shí)返回0,錯(cuò)誤時(shí)返回1
uchar Status_Read(void)
{
uchar Status;
Comm_Latch_Cycle(0x70);
Status=Read_Byte();
Status=Status & 0x01;
return Status; //status=0 erease ok else failled
}
//從FLASH讀一個(gè)字節(jié)
uchar Read_Byte(void) //read one bytefromflash
{
uchar Rdata;
char SFRPAGE_SAVE = SFRPAGE;
SFRPGCN=CONFIG_PAGE;
DBMODE = DBIN; //Data Bus input mode
DBus = 0xFF;
ALE =0;
CLE =0;
RE =1;
RE =0;
Rdata =DBus;
RE=1;
SFRPAGE = SFRPAGE_SAVE;
return(Rdata);
}
//------------------Block Erease------------------------------
uchar Block_Erase(uint page_number) //
{
uchar SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
DBMODE= DBOUT;
Comm_Latch_Cycle(0x60);
ALE=1;
CLE=0;
DBus=(uchar)page_number; //A9-A16
WE=0;
WE=1;
DBus=(uchar)(page_number>>8); //A17-A24
WE=0;
WE=1;
ALE=0;
CLE=0;
Comm_Latch_Cycle(0xD0);
SFRPAGE = CONFIG_PAGE;
RB = 1;
while(!RB);
SFRPAGE=SFRPAGE_SAVE;
return Status_Read(); //status=1,Bad Block; Status = 0, Erease OK
}
評(píng)論