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

📄 spi_at45db321c.c

📁 at45db321驱动程序
💻 C
字号:
#include "io.h"   
#include "type.h"
#include "45DB321.h"  
/*AT45DB321     单片机	
2 /RESET	PB0 40	    
3 /WP		PB1 41
11 /CS		PB4 42
12 SCK	    PB7 3
13 SI		PB5 1
14 SO		PB6 2
*/    
#define CS  4
#define WP 1
#define RESET 0
#define SI 5
#define SO 6
#define SCK 7
#define AT45DB041_RESET_0 PORTB&=~(1<<RESET)
#define AT45DB041_RESET_1 PORTB|=(1<<RESET)
#define AT45DB041_CS_0 PORTB&=~(1<<CS)
#define AT45DB041_CS_1 PORTB|=(1<<CS)
#define AT45DB041_SO_0 PORTB&=~(1<<SO)
#define AT45DB041_SO_1 PORTB|=(1<<SO)
#define AT45DB041_SI_0 PORTB&=~(1<<SI)
#define AT45DB041_SI_1 PORTB|=(1<<SI)
#define AT45DB041_SCK_0 PORTB&=~(1<<SCK)
#define AT45DB041_SCK_1 PORTB|=(1<<SCK)

void SPI_Init(void)
{
   DDRB=0X0D; //spi端口初始化,cs,sck,mosi输出,miso输入
   SPCR=0X5D;//SPI充许,主模式,高字节在前,工作模式3,fck/16
}

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

Func:      Send a byte of data to the device using
           SPI interface. 
Input:     the data(one byte) you want to send.
Output:    No.
Attention: MODE-->Inactive Clock Polarity High 
           and SPI Mode 3
--------------------------------------------------------*/
void SPI_WriteByte(char spi_write_data)
{
    SPDR=spi_write_data;
    while(!(SPSR&0X80)); 
    return SPDR;
}

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

Func:      Read a byte of data from the device using
           SPI interface. 
Input:     No.
Output:    the data(one byte) read.
Attention: MODE-->Inactive Clock Polarity High 
           and SPI Mode 3
--------------------------------------------------------*/
uchar SPI_ReadByte(void)
{
    while(!(SPSR&0X80)); 
    return SPDR;
}
          

/*-------------------------------------------------------
            AT45_Write_Buffer()

Func:      Write a block of data to buffer 1 or 2 of 
           the device AT45DB321C. 
Input:     (1)0: choice buffer 1; 
              1: choice buffer 2.
           (2)address of the buffer(0~257,nine bits:BFA8~BFA0).
           (3)a pointer poited to the first address of an array; 
                  
Output:    No.
Attention: (1)the data stream load to the device before
              you write user_data into the device is:
            
           1000 0100 (for buffer 1)
           1000 0111 xxxx xxxx xxxx xxxBFA8 BFA7~BFA0 
           (2) the end of the data array you want to load
               into the device must be '\n'. 
           (3) 1 buffer=258 bytes
---------------------------------------------------------*/
void AT45_Write_Buffer(uchar buffer_choice,uint address,const char * const string, uint buf_len)
{
uint i; 

AT45DB041_CS_0; //active the device

if(buffer_choice)
{
     SPI_WriteByte(0x87);   //buffer 2
}
else
{
 SPI_WriteByte(0x84); //buffer 1
}
SPI_WriteByte(BYTE_DUMMY);      //xxxx xxxx don’t care bits

SPI_WriteByte((uchar)((address>>8)&0x11));   //BFA9,BFA8

SPI_WriteByte((uchar)address); //BFA7~BFA0

//now send the user_data

 //when i>263,write the data from the 
                     //beginning of the buffer meaning only the
                  //last 264 bytes will be stored in the 
for(i=0;i<buf_len;i++)              /*buffer. More detail in the datasheet.*/
{
    SPI_WriteByte(string[i]);
}    
AT45DB041_CS_1; //inactive the device
}



/*-----------------------------------------------------------------
            AT45_Read_Buffer()

Func:      Read a block of data from buffer 1 or 2 of 
           the device AT45DB041B. 
Input:     (1)0: choice buffer 1; 
              1: choice buffer 2.
           (2)nine address bits(BFA8~BFA0):
           (3)how many bytes do you want to read?(<258)
           (4)a pointer poited to the first address of an array; 
                  
Output:    No.
Attention: (1)the data stream load to the device before
              you write user_data into the device is:      
      
           0101 0100 (for buffer 1)
           0101 0110 xxxx xxxx xxxx xxxBFA8 BFA7~BFA0 xxxx xxxx           
------------------------------------------------------------------*/
void AT45_Read_Buffer(uchar buffer_choice,uint address,uint buf_len,char * string)
{
uint i;

AT45DB041_CS_0; //active the device

if(buffer_choice)
{
     SPI_WriteByte(0x0D6);   //buffer 2
}
else
{
 SPI_WriteByte(0x0D4); //buffer 1
}
SPI_WriteByte(BYTE_DUMMY);      //xxxx xxxx

SPI_WriteByte((uchar)((address>>8)&0x11));   //BFA9,BFA8

SPI_WriteByte((uchar)address); //BFA7~BFA0

SPI_WriteByte(BYTE_DUMMY);      //xxxx xxxx

//now read the user_data

for(i=0;i<buf_len;i++) 
   {
   string[i]=SPI_ReadByte();
   }
AT45DB041_CS_1; 
}



/*-----------------------------------------------------------
         AT45_MemoryRead()

Func:   sequentially read a continuous stream of data 
        from the device's main memory.
Input: (1)which page?(0~8192)---------------------page
        (2)from where of the page(0~258)--------address
        (3)how many bytes do you want to read?------num
        (4)a pointer poited to the first address 
           of an array.the data is store in this array.           
Output: No.
Attention: D2h--r PA12~PA0 BA9--BA0--32bit Dummy--
           xxxx xxxx--xxxx xxxx--xxxx xxxx--xxxx xxxx
-------------------------------------------------------------*/
void AT45_MemoryRead(uint page,uint address,uint num,char * string)
{
   uint i;
   uchar temp;

   AT45DB041_CS_0;
   
   //send command stream of data
   SPI_WriteByte(0x68);// MEM_PAGE_READ

   SPI_WriteByte((uchar)(page>>6)); //地址最高字节

   temp = (((uchar)page)<<2);
   temp = temp|((address>>8)&0x03);//地址高字节
   SPI_WriteByte(temp);

   SPI_WriteByte((uchar)address);//地址低字节

   SPI_WriteByte(BYTE_DUMMY);//32 don't care bits 
   SPI_WriteByte(BYTE_DUMMY);
   SPI_WriteByte(BYTE_DUMMY);
   SPI_WriteByte(BYTE_DUMMY);
   
   //now read the user_data

   //2048*264=538624,but don't read so many 
   //bytes at one time.

   for(i=0;i<num;i++)
   {
     string[i]=SPI_ReadByte();
   }
   AT45DB041_CS_1; 
} 
/*----------------------------------------------------
              AT45_MemoryPageWrite()

Func:     Main Memory Page Program through Buffer 1 or 2.
Input:    (1)0: use buffer 1
             1: use buffer 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            
        
------------------------------------------------------*/
void AT45_MemoryPageWrite(uchar buffer_choice,uint page,uint address,uint num,const char * const string)
{
   uint i;
   uchar temp;   
   AT45DB041_CS_0;
   
   //send command stream of data
   if(buffer_choice)
   {
       SPI_WriteByte(0x85);    //buffer 2
   }
   else
   {
   SPI_WriteByte(0x82); //buffer 1
   }
   SPI_WriteByte((uchar)(page>>6)); //地址最高字节

   temp = (((uchar)page)<<2);
   temp = temp|((address>>8)&0x03);//地址高字节
   SPI_WriteByte(temp);

   SPI_WriteByte((uchar)address);//地址低字节

   //now write the user_data
   i=0;
   while(i<num)
   {
      SPI_WriteByte(string[i]);
      i++;
   }

   AT45DB041_CS_1;

   //wait
}
  


/*-------------------------------------------------------------------
              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)
{
   AT45DB041_CS_0;
   
   if(buffer_choice) 
   {
     SPI_WriteByte(0x55);    //buffer 2
   } 
   else 
   {
   SPI_WriteByte(0x53); //buffer 1
   }

   SPI_WriteByte((uchar)(page >>6));

   SPI_WriteByte((uchar)(page <<2));

   SPI_WriteByte(BYTE_DUMMY);

   AT45DB041_CS_1;

   //wait
}


/*----------------------------------------------------------
              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)
{
   AT45DB041_CS_0;
   
   if(buffer_choice) 
   {
     SPI_WriteByte(0x86);    //buffer 2
   } 
   else 
   {
   SPI_WriteByte(0x83); //buffer 1
   }

   SPI_WriteByte((uchar)(page >>6));

   SPI_WriteByte((uchar)(page <<2));

   SPI_WriteByte(BYTE_DUMMY);

   AT45DB041_CS_1;

   //wait
}

void AT45_ErasePage(uint page) //page(0~8191)
{
   uchar temp;

   AT45DB041_CS_0;
   
   //send command stream of data
   SPI_WriteByte(0x81);// MEM_PAGE_ERASE

   SPI_WriteByte((uchar)(page>>6)); //地址最高字节

   temp = (((uchar)page)<<2);
   SPI_WriteByte(temp);       //地址高字节

   SPI_WriteByte(BYTE_DUMMY);       //地址低字节

   AT45DB041_CS_1;
   
}

void AT45_EraseBlock(uint block)//Block(0~1023)
{
   uchar temp;

   AT45DB041_CS_0;
   
   //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);       //地址低字节

   AT45DB041_CS_1;
   
}

//================================================================================


void main(void)
{
    	uchar i;
    	uchar data[256];
    	
    	PORTC=0XC0;  //我加
	DDRC=0xC0;  // 
	PORTD=0x7A;   //01111010
	DDRD=0x7A;    //01111010 
        SPI_Init(); 
        for(i=0;i<128;i++)
        	data[i]=i;     
        AT45_Write_Buffer(0,0,&data[0],128);
 	AT45_Read_Buffer(0,0,&data[128],128);    
 	AT45_MemoryToBuffer(1,1);  
        AT45_BufferToMemory(1,1);  
        AT45_ErasePage(2); //page(0~8191)
        AT45_MemoryRead(1,0,128,&data[0]);
        AT45_MemoryPageWrite(0,1,0,1,&data[0]);   
        AT45_EraseBlock(0);//Block(0~1023)
}

⌨️ 快捷键说明

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