⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c8051-m25p40.c

📁 这是C8051F020单片机读、擦、写串行EEROM芯片M25P40(4M)的驱动程序。全部为自己根据25p40的datasheet编写的
💻 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 + -