📄 at45db041b的模拟spi程序.c
字号:
/*********************************************************************************
* 函数原型:unsigned char SPI_HostReadByte(void);
* 名 称:SPI_HostReadByte
* 功 能:从AT45DB041B通过SPI_SO口读入1BYTE数据
* 入口参数:无
* 出口参数:返回读入的1BYTE数据
**********************************************************************************/
unsigned char SPI_HostReadByte(void)
{
unsigned char i,rByte=0;
for(i=0;i<8;i++)
{
clr_spi_sck;
// delay_1us();
set_spi_sck;
// delay_1us();
rByte<<=1;
if(PINB&8) //m64 MISO---PB3
// if(PINB&0x40) //M16 MISO---PB6
{
rByte|=1;
}
}
return (rByte);
}
/*********************************************************************************
* 函数原型:void SPI_HostWriteByte(unsigned char wByte);
* 名 称:SPI_HostWriteByte
* 功 能:将1字节数据wByte由SPI_SI口写入AT45DB041B
* 入口参数:要写入的1BYTE数据
* 出口参数:无
**********************************************************************************/
void SPI_HostWriteByte(unsigned char wByte)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(wByte&0x80)
{
set_spi_si;
}
else
{
clr_spi_si;
}
wByte=wByte<<1;
clr_spi_sck;
// delay_1us();
set_spi_sck;
// delay_1us();
}
}
/*********************************************************************************
* 函数原型:unsigned char AT45DB041B_StatusRegisterRead(void);
* 名 称:AT45DB041B_StatusRegisterRead
* 功 能:由SPI_SI口读AT45DB041B的状态字
* 入口参数:无
* 出口参数:返回状态字
**********************************************************************************/
/*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位用来标记器件密度,对于AT45DB041B,这4位应该是0111,一共能标记 */
/* 16种不同密度的器件。 */
/* bit1 */
/* bit0 - 这2位暂时无效 */
/******************************************************************************/
unsigned char AT45DB041B_StatusRegisterRead(void)
{
unsigned char i;
clr_spi_cs;
SPI_HostWriteByte(0xd7);
i=SPI_HostReadByte();
set_spi_cs;
return (i);
}
/*********************************************************************************
* 函数原型:void AT45DB041B_ContinuousArrayRead(uint PA,uint BFA,unsigned char *pHeader,uint len);
* 名 称:AT45DB041B_ContinuousArrayRead
* 功 能:由SPI_SI口读AT45DB041B中指定首地址的数据块到数组pHeader[]
* 入口参数:
* PA - 页地址,0~2047
* BFA - 指定flash页中的起始读入地址
* rbuff - 存入指定数据的首地址
* len - 读出指定数据的长度
* 出口参数:无
**********************************************************************************/
void AT45DB041B_ContinuousArrayRead(uint PA,uint BFA,unsigned char *pHeader,uint len)
{
uchar i=0;
while(i++<255)
{
if(AT45DB041B_StatusRegisterRead()&0x80)
{
break;
}
}
clr_spi_cs;
SPI_HostWriteByte(0xe8);
SPI_HostWriteByte((unsigned char)(PA>>7));
SPI_HostWriteByte((unsigned char)((PA<<1)|(BFA>>8)));
SPI_HostWriteByte((unsigned char)BFA);
for(i=0;i<4;i++)
{
SPI_HostWriteByte(0x00);
}
for(j=0;j<len;j++)
{
pHeader[j]=SPI_HostReadByte();
}
set_spi_cs;
}
/*********************************************************************************
* 函数原型:void SPI_HostFlashToBuffer(uchar buffer,uint PA);
* 名 称:SPI_HostFlashToBuffer
* 功 能:将FLASH中的数据传送到BUFFER中
* 入口参数:
* buffer - 选择BUFFER,01H选择BUFFER 1,02H选择BUFFER 2
* 在该指令序列中,操作码84H选择BUFFER 1,87H选择BUFFER 2
* PA - 页地址,0~2047
* 出口参数:无
**********************************************************************************/
void SPI_HostFlashToBuffer(uchar buffer,uint PA)
{
while(i++<5)
{
if(AT45DB041B_StatusRegisterRead()&0x80);
{
break;
}
}
//SPI_CS=0;
nop;
nop;
clr_spi_cs;
switch(buffer)
{
case 1:SPI_HostWriteByte(FLASH_TO_BUFFER1);break;
case 2:SPI_HostWriteByte(FLASH_TO_BUFFER2);break;
}
SPI_HostWriteByte((unsigned char)(PA>>7));
SPI_HostWriteByte((unsigned char)((PA<<1)|0x01));
SPI_HostWriteByte(0x00);
set_spi_cs;
nop;
nop;
}
/*********************************************************************************
* 函数原型:void AT45DB041B_BufferRead(uchar buffer,uint BFA,uchar *pHeader,uint len);
* 名 称:AT45DB041B_BufferRead
* 功 能:读BUFFER中指定首地址的数据块到数组rbuff[]
* 入口参数:
* buffer - 选择BUFFER,01H选择BUFFER 1,02H选择BUFFER 2
* 在该指令序列中,操作码84H选择BUFFER 1,87H选择BUFFER 2
* BFA - BUFFER中的起始地址,0~263
* pHeader - 待写入数据的首地址(指针)
* len - 待存数据的长度1~264
* 出口参数:无
**********************************************************************************/
void AT45DB041B_BufferRead(uchar buffer,uint BFA,uchar *pHeader,uint len)
{
uchar i=0;
while(i++<255)
{
if(AT45DB041B_StatusRegisterRead()&0x80)
{
break;
}
}
clr_spi_cs;
switch(buffer)
{
case 1:SPI_HostWriteByte(READ_BUFFER1);break;
case 2:SPI_HostWriteByte(READ_BUFFER2);break;
}
SPI_HostWriteByte(0x00);
SPI_HostWriteByte((unsigned char)(BFA>>8));
SPI_HostWriteByte((unsigned char)BFA);
for(j=0;j<len;j++)
{
pHeader[j]=SPI_HostReadByte();
}
set_spi_cs;
}
/*********************************************************************************
* 函数原型:void AT45DB041B_BufferWrite(uchar buffer,uint BFA,uchar *pHeader,uint len);
* 名 称:AT45DB041B_BufferWrite
* 功 能:将指定数据写入从某个地址(0~263)开始的BUFFER中
* 入口参数:
* buffer - 选择BUFFER,01H选择BUFFER 1,02H选择BUFFER 2
* 在该指令序列中,操作码84H选择BUFFER 1,87H选择BUFFER 2
* BFA - BUFFER中的起始地址,0~263
* pHeader - 待写入数据的首地址(指针)
* len - 待存数据的长度1~264
* 出口参数:无
**********************************************************************************/
void AT45DB041B_BufferWrite(uchar buffer,uint BFA,uchar *pHeader,uint len)
{
uchar i=0;
while(i++<255)
{
if(AT45DB041B_StatusRegisterRead()&0x80)
{
break;
}
}
clr_spi_cs;
switch(buffer)
{
case 1:SPI_HostWriteByte(WRITE_BUFFER1);break;
case 2:SPI_HostWriteByte(WRITE_BUFFER2);break;
}
SPI_HostWriteByte(0x00);
SPI_HostWriteByte((unsigned char)(BFA>>8));
SPI_HostWriteByte((unsigned char)BFA);
for(j=0;j<len;j++)
{
SPI_HostWriteByte(pHeader[j]);
}
set_spi_cs;
Delay(50); //延时20MS
}
/*********************************************************************************
* 函数原型:void AT45DB041B_BufferToMainMemoryPageProgramWithBuilt_inErase;
* 名 称:AT45DB041B_BufferToMainMemoryPageProgramWithBuilt_inErase
* 功 能:将指定数据写入从某个地址(0~263)开始的页中:包含2个动作,首先将指定数据*
* 写入到BUFFER 1或者BUFFER 2中,其中可以指定BUFFER中的起始写入地址,此写入*
* 动作不影响BUFFER中其它地址中的数据,然后再将BUFFER中的整个数据写入到某指*
* 定页中(带预擦除)。 *
* 入口参数:* buffer - 选择BUFFER,01H选择BUFFER 1,02H选择BUFFER 2 *
* PA - 页地址,0~2047 *
* BFA - 指定BUFFER中的起始写入地址 *
* pHeader - 指定数据的首地址 *
* len - 指定数据的长度 *
* 出口参数:无
**********************************************************************************/
void AT45DB041B_BufferToMainMemoryPageProgramWithBuilt_inErase
(uchar buffer,uint PA,uint BFA,uchar *pHeader,uint len)
{
unsigned int i=0;
//将长度为len,首地址为pHeader的数写入以 BFA为首地址的buffer中
AT45DB041B_BufferWrite(buffer,BFA,pHeader,len);
//等待 AT45DB041B 的ready
while(i++<1000)
{
if(AT45DB041B_StatusRegisterRead()&0x80)
{
break;
}
}
clr_spi_cs;
switch(buffer)
{
case 1:SPI_HostWriteByte(0x83);break;
case 2:SPI_HostWriteByte(0x86);break;
}
SPI_HostWriteByte((unsigned char)(PA>>7));
SPI_HostWriteByte((unsigned char)(PA<<1));
SPI_HostWriteByte(0x00);
set_spi_cs;
Delay(50); //延时20MS
}
/*********************************************************************************
* 函数原型:void Read_32ByteFromAT45DB041B(uchar code_hz1,uchar code_hz2);
* 名 Read_32ByteFromAT45DB041B
* 功 能:从AT45DB041B中读指定区位码的汉字字模32BYTES数据块到数组HZ_RAM[]中
* 入口参数:区位码:uchar code_hz1,uchar code_hz2
* PA - 页地址,0~2047
* BFA - 指定flash页中的起始读入地址
* pHeader - 存入指定数据的首地址
* len - 读出指定数据的长度
* 出口参数:无
**********************************************************************************/
void Read_32ByteFromAT45DB041B(uchar code_hz1,uchar code_hz2)
{
uint PA,BFA;
// addr_hz = (code_hz1-1)*94+code_hz2-1; //计算汉字字模地址((区码-1)*94+(位码-1))*32
addr_hz = (code_hz1-0xa1)*94+code_hz2-0xfe; //计算汉字字模地址((内码高-0xa1)*94+(内码低-0xa1))*32
//字模从第一区第94个汉字开始的,故-0xa1-93=-0xfe
addr_hz = addr_hz<<5;
PA=addr_hz/264;
PA=(uint)PA;
BFA=addr_hz%264;
BFA=(uint)BFA;
unsigned int i=0;
while(i++<5)
{
if(AT45DB041B_StatusRegisterRead()&0x80)
{
break;
}
}
clr_spi_cs;
SPI_HostWriteByte(0xe8);
SPI_HostWriteByte((unsigned char)(PA>>7));
SPI_HostWriteByte((unsigned char)((PA<<1)|(BFA>>8)));
SPI_HostWriteByte((unsigned char)BFA);
for(i=0;i<4;i++)
{
SPI_HostWriteByte(0x00);
}
for(i=0;i<32;i++)
{
hz_ram[i]=SPI_HostReadByte();
}
set_spi_cs;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -