把已经调试好的flash存储芯片W25X40的C语言驱动代码分享给大家。
本帖最后由 jianhong_wu 于 2015-7-22 10:19 编辑★坚鸿-深圳:
今天在一个项目上要用到flash存储芯片W25X40,现在把已经调试好的C语言驱动代码分享给大家参考:
unsigned char Read_Status_Register();
unsigned char Get_Byte();
void Send_Byte(unsigned char dd);
unsigned char Read25(unsigned long byte_address);//从指定地址中读取一个字节数据
void Byte_Program(unsigned long byte_address, unsigned char tempdata) ;//写入一个字节数据到指定存储地址里。
void Chip_Erase();//整片擦除。
void busy_check();
unsigned char Read_Status_Register()
{
unsigned char outdata= 0;
TRISB5=0;
CE=0;
asm("nop");asm("nop");
Send_Byte(0x05);
outdata=Get_Byte();
asm("nop");asm("nop");
CE=1;
asm("nop");asm("nop");
return outdata;
}
unsigned char Get_Byte()
{
unsigned char outdata,tempdata;
outdata=0;
TRISB7=0;TRISB6=1;TRISB5=0;TRISB4=0;
RB6=1;
asm("nop");asm("nop");
CE=0;
for(tempdata=0;tempdata<8;tempdata++)
{ CLRWDT();
outdata<<=1;
SCK=1;
asm("nop");asm("nop");
if(RB6==1)outdata++;
SCK=0;
asm("nop");asm("nop");
}
return(outdata);
}
void Send_Byte(unsigned char dd)
{
unsigned char tempdata=0;
TRISB7=0;TRISB6=1;TRISB5=0;TRISB4=0;
CE=0;
for(tempdata=0;tempdata<8;tempdata++)
{ CLRWDT();
if(dd>=0x80)SI=1;
else SI=0;
dd<<=1;
SCK=1;
asm("nop");asm("nop");
SCK=0;
asm("nop");asm("nop");
}
}
unsigned char Read25(unsigned long byte_address)
{
unsigned char tempdata = 0;
unsigned long temp_address;
DS1302_CE=0; //CE引脚为低,数据传送中止
TRISB5=0;
CE=0;
Send_Byte(0x03);
temp_address=byte_address;
Send_Byte((temp_address&0x00ff0000)>>16); /* send 3 address bytes */
temp_address=byte_address;
Send_Byte((temp_address&0x0000ff00)>>8);
temp_address=byte_address;
Send_Byte((temp_address&0x000000ff));
tempdata= Get_Byte();
CE=1;
return tempdata;
}
void Byte_Program(unsigned long byte_address, unsigned char tempdata)
{
unsigned long temp_address;
DS1302_CE=0; //CE引脚为低,数据传送中止
TRISB5=0;
CE=0;
Send_Byte(0x06);
CE=1;
busy_check();
CE=0;
Send_Byte(0x02);
temp_address=byte_address;
Send_Byte((temp_address&0x00ff0000)>>16); /* send 3 address bytes */
temp_address=byte_address;
Send_Byte((temp_address&0x0000ff00)>>8);
temp_address=byte_address;
Send_Byte((temp_address&0x000000ff));
Send_Byte(tempdata);
CE=1;
Delay1(1);
}
void Chip_Erase()
{
DS1302_CE=0; //CE引脚为低,数据传送中止
TRISB5=0;
CE=0;
Send_Byte(0x06);
CE=1;
CE=0;
Send_Byte(0x60);
CE=1;
busy_check();
Delay1(300);
check25=Read25(520190);
if(check25!=0x55)
{
Byte_Program(520190,0x55);
}
}
void busy_check()
{
unsigned char data_temp;
TRISB5=0;
while(data_temp== 0x03)
{
CLRWDT();
data_temp=Read_Status_Register();
}
}
上述代码用到的Delay1延时函数如下:void Delay1(unsigned int MS)
{
unsigned char us,usn;
while(MS!=0) //for 12M
{
CLRWDT();
usn = 2;
while(usn!=0)
{ CLRWDT();
us=0xf5;
while (us!=0){us--;};
usn--;
}
MS--;
}
}
页:
[1]