微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > C8051F12X操作FLASH存储器程序

C8051F12X操作FLASH存储器程序

时间:11-30 来源:互联网 点击:
DBMODE 为数据线的输入输出模式 为单片机的PXMDOUT
DBOUT 为输出模式
DBus 为数据端口 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为页地址,从addr页第0字节开始
{
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;
}
//读状态,状态正确时返回0,错误时返回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读一个字节
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
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top