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

📄 spiflash.c

📁 此为SPI通信模块
💻 C
📖 第 1 页 / 共 2 页
字号:
//Header:   V-I_measureRecorder
//Filename: V-I_measureRecorder
//Author:	LW
//Date:	    2008.06.18-2008.07.05
//Version:  
//Desciption:数据存储模块
#include   "IO.h"

void  AT45DB161D_ChipSelect(uchar select,bit hl); //Flash存储器片选
void  AT45DB161D_ContinuousArrayRead(uint PA,uint BFA,uchar *pHeader,uint len);
void  AT45DB161D_BufferToMainMemoryPageProgramWithBuilt_inErase(uchar buffer,uint PA,uint BFA,uchar *pHeader,uint len);
void  AT45DB161D_BufferRead(uchar buffer,uint PA);
void  AT45DB161D_MainMemoryPageRead(uint PA,uchar *pHeader);
//void  AT45DB161D_Chip_inErase(void);
 /*****************************************************************************/ 
/*正常操作电压为2.7~3.6V,实验中发现当电压超过4.25V后读出的状态字节为9A(正常 */ 
/*的状态字节值为9D),并且读写数据均不准确,所以应当保证卡片的供电电压不超过  */ 
/*4.25V。                                                                     */ 
/*SPI规范:Data is always clocked into the device on the rising edge of SCK a-*/ 
/*    nd clocked out of the device on the falling edge of SCK.All instruction-*/ 
/*    s,addresses and data are transferred with the most significant bit(MSB) */ 
/*    first.                                                                  */ 
/*                                                                  2005-06-02*/ 
/******************************************************************************/ 
//#pragma    optimize(5) 

/*void  AT45DB161D_ChipSelect(uchar select,bit hl) // uchar select 这里只取1,2,3表示使用那片FLASH,hl表示片选信号的高低
{
   switch(select)
     {
       case 1:NSS=hl;break;
       case 2:NSS=hl;break;
       case 3:NSS=hl;break;
       default:break;
     }
}*/

void  AT45DB161D_ChipSelect(uchar select,bit hl) // uchar select 这里只取1,2,3表示使用那片FLASH,hl表示片选信号的高低
{
   select=select;
//   NSS=hl;
   CS=hl;
}

/*
unsigned char SPI_HostReadByte(void)
{ 
   unsigned char i,rByte=0; 
    //EA=0; 
    for(i=0;i<8;i++)
      { 
        SCK=0; 
        SCK=1;  
        rByte<<=1; 
        rByte|=MISO;                                                                      
      }
	//EA=1;   
    return rByte; 
	    
}

 
void SPI_HostWriteByte(unsigned char wByte)
{ 
    unsigned char i; 
	//EA=0;
    for(i=0;i<8;i++)
      { 
        if((wByte<<i)&0x80)
           {
             MOSI=1;
           } 
        else
           {
             MOSI=0;
           } 
         
        SCK=0; 
        SCK=1; 
      }
   //EA=1;       
} */


void SPI_HostWriteByte(unsigned char ch)
{
  SPIF = 0;
  SPI0DAT = ch;
  while (SPIF == 0);      // 等待写结束
} 

unsigned char SPI_HostReadByte(void)
{
        SPIF = 0;
  SPI0DAT = 0;
  while (SPIF == 0);
  return  SPI0DAT;      // 等待读结束
}

/******************************************************************************/ 
/*Status Register Format:                                                     */ 
/*   -----------------------------------------------------------------------  */ 
/*  |  bit7  |  bit6  |  bit5  |  bit4  |  bit3  |  bit2  |  bit1  |  bit0  | */ 
/*  |--------|--------|--------|--------|--------|--------|--------|--------| */ 
/*  |RDY/BUSY|  COMP  |   0    |   1    |   1    |   1    |   X    |   X    | */ 
/*   -----------------------------------------------------------------------  */ 
/*  bit7 - 忙标记,0为忙1为不忙。                                             */ 
/*         当Status Register的位0移出之后,接下来的时钟脉冲序列将使SPI器件继续*/ 
/*         将最新的状态字节送出。                                             */ 
/*  bit6 - 标记最近一次Main Memory Page和Buffer的比较结果,0相同,1不同。     */ 
/*  bit5                                                                      */ 
/*  bit4                                                                      */ 
/*  bit3                                                                      */ 
/*  bit2 - 这4位用来标记器件密度,对于AT45DB161D,这4位应该是0111,一共能标记 */ 
/*         16种不同密度的器件。                                               */ 
/*  bit1                                                                      */ 
/*  bit0 - 这2位暂时无效                                                      */ 
/******************************************************************************/ 
unsigned char AT45DB161D_StatusRegisterRead(void)
{ 
    unsigned char i=0; 
                 
    AT45DB161D_ChipSelect(1,0);     
    SPI_HostWriteByte(0xd7); //Status Register Read
    i=SPI_HostReadByte(); 
    AT45DB161D_ChipSelect(1,1);      
    return i;     
} 

/*
unsigned char AT45DB161D_StatusRegisterRead1(void)
{ 
    unsigned char i; 
                 
    AT45DB161D_ChipSelect(1,0);     
    SPI_HostWriteByte(0x9f); //Status Register Read
    i=SPI_HostReadByte(); 
    AT45DB161D_ChipSelect(1,1);      
    return i;     
}*/
/******************************************************************************/ 
/*描述:                                                                      */ 
/*    When the last bit in the main memory array has been read,the device will*/ 
/*    continue reading back at the beginning of the first page of memory.As w-*/ 
/*    ith crossing over page boundaries,no delays will be incurred when wrapp-*/ 
/*    ing around from the end of the array to the beginning of the array.     */ 
/*参数:                                                                      */ 
/*    PA      - 页地址,0~4096                                                */ 
/*    BFA     - 指定BUFFER中的起始写入地址                                    */ 
/*    pHeader - 指定数据的首地址                                              */ 
/*    len     - 指定数据的长度                                                */ 
/******************************************************************************/ 
/*void AT45DB161D_ContinuousArrayRead(uint PA,uint BFA,uchar *pHeader,uint len)	  //2008.06.24
{        
    static unsigned int i;       
     
    while(i++<255)
         {
            if(AT45DB161D_StatusRegisterRead()&0x80) //busy?
              {
                break;
              }
         }
          
    AT45DB161D_ChipSelect(1,0);  
       
    SPI_HostWriteByte(0xe8);               //  Continuous Array Read (Legacy Command) 
    
    SPI_HostWriteByte((unsigned char)(PA>>6));     
    SPI_HostWriteByte((unsigned char)((PA<<2)|(BFA>>8))); 
    SPI_HostWriteByte((unsigned char)BFA);*/
    
/******************************************************************************/
/*     To perform a continuous read from the binary page size (512 bytes), the 
/*  opcode (E8H) must be clocked into the device followed by three address bytes
/* and 4 don’t care bytes.
/******************************************************************************/ 
/*    for(i=0;i<4;i++)
       {
         SPI_HostWriteByte(0x00); 
       } 
     
    for(i=0;i<len;i++)
      {
        pHeader[i]=SPI_HostReadByte();  //连续读取len长度的数据
      } 
    AT45DB161D_ChipSelect(1,1); 
} */                                       


 /******************************************************************************/ 
/*描述:                                                                      */ 
/*    When the last bit in the main memory page has been read,the device will*/ 
/*    continue reading back at the beginning of the first page of memory.As w-*/ 
/*    ith crossing over page boundaries,no delays will be incurred when wrapp-*/ 
/*    ing around from the end of the array to the beginning of the array.     */ 
/*参数:                                                                      */ 
/*    PA      - 页地址,0~4096                                                */ 
/*    BFA     - 指定BUFFER中的起始写入地址                                    */ 
/*    pHeader - 指定数据的首地址                                              */ 
/*    len     - 指定数据的长度                                                */ 
/******************************************************************************/ 
void AT45DB161D_MainMemoryPageRead(uint PA,uchar *pHeader)
{        
    unsigned int i=0;       
     
    while(i++<255)
         {
            if(AT45DB161D_StatusRegisterRead()&0x80) //busy?
              {
                break;
              }
         }
          
    AT45DB161D_ChipSelect(1,0);  
       
    SPI_HostWriteByte(0xd2);               //  main memory page Read (Legacy Command) 
    
    SPI_HostWriteByte((unsigned char)(PA>>6));     

⌨️ 快捷键说明

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