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

📄 at45db041.txt

📁 完成了AVR通过SPI接口实现了对AT45DB041的读写及其它各种操作。
💻 TXT
字号:
void SPI_Host_Init()
{
	DDRB|=0x07;
	PORTB|=0x0f;	
	SPCR=0x5c;	//SPI允许,主机模式,MSB,极性方式01,1/16系统时钟速率
	//SPCR=0x5C;	//SPI允许,主机模式,MSB,极性方式01,1/4系统时钟速率
}


unsigned char SPI_HostWrite_Read_Byte(unsigned char cData)
{
  SPDR = cData;
  while(!(SPSR&0x80));
  return SPDR;
}

/*
void SPI_HostWriteByte(unsigned char cData)
{
	//unsigned char u;
	//SPDR=0x00;	
	SPDR = cData;
	//u = SPDR;
 	// 等待传输结束 
	while(!(SPSR & (1<<SPIF)))
		;
}
*/
/*
unsigned char SPI_HostReadByte()
{
    while(!(SPSR & (1<<SPIF)))
		;
	//返回数据 
	return SPDR;
}
*/


unsigned char AT45DB041_StatusRegisterRead()
{ 
  unsigned char State_AT45DB041; 
          
  PORTB&=~BIT(0);   
  SPI_HostWrite_Read_Byte(0x57); 
  State_AT45DB041=SPI_HostWrite_Read_Byte(0x00); 
  PORTB|=BIT(0);
  return State_AT45DB041;   
} 

void jude_BusyState()
{
	while((AT45DB041_StatusRegisterRead()&0x80)!=128)
		;
}

/*******************************************************
描述: 主存读取指定页地址与字节地址固写长度

参数: 
          *Read_pHeader 读出数据存贮地址指针

          length   读入数据的长度,<=263

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

void AT45DB041_ContinuousArrayRead(unsigned char *Read_pHeader,unsigned int length)
{     
  unsigned int i;   
  jude_BusyState();
  PORTB&=~BIT(0);   
  SPI_HostWrite_Read_Byte(0xe8);   
  SPI_HostWrite_Read_Byte(address_history_16);   
  SPI_HostWrite_Read_Byte((address_history_128<<1)|(unsigned char)(address_history_264>>8)); 
  SPI_HostWrite_Read_Byte((unsigned char)address_history_264); 
  for(i=0;i<4;i++)
    {
      SPI_HostWrite_Read_Byte(0x00);
    } 
  for(i=0;i<length;i++)
    {
      Read_pHeader[i]=SPI_HostWrite_Read_Byte(0);
    } 
  PORTB|=BIT(0); 
} 


/*******************************************************
描述: 主存读取指定字节地址的1个单元字节


*******************************************************/
unsigned char AT45DB041_MainMemoryReadByte()
{
  unsigned char i;
  unsigned char R_Byte;   
  jude_BusyState();
  PORTB&=~BIT(0);   
  SPI_HostWrite_Read_Byte(0x52);   
  SPI_HostWrite_Read_Byte(address_history_16);   
  SPI_HostWrite_Read_Byte((address_history_128<<1)|(unsigned char)(address_history_264>>8)); 
  SPI_HostWrite_Read_Byte((unsigned char)address_history_264); 
  for(i=0;i<4;i++)
    {
      SPI_HostWrite_Read_Byte(0x00);
    } 
  R_Byte=SPI_HostWrite_Read_Byte(0);
  return R_Byte;
}




/*******************************************************
描述: 主存读取指定字节地址的n个单元字节<=263

参数:    page_adderss 主存的页地址<=2047

         

*******************************************************/
/*
void AT45DB041_MainMemoryRead_nBytes(unsigned char *p_nBytes,unsigned char n)
{
 unsigned char i;
 for(i=0;i<n;i++)
  {
    *p_nBytes=AT45DB041_MainMemoryReadByte();
	p_nBytes++;
	address_history_264++;
  }
  address_history_264=address_history_264-(n-1);
}
*/




/*******************************************************
描述:主存储页读取一页数据

参数:
          *Read_pHeader 读出数据存贮地址指针
          
*******************************************************/
void AT45DB041_MainMemoryRead(unsigned char *Read_pHeader)
{     
  unsigned int j;
  unsigned char i;   
  jude_BusyState();
  PORTB&=~BIT(0);    
  SPI_HostWrite_Read_Byte(0x52); 
  SPI_HostWrite_Read_Byte(address_history_16);   
  SPI_HostWrite_Read_Byte(address_history_128<<1);
  SPI_HostWrite_Read_Byte(0x00);
  for(i=0;i<4;i++)
    {
      SPI_HostWrite_Read_Byte(0x00);
    } 
  for(j=0;j<264;j++)
    {  
       Read_pHeader[j]=SPI_HostWrite_Read_Byte(0); 
    }
  PORTB|=BIT(0); 
} 


/*******************************************************
描述: 将指定的数据写入buffer1 中的指定地址(0-263)

参数: 

          *write_pHeader 待写入数据的指针

*******************************************************/
void AT45DB041_BufferWrite(unsigned char *write_pHeader)
{ 
  unsigned int i=0; 
  jude_BusyState();
  PORTB&=~BIT(0);
  
  SPI_HostWrite_Read_Byte(0x84);
 
  SPI_HostWrite_Read_Byte(0x00); 
  SPI_HostWrite_Read_Byte(0x00); 
  SPI_HostWrite_Read_Byte(0x00); 
  for(i=0;i<264;i++)
    {
      SPI_HostWrite_Read_Byte(*write_pHeader);
	  write_pHeader++;
    } 
  PORTB|=BIT(0);      
} 


/*******************************************************
描述:    一、 将指定的数据写入buffer1 中

          二、带擦除将buffer1 中的数据写入到指定的页中

参数:   

          *writ_pHeader 待写入数据的指针


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

void AT45DB041_BufferToMainMemoryPageProgramWithBuilt_inErase(unsigned char *write_pHeader)
{ 
  SREG &= 0x7f;
  AT45DB041_BufferWrite(write_pHeader);
  jude_BusyState();
  PORTB&=~BIT(0);      
  SPI_HostWrite_Read_Byte(0x83);
  SPI_HostWrite_Read_Byte(address_history_16); 
  SPI_HostWrite_Read_Byte(address_history_128<<1); 
  SPI_HostWrite_Read_Byte(0x00); 
  PORTB|=BIT(0); 
  SREG |= 0x80;
}

/********************************************************************************* 
* 函数原型:void AT45DB041B_Buffer2Read(unsigned char *pHeader);                                       
* 名   称:AT45DB041B_BufferRead 
* 功   能:读buffer2中指定首地址的数据块到数组
* 入口参数: 
*                  
*             pHeader - 待写入数据的首地址(指针)                             
*                                            
* 出口参数:无 
**********************************************************************************/ 
void AT45DB041B_Buffer2Read(unsigned char *pHeader) 
{ 
  unsigned int j=0; 
  jude_BusyState();
  PORTB&=~BIT(0);
  SPI_HostWrite_Read_Byte(0x56);
  SPI_HostWrite_Read_Byte(0x00); 
  SPI_HostWrite_Read_Byte(0x00);
  SPI_HostWrite_Read_Byte(0x00);
  SPI_HostWrite_Read_Byte(0x00);
  for(j=0;j<264;j++) 
  { 
    pHeader[j]=SPI_HostWrite_Read_Byte(0x00);
  } 
  PORTB|=BIT(0); 
} 

/********************************************************************************* 
* 函数原型:void AT45DB041B_Buffer1Read(unsigned char *pHeader);                                       
* 名   称:AT45DB041B_BufferRead 
* 功   能:读buffer1中指定首地址的数据块到数组
* 入口参数: 
*                  
*             pHeader - 待写入数据的首地址(指针)                             
*                                            
* 出口参数:无 
**********************************************************************************/ 
void AT45DB041B_Buffer1Read(unsigned char *pHeader) 
{ 
  unsigned int j=0; 
  jude_BusyState();
  PORTB&=~BIT(0);
  SPI_HostWrite_Read_Byte(0x54);
  SPI_HostWrite_Read_Byte(0x00); 
  SPI_HostWrite_Read_Byte(0x00);
  SPI_HostWrite_Read_Byte(0x00);
  SPI_HostWrite_Read_Byte(0x00);
  for(j=0;j<264;j++) 
  { 
    pHeader[j]=SPI_HostWrite_Read_Byte(0x00);
  } 
  PORTB|=BIT(0); 
} 

/********************************************************************************* 
* 函数原型:void AT45DB041B_Buffer2Read_nByte(unsigned char *pHeader,unsigned char len);                                       
* 名   称:AT45DB041B_BufferRead 
* 功   能:读buffer2中指定首地址的数据块到数组
* 入口参数: 
*             len     -读出数据长度
*             pHeader - 待写入数据的首地址(指针)                             
*                                            
* 出口参数:无 
**********************************************************************************/ 
void AT45DB041B_Buffer2Read_nByte(unsigned char *pHeader,unsigned char len) 
{ 
  unsigned int j=0; 
  jude_BusyState();
  PORTB&=~BIT(0);
  SPI_HostWrite_Read_Byte(0x56);
  SPI_HostWrite_Read_Byte(0x00); 
  SPI_HostWrite_Read_Byte(0x00|(unsigned char)(address_history_264>>8));
  SPI_HostWrite_Read_Byte((unsigned char)address_history_264);
  SPI_HostWrite_Read_Byte(0x00);
  for(j=0;j<len;j++) 
  { 
    pHeader[j]=SPI_HostWrite_Read_Byte(0x00);
  } 
  PORTB|=BIT(0); 
} 



/********************************************************************************* 
* 函数原型:void SPI_HostFlashToBuffer2(void);                                       
* 名   称:SPI_HostFlashToBuffer 
* 功   能:将FLASH中的数据传送到buffer2中 
* 入口参数: 
*                         
* 出口参数:无 
**********************************************************************************/ 
void SPI_HostFlashToBuffer2(void) 
{ 
  unsigned int j=0; 
  jude_BusyState();
  PORTB&=~BIT(0);
  SPI_HostWrite_Read_Byte(0x55); 
  SPI_HostWrite_Read_Byte(address_history_16); 
  SPI_HostWrite_Read_Byte(address_history_128<<1); 
  SPI_HostWrite_Read_Byte(0x00); 
  PORTB|=BIT(0);
} 


/********************************************************************************* 
* 函数原型:unsigned char SPI_HostFlashToBuffer1Compare(void);                                       
* 名   称:SPI_HostFlashToBuffer1Compare
* 功   能:将FLASH中的数据与buffer1中内容比较
* 入口参数: 
*                         
* 出口参数: Compare
**********************************************************************************/ 
unsigned char SPI_HostFlashToBuffer1Compare(void)
{
  unsigned char Compare;
  jude_BusyState();
  PORTB&=~BIT(0);
  SPI_HostWrite_Read_Byte(0x60); 
  SPI_HostWrite_Read_Byte(address_history_16); 
  SPI_HostWrite_Read_Byte(address_history_128<<1); 
  SPI_HostWrite_Read_Byte(0x00); 
  PORTB|=BIT(0);
  
  Compare=AT45DB041_StatusRegisterRead();
  return Compare;
}


/********************************************************************************* 
* 函数原型:unsigned char SPI_HostFlashToBuffer2Compare(void);                                       
* 名   称:SPI_HostFlashToBuffer2Compare
* 功   能:将FLASH中的数据与buffer1中内容比较
* 入口参数: 
*                         
* 出口参数: Compare
**********************************************************************************/ 
unsigned char SPI_HostFlashToBuffer2Compare(void)
{
  unsigned char Compare;
  jude_BusyState();
  PORTB&=~BIT(0);
  SPI_HostWrite_Read_Byte(0x61); 
  SPI_HostWrite_Read_Byte(address_history_16); 
  SPI_HostWrite_Read_Byte(address_history_128<<1); 
  SPI_HostWrite_Read_Byte(0x00); 
  PORTB|=BIT(0);
  
  Compare=AT45DB041_StatusRegisterRead();
  return Compare;
}

⌨️ 快捷键说明

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