?? 45db041.c
字號:
/**************************************************
Name: 45DB041.C
Func: Provide some operations of AT45DB041B-SC
(ATMEL Inc. DataFlash, 4M bits, SPI interface)
Author: Pan.L.F
Date: 2005-11-6
---------------------------------------------------*/
#include <intrins.h>
#include "45DB041.h"
#include "main.h"
/*-----------------------------------------------------
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(tByte spi_write_data)
{
tByte temp,i;
temp=spi_write_data;
//AT45DB041_CS=0; /CS should not be control here
for(i=0;i<8;i++) //send 8 bits
{
AT45DB041_SCK=0;
AT45DB041_SI=(bit)((temp & 0x80)>>7);//MSB first
temp=temp<<1;
_nop_();
AT45DB041_SCK=1;
_nop_();
}
}
/*-----------------------------------------------------
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
--------------------------------------------------------*/
tByte SPI_ReadByte(void)
{
tByte i,temp;
bit bit_in;
temp=0x00;//initialize the data output
for(i=0;i<8;i++)
{
AT45DB041_SCK=0;
_nop_();
_nop_();
AT45DB041_SCK=1;
bit_in=AT45DB041_SO;
if(bit_in) temp=temp | 0x01;
if(i<7) temp=temp << 1;
}
return (temp);
}
/*-------------------------------------------------------
AT45_Write_Buffer()
Func: Write a block of data to buffer 1 or 2 of
the device AT45DB041B.
Input: (1)0: choice buffer 1;
1: choice buffer 2.
(2)address of the buffer(0~263,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=264 bytes
---------------------------------------------------------*/
void AT45_Write_Buffer(bit buffer_choice,tWord address,const char * const string)
{
tWord i;
AT45DB041_CS=0; //active the device
if(buffer_choice)
{
SPI_WriteByte(0x87); //buffer 2
}
else SPI_WriteByte(0x84); //buffer 1
SPI_WriteByte(0x11); //xxxx xxxx don’t care bits
if(address>255)
{
SPI_WriteByte(0x11); //BFA8=1
}
else SPI_WriteByte(0x10); //BFA8=0
SPI_WriteByte((tByte)(address & 0x00ff)); //BFA7~BFA0
//now send the user_data
i=0;
while((string[i]!='\n')&(i<0xffff)) /*when i>263,write the data from the
beginning of the buffer meaning only the
last 264 bytes will be stored in the
buffer. More detail in the datasheet.*/
{
SPI_WriteByte(string[i]);
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?(<265)
(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(bit buffer_choice,tWord address,tWord num,char * string)
{
tWord i;
AT45DB041_CS=0; //active the device
if(buffer_choice)
{
SPI_WriteByte(0x56); //buffer 2
}
else SPI_WriteByte(0x54); //buffer 1
SPI_WriteByte(0x11); //xxxx xxxx
if(address>255)
{
SPI_WriteByte(0x11); //BFA8=1
}
else SPI_WriteByte(0x10); //BFA8=0
SPI_WriteByte((tByte)(address & 0x00ff));//BFA7~BFA0
SPI_WriteByte(0x33); //xxxx xxxx
//now read the user_data
_nop_();
_nop_();
for(i=0;((i<=263)&(i<num));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~2047)---------------------page
(2)from where of the page(0~263)--------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: 68h--xxxx PA10~PA7--PA6~PA0 BFA8--BFA7~BFA0--
xxxx xxxx--xxxx xxxx--xxxx xxxx--xxxx xxxx
-------------------------------------------------------------*/
void AT45_MemoryRead(tWord page,tWord address,tWord num,char * string)
{
tWord i;
tByte temp;
AT45DB041_CS=0;
//send command stream of data
SPI_WriteByte(0x68);
SPI_WriteByte((tByte)((page & 0x0780)>>7));
temp=(tByte)(page & 0x007F);
temp=temp<<1;
if(address & 0x0100) temp=temp | 0x01;
SPI_WriteByte(temp);
SPI_WriteByte((tByte)(address & 0x00ff));
SPI_WriteByte(0x33);//32 don't care bits
SPI_WriteByte(0x33);
SPI_WriteByte(0x33);
SPI_WriteByte(0x33);
//now read the user_data
_nop_();
_nop_();
//2048*264=538624,but don't read so many
//bytes at one time.
for(i=0;((i<=65535)&(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(bit buffer_choice,tWord page,tWord address,tWord num,const char * const string)
{
tWord i;
tByte 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((tByte)((page & 0x0780)>>7));
temp=(tByte)(page & 0x007F);
temp=temp<<1;
if(address & 0x0100) temp=temp | 0x01;
SPI_WriteByte(temp);
SPI_WriteByte((tByte)(address & 0x00ff));
//now write the user_data
i=0;
while(i<num)
{
SPI_WriteByte(string[i]);
i++;
}
AT45DB041_CS=1;
}
/*-------------------------------------------------------------------
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--xxxx PA10~PA7--PA6~PA0 x--xxxx xxxx
---------------------------------------------------------------------*/
void AT45_MemoryToBuffer(bit buffer_choice,tWord page)
{
tByte temp;
tWord i;
AT45DB041_CS=0;
if(buffer_choice)
{
SPI_WriteByte(0x55); //buffer 2
}
else SPI_WriteByte(0x53); //buffer 1
SPI_WriteByte((tByte)((page & 0x0780)>>7));
temp=(tByte)(page & 0x007f);
temp=temp<<1;
SPI_WriteByte(temp);
SPI_WriteByte(0x33);
AT45DB041_CS=1;
//wait
i=0x8fff;
while(i--);
}
/*----------------------------------------------------------
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(bit buffer_choice,tWord page)
{
tByte temp;
tWord i;
AT45DB041_CS=0;
if(buffer_choice)
{
SPI_WriteByte(0x86); //buffer 2
}
else SPI_WriteByte(0x83); //buffer 1
SPI_WriteByte((tByte)((page & 0x0780)>>7));
temp=(tByte)(page & 0x007f);
temp=temp<<1;
SPI_WriteByte(temp);
SPI_WriteByte(0x33);
AT45DB041_CS=1;
//wait
i=0x8fff;
while(i--);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -