📄 at45db321.c
字号:
#include <STC89C51RC_RD_PLUS.H>
#include <intrins.h>
#include <absacc.h>
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long
#define PAGE_ERASE 0x81
#define BLOCK_ERASE 0x50
#define STATUS_REGISTER_READ 0xD7
#define MAIN_MEMORY_PAGE_READ 0xD2
#define MAIN_MEMORY_PAGE_WRITE_THROUGH_BUFFER1 0x82
#define MAIN_MEMORY_PAGE_WRITE_THROUGH_BUFFER1_ERASE 0x83
#define DEVICE_ID_INFO 0x9F
#define READY 1
#define BUSY 0
sbit SCK = P1^3;
sbit SI = P1^4;
sbit SO = P1^0;
sbit CS = P1^2;
sbit LED = P1^0;
//#define P40 LED
/*
一片45DB321包括8192页,每页512字节
页擦除时间:最多35毫秒
页编程时间:最多15毫秒
芯片使用spi模式0
commond:0x84 -->write buffer1
commond:0x87 -->write buffer2
commond:0x82 -->Main Memory Page Program Through Buffer 1
commond:0x85 -->Main Memory Page Program Through Buffer 2
commond:0x81 -->Page Erase
commond:0xd2 -->Main Memory Page Read
commond:0x54 -->Buffer 1 Read
commond:0x56 -->Buffer 2 Read
*/
/*延时1ms-11.0592MHz
void Delay(uint number)
{
uint j;
uint i;
for(j=0;j<number;j++)
{
for(i=0;i<110;i++);
}
}*/
/*延时1ms-22.1184MHz*/
void Delay(uint number)
{
uint j;
uint i;
for(j=0;j<number;j++)
{
for(i=0;i<220;i++);
}
}
void InitSys(void)
{
TH1 = 0xfd; //9600bps 11.0592MHz
TL1 = 0xfd;
TMOD = 0x21; //定时器1方式2,定时器0方式1
TR1 = 1; //启动定时器1
SCON = 0x50; //串口方式1,允许接受
ES = 1; //允许串口中断
ET0 = 1; //允许定时器0中断
TR1 = 1; //启动定时器1
IT1 = 1; //外部中断边沿方式
IT0 = 1; //外部中断边沿方式
EA = 1;
}
/*******************************************************************
发送字符串
*******************************************************************/
void PutStr(uchar *p,uint num)
{
uint i;
if(num != 0)
{
for(i = 0;i < num;i ++)
{
SBUF =p[i] ;
while(!TI);
//Delay(10);
TI = 0;
}
}
else
{
for(i=0;i<30;i++)
{
if(p[i] != 0)
{
SBUF = p[i];
while(!TI);
TI = 0;
}
}
}
}
/*************************************************/
/*函数原型:
/*输入参数:无
/*返回参数:无
/*************************************************/
void SPI_Init(void)
{
CS = 1;
SCK = 0;
SI = 1;
SO = 1;
Delay(20);//20ms
}
/*************************************************/
/*函数原型:
/*输入参数:要发送的命令或数据
/*返回参数:无
/*************************************************/
void SPI_SendByte(uchar SendData)
{
uchar i;
for(i=0;i<8;i++)
{
SI = (bit)(SendData&0x80);
SendData <<= 1;
SCK = 1;
SCK = 0;
}
}
/*************************************************/
/*函数原型:
/*输入参数:无
/*返回参数:读得的一字节数据
/*************************************************/
uchar SPI_ReadByte(void)
{
uchar i,RecData = 0;
for(i=0;i<8;i++)
{
SCK = 1;
RecData <<= 1;
if(SO)RecData += 1;
SCK = 0;
}
return (RecData);
}
//指定页擦除
void PageErase(uint Page)
{
uchar i;
if(Page>8191)return;
CS = 0;
SPI_SendByte(PAGE_ERASE);
i = Page>>6;
SPI_SendByte(i);
i = Page&0x3f;
i = i<<2;
SPI_SendByte(i);
i=0;
SPI_SendByte(i);
CS = 1;
Delay(70);
}
//读芯片状态寄存器,返回0表示busy,返回1表示ready
bit ReadStatus(void)//status register read
{
uchar i;
CS = 0;
SPI_SendByte(STATUS_REGISTER_READ);
i = SPI_ReadByte();
CS = 1;
return((bit)(i&0x80));
}
//得到器件的信息共4个字节
void ReadDeviceId(uchar *p)
{
uchar i;
CS = 0;
SPI_SendByte(DEVICE_ID_INFO);
for(i=0;i<4;i++)p[i] = SPI_ReadByte();
CS = 1;
}
//块擦除
void BlockErase(uint Block)
{
uchar i;
CS = 0;
SPI_SendByte(BLOCK_ERASE);
i = (uchar)(Block>>3);
SPI_SendByte(i);
i = (uchar)(Block<<5);
SPI_SendByte(i);
i=0;
SPI_SendByte(i);
CS = 1;
Delay(100);
}
//芯片擦除
void ChipErase(void)
{
uint i;
for(i=0;i<1024;i++)BlockErase(i);
}
//直接从存储空间读到外部缓冲区
void MainMemPageRead(uint Page,uint InPageAdd,uchar *Buffer,uint Num)
{
uint i = 0;
if(Page>8191)return;
CS = 0;
SPI_SendByte(MAIN_MEMORY_PAGE_READ);
i = Page>>6;
SPI_SendByte(i);
i = Page&0x3f;
i = i<<2;
Page = InPageAdd>>8;
Page += i;
i = (uchar)Page;
SPI_SendByte(i);
SPI_SendByte((uchar)(InPageAdd&0xff));
for(i=0;i<32;i++){SCK = 1;SCK = 0;}//dummy
for(i=0;i<Num;i++)Buffer[i] = SPI_ReadByte();
CS = 1;
}
//通过buffer1编程flash
void MainMemoryPageProgramThroughBuffer(uint Page,uint InPageAdd,uchar *Buffer,uint Num)
{
uint i = 0;
if(Page>8191)return;
CS = 0;
SPI_SendByte(MAIN_MEMORY_PAGE_WRITE_THROUGH_BUFFER1);
i = Page>>6;
SPI_SendByte(i);
i = Page&0x3f;
i = i<<2;
Page = InPageAdd>>8;
Page += i;
i = (uchar)Page;
SPI_SendByte(i);
SPI_SendByte((uchar)(InPageAdd&0xff));
for(i=0;i<Num;i++) SPI_SendByte(Buffer[i]);
CS = 1;
Delay(90);
}
//通过buffer1编程flash,同时内建擦除,不需要单独擦除指令
void MainMemoryPageProgramThroughBufferAndErase(uint Page,uchar *Buffer)
{
uint i = 0;
if(Page>8191)return;
CS = 0;
SPI_SendByte(MAIN_MEMORY_PAGE_WRITE_THROUGH_BUFFER1_ERASE);
i = Page>>6;
SPI_SendByte(i);
i = Page&0x3f;
i = i<<2;
SPI_SendByte(i);
i=0;
SPI_SendByte(i);
for(i=0;i<512;i++) SPI_SendByte(Buffer[i]);
CS = 1;
Delay(90);
}
uchar xdata buf[255];
uchar xdata buf1[255];
uchar idata flag=0;
uint idata i;
uchar idata c,a;
uchar xdata *p _at_ 32767;
void main(void)
{
InitSys();
SPI_Init();
Delay(100);
/*
MainMemPageRead(8190,0,buf,512);//需要37ms@11.0592 18ms@22.1184
Delay(100);
PutStr(buf,512);
while(1);
*/
/* ram test */
flag=0;
p = 0;
for(i=0;i<32000;i++)
{
c=i%256;
*p=c;
_nop_();_nop_();_nop_();_nop_();
a = *p;
p++;
if(a!=c){flag = 1;break;}
}
if(flag)
{
PutStr("RAM TEST FALSE!\r\n",17);
Delay(200);
c = i/256;
a = i%256;
PutStr(&c,1);
PutStr(&a,1);
PutStr("\r\n",2);
Delay(200);
}
else PutStr("RAM TEST OK!\r\n",14);
Delay(500);
c=i/256;
PutStr(&c,1);
c=i%256;
PutStr(&c,1);
Delay(500);
ReadDeviceId(buf1);
if((buf1[0]==0x1f)&&(buf1[1]==0x27)&&(buf1[2]==0x01)&&(buf1[3]==0x00))PutStr("CheckDeviceIdOk\r\n",17);
else PutStr("DeviceIdError\r\n",17);
Delay(500);
PutStr(buf1,4);
Delay(500);
for(i=0;i<256;i++)buf[i] = i;
for(i=0;i<256;i++)buf1[i] = 0;
PageErase(8191);
PutStr("Program Flash Chip Page 8191\r\n",30);
MainMemoryPageProgramThroughBuffer(8191,0,buf,255);//需要45ms@11.0592 20ms@22.1184
Delay(500);
PutStr("Flash Chip Read\r\n",17);
MainMemPageRead(8191,0,buf1,255);//需要37ms@11.0592 18ms@22.1184
Delay(500);
PutStr(buf,255);
Delay(500);
PutStr(buf1,255);
Delay(500);
flag = 0;
for(a=0;a<255;a++)
{
if(buf[a]!=buf1[a])flag=1;
}
if(flag==0)PutStr("Check Flash Chip Ok\r\n",21);
else PutStr("Check Flash Chip False\r\n",24);
Delay(500);
PutStr("Flash Chip Read\r\n",17);
for(i=0;i<10;i++)
{
Delay(500);
MainMemPageRead(i,0,buf1,255);
PutStr(buf1,255);
}
Delay(500);
PutStr("erase Flash Chip Page 0--10\r\n",29);
for(i=0;i<10;i++)
{
PageErase(i);
//Delay(100);
}
Delay(500);
PutStr("Read Flash Chip Page 0--10\r\n",28);
for(i=0;i<10;i++)
{
Delay(500);
MainMemPageRead(i,0,buf1,255);
PutStr(buf1,255);
PutStr("\r\n",2);
}
PutStr("erase Flash Chip Page 8000--8191\r\n",34);
for(i=8000;i<8192;i++)
{
PageErase(i);
}
PutStr("Flash Chip write Page 8000 -- 8191\r\n",36);
for(i=8000;i<8192;i++)
{
MainMemoryPageProgramThroughBuffer(i,0,buf,255);//需要45ms@11.0592 20ms@22.1184
}
Delay(50);
PutStr("Flash Chip Read Page 8000 -- 8191\r\n",36);
for(i=8000;i<8192;i++)
{
Delay(100);
MainMemPageRead(i,0,buf1,255);
PutStr(buf1,255);
Delay(100);
PutStr("page",4);
}
/*
*/
while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -