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

📄 nx25p40.c.c

📁 大容量flash存储芯片nx25p40/80/16/32的单片机驱动程序
💻 C
字号:

/************************************************************************************
		
* File    : nx25p40.c												
* Author  : 洪运富 ,hongyunfu@sohu.com                                              
* description: nx25p40/80/160驱动程序
* Date:2005-8-4							                    
*************************************************************************************
*/
#define _FLASH_C

sbit CS       = P1^4; /* w25p40 cs,0 active*/
sbit SO       = P1^5; /*SPI flash data out pin*/
sbit SI       = P1^6; /*SPI flash data in pin*/
sbit SK       = P1^7;

uchar bdata sbyte;
sbit s0=sbyte^0;
sbit s1=sbyte^1;
sbit s2=sbyte^2;
sbit s3=sbyte^3;
sbit s4=sbyte^4;
sbit s5=sbyte^5;
sbit s6=sbyte^6;
sbit s7=sbyte^7;


/*读取8bit数据*/
uchar Read8(void)
{
	SK=1;SK=0;s7 = SO;
	SK=1;SK=0;s6 = SO;
	SK=1;SK=0;s5 = SO;
	SK=1;SK=0;s4 = SO;
	SK=1;SK=0;s3 = SO;
	SK=1;SK=0;s2 = SO;
	SK=1;SK=0;s1 = SO;
	SK=1;SK=0;s0 = SO;
	return(sbyte);
}

/*向flash中写入一个byte数据*/
void Write8(uchar cData)
{
	sbyte = cData;
	SI = s7; SK=0;SK=1;
	SI = s6; SK=0;SK=1;
	SI = s5; SK=0;SK=1;
	SI = s4; SK=0;SK=1;
	SI = s3; SK=0;SK=1;
	SI = s2; SK=0;SK=1;
	SI = s1; SK=0;SK=1;
	SI = s0; SK=0;SK=1;
}

/*读状态寄存器*/
uchar ReadSR(void)
{
	uchar cData;
	CS=0;
	Write8(RDSR);
	cData=Read8();
	CS=1;
	return cData;
}
/*
***********************************************************************
                                    write status register
cData:传递过来写入寄存器的值
功能:可设置flash的保护区域扇区
for:NX25P40
state register bit
7   6    5     4    3    2    1    0
               BP2 BP1 BP0
               000   none of the flash sector is protected
               001   保护第七扇区数据写操作,只读upper 1/8            
               010   upper 1/4
               011   upper 1/2
               1xx    all
bit7: in conjunction with the WP pin, 0 factory default
只有 7,4,3,2四个bit可写,其它位对寄存器操作不改变
cData:000,001,00    0x04  第七扇区写保护
cData:000,000,00    0x00  全部可写
***********************************************************************
*/
void WriteSR(uchar cData)
{
	while((ReadSR()&0x01)==1)
	{;}
	CS=0;
	Write8(WREN);
	CS=1;
	CS=0;
	Write8(WRSR);
	Write8(cData);
	CS=1;

}


/*
***********************************************************************
                      写入数据操作
cData:传递过来需要写入的数据地址,指针变量
Address:写入地址
N:写入字节个数
Return:无
Author:洪运富 hongyunfu@sohu.com
Date:2005-8-4
***********************************************************************
*/
void Write(uchar xdata *cData, uchar xdata *cAddress, uchar N)
{
	uchar i;
	while((ReadSR()&0x01)==1)
	{;}
	CS=0;
	Write8(WREN);
	CS=1;
	CS=0;
	Write8(PAGEPRO);
	Write8(*(cAddress+2));
	Write8(*(cAddress+1));
	Write8(*cAddress);
	for(i=0;i<N;i++)
		Write8(*(cData+i));
	SK=0;
	CS=1;
}
/*
***********************************************************************
                      读取数据操作
cData:存储读取数据指针地址                      
Address:读取起始地址,指针变量
N:读取字节个数
Return:无
Author:洪运富
Date:2005-8-4
***********************************************************************
*/

void Read(uchar xdata *cData,uchar xdata *cAddress,uchar N)
{
	uchar i;
	while((ReadSR()&0x01)==1)
	{;}
	CS=0;
	Write8(READDATA);
	Write8(*(cAddress+2));
	Write8(*(cAddress+1));
	Write8(*cAddress);
	for(i=0;i<N;i++)
	   *(cData+i)=Read8();
	CS=1;
}


/*
***********************************************************************
              块擦除flash
传递参数:n,待擦除扇区
Return:无
Author:洪运富
Date:2005-8-4
***********************************************************************
*/
/*
void SectorErase(uchar n)
{
	while((ReadSR()&0x01)==1)
	{;}
	CS=0;
	Write8(WREN);
	CS=1;
	CS=0;
	Write8(SECTORERA);
	Write8(n);
	Write8(0);
	Write8(0);
	CS=1;
}*/


/*
***********************************************************************
            全部擦除flash
Return:无
Author:洪运富
Date:2005-8-4
***********************************************************************
*/

void AllErase(void)
{
	while((ReadSR()&0x01)==1)
	{;}
	CS=0;
	Write8(WREN);
	CS=1;
	CS=0;
	Write8(ERASEALL);
	CS=1;
}

/**********************************************************************
Manufacturer ID  90h dummy dummy 00h  M7-M0  ID7-ID0

Manufacturer ID (M7-M0)
NexFlash EFh
Device ID    ID7-ID0
NX25P10        10h    2
NX25P20	       11h    4
NX25P40	       12h    8		
NX25P80	       13h    16
NX25P16	       14h    32
NX25P32	       15h    64    

Description:读取flash型号
************************************************************************
*/
uchar ReadDeviceID(void)
{
	uchar ManufacturerID;
	uchar DeviceID;

	while((ReadSR()&0x01)==1)
	{;}
	CS=0;
	Write8(0x90);
	Write8(0x00);
	Write8(0x00);
	Write8(0x00);

	ManufacturerID = Read8();
	DeviceID       = Read8();
	CS=1;

	if(ManufacturerID == 0xEF)
	{
		if      (DeviceID == 0x10)    // W25P10
			return 2;
		else if(DeviceID == 0x11)
			return 4;
		else if(DeviceID == 0x12)
			return 8;
		else if(DeviceID == 0x13)
			return 16;
		else if(DeviceID == 0x14)
			return 32;
		else if(DeviceID == 0x15)
			return 64;	
		else return 0;
	}	
	else 
		return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -