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

📄 spi_con.txt

📁 at45db321驱动程序
💻 TXT
📖 第 1 页 / 共 2 页
字号:
          (2)the start address of the buffer.-----address
          (3)which page will be write?(0~2047)-------page
          (4) how many bytes will be write?-----------num
          (5)a pointer poited to the first address of
             the array in which you store your data.

Output:   No.
Attention: (1)This operation write data to buffer,until the
            /cs change from low to high, then load the
            data from buffer to the choiced page.
            the initial stream of data is:

           0x82 (use buffer 1)
           0x85--xxxx PA10~PA7--PA6~PA0 BFA8--BFA7~BFA0

------------------------------------------------------*/
static void AT45_MemoryPageWrite(unsigned char buffer_choice,unsigned int page,unsigned int address,unsigned int num,unsigned char *string)
{
unsigned int i;
unsigned char temp;
IsBusy();
CS_0;
#asm("nop");
#asm("nop");
//send command stream of data
if(buffer_choice)
{
    SPI_WriteByte(0x85);    //buffer 2
}
else
{
    SPI_WriteByte(0x82); //buffer 1
}
temp=(uchar)(page>>6);
SPI_WriteByte(temp); //地址最高字节
temp = (((uchar)page)<<2);
temp = temp|((address>>8)&0x03);//地址高字节
SPI_WriteByte(temp);
SPI_WriteByte((uchar)address);//地址低字节
//now write the user_data
for(i=0;i<num;i++)
{
   //delayUS(10);
   #asm("nop");
   SPI_WriteByte(string[i]);
}
CS_1;
delayMS(50);
//wait
IsBusy();
}

/*-------------------------------------------------------------------
              AT45_MemoryToBuffer()

Func: transfer of the page of data from the main memory to the buffer
Input:    (1) 0: choice buffer 1
              1: choice buffer 2
          (2) which page of the memory?-------page
Output:    No.
Attention: the initial stream of data:

           0x53(for buffer 1)
           0x55--xxx PA10~PA7--PA6~PA0 x--xxxx xxxx
---------------------------------------------------------------------*/
void AT45_MemoryToBuffer(uchar buffer_choice,uint page)  //正确
{
   uchar tt;
   CS_0;
   #asm("nop");
   #asm("nop");
   if(buffer_choice)
   {
     SPI_WriteByte(0x55);    //buffer 2
   }
   else
   {
   SPI_WriteByte(0x53); //buffer 1
   }
   tt=(uchar)(page >>6);
   SPI_WriteByte(tt);       // 6 to 7
   tt=(uchar)(page <<2);
   SPI_WriteByte(tt);
   SPI_WriteByte(BYTE_DUMMY);
   CS_1;
   //wait
   #asm("nop");
   #asm("nop");
   #asm("nop");
   #asm("nop");
   delayMS(50);
   IsBusy();
}


/*----------------------------------------------------------
              AT45_BufferToMemory()

Func:   BUFFER TO MAIN MEMORY PAGE PROGRAM WITH BUILT-IN ERASE.
Input: (1) 0: choice buffer 1
            1: choice buffer 2
        (2) which page to store the data from buffer----page
Output: No.
Attention: the initial stream of data:

           0x83(for buffer 1)
           0x86--xxxx PA10~PA7--PA6~PA0 x--xxxx xxxx
------------------------------------------------------------*/
void AT45_BufferToMemory(uchar buffer_choice,uint page)    //带擦除命令0x83,0x86
{
   uchar tp;
   CS_0;
   #asm("nop");
   #asm("nop");
   if(buffer_choice)
   {
     SPI_WriteByte(0x86);    //buffer 2
   }
   else
   {
   SPI_WriteByte(0x83); //buffer 1
   }
   tp=(uchar)(page >>6);
   SPI_WriteByte(tp);   //6 我改为7
   tp=(uchar)(page <<2);
   SPI_WriteByte(tp);
   SPI_WriteByte(BYTE_DUMMY);
   SPI_WriteByte(0xd7);
   CS_1;
   #asm("nop");
   #asm("nop");
   #asm("nop");
   #asm("nop");
   //wait
   IsBusy();
}


//擦除一页
void AT45_ErasePage(uint page) //page(0~8191)
{
   uchar temp;
   CS_0;
   #asm("nop");
   #asm("nop");
   //send command stream of data
   SPI_WriteByte(0X81);// MEM_PAGE_ERASE
   temp=(uchar)(page>>6);
   SPI_WriteByte(temp); //地址最高字节
   temp = (((uchar)page)<<2);
   SPI_WriteByte(temp);       //地址高字节
   SPI_WriteByte(BYTE_DUMMY);       //地址低字节
   CS_1;
   #asm("nop");
   #asm("nop");
   #asm("nop");
   #asm("nop");
   delayMS(40); //40MS
   IsBusy();
}


//擦除一块,连续的8页。
void AT45_EraseBlock(uint block)//Block(0~1023)
{
   uchar temp;
   CS_0;
   #asm("nop");
   #asm("nop");
   //send command stream of data
   SPI_WriteByte(0x50);// MEM_BLOCK_ERASE
   SPI_WriteByte((uchar)(block>>6));//地址最高字节
   temp = (((uchar)block)<<2);
   SPI_WriteByte(temp);       //地址高字节
   SPI_WriteByte(BYTE_DUMMY);       //地址低字节
   CS_1;
   #asm("nop");
   #asm("nop");
   #asm("nop");
   #asm("nop");
   delayMS(80);   //60MS
   IsBusy();
}
//复位存储器
static void DataFlashReset(void)  //复位存储器
{
   CS_0;
   #asm("nop");
   #asm("nop");
   #asm("nop");
   #asm("nop");
   RESET_0;
   delayUS(15);
   CS_1;
   #asm("nop");
   #asm("nop");
   #asm("nop");
   #asm("nop");
   RESET_1;
   delayUS(2);
}
//读ID
static void ReadID(void)
{
   uchar i;
   uchar d[4];
   CS_0;
   #asm("nop");
   #asm("nop");
   SPI_WriteByte(0X9F);
   for(i=0;i<4;i++)
   {
   	d[i]=SPI_ReadByte();
   }
   CS_1;
   #asm("nop");
   #asm("nop");
   #asm("nop");
   #asm("nop");
}

static void IsBusy(void)
{
   while(!(ReadFlashState()&0x80))
   {
   ;
   }
}

static unsigned char ReadFlashState(void)
{
    unsigned char result=0;
    CS_0;
    #asm("nop");
    #asm("nop");
    SPI_WriteByte(0XD7);
    result=SPI_ReadByte();
    CS_1;
    #asm("nop");
    #asm("nop");
    #asm("nop");
    #asm("nop");
    return result;
}

static ClearProtect(void)
{
    //unsigned char comand=0;
    CS_0;
    #asm("nop");
    #asm("nop");
    SPI_WriteByte(0x3D);
    SPI_WriteByte(0x2A);
    SPI_WriteByte(0x7F);
    SPI_WriteByte(0x9A);
    CS_1;
    #asm("nop");
    #asm("nop");
    #asm("nop");
    #asm("nop");
}

//=================================================================================
void main(void)
{
    	unsigned int i;
        unsigned int k;
    	PORTA=0X00;
    	DDRA=0XFF;
    	PORTC=0X00;  //采集器
	DDRC=0xFF;  //
	PORTD=0x00;   //01111010
	DDRD=0xFF;    //01111010
        SPI_Init();
        DataFlashReset();
        //测试buffer1\2读取
        /*for(i=0;i<50;i++)
        {
        data[i]=i;
        }
        AT45_Write_Buffer(0,0,&data[0],50);
        for(i=0;i<100;i++)
        {
        	data[i]=0;
 	}
 	AT45_Read_Buffer(0,0,50,&data[0]);
        */

 	//测试写主存,并读取
 	for(i=0;i<528;i++)
        {
        	data[i]=i+1;
 	}
        AT45_MemoryPageWrite(0,0,0,528,data);  //b,p,a,n,s
        for(i=0;i<528;i++)
        {
        	data[i]=0;
 	}
 	AT45_MemoryRead(0,0,528,data); //p,a,n,s  //读出的全为FF

        for(k=0;k<100;k++)
        {
          for(i=0;i<528;i++)
          {
        	data[i]=0;
 	      }
 	      AT45_MemoryRead(k,0,528,data); //p,a,n,s  //读出的全为FF
        }

        //测试buffer到主存
        /*for(i=0;i<100;i++)
        {
        	data[i]=40+i;
 	}
        AT45_Write_Buffer(0,0,&data[0],50);
        AT45_ErasePage(0);
        AT45_BufferToMemory(0,0);
        for(i=0;i<100;i++)
        {
        	data[i]=0;
 	}
        AT45_MemoryRead(0,0,50,&data[0]); //p,a,n,s  //正确,读出的全为FF


        AT45_MemoryToBuffer(1,0);
        AT45_EraseBlock(2);

        //读ID,及测试忙
        ReadID();
        ClearProtect();
        IsBusy();

 	AT45_EraseBlock(0);
 	IsBusy();

 	AT45_EraseBlock(0);
 	*/
        delay_ms(10);
}

⌨️ 快捷键说明

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