/****************************************************************
* ATMEL的DATAFLASH操作函数库(FOR AT45DB161D) *
* *
* MSP430F149
// -----------------
// /|\| XIN|-
// | | |
// --|RST XOUT|-
// | |
// LED <-|P1.0 P3.1_p5.1|-> Data Out (SIMO0)
// | |
// Scope <-|P3.4 P3.2_p5.2|<- Data In (SOMI0)
// | |
// Slave RST <-|P3.5 P3.3_p5.3|-> Serial Clock Out (UCLK)
| _p4.7|-> cs
*****************************************************************/
#include <msp430x14x.h>
#include "AT45DB161D.h"
#include "spi.h"
//DB161D的初始化
void DB161D_Init()
{
P5DIR |= 0x10; // CS
}
//检查状态寄存器最高位是否为忙,并等待空闲
//D7h: 1 1 0 1 0 1 1 1 N/A N/A N/A N/A
//Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
//RDY/BUSY COMP 1 0 1 1 PROTECT PAGE SIZE
void DF_wait_busy(void)
{
unsigned char state_reg=0x00;
DF_SELECT_1;
SPIflash_Write(READ_STATE_REGISTER);
while((state_reg&0x80) == 0)
{
state_reg = SPIflash_Read();
}
DF_DESELECT_1;
}
//格式化主存储器(以扇区<0A,0B,1……15>为单位删除所有页数据)
//7Ch 0 1 1 1 1 1 0 0 x x P P P P x x x x x x x x x x x x x x x x x x N/A ( 528字节模式)
void DF_format(void)
{
unsigned char i;
DF_SPI_ON; //开SPI使能
DF_wait_busy();
DF_SELECT_1; //P4.7接CS,使CS变成低电平
SPIflash_Write(SECTOR_ERASE); //扇区擦除指令
SPIflash_Write(0x00); //对扇区0A进行擦除 2无关位+9位页地址(PA11--PA3)+13无关位
SPIflash_Write(0x00);
SPIflash_Write(0x00);
DF_DESELECT_1; //CS变高电平,结束操作
DF_wait_busy();
DF_SELECT_1;
SPIflash_Write(SECTOR_ERASE); //对扇区0B进行擦除
SPIflash_Write(0x00);
SPIflash_Write(0x20);
SPIflash_Write(0x00);
DF_DESELECT_1;
for (i=1;i<16;i++)
{ //对扇区1--扇区15进行擦除
DF_wait_busy();
DF_SELECT_1;
SPIflash_Write(SECTOR_ERASE);
SPIflash_Write(i << 2);
SPIflash_Write(0x00);
SPIflash_Write(0x00);
DF_DESELECT_1;
}
DF_SPI_OFF;
}
//擦除指定的主存储器页(地址范围0-4095)一共就有4095个页 无预擦除
//81h 1 0 0 0 0 0 0 1 x x P P P P P P P P P P P P x x x x x x x x x x N/A (528字节模式)
void DF_page_earse(unsigned int page)
{
DF_SPI_ON;
DF_wait_busy();
DF_SELECT_1;
SPIflash_Write(PAGE_ERASE); //写页擦除指令0x81H + 3字节地址 (2个无关位,12位页地址 PA11~P10 与10个无关位)
SPIflash_Write((unsigned char)(page >> 6)); //因为前两位是无关位,所以左移
SPIflash_Write((unsigned char)(page << 2)); //后两位是无关会,所以右移
SPIflash_Write(0x00);
DF_DESELECT_1;
DF_SPI_OFF;
}
//将保存在数组DF_buffer[]中的一页数据写入第一或者第二缓冲区后送入主存储区
//(先擦除后写入模式,页地址范围0-4095)
// 写缓冲区 指令码 + 3字节地址( 14位无关位 + 10位缓冲区地址 BFA9~BFA0 )
//缓冲区1:84h 1 0 0 0 0 1 0 0 x x x x x x x x x x x x x x B B B B B B B B B B N/A(528字节模式)
//缓冲区2:87h 1 0 0 0 0 1 1 1 x x x x x x x x x x x x x x B B B B B B B B B B N/A(528字节模式)
//缓冲区向主存储页写 指令码 + 3字节地址 ( 2无关位 + 12位页地址 PA11~PA0 + 10个无关位)
//缓冲区1:83h 1 0 0 0 0 0 1 1 x x P P P P P P P P P P P P x x x x x x x x x x N/A
//缓冲区2: 86h 1 0 0 0 0 1 1 0 x x P P P P P P P P P P P P x x x x x x x x x x N/A
void DF_write_page(unsigned char buffer, unsigned int page,unsigned char *DF_buffer,unsigned int length) //先给定缓冲区号,再给定页地址
{
unsigned int i;
DF_SPI_ON;
DF_wait_busy();
DF_SELECT_1;
if(buffer == 1)
SPIflash_Write(BUFFER_1_WRITE); //写入缓冲区1
else
SPIflash_Write(BUFFER_2_WRITE); //写入缓冲区2
SPIflash_Write(0x00); //从缓冲区的地址0开始写
SPIflash_Write(0x00);
SPIflash_Write(0x00);
for (i=0;i<length;i++) //写入缓冲区
SPIflash_Write(DF_buffer[i]);
DF_DESELECT_1;
if (page<4096) { //缓冲区的数据写入主存储区
DF_SELECT_1;
if(buffer == 1)
SPIflash_Write(B1_TO_MM_PAGE_PROG_WITH_ERASE); //第一缓冲区的数据写入主存储器(擦除模式)
else
SPIflash_Write(B2_TO_MM_PAGE_PROG_WITH_ERASE); //第二缓冲区的数据写入主存储器(擦除模式)
SPIflash_Write((unsigned char)(page>>6)); //给定地址
SPIflash_Write((unsigned char)(page<<2));
SPIflash_Write(0x00);
DF_DESELECT_1;
DF_wait_busy();
}
DF_SPI_OFF;
}
//将指定主存储器页的数据转入缓冲区后读出,保存在DF_buffer[]数组中
//(页地址范围0-4095)
// 53h: 0 1 0 1 0 0 1 1 x x P P P P P P P P P P P P x x x x x x x x x x N/A
// 55h: 0 1 0 1 0 1 0 1 x x P P P P P P P P P P P P x x x x x x x x x x N/A
// D4h: 1 1 0 1 0 1 0 0 x x x x x x x x x x x x x x B B B B B B B B B B 1
// D6h: 1 1 0 1 0 1 1 0 x x x x x x x x x x x x x x B B B B B B B B B B 1
void DF_read_page(unsigned char buffer, unsigned int page,unsigned char *DF_buffer,unsigned int length)
{
unsigned int i;
DF_SPI_ON;
DF_wait_busy();
DF_SELECT_1;
if(buffer == 1)
SPIflash_Write(MM_PAGE_TO_B1_XFER); // 将主存储器的指定页数据加载到第一缓冲区
else
SPIflash_Write(MM_PAGE_TO_B2_XFER); // 将主存储器的指定页数据加载到第二缓冲区
SPIflash_Write((unsigned char)(page >> 6));
SPIflash_Write((unsigned char)(page << 2));
SPIflash_Write(0x00);
DF_DESELECT_1;
DF_wait_busy();
DF_SELECT_1;
if(buffer == 1)
SPIflash_Write(BUFFER_1_READ); // 读取第一缓冲区
else
SPIflash_Write(BUFFER_2_READ); // 读取第二缓冲区
SPIflash_Write(0x00);
SPIflash_Write(0x00);
SPIflash_Write(0x00);
SPIflash_Write(0x00);
for (i=0;i<length;i++)
{
DF_buffer[i] = SPIflash_Read();
}
DF_DESELECT_1;
DF_SPI_OFF;
}
//以直接读取方式读取指定的主存储器页数据(页地址范围0-4095)
//D2h: 1 1 0 1 0 0 1 0 x x P P P P P P P P P P P P B B B B B B B B B B 4
void DF_MM_read_page(unsigned int page,unsigned char *DF_buffer,unsigned int length)
{
unsigned int i;
DF_SPI_ON;
DF_wait_busy();
DF_SELECT_1;
SPIflash_Write(MM_PAGE_READ);
SPIflash_Write((unsigned char)(page >> 6));
SPIflash_Write((unsigned char)(page << 2));
SPIflash_Write(0x00);
SPIflash_Write(0x00);
SPIflash_Write(0x00);
SPIflash_Write(0x00);
SPIflash_Write(0x00);
for (i=0;i<length;i++)
{
DF_buffer[i] = SPIflash_Read();
}
DF_DESELECT_1;
DF_SPI_OFF;
}
//读取状态寄存器
//bit7 bit6 bit6 bit6 bit6 bit6 bit6 bit6
//RDY/BUSY COMP 1 0 1 1 PROTECT PAGE SIZE
//D7h: 1 1 0 1 0 1 1 1 N/A N/A N/A N/A
unsigned char DF_read_reg(void)
{
unsigned char temp;
DF_SPI_ON;
DF_SELECT_1;
SPIflash_Write(READ_STATE_REGISTER);
temp = SPIflash_Read();
DF_DESELECT_1;
DF_SPI_OFF;
return temp;
}
//将指定主存储器页的数据转入指定缓冲区
//53h: 0 1 0 1 0 0 1 1 x x P P P P P P P P P P P P x x x x x x x x x x N/A
//55h: 0 1 0 1 0 1 0 1 x x P P P P P P P P P P P P x x x x x x x x x x N/A
void DF_mm_to_buf(unsigned char buffer,unsigned int page)
{
DF_SPI_ON;
DF_wait_busy();
DF_SELECT_1;
if (buffer==1)
SPIflash_Write(MM_PAGE_TO_B1_XFER);
else
SPIflash_Write(MM_PAGE_TO_B2_XFER);
SPIflash_Write((unsigned char)(page >> 6));
SPI
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
AT45DB161D.rar (24个子文件)
AT45DB161D.c 11KB
main.c 1KB
settings
161D.dbgdt 8KB
161D.dni 1KB
161D.cspy.bat 3KB
161d.wsdt 6KB
AT45DB161D.h 3KB
max485.c 871B
161D.ewd 17KB
spi.h 502B
161d.eww 158B
Debug
Obj
161D.pbd 313B
max485.r43 6KB
spi.r43 7KB
main.r43 11KB
AT45DB161D.r43 40KB
Uart.r43 6KB
List
Exe
161D.d43 24KB
max485.h 187B
Uart.c 3KB
161D.ewp 46KB
uart.h 252B
161D.dep 5KB
spi.c 1KB
共 24 条
- 1
资源评论
- joyxiong2014-08-06代码阅读起来不是很方便。
- fox09422011-09-10和网上的其他代码 差不多一般般
- anning8652013-10-08没什么特别的,我的本意是要一个第三方的软件。
yongli_forever
- 粉丝: 0
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功