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

📄 spiflash.c

📁 此为SPI通信模块
💻 C
📖 第 1 页 / 共 2 页
字号:
    SPI_HostWriteByte((unsigned char)(PA<<2));  
    SPI_HostWriteByte(0x00);
    for(i=0;i<4;i++)
       {
         SPI_HostWriteByte(0x00); 
       } 
     
    for(i=0;i<528;i++)
      {
        pHeader[i]=SPI_HostReadByte();  //连续读取len长度的数据
      } 
    AT45DB161D_ChipSelect(1,1); 
} 

 
/******************************************************************************/ 
/*描述:                                                                      */ 
/*    将指定页数据读BUFFER中。                       */ 
/*参数:                                                                      */ 
/*    buffer  - 选择BUFFER,01H选择BUFFER 1,02H选择BUFFER 2                  */ 
/*              在该指令序列中,操作码84H选择BUFFER 1,87H选择BUFFER 2        */ 
/*    BFA     - BUFFER中的起始地址,0~528                                     */ 
/*    pHeader - 待存数据的头指针                                              */ 
/*    len     - 待存数据的长度1~528                                           */ 
/******************************************************************************/ 
void AT45DB161D_BufferRead(uchar buffer,uint PA)
{ 
    unsigned int i=0; 
     
    while(i++<255)
         {
           if(AT45DB161D_StatusRegisterRead()&0x80)
             {
               break;
             }
         }
          
    AT45DB161D_ChipSelect(1,0);
         
    switch(buffer)
       { 
        case 1:SPI_HostWriteByte(0x53);break; //main memory page to buffer 1 transfer
        case 2:SPI_HostWriteByte(0x55);break; //main memory page to buffer 2 transfer
       }

    SPI_HostWriteByte((unsigned char)(PA>>6)); 
    SPI_HostWriteByte((unsigned char)(PA<<2)); 
    SPI_HostWriteByte(0x00); 
        
    AT45DB161D_ChipSelect(1,1);         
}


/******************************************************************************/ 
/*描述:                                                                      */ 
/*    将指定数据写入从某个地址(0~528)开始的BUFFER中。                       */ 
/*参数:                                                                      */ 
/*    buffer  - 选择BUFFER,01H选择BUFFER 1,02H选择BUFFER 2                  */ 
/*              在该指令序列中,操作码84H选择BUFFER 1,87H选择BUFFER 2        */ 
/*    BFA     - BUFFER中的起始地址,0~528                                     */ 
/*    pHeader - 待存数据的头指针                                              */ 
/*    len     - 待存数据的长度1~528                                           */ 
/******************************************************************************/ 
void AT45DB161D_BufferWrite(uchar buffer,uint BFA,uchar *pHeader,uint len)
{ 
    unsigned int i=0;
     
    while(i++<255)
         {
           if(AT45DB161D_StatusRegisterRead()&0x80)
             {
               break;
             }
         }
          
    AT45DB161D_ChipSelect(1,0);
         
    switch(buffer)
       { 
        case 1:SPI_HostWriteByte(0x84);break; 	 //buffer 1 write
        case 2:SPI_HostWriteByte(0x87);break; 	 //buffer 2 write
       }

    SPI_HostWriteByte(0x00); 
    SPI_HostWriteByte((unsigned char)(BFA>>8)); 
    SPI_HostWriteByte((unsigned char)BFA); 
     
    for(i=0;i<len;i++)
       {
         SPI_HostWriteByte(pHeader[i]);//连续写入len长度的数据
       }
        
    AT45DB161D_ChipSelect(1,1);         
}  
/******************************************************************************/ 
/*描述:                                                                      */ 
/*    将指定数据写入从某个地址(0~528)开始的页中:包含2个动作,首先将指定数据*/ 
/*    写入到BUFFER 1或者BUFFER 2中,其中可以指定BUFFER中的起始写入地址,此写入*/ 
/*    动作不影响BUFFER中其它地址中的数据,然后再将BUFFER中的整个数据写入到某指*/ 
/*    定页中(带预擦除)。                                                      */ 
/*参数:                                                                      */ 
/*    buffer  - 选择BUFFER,01H选择BUFFER 1,02H选择BUFFER 2                  */ 
/*    PA      - 页地址,0~4096                                                */ 
/*    BFA     - 指定BUFFER中的起始写入地址                                    */ 
/*    pHeader - 指定数据的首地址                                              */ 
/*    len     - 指定数据的长度                                                */ 
/******************************************************************************/ 
void AT45DB161D_BufferToMainMemoryPageProgramWithBuilt_inErase(uchar buffer,uint PA,uint BFA,uchar *pHeader,uint len)
{ 
    unsigned int i=0;

    AT45DB161D_BufferWrite(buffer,BFA,pHeader,len); 

    while(i++<1000)
        {
          if(AT45DB161D_StatusRegisterRead()&0x80)
            {
              break;
            }
        } 
             
    AT45DB161D_ChipSelect(1,0); 
            
    switch(buffer)
       { 
        case 1:SPI_HostWriteByte(0x83);break;  // Buffer 1 to Main Memory Page Program with Built-in Erase
        case 2:SPI_HostWriteByte(0x86);break;  // Buffer 2 to Main Memory Page Program with Built-in Erase
       }
        
    SPI_HostWriteByte((unsigned char)(PA>>6)); 
    SPI_HostWriteByte((unsigned char)(PA<<2)); 
    SPI_HostWriteByte(0x00); 

    AT45DB161D_ChipSelect(1,1); 
} 
/******************************************************************************/ 
/*描述:                                                                      */ 
/*    与上一个函数的唯一区别是不带预擦除。                                    */ 
/******************************************************************************/
/* 
void AT45DB161D_BufferToMainMemoryPageProgramWithoutBuilt_inErase(uchar buffer,uint PA,uint BFA,uchar *pHeader,uint len)
{ 
    unsigned int i=0; 

    AT45DB161D_BufferWrite(buffer,BFA,pHeader,len); 

    while(i++<1000)
    {
      if(AT45DB161D_StatusRegisterRead()&0x80)
        {
          break;
        }
    } 
     
    AT45DB161D_ChipSelect(1,0); 

    SPI_HostWriteByte(0x87+buffer);
     
    SPI_HostWriteByte((unsigned char)(PA>>6)); 
    SPI_HostWriteByte((unsigned char)(PA<<2)); 
    SPI_HostWriteByte(0x00); 
     
  //for(i=0;i<len;i++)
  //     {
  //       SPI_HostWriteByte(pHeader[i]);
  //     } 
             
    AT45DB161D_ChipSelect(1,1); 
} 
*/

/******************************************************************************/ 
/*描述:                                                                      */ 
/*    页擦除函数                                  */ 
/******************************************************************************/ 
/*void AT45DB161D_Page_inErase(uint16 PA)
{ 
   uint16 j=0;
    while(j++<1000)
    {
      if(AT45DB161D_StatusRegisterRead()&0x80)
        {
          break;
        }
    } 
     
    AT45DB161D_ChipSelect(1,0); 

    SPI_HostWriteByte(0x81);
         
    SPI_HostWriteByte((unsigned char)(PA>>6)); 
    SPI_HostWriteByte((unsigned char)(PA<<2)); 
    SPI_HostWriteByte(0x00); 
     
    AT45DB161D_ChipSelect(1,1); 
} 

void AT45DB161D_Chip_inErase(void)
{ 
   uint j=0;
    while(j++<1000)
    {
      if(AT45DB161D_StatusRegisterRead()&0x80)
        {
          break;
        }
    } 
     
    AT45DB161D_ChipSelect(1,0); 

    SPI_HostWriteByte(0xc7);     
    SPI_HostWriteByte(0x94); 
    SPI_HostWriteByte(0x80); 
    SPI_HostWriteByte(0x9a); 
     
    AT45DB161D_ChipSelect(1,1); 
} 
*/

⌨️ 快捷键说明

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