本帖最后由 hlln20100410 于 2015-9-22 10:09 编辑
M25P32驱动程序调试时发现首次读写都正常,由于flash每次写必须要擦除,执行擦除指令后,片内变成0XFF,这也正常,但是擦除指令后不能再写入新的数据了,始终都是0XFF,求解
#include <MSP430x14x.h>
#include <MSP430x14x.h>
#define uchar unsigned char
#define uint unsigned int
/*****************************/
#define Busy 0x80
#define Q_IN P4IN & BIT3 //Q P4.3 FLASH OUT
#define CLR_clock P4OUT &= ~BIT1; //C = P4.1
#define SET_clock P4OUT |= BIT1;
#define CLR_CS1 P4OUT &= ~BIT2; //S1 = P4.2
#define SET_CS1 P4OUT |= BIT2;
#define CLR_DOUT P4OUT &= ~BIT0; //D = P4.0 --FLASH DIN
#define SET_DOUT P4OUT |= BIT0;
/***********************************/
uchar addr[3],r_data32[256];
void check();
/***********m25p32???******************/
void InitM25p32(void)
{
P4DIR &= ~BIT3; //Q 25P32????
P4DIR |= BIT0; //D 25P32????
P4DIR |= BIT1; //C 25P32????
P4DIR |= BIT2; //S1 25P32??
SET_CS1; //?S1
}
//*****************************************
void delayy(uint s)
{
uint tmp,ms;
for(ms=0;ms<s;ms++)
{
for(tmp=0;tmp<100;tmp++);
}
}
//******************************************
void write_yOrn(uchar instr)
{
uchar p,data;
data=instr;
SET_CS1;
delayy(10);
CLR_CS1;
delayy(10);
for(p=1;p<=8;p++)
{
CLR_clock;
delayy(10);
if((data&0x80)==0x80)
{SET_DOUT;}
else
{CLR_DOUT;}
SET_clock;
data=data<<1;
delayy(10);
}
SET_CS1;
}
//*************//??3 ????? ??????****************
void write_addr(uchar add_hh,uchar add_h,uchar add_l)
{
uchar add_data[3],j,i;
add_data[0]=add_hh;
add_data[1]=add_h;
add_data[2]=add_l;
for(j=0;j<3;j++)
{
for(i=0;i<8;i++)
{
CLR_clock;
delayy(10);
if((add_data[j]&0x80)==0x80)
{SET_DOUT;}
else
{ CLR_DOUT;}
SET_clock;
add_data[j]=add_data[j]<<1;
delayy(10);
}
}
}
//********************************
void write_instruction(uchar win)
{
uchar i,x_data;
x_data=win;
for(i=0;i<8;i++)
{
CLR_clock;
delayy(10);
if((x_data&0x80)==0x80)
{ SET_DOUT;}
else
{CLR_DOUT;}
SET_clock;//?? ????????
delayy(10);
x_data=x_data<<1;
}
}
//**************************
uchar readbyte(void)//???????
{ uchar s;
uchar read_data=0x00;
for(s=0;s<8;s++)
{ //?? ??? ????
CLR_clock;
delayy(10);
read_data=read_data<<1;
if(Q_IN) read_data |= 0x01; //???
else read_data&=0xfe;
SET_clock;
delayy(10);
}
return(read_data);
}
//******************??????*****************
void readsome(uchar r_addhh,uchar r_addh,uchar r_addl,uint data_count,uchar *flash_256)
{
uint i;
SET_CS1;
SET_clock;
CLR_CS1;
write_instruction(0x03); //read data bytes
write_addr(r_addhh,r_addh,r_addl);
delayy(10);
for( i=0;i<data_count;i++)
{
*flash_256=readbyte();
flash_256++;
}
SET_CS1;
}
//************************
//***********************
void writeinPacket(unsigned char w_addHH,unsigned char w_addH,unsigned char w_addL,unsigned char sdat[256]) //??????
{ uchar xx,i;
uint w;
write_yOrn(0x06);
delayy(10);
CLR_CS1;
write_instruction(0x02);
write_addr(w_addHH,w_addH,w_addL);
delayy(10);
for(w=0;w<256;w++)
{
xx=sdat[w];
for(i=0;i<8;i++)
{
CLR_clock;
delayy(10);
if(xx&0x80)
{
SET_DOUT;
}
else
CLR_DOUT;
SET_clock;
xx=xx<<1;
delayy(10);
}
}
SET_CS1;
// delayy(1000);
}
//**********************
void SE(uchar hex2,uchar hex1,uchar hex0) //???? D8H Delay(1000);
{
uchar addr2[3];
addr2[0]=hex0;
addr2[1]=hex1;
addr2[2]=hex2;
write_yOrn(0x06);//写使能
CLR_CS1;//片选
write_instruction(0xd8);
write_addr(addr2[2],addr2[1],addr2[0]);
SET_CS1;
check();
write_yOrn(0x04);
}
//**********************
void BE(void)//???? C7H Delay(17000);
{
write_yOrn(0x06);
CLR_CS1;
write_instruction(0xc7);
SET_CS1;
check();
write_yOrn(0x04);
}
void Write_Status(uchar data)
{
write_yOrn(0x06);
delayy(100);
CLR_CS1;
write_instruction(0x01);
write_instruction(data);
SET_CS1;
check();
write_yOrn(0x04);
delayy(10);
}
void check()
{
uchar temp;
CLR_CS1;
write_instruction(0x05); //读状态寄存器
temp=readbyte();
SET_CS1;
delayy(10);
while(temp&0x01!=0)
{
;
}
}
|