?? 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 + -