AT45DB161.c
- #include "at45db161.h"
- #include "spi.h"
- /*******************************************************
- +---------------------------------------+
- | 振南电子 驱动程序模块 AT45DB161 部分 |
- +---------------------------------------+
- 此源码版权属 振南 全权享有,如欲引用,敬请署名并告知
- 严禁随意用于商业目的,违者必究,后果自负
- 振南电子
- ->产品网站 http://www.znmcu.cn/
- ->产品网店 http://shop.znmcu.cn/
- ->产品咨询 QQ:987582714
- MSN:yzn07@126.com
- WW:yzn07
-
- ********************************************************/
- /******************************************************************
- - 功能描述:读取AT45DB161的状态
- - 隶属模块:AT45DB161模块
- - 函数属性:内部
- - 参数说明:无
- - 返回说明:无
- - 注:无
- ******************************************************************/
- unsigned char AT45DB161_ReadStatus()
- {
- unsigned char status;
- AT45DB161_CS=0;
- SPI_WriteByte(0xd7);
- status=SPI_ReadByte();
- AT45DB161_CS=1;
- return status;
- }
- /***********************************************************************
- - 功能描述:从某一扇区的某个位置开始读取一定长度的数据,放入数据缓冲区
- - 隶属模块:AT45DB161模块
- - 函数属性:外部,供用户调用
- - 参数说明:addr:扇区地址
- start:扇区内的起始位置
- len:要读取的数据长度
- pbuf:指向数据缓冲中的指针
- - 返回说明:无
- - 注:无
- ***********************************************************************/
- void AT45DB161_ReadBytes(unsigned int addr,unsigned int start,unsigned int len,unsigned char *pbuf)
- {
- unsigned int i;
- while(!(AT45DB161_ReadStatus()&0x80));
- AT45DB161_CS=0;
- SPI_WriteByte(0xe8);
- SPI_WriteByte((unsigned char)(addr>>6));
- SPI_WriteByte((unsigned char)((addr<<2)|(start>>8)));
- SPI_WriteByte((unsigned char)start);
- for(i=0;i<4;i++) SPI_WriteByte(0x00);
- for(i=0;i<len;i++) pbuf[i]=SPI_ReadByte();
- AT45DB161_CS=1;
- }
- /***********************************************************************
- - 功能描述:向某一个数据缓冲通道中从某一位置开始写入一定长度的数据
- - 隶属模块:AT45DB161模块
- - 函数属性:外部,供用户调用
- - 参数说明:nbuf:缓冲通道
- start:扇区内的起始位置
- len:要读取的数据长度
- pbuf:指向数据缓冲中的指针
- - 返回说明:无
- - 注:无
- ***********************************************************************/
- void AT45DB161_WriteBuffer(unsigned char nbuf,unsigned int start,unsigned int len,unsigned char *pbuf)
- {
- unsigned int i;
- while(!(AT45DB161_ReadStatus()&0x80));
- AT45DB161_CS=0;
- switch(nbuf)
- {
- case 1:
- SPI_WriteByte(0x84);
- break;
- case 2:
- SPI_WriteByte(0x87);
- break;
- }
- SPI_WriteByte(0x00);
- SPI_WriteByte((unsigned char)(start>>8));
- SPI_WriteByte((unsigned char)start);
- for(i=0;i<len;i++) SPI_WriteByte(pbuf[i]);
- AT45DB161_CS=1;
- }
- /****************************************************************************
- - 功能描述:通过某一条缓冲通道将一定长度的数据写入到某一扇区中的某个位置上去
- - 隶属模块:AT45DB161模块
- - 函数属性:内部
- - 参数说明:nbuf:缓冲通道
- start:扇区内的起始位置
- len:要写入的数据长度
- pbuf:指向数据缓冲中的指针
- - 返回说明:无
- - 注:无
- ***************************************************************************/
- void AT45DB161_WriteBytes(unsigned char nbuf,unsigned int addr,unsigned int start,unsigned int len,unsigned char *pbuf)
- {
- AT45DB161_WriteBuffer(nbuf,start,len,pbuf);
- while(!(AT45DB161_ReadStatus()&0x80));
- AT45DB161_CS=0;
- switch(nbuf)
- {
- case 1:
- SPI_WriteByte(0x83);
- break;
- case 2:
- SPI_WriteByte(0x86);
- break;
- }
- SPI_WriteByte((unsigned char)(addr>>6));
- SPI_WriteByte((unsigned char)(addr<<2));
- SPI_WriteByte(0x00);
- AT45DB161_CS=1;
- }
- /****************************************************************************
- - 功能描述:读取AT45DB161的扇区数据(512字节)到缓冲区
- - 隶属模块:AT45DB161模块
- - 函数属性:外部,供用户调用
- - 参数说明:addr:扇区地址
- pbuf:指向数据缓冲中的指针
- - 返回说明:无
- - 注:无
- ***************************************************************************/
- void AT45DB161_ReadSector(unsigned long addr,unsigned char *pbuf)
- {
- AT45DB161_ReadBytes(addr,0,512,pbuf);
- }
- /****************************************************************************
- - 功能描述:将缓冲区中的数据写入AT45DB161的扇区(512字节)
- - 隶属模块:AT45DB161模块
- - 函数属性:外部,供用户调用
- - 参数说明:addr:扇区地址
- pbuf:指向数据缓冲中的指针
- - 返回说明:无
- - 注:无
- ***************************************************************************/
- void AT45DB161_WriteSector(unsigned long addr,unsigned char *pbuf)
- {
- AT45DB161_WriteBytes(1,addr,0,512,pbuf);
- }
AT45DB161.h
- #ifndef _AT45DB161_H_
- #define _AT45DB161_H_
- /*******************************************************
- +---------------------------------------+
- | 振南电子 驱动程序模块 AT45DB161 部分 |
- +---------------------------------------+
- 此源码版权属 振南 全权享有,如欲引用,敬请署名并告知
- 严禁随意用于商业目的,违者必究,后果自负
- 振南电子
- ->产品网站 http://www.znmcu.cn/
- ->产品网店 http://shop.znmcu.cn/
- ->产品咨询 QQ:987582714
- MSN:yzn07@126.com
- WW:yzn07
-
- ********************************************************/
- #include "reg51.h"
- //sbit SPI_SI =P1^5;
- //sbit SPI_SCK =P1^7;
- sbit AT45DB161_RESET=P1^0;
- sbit AT45DB161_CS =P0^0;
- //sbit SPI_SO =P1^6;
- void AT45DB161_ReadBytes(unsigned int addr,unsigned int start,unsigned int len,unsigned char *pbuf);
- void AT45DB161_WriteBytes(unsigned char nbuf,unsigned int addr,unsigned int start,unsigned int len,unsigned char *pbuf);
- void AT45DB161_ReadSector(unsigned long addr,unsigned char *pbuf);
- void AT45DB161_WriteSector(unsigned long addr,unsigned char *pbuf);
- #endif
|