📄 c8051-m25p40.c
字号:
/************ 操作 EEROM *********/
#include <c8051f020.h>
#define uchar unsigned char
#define uint unsigned int
sbit eerom_cs = P0^6;
uchar romData[720];
void Write_enable()
{
eerom_cs = 0;
SPI0DAT = 0x06;
while(!SPIF){;}
SPIF = 0;
eerom_cs = 1;
}
void Write_disable()
{
eerom_cs = 0;
SPI0DAT = 0x04;
while(!SPIF){;}
SPIF = 0;
eerom_cs = 1;
}
uchar Read_status()
{
uchar Rdata;
eerom_cs = 0;
SPI0DAT = 0x05;
while(!SPIF){;}
SPIF = 0;
SPI0DAT = 0x00; // dummy byte input
while(!SPIF){;}
SPIF = 0;
Rdata = SPI0DAT;
eerom_cs = 1;
return Rdata;
}
uchar Read_data(unsigned long Address)
{
uchar Rdata;
uint HigAddress, MidAddress, LowAddress;
LowAddress = Address & 0xFF;
MidAddress = (Address >> 8) & 0xFF;
HigAddress = (Address >> 16) & 0x07;
eerom_cs = 0;
SPI0DAT = 0x03;
while(!SPIF){;}
SPIF = 0;
SPI0DAT = HigAddress;
while(!SPIF){;}
SPIF = 0;
SPI0DAT = MidAddress;
while(!SPIF){;}
SPIF = 0;
SPI0DAT = LowAddress;
while(!SPIF){;}
SPIF = 0;
SPI0DAT = 0x00; //dummy data input
while(!SPIF){;}
SPIF = 0;
Rdata = SPI0DAT;
eerom_cs = 1;
return Rdata;
}
void FastRead_data(unsigned long Address, uint dataNumber)
{
uint i, HigAddress, MidAddress, LowAddress;
LowAddress = Address & 0xFF;
MidAddress = (Address >> 8) & 0xFF;
HigAddress = (Address >> 16) & 0x07;
eerom_cs = 0;
SPI0DAT = 0x0B;
while(!SPIF){;}
SPIF = 0;
SPI0DAT = HigAddress;
while(!SPIF){;}
SPIF = 0;
SPI0DAT = MidAddress;
while(!SPIF){;}
SPIF = 0;
SPI0DAT = LowAddress;
while(!SPIF){;}
SPIF = 0;
SPI0DAT = 0x00; //dummy data input
while(!SPIF){;}
SPIF = 0;
for (i = 0; i < dataNumber; i++) // get the data of indicated number
{
SPI0DAT = 0x00; //dummy data input
while(!SPIF){;}
romData[i] = SPI0DAT;
SPIF = 0;
}
eerom_cs = 1;
}
void Page_program(unsigned long Address, uint dataNumber)
{
// note: the LowAddress should be 0x00
// before pape program, the bytes of memory need to have been erased.
uchar RegStatus;
uint i, HigAddress, MidAddress, LowAddress;
LowAddress = Address & 0xFF;
MidAddress = (Address >> 8) & 0xFF;
HigAddress = (Address >> 16) & 0x07;
RegStatus = Read_status();
while (RegStatus & 0x01);
Write_enable(); //?????????????????
eerom_cs = 0;
SPI0DAT = 0x02;
while(!SPIF){;}
SPIF = 0;
SPI0DAT = HigAddress;
while(!SPIF){;}
SPIF = 0;
SPI0DAT = MidAddress;
while(!SPIF){;}
SPIF = 0;
SPI0DAT = LowAddress;
while(!SPIF){;}
SPIF = 0;
for (i = 0; i < dataNumber; i++)
{
SPI0DAT = romData[i];
while(!SPIF){;}
SPIF = 0;
}
//Write_disable(); ??????? 放到函数外或者根本不需要,或者放到程序最后,即关掉芯片时再使用?
eerom_cs = 1; // initial the write progress
}
void Bulk_erase()
{
uchar RegStatus;
RegStatus = Read_status();
while (RegStatus & 0x01);
Write_enable();
eerom_cs = 0;
SPI0DAT = 0xC7;
while(!SPIF){;}
SPIF = 0;
eerom_cs = 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -