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

📄 at25320.c

📁 Provide the operations include Read and Write to the AT25320A, SPI-interface EEPROM with 32k bits,AT
💻 C
字号:
/********************************************************************
 
Name	 : AT25320.C
Function : Provide the operations include Read and Write to the 
		   AT25320A, SPI-interface EEPROM with 32k bits,ATMEL Icn.
Author	 : Pan.L.F
Date	 : 2006-02-19
**********************************************************************/

/*********************************************************************

Modify	:
Version	:
Modifier:
Date	:
*********************************************************************/

#include 	<intrins.h> 
#include	"includes.h"
#include	"AT25320.H"


/*  AT25320 op_code */
#define	WREN	6	//	0000 x110,write enable
#define	WRDI	4	//	0000 x100,write disable
#define	RDSR	5	//	0000 x101,read status register
#define	WRSR	1	//	0000 x001,write status register
#define	READ	3	//	0000 x011,read sequence
#define	WRITE	2	//	0000 x010,write sequence


/*-------------------------------------------------------
             SPI_WriteByte()

Function:  Send a byte of data to the device using
           SPI interface. 
Input:     the data(one byte) want to send.
Output:    None.
NOTE:	   Using SPI Mode 3/0
--------------------------------------------------------*/
void SPI_WriteByte(uchar spi_write_data)
{
    uchar temp=0;
	uchar i=0;
    temp=spi_write_data;   
   
    for(i=0;i<8;i++)  //send 8 bits
    {
		AT25320_SCK=0;  
    
	    if(temp & 0x80)//MSB first		
			AT25320_SI=1;
		else
			AT25320_SI=0;
		 
		temp=temp<<1;    

	    AT25320_SCK=1; 	 			
    }	

}  
   


/*-------------------------------------------------------
             SPI_ReadByte()

Function:  Read a byte of data from the device using
           SPI interface. 
Input:     None.
Output:    The data(one byte) been read,as a return value.
NOTE: 	   Using SPI Mode 3/0
--------------------------------------------------------*/
uchar SPI_ReadByte(void)
{
	uchar temp=0;
	uchar i=0;
    bit bit_in;

    temp=0x00;//initialize the data output
   
    for(i=0;i<8;i++) 
	{
   		AT25320_SCK=1;//Data is output on the SO line by the falling edge of SCK.  
		temp=temp << 1;  		  	
     	AT25320_SCK=0;
     	bit_in=AT25320_SO;     	
     	if(bit_in) temp=temp | 0x01;     	
  	}

  	return (temp);
}



/*---------------------------------------------------------
             AT25_GetStatusReg()

Function:  Read the Status Register of the AT25320A
Input:     None.
Output:    Content of the status register,as a return value.
-----------------------------------------------------------*/
uchar AT25_GetStatusReg(void)
{
	uchar temp;
    
	AT25320_CS=0;

	SPI_WriteByte(RDSR);
	temp=SPI_ReadByte();

	AT25320_CS=1;	
	return temp;
}


/*---------------------------------------------------------
             AT25_IsReady()

Function:  Return 0 if AT25320A is busy and 1 if ready.
-----------------------------------------------------------*/
bit AT25_IsReady(void)
{
	
	if(0x01 & AT25_GetStatusReg()) return 0;
	else return 1;
}



/*---------------------------------------------------------
             AT25_SetStatusReg()

Function:  Set the Status Register of the AT25320A
Input:     The Byte of the status register. NOTE: Only 
		   bit7(WPEN),bit3(BP1) and bit2(BP0) can be changed,
		   so this byte should in the form of: x000 xx00		
Output:    None. 
-----------------------------------------------------------*/
void AT25_SetStatusReg(uchar status_data)
{	
	/* wait when device is busy */
	while(!AT25_IsReady());

	/* make sure that the device is write enabled */
	AT25320_CS=0;
	_nop_();
	SPI_WriteByte(WREN);
	AT25320_CS=1;
	_nop_();
	_nop_();

	AT25320_CS=0;
	_nop_();	
	SPI_WriteByte(WRSR);
	SPI_WriteByte(status_data & 0x8C);	
	AT25320_CS=1;	
}




/*---------------------------------------------------------
             AT25_ReadArray()

Function:  Read  data streams from AT25320A.
Input:     address,num_of_byte,destination
Output:    No return value.
NOTE:	   The function do not report error if the parameters
		   are wrong(eg.address>4096),it modify the parameters.
		   So be careful.	
-----------------------------------------------------------*/
void AT25_ReadArray(uint address, 		//from this address;
					uint num_of_byte,	//the number of bytes to read;
					uchar* destination	//store the result.
					)
{	
	uint i=0;

	/* Filter the parameters */
	if(num_of_byte>4096) num_of_byte=1;
	if(address>4096) address=0;
	
	/* wait when device is busy */
	while(!AT25_IsReady());

	AT25320_CS=0;//Enable the AT25320A.
	_nop_();
	
	SPI_WriteByte(READ);//op_code
	SPI_WriteByte((uchar)((address & 0x0F00)>>8)); 	//write address A11~A0
	SPI_WriteByte((uchar)(address & 0x00FF));		
	

	for(i=0;i<num_of_byte;i++)
	{
		*destination=SPI_ReadByte();
		destination++;
	}

	AT25320_CS=1;//Disable the AT25320A.
	
}	



/*---------------------------------------------------------
             AT25_ReadByte()

Function:  Read only one byte from the AT25320A,as a return 
		   value.
-----------------------------------------------------------*/
/*
uchar AT25_ReadByte(uint address)
{
	uchar temp[1];

	AT25_ReadArray(address,1,&temp);
	return temp[0];	
}
*/


/*---------------------------------------------------------
             AT25_WritePage()

Function:  Write data(<=32 bytes) to the AT25320A.
Input:     address,num_of_byte,source
Output:    No return value.
NOTE:	   The function do not report error if the parameters
		   are wrong(eg.address>4096),it modify the parameters.
		   So be careful.	
-----------------------------------------------------------*/
void AT25_WritePage(uint address, 		//from this address;
					uchar num_of_byte,	//the number(<32) of bytes to write;
					uchar* source		//data to write.
					)
{	
	uchar i=0;
		
	/* Filter the parameters */
	if(num_of_byte>32) num_of_byte=32;
	if(address>4096) address=0;

	/* make sure that the device is ready */
	/* NOTE:No time out strategy here.    */
	while(!AT25_IsReady());

	/* make sure that the device is write enabled */
	AT25320_CS=0;
	_nop_();
	SPI_WriteByte(WREN);
	AT25320_CS=1;
	_nop_();
	_nop_();

	/* write op_code,address and data into the device */
	AT25320_CS=0;
	_nop_();
	SPI_WriteByte(WRITE);//op_code
	SPI_WriteByte((uchar)((address & 0x0F00)>>8)); 	//write address A11~A0
	SPI_WriteByte((uchar)(address & 0x00FF));		
	

	for(i=0;i<num_of_byte;i++)
	{
		SPI_WriteByte(*source);
		source++;	
	}
	AT25320_CS=1;
	
}



/*---------------------------------------------------------
             Init_AT25320()

Function:  Initialize the AT25320A.
Input:     None.
Output:    None.
NOTE:	   This function initialize the AT25320A with no 
		   data protected and #WP disable.	
-----------------------------------------------------------*/
void Init_AT25320(uchar SetSR)
{
	AT25_SetStatusReg(SetSR);
}



⌨️ 快捷键说明

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