AT45D081/AT45DB021/AT45DB0xx驱动程序(C51)用于大型存储的串行Flash
出自:单片机坐标 作者:梁衍龙
[CODE]/*---------------------------------------------------------
版 本: V2.2
作 者:梁衍龙
创作时间:2002年9月
==========================================================*/
#include "reg51.h"
#include "intrins.h"
#define NOP() { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); }
sbit SPI45D081_CS =P1^5;//片选接口
sbit SPI45D081_SK =P1^6;//时钟接口
sbit SPI45D081_DI =P1^7;//数据输入接口
sbit SPI45D081_DO =P1^4;//数据输出接口
delay(unsigned int t)
{ while(t--);
}
// SPI_SK上升沿数据有效 _/-
SPI45D081_WriteBits(unsigned char byte,unsigned char num_of_bits)
{ unsigned char count; byte=byte<<(8-num_of_bits); for(count=0;count<num_of_bits;count++) { SPI45D081_SK=0; byte<<=1; NOP(); SPI45D081_DI=CY; NOP(); SPI45D081_SK=1; NOP(); }
}
// SPI_SK下降沿数据有效 -\_
unsigned char SPI45D081_ReadByte()
{ unsigned char count,byte; for(count=0;count<8;count++) { SPI45D081_SK=1; NOP(); byte<<=1; SPI45D081_SK=0; NOP(); if(SPI45D081_DO) byte++; NOP(); } return(byte);
}
//Main Memory Page to Buffer 1 Transfer
void PageToBuffer1(unsigned int page)
{ SPI45D081_SK=1; //?? start NOP(); SPI45D081_CS=0; //start NOP(); //send 8 bit opcode, Main Memory Page to Buffer 1 Transfer: 0x53 SPI45D081_WriteBits(0x53,8); // fill 3 sk,don't care. SPI45D081_WriteBits(0x00,3); //send 12 bit address to 45D081 SPI45D081_WriteBits( (unsigned char)(page>>8) , 4 ); SPI45D081_WriteBits( (unsigned char)(page) , 8 ); // fill 9 sk,don't care. SPI45D081_WriteBits( 0x00 , 4 ); SPI45D081_WriteBits( 0x00 , 5 ); //total 32 bit SPI45D081_CS=1; // end delay(200);}
unsigned char ReadDataFromBuffer1(unsigned int address) { unsigned char dat; SPI45D081_SK=1; //?? start NOP(); SPI45D081_CS=0; //start NOP(); //send 8 bit opcode:0x54 SPI45D081_WriteBits(0x54,8); // fill 15 sk,don't care. SPI45D081_WriteBits(0x00,8); SPI45D081_WriteBits(0x00,7); //send 9 bit address to 45D081 buffer1 SPI45D081_WriteBits( (unsigned char)(address>>8) , 1 ); SPI45D081_WriteBits( (unsigned char)(address) , 8 ); //fill 8 bits blank SPI45D081_WriteBits( 0x00 , 8 ); // receive data dat=SPI45D081_ReadByte() ; SPI45D081_CS=1; // end return(dat);
}
//注:该程序适用程度可靠,注意使用的方法和硬件连接方式[/CODE] |