AT45db081的spi读写程序.docx
- 文档编号:5710619
- 上传时间:2022-12-31
- 格式:DOCX
- 页数:14
- 大小:17.54KB
AT45db081的spi读写程序.docx
《AT45db081的spi读写程序.docx》由会员分享,可在线阅读,更多相关《AT45db081的spi读写程序.docx(14页珍藏版)》请在冰豆网上搜索。
AT45db081的spi读写程序
/***************************************************************
功能:
实现存储器EEPROMAT45db081的数据读写操作,同时了解硬件spi读写操作
作者:
ZDP
时间:
2005-11-30
版本:
V1.0
***************************************************************/
#include
#include
#include
unsignedintCount1msInc;
unsignedintAT450XXPageAddr;
voidSYSCLK_Init(void);
voidPORT_Init(void);
voidSPI0_Init(void);
voidTimer0_Init(void);
#defineSYSCLK
#defineDF_RESETP4&=~0x10;P4|=0x10;//AT45DB081复位
#defineDF_RDY_BUSYP4|=0x20;while(!
(P4&0x20));//等待AT45DB081准备好
#defineDF_CHIP_SELECTP4&=~0x4;//AT45DB081片选开
#defineDF_CHIP_NOSELECTP4|=0x4;//AT45DB081片选关
#defineRAM_CHIP_SELECT_BANK0ES0=1;P4=0xF4;
#defineRAM_CHIP_SELECT_BANK1P4=0xF5;
#defineBUFFER_10x00//buffer1
#defineBUFFER_20x01//buffer2
#defineBUFFER_1_WRITE0x84//buffer1write
#defineBUFFER_2_WRITE0x87//buffer2write
#defineBUFFER_1_READ0x54//buffer1read(changeto0xD4forSPImode0,3)
#defineBUFFER_2_READ0x56//buffer2read(changeto0xD6forSPImode0,3)
#defineB1_TO_PAGE_WITH_ERASE0x83//buffer1tomainmemorypageprogramwithbuilt-inerase
#defineB2_TO_PAGE_WITH_ERASE0x86//buffer2tomainmemorypageprogramwithbuilt-inerase
#defineB1_TO_PAGE_WITHOUT_ERASE0x88//buffer1tomainmemorypageprogramwithoutbuilt-inerase
#defineB2_TO_PAGE_WITHOUT_ERASE0x89//buffer2tomainmemorypageprogramwithoutbuilt-inerase
#definePAGE_PROG_THROUGH_B10x82//mainmemorypageprogramthroughbuffer1
#definePAGE_PROG_THROUGH_B20x85//mainmemorypageprogramthroughbuffer2
#defineAUTO_PAGE_REWRITE_THROUGH_B10x58//autopagerewritethroughbuffer1
#defineAUTO_PAGE_REWRITE_THROUGH_B20x59//autopagerewritethroughbuffer2
#definePAGE_TO_B1_COMP0x60//mainmemorypagecomparetobuffer1
#definePAGE_TO_B2_COMP0x61//mainmemorypagecomparetobuffer2
#definePAGE_TO_B1_XFER0x53//mainmemorypagetobuffer1transfer
#definePAGE_TO_B2_XFER0x55//mainmemorypagetobuffer2transfer
#defineSTATUS_REGISTER0x57
#defineMAIN_MEMORY_PAGE_READ0x52//mainmemorypageread(changeto0xD2forSPImode0,3)
voidSendSPIByte(unsignedcharch)
{
SPIF=0;
SPI0DAT=ch;
while(SPIF==0);//等待写结束
}
unsignedcharGetSPIByte(void)
{
SPIF=0;
SPI0DAT=0;
while(SPIF==0);
returnSPI0DAT;//等待读结束
}
//启用缓存1读
voidFlashBuffer1Read(unsignedintstar_addr,unsignedintlen,unsignedchar*buffer)//从缓冲区1的指定位置(0-263)
中读入指定字节
{
unsignedinti;
DF_RDY_BUSY;//测芯片准备好?
DF_CHIP_SELECT;//芯片选择
SendSPIByte(BUFFER_1_READ);//缓冲区1为54H
SendSPIByte(0);
SendSPIByte((unsignedchar)(star_addr>>8));
SendSPIByte((unsignedchar)star_addr);
SendSPIByte(0);
for(i=0;i { buffer=GetSPIByte();//readdatafromSPI } DF_CHIP_NOSELECT; } //启用缓存2读 voidFlashBuffer2Read(unsignedintstar_addr,unsignedintlen,unsignedchar*buffer)//从缓冲区2的指定位置(0-263) 中读入指定字节 { unsignedinti; DF_RDY_BUSY;//测芯片准备好? DF_CHIP_SELECT;//芯片选择 SendSPIByte(BUFFER_2_READ);//缓冲区2为56H SendSPIByte(0); SendSPIByte((unsignedchar)(star_addr>>8)); SendSPIByte((unsignedchar)star_addr); SendSPIByte(0); for(i=0;i { *buffer=GetSPIByte();//readdatafromSPI buffer++; } DF_CHIP_NOSELECT; } //启用缓存1写 voidFlashBuffer1Write(unsignedintstart_addr,unsignedintlen,unsignedchar*buffer)//向缓冲区1的指定位置(0- 263)写入指定字节 { unsignedinti; DF_RDY_BUSY;//测芯片准备好? DF_CHIP_SELECT;//芯片选择 SendSPIByte(BUFFER_1_WRITE);//缓冲区1为84H SendSPIByte(0x00); SendSPIByte((unsignedchar)(start_addr>>8)); SendSPIByte((unsignedchar)start_addr); for(i=0;i SendSPIByte(buffer); DF_CHIP_NOSELECT; } //启用缓存2写 voidFlashBuffer2Write(unsignedintstart_addr,unsignedintlen,unsignedchar*buffer)//向缓冲区2的指定位置(0- 263)写入指定字节 { unsignedinti; DF_RDY_BUSY;//测芯片准备好? DF_CHIP_SELECT;//芯片选择 SendSPIByte(BUFFER_2_WRITE);//缓冲区2为87H SendSPIByte(0x00); SendSPIByte((unsignedchar)(start_addr>>8)); SendSPIByte((unsignedchar)start_addr); for(i=0;i SendSPIByte(buffer); DF_CHIP_NOSELECT; } //读取状态 unsignedcharGetFlashStatus()//读Flash当前状态Bit7=1: 空闲0: 忙Bit6=1: 比较结果不匹配0: 匹配Bit5Bit3Bit2: 芯片标志 100: 081 { unsignedcharidataret; DF_CHIP_SELECT;//芯片选择 SendSPIByte(STATUS_REGISTER); ret=GetSPIByte(); DF_CHIP_NOSELECT; returnret; } //读主存至缓存 voidPageToBuffer1(void)//从Flash读一页至缓存1 { DF_RDY_BUSY;//测芯片准备好? DF_CHIP_SELECT;//芯片选择 SendSPIByte(PAGE_TO_B1_XFER); SendSPIByte((unsignedchar)(AT450XXPageAddr>>7)); SendSPIByte((unsignedchar)(AT450XXPageAddr<<1)); SendSPIByte(0); DF_CHIP_NOSELECT; } //读主存至缓存 voidPageToBuffer2(void)//从Flash读一页至缓存2 { DF_RDY_BUSY;//测芯片准备好? DF_CHIP_SELECT;//芯片选择 SendSPIByte(PAGE_TO_B2_XFER); SendSPIByte((unsignedchar)(AT450XXPageAddr>>7)); SendSPIByte((unsignedchar)(AT450XXPageAddr<<1)); SendSPIByte(0); DF_CHIP_NOSELECT; } //缓存1写主存 voidFlashBuffer1ProgNoErase(void)//直接将缓存1写入Flash一页(不擦除) { DF_RDY_BUSY;//测芯片准备好? DF_CHIP_SELECT;//芯片选择 SendSPIByte(B1_TO_PAGE_WITHOUT_ERASE); SendSPIByte((unsignedchar)(AT450XXPageAddr>>7)); SendSPIByte((unsignedchar)(AT450XXPageAddr<<1)); SendSPIByte(0x00); DF_CHIP_NOSELECT; } //缓存2写主存 voidFlashBuffer2ProgNoErase(void)//直接将缓存2写入Flash一页(不擦除) { DF_RDY_BUSY;//测芯片准备好? DF_CHIP_SELECT;//芯片选择 SendSPIByte(B2_TO_PAGE_WITHOUT_ERASE); SendSPIByte((unsignedchar)(AT450XXPageAddr>>7)); SendSPIByte((unsignedchar)(AT450XXPageAddr<<1)); SendSPIByte(0x00); DF_CHIP_NOSELECT; } //擦除+缓存1写主存 voidFlashBuffer1ProgAutoErase(void)//直接将缓存1写入Flash一页(先擦除) { DF_RDY_BUSY;//测芯片准备好? DF_CHIP_SELECT;//芯片选择 SendSPIByte(B1_TO_PAGE_WITH_ERASE); SendSPIByte((unsignedchar)(AT450XXPageAddr>>7)); SendSPIByte((unsignedchar)(AT450XXPageAddr<<1)); SendSPIByte(0x00); DF_CHIP_NOSELECT; } //擦除+缓存2写主存 voidFlashBuffer2ProgAutoErase(void)//直接将缓存2写入Flash一页(先擦除) { DF_RDY_BUSY;//测芯片准备好? DF_CHIP_SELECT;//芯片选择 SendSPIByte(B2_TO_PAGE_WITH_ERASE); SendSPIByte((unsignedchar)(AT450XXPageAddr>>7)); SendSPIByte((unsignedchar)(AT450XXPageAddr<<1)); SendSPIByte(0x00); DF_CHIP_NOSELECT; } //读主存 //写缓存+缓存写主存 voidFlashProgViaBuffer1(unsignedintstart_addr,unsignedintlen,unsignedchar*buffer)//将指定数据通过缓存1写入 Flash指定位置(不擦除) { unsignedinti; DF_RDY_BUSY;//测芯片准备好? DF_CHIP_SELECT;//芯片选择 SendSPIByte(PAGE_PROG_THROUGH_B1);//缓冲区1为82H SendSPIByte((unsignedchar)(AT450XXPageAddr>>7)); SendSPIByte((unsignedchar)(AT450XXPageAddr<<1+start_addr>>8)); SendSPIByte((unsignedchar)start_addr); for(i=0;i SendSPIByte(buffer); DF_CHIP_NOSELECT; } //写缓存+缓存写主存 voidFlashProgViaBuffer2(unsignedintstart_addr,unsignedintlen,unsignedchar*buffer)//将指定数据通过缓存2写入 Flash指定位置(不擦除) { unsignedinti; DF_RDY_BUSY;//测芯片准备好? DF_CHIP_SELECT;//芯片选择 SendSPIByte(PAGE_PROG_THROUGH_B2);//缓冲区2为85H SendSPIByte((unsignedchar)(AT450XXPageAddr>>7)); SendSPIByte((unsignedchar)(AT450XXPageAddr<<1+start_addr>>8)); SendSPIByte((unsignedchar)start_addr); for(i=0;i SendSPIByte(buffer); DF_CHIP_NOSELECT; } //写缓存+擦除+缓存写主存 voidFlashAutoProgViaBuffer1(unsignedintstart_addr,unsignedintlen,unsignedchar*buffer)//将指定数据通过缓存1 写入Flash指定位置(先擦除) { unsignedinti; DF_RDY_BUSY;//测芯片准备好? DF_CHIP_SELECT;//芯片选择 SendSPIByte(AUTO_PAGE_REWRITE_THROUGH_B1);//缓冲区1为84H SendSPIByte((unsignedchar)(AT450XXPageAddr>>7)); SendSPIByte((unsignedchar)(AT450XXPageAddr<<1+start_addr>>8)); SendSPIByte((unsignedchar)start_addr); for(i=0;i SendSPIByte(buffer); DF_CHIP_NOSELECT; } //写缓存+擦除+缓存写主存 voidFlashAutoProgViaBuffer2(unsignedintstart_addr,unsignedintlen,unsignedchar*buffer)//将指定数据通过缓存2 写入Flash指定位置(先擦除) { unsignedinti; DF_RDY_BUSY;//测芯片准备好? DF_CHIP_SELECT;//芯片选择 SendSPIByte(AUTO_PAGE_REWRITE_THROUGH_B2);//缓冲区2为87H SendSPIByte((unsignedchar)(AT450XXPageAddr>>7)); SendSPIByte((unsignedchar)(AT450XXPageAddr<<1+start_addr>>8)); SendSPIByte((unsignedchar)start_addr); for(i=0;i SendSPIByte(buffer); DF_CHIP_NOSELECT; } voidmain(void){ unsignedcharxdataMyBuff[270]; unsignedcharxdataMyBuff1[270]; unsignedcharxdata*idataMyBuff11; unsignedinti,j; WDTCN=0xde;//disablewatchdogtimer WDTCN=0xad; SYSCLK_Init();//initializeoscillator Timer0_Init(); PORT_Init();//initializecrossbarandGPIO EA=1; DF_RESET; SPI0_Init();//initializeSPI0 //RAM_CHIP_SELECT_BANK0; //EMI0CF=0x2F; /*--------------------------------------------------------------------------- 缓存读写测试 ---------------------------------------------------------------------------*/ for(j=0;j<264;j++)//初始化数组数据为: 0--264 { MyBuff[j]=j; } FlashBuffer1Write(0,264,MyBuff);//写数据至数据缓存区1 _nop_(); for(j=0;j<264;j++)//初始化数组数据全为: 0 { MyBuff[j]=0; } FlashBuffer1Read(0,264,MyBuff);//读数据缓存区1数据至数组 _nop_(); /*--------------------------------------------------------------------------- FLASH读写测试 ---------------------------------------------------------------------------*/ for(i=0;i<4096;i++) { MyBuff[0]=i>>8; MyBuff[1]=i;//将页地址写入数组的前两个字节. for(j=2;j<264;j++) { MyBuff[j]=i; }//向数组写数据共262个字节 AT450XXPageAddr=i;//指定待写页 FlashBuffer1Write(0,264,MyBuff);//将数组的内容写入缓存1 FlashBuffer1ProgAutoErase();//将缓存的内容写入主存(带擦除) } _nop_(); for(i=0;i<4096;i++) { AT450XXPageAddr=i;//指定待读页 MyBuff11=0x0+i*264;//指定数据放置的XRAM的地址(首地址为0x0). PageToBuffer2();//将主存内容读入缓存2 FlashBuffer2Read(0,264,MyBuff11);//将缓存2的内容读入XRAM _nop_();//在此设断点观察XRAM中的数据是否与写入的数据相符. } _nop_();//同时在此设一断点在全部读完主存后观察XRAM中的内容. /*--------------------------------------------------------------------------- 其它测试 ---------------------------------------------------------------------------*/ AT450XXPageAddr=0; FlashBuffer1Write(20,14,"U
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- AT45db081 spi 读写 程序