📄 at45db161d_driver.c
字号:
#include <w77e58.h>
#include <string.h>
#include"ds1302.h"
#include <stdlib.h>
#include <stdio.h>
#include <intrins.h>
#define data_ora P0 //MCU P1<------> LCM
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
sbit req =P3^5; //请求信号,H有效
//sbit req =P2^7;
sbit busy =P2^7;
//sbit busy =P2^5;
uchar temp;
/*正常操作电压为2.7~3.6V,实验中发现当电压超过4.25V后读出的状态字节为9A(正常 */
/*的状态字节值为9D),并且读写数据均不准确,所以应当保证卡片的供电电压不超过 */
/*4.25V。 */
/*SPI规范:Data is always clocked into the device on the rising edge of SCK a-*/
/* nd clocked out of the device on the falling edge of SCK.All instruction-*/
/* s,addresses and data are transferred with the most significant bit(MSB) */
/* first. */
/* 2005-06-02*/
sbit SPI_CS = P2^3;
//sbit SPI_CS = P2^2;
sbit SPI_SCK = P2^4;
//sbit SPI_SCK = P2^1;
//sbit SPI_SO = P2^6;
sbit SPI_SO = P3^7;
// sbit SPI_SO = P2^3;
sbit SPI_SI = P3^3;
//sbit SPI_SI = P2^5;
// sbit SPI_SI = P2^0;
//sbit P37=P3^7;
//sbit SPI_RESET = P2^3;
//sbit SPI_WP = P2^1;
uchar buff1[1]; //从buffer中取出的时间值放入buff1中送显示
//spi0与spi3上升沿输入下降沿输出
/*uchar SPI_HostReadByte(void)
{
uchar i,rByte=0;
SPI_SCK=0;
for(i=0;i<8;i++)
{rByte<<=1;
SPI_SCK=1;
if(SPI_SO) rByte++;
SPI_SCK=0;
//rByte<<=1;
}
return(rByte);
}*/
uchar SPI_HostReadByte(void)
{ //读一个字节
uchar i,rByte=0;
for(i=0;i<8;i++)
{
SPI_SCK=0;
SPI_SCK=1;
rByte<<=1;
rByte|=SPI_SO;
}
return rByte;
}
/**************************************************/
/*void SPI_HostWriteByte(uchar wByte){
uchar i;
for(i=0;i<8;i++)
{
SPI_SCK=0;
if(wByte&0x80){SPI_SI=1;}
else{SPI_SI=0;}
wByte<<=1;
// SPI_SCK=0;
SPI_SCK=1;
}
}*/
void SPI_HostWriteByte(uchar wByte) //写一个字节
{
uchar i;
SPI_CS=0;
for(i=0;i<8;i++)
{
SPI_SCK=0;
if(wByte&0x80){SPI_SI=1;}
else{SPI_SI=0;}
SPI_SCK=1;
wByte<<=1;
} }
/*void SPI_HostWriteByte(uchar wByte){ //写一个字节
uchar i;
/// SPI_SCK=1;
for(i=0;i<8;i++){
SPI_SCK=0; ///
if((wByte<<i)&0x80) {SPI_SI=1;}
else {SPI_SI=0;}
/// SPI_SCK=0;
/// SPI_SCK=1;
SPI_SCK=1;////
////////SPI_SCK=0;
}
} */
/*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器件继续*/
/* 将最新的状态字节送出。 */ //[Page]
/* bit6 - 标记最近一次Main Memory Page和Buffer的比较结果,0相同,1不同。 */
/* bit5 */
/* bit4 */
/* bit3 */
/* bit2 - 这4位用来标记器件密度,对于AT45DB041B,这4位应该是0111,一共能标记 */
/* 16种不同密度的器件。对于AT45DB081B,这4位应该是1001, 对于AT45DB161B,这4位应该是1011 */
/* bit1 */
/* bit0 - 这2位暂时无效 */
uchar AT45DB161B_StatusRegisterRead(void)
{
uchar i;
SPI_CS=0;
SPI_HostWriteByte(0xd7);
i=SPI_HostReadByte();
SPI_CS=1;
return i;
}
/*描述: */
/* When the last bit in the main memory array has been read,the device will*/
/* continue reading back at the beginning of the first page of memory.As w-*/
/* ith crossing over page boundaries,no delays will be incurred when wrapp-*/
/* ing around from the end of the array to the beginning of the array. */
/*从主存储页中连续读*/
/*参数: */
/* PA - 页地址,0~2047 */
/* BFA - 指定BUFFER中的起始写入地址(我认为确切的说应该是页内地址,即要读的数据在页内的首地址)*/
/* pHeader - 指定数据的首地址 */
/* len - 指定数据的长度 */
void AT45DB161B_ContinuousArrayRead(uint PA,uint BFA,uchar *pHeader,uint len)
{
uint i;
while(!(AT45DB161B_StatusRegisterRead()&0x80));
// while(i++<255){if(AT45DB161B_StatusRegisterRead()&0x80){break;}}
SPI_CS=0;
SPI_HostWriteByte(0xe8); // [Page]
//SPI_HostWriteByte(0x0B);
SPI_HostWriteByte((uchar)(PA>>6));
SPI_HostWriteByte((uchar)((PA<<2)|(BFA>>8))); //改
SPI_HostWriteByte((uchar)BFA);
for(i=0;i<4;i++){SPI_HostWriteByte(0x00);}
// SPI_HostWriteByte(0x00);
for(i=0;i<len;i++){pHeader[i]=SPI_HostReadByte();}
SPI_CS=1;
}
/*读主存储页中数据到BUFFER1*/
/*参数: */
/* PA - 页地址,0~2047 */
/* BFA - 指定BUFFER中的起始写入地址(我认为确切的说应该是页内地址,即要读的数据在页内的首地址)*/
/* pHeader - 指定数据的首地址 */
/* len - 指定数据的长度 */
void AT45DB161B_MainMemorytoBufferRead(uint PA)///我改
{
// uint i;
while(!(AT45DB161B_StatusRegisterRead()&0x80));
// while(i++<255){if(AT45DB161B_StatusRegisterRead()&0x80){break;}}
SPI_CS=0;
SPI_HostWriteByte(0x53); // [Page]
//SPI_HostWriteByte(0x0B);
SPI_HostWriteByte((uchar)(PA>>6));
SPI_HostWriteByte((uchar)(PA<<2));
// SPI_HostWriteByte((uchar)BFA);
// for(i=0;i<4;i++){SPI_HostWriteByte(0x00);}
// SPI_HostWriteByte(0x00);
//for(i=0;i<len;i++){pHeader[i]=SPI_HostReadByte();}
SPI_CS=1;
}
/*描述: */
/* 将指定数据写入从某个地址(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 AT45DB161B_BufferWrite(uchar buffer,uint BFA,uchar *pHeader,uint len)
{
uint i;
while(!(AT45DB161B_StatusRegisterRead()&0x80));
//while(i++<255){if(AT45DB161B_StatusRegisterRead()&0x80){break;}}
SPI_CS=0;
switch(buffer){
case 1:SPI_HostWriteByte(0x84);break;
case 2:SPI_HostWriteByte(0x87);break;
}
SPI_HostWriteByte(0x00);
SPI_HostWriteByte((uchar)(BFA>>8));
SPI_HostWriteByte((uchar)BFA);
for(i=0;i<len;i++){SPI_HostWriteByte(pHeader[i]);} //改
SPI_CS=1;
}
/*描述: */
/* 将指定数据从某个地址(0~263)开始的BUFFER中读出。 */
/*参数: */
/* buffer - 选择BUFFER,01H选择BUFFER 1,02H选择BUFFER 2 */
/* 在该指令序列中,操作码D4H选择BUFFER 1,D6H选择BUFFER 2 */
/* BFA - BUFFER中的起始地址,0~263 */
/* pHeader - 待存数据的头指针 */
/* len - 待存数据的长度1~264 */
void AT45DB161B_BufferRead(uchar buffer,uint BFA,uchar *pHeader,uint len) //我改写的
{
uint i;
while(!(AT45DB161B_StatusRegisterRead()&0x80));
//while(i++<255){if(AT45DB161B_StatusRegisterRead()&0x80){break;}}
SPI_CS=0;
switch(buffer){
case 1:SPI_HostWriteByte(0xD4);break;
case 2:SPI_HostWriteByte(0xD6);break;
}
SPI_HostWriteByte(0x00);
SPI_HostWriteByte((uchar)(BFA>>8));
SPI_HostWriteByte((uchar)BFA);
SPI_HostWriteByte(0x00);
for(i=0;i<len;i++){pHeader[i]=SPI_HostReadByte();}
SPI_CS=1;
}
/*描述: */
/* 将指定数据写入从某个地址(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 AT45DB161B_BufferToMainMemoryPageProgramWithBuilt_inErase(uchar buffer,uint PA,uint BFA,uchar *pHeader,uint len)
{
//uint i;
AT45DB161B_BufferWrite(buffer,BFA,pHeader,len);
while(!(AT45DB161B_StatusRegisterRead()&0x80));
// while(i++<1000){if(AT45DB161B_StatusRegisterRead()&0x80){break;}}
SPI_CS=0;
switch(buffer){
case 1:SPI_HostWriteByte(0x83);break;
case 2:SPI_HostWriteByte(0x86);break;
}
SPI_HostWriteByte((uchar)(PA>>6)); //3位保留位,12位页地址位,9位无关位
SPI_HostWriteByte((uchar)(PA<<2));
// SPI_HostWriteByte((uchar)(PA>>8));
// SPI_HostWriteByte((uchar)PA);
SPI_HostWriteByte(0x00);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -