这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » 际踅涣?W29C040 哪位能帮我一下,W29C040能读不能写

共2条 1/1 1 跳转至

际踅涣?W29C040 哪位能帮我一下,W29C040能读不能写

院士
2006-09-17 18:14:16     打赏
际踅涣?W29C040 哪位能帮我一下,W29C040能读不能写



关键词: 际踅     W29C040     位能     帮我     一下     能读     不能    

院士
2006-12-22 22:43:00     打赏
2楼
问 #include <INCLUDE.H>

typedef unsigned char           BYTE;
typedef unsigned int            WORD;
#define  uchar unsigned char
#define  uint  unsigned int
#define  ulong unsigned long
sbit   Cs29f040  =               P2^7;

#define FALSE                   0
#define TRUE                    1

#define SECTOR_SIZE             4096    /* Must be 4096 bytes for 39VF080 */
#define BLOCK_SIZE              65536   /* Must be 64K bytes for 39VF080  */
#define SECTORLEN      256//128
#define SST_ID                  0xBF      /* SST Manufacturer's ID code  */
#define SST_29SF040             0x13      /* SST39VF040 device code      */
#define SST_39VF080             0xD8      /* SST39VF080 device code      */
xdata  uchar   U6Add         _at_ 0x6000 ;
idata  uchar   U6Data;
xdata WORD SST_id2;
xdata WORD SST_id1;
idata uint iFlashTmp1,iFlashTmp2;
xdata uchar cFlashTmp1,cFlashTmp2;
xdata uchar *pFlashTmp1;
xdata ulong  FlashOneByteAdd;
xdata uchar  FlashOneByte;
xdata ulong  FlashNByteAdd,FlashAddTmp;
xdata uint   FlashNByteLen;
xdata uchar  *pFlashNByte;
xdata uchar  SectorBuf[SECTORLEN];

extern xdata uchar ReBuf[];
extern union
{
     ulong TmpLong;
     uint  TmpUint[2];
     uchar TmpUchar[4];
}UTmpLong;
///////////////////////////////////////////////////////////
//使用的全局变量有pFlashTmp1,SST_id1 ,SST_id2  ;
uchar Check_SST_39VF080()
{
        CFLASH=0;
        vDelayMS(1);
        U6Add=0x80;        //高四位为0
        /*  Issue the Software Product ID code to 39VF080   */
        pFlashTmp1= (BYTE xdata *)(0x5555+0x8000);  /* set up address to be C000:5555h  */
        *pFlashTmp1= 0xAA;                     /* write data 0xAA to the address   */
        pFlashTmp1= (BYTE xdata *)(0x2AAA+0x8000);   /* set up address to be C000:2AAAh  */
        *pFlashTmp1= 0x55;                    /* write data 0x55 to the address   */
        pFlashTmp1= (BYTE xdata *)(0x5555+0x8000); /* set up address to be C000:5555h  */
        *pFlashTmp1= 0x90;              /* write data 0x90 to the address   */
        vDelayMS(1);
        /* Read the product ID from 39VF080 */
        pFlashTmp1= (BYTE xdata *)0x8000;   /* set up address to be C000:0000h */
        SST_id1  =  *pFlashTmp1;               /* get first ID byte               */
        SST_id1  =  SST_id1 & 0x00FF;   /* mask of higher byte             */
        pFlashTmp1= (BYTE xdata *)0x8001; /* set up address to be C000:0001h */
        SST_id2  =  *pFlashTmp1;             /* get second ID byte              */
        pFlashTmp1= (BYTE xdata *)0x8002; /* set up address to be C000:0001h */
        SST_id2  =  *pFlashTmp1;             /* get second ID byte              */
        U6Add=0x8f;//7fff2
        pFlashTmp1= (BYTE xdata *)0xfff2; /* set up address to be C000:0001h */
        SST_id2  =  *pFlashTmp1;             /* get second ID byte              */
        /* Determine whether there is a SST39VF080 installed or not */
        if ((SST_id1 == SST_ID) && (SST_id2 ==SST_29SF040))
                SST_id1 = TRUE;
        else
                SST_id1 = FALSE;

        /* Issue the Soffware Product ID Exit code thus returning the 39VF080 */
        /* to the read operating mode                                         */
       //Read/Reset
        pFlashTmp1= (BYTE xdata *)(0x5555+0x8000); /* set up address to be C000:5555h   */
        *pFlashTmp1= 0xAA; _nop_();                   /* write data 0xAA to the address    */
        pFlashTmp1= (BYTE xdata *)(0x2AAA+0x8000); /* set up address to be C000:2AAAh   */
        *pFlashTmp1= 0x55; _nop_();                   /* write data 0x55 to the address    */
        pFlashTmp1= (BYTE xdata *)(0x5555+0x8000); /* set up address to be C000:5555h   */
        *pFlashTmp1= 0xF0; _nop_();                   /* write data 0xF0 to the address    */

        //Delay_150_Nano_Seconds();        /* insert delay time = Tida          */
        DeyNTip(250);
        CFLASH=1;
        return(SST_id1 );
}
//////////////////////////////////////////////////////////////////////////
//使用到全局变量pFlashTmp1,FlashOneByte,FlashOneByteAdd
/*
void    Read_One_Byte ()
{
        UTmpLong.TmpLong=(FlashOneByteAdd<<1);
    U6Data&=0xf0;
    U6Data+=UTmpLong.TmpUchar[1];
        U6Add=U6Data;  //置地址高4位

    iFlashTmp1=(uint)FlashOneByteAdd;
        iFlashTmp1|= 0x8000;
        FlashOneByte=*(BYTE xdata *)iFlashTmp1;
}
*/
/////////////////////////////////////////////////////////////
#if 1
//使用到全局变量FlashNByteAdd,lashNByteLen,pFlashNByte
void  Read_N_Byte (ulong Add,uint Len,uchar *pBuf)
{
    xdata uint  iTmp1;
    CFLASH=0;

    ET0=0;
    for(iTmp1=0;iTmp1<Len;iTmp1++)
   {

        UTmpLong.TmpLong=(Add<<1);
    U6Data&=0xf0;
    U6Data+=UTmpLong.TmpUchar[1];

        //A15=OPENFLASH;
        U6Add=U6Data;  //置地址高4位
    iFlashTmp1=(uint)Add;
        iFlashTmp1|= 0x8000;


        *pBuf=*(BYTE xdata *)iFlashTmp1;

    //下一个字节
    Add++;
    pBuf++;

    }
    A15=CLOSEFLASH;
    ET0=1;
    CFLASH=1;
}
#endif
/////////////////////////////////////////////
//使用到全局变量FlashNByteAdd,lashNByteLen,pFlashNByte
/*
void  Read_N_Byte (ulong Add,uint Len,uchar *pBuf)
{
    xdata uint  iTmp1;
    CFLASH=0;

    ET0=0;
    for(iTmp1=0;iTmp1<Len;iTmp1++)
   {

        UTmpLong.TmpLong=(Add<<1);
    U6Data&=0xf0;
    U6Data+=UTmpLong.TmpUchar[1];

        A15=OPENFLASH;
        U6Add=U6Data;  //置地址高4位
    iFlashTmp1=(uint)Add;
        iFlashTmp1|= 0x8000;

        //*pBuf=*(BYTE xdata *)iFlashTmp1;
        PORT_2=iFlashTmp1>>8;
        WR=1;
        RD=0;
        PORT_0=iFlashTmp1;
        ALE=1;
        _nop_();
        ALE=0;
        _nop_();_nop_();

        *pBuf=PORT_0;

        WR=1;
        RD=1;


    //下一个字节
    Add++;
    pBuf++;

    }
    A15=CLOSEFLASH;
    ET0=1;
    CFLASH=1;
}
*/
//////////////////////////////////////////////////////////////////////////
void Erase_One_Sector()
{
        return;
        ResetWatchDog();
        A15=OPENFLASH;
        UTmpLong.TmpLong=(FlashNByteAdd<<1);
          U6Data&=0xf0;
        U6Data+=UTmpLong.TmpUchar[1];
        U6Add=U6Data;  //置地址高4位

        *(BYTE xdata *)0x5555 = 0xAA;
        *(BYTE xdata *)0x2AAA = 0x55;
        *(BYTE xdata *)0x5555 = 0x80;
        *(BYTE xdata *)0x5555 = 0xAA;
        *(BYTE xdata *)0x2AAA = 0x55;
        U6Add=U6Data;  //置地址高4位
        iFlashTmp1=FlashNByteAdd;
        iFlashTmp1|=0x8000;

        //pFlashTmp1=(BYTE xdata *)iFlashTmp1;
          //*pFlashTmp1=0x20;
        *(BYTE xdata *)iFlashTmp1 =0x20;
        vDelayMS(28);
        ResetWatchDog();
}
////////////////////////////////////////////////////////
//使用到全局变量pFlashTmp1,FlashOneByte,FlashOneByteAdd
data uchar CharTmp;

void Program_One_Sector()
{
     if(FlashNByteAdd<ZIKUENDADDR)
        return;
     //A15=OPENFLASH;
     //关闭写保护
     
        ResetWatchDog();
        U6Data&=0xf0;
        U6Add=U6Data;  //置地址高4位
        *(BYTE xdata *)(0x5555+0x8000) = 0xAA;
        *(BYTE xdata *)(0x2AAA+0x8000) = 0x55;
        *(BYTE xdata *)(0x5555+0x8000) = 0x80;
        *(BYTE xdata *)(0x5555+0x8000) = 0xAA;
        *(BYTE xdata *)(0x2AAA+0x8000) = 0x55;
        *(BYTE xdata *)(0x5555+0x8000) = 0x20;
        //vDelayMS(15);
     
       /*
      //启动写保护时要加此三个字节的命令
        ResetWatchDog();
        U6Data&=0xf0;
        U6Add=U6Data;  //置地址高4位
        *(BYTE xdata *)(0x5555+0x8000) = 0xAA;
        *(BYTE xdata *)(0x2AAA+0x8000) = 0x55;
        *(BYTE xdata *)(0x5555+0x8000) = 0xA0;
       */


     FlashAddTmp=FlashNByteAdd;
     UTmpLong.TmpLong=(FlashAddTmp<<1);

     U6Data&=0xf0;
     U6Data+=UTmpLong.TmpUchar[1];
     U6Add=U6Data;  //置地址高4位
     FlashAddTmp>>=8;
     FlashAddTmp<<=8;

     iFlashTmp1=(uint)FlashAddTmp;
     iFlashTmp1|=0x8000;

     for(iFlashTmp2=0;iFlashTmp2<SECTORLEN;iFlashTmp2++)
    {

        CharTmp=SectorBuf[iFlashTmp2];
        U6Add=U6Data;  //置地址高4位
        *((BYTE xdata *)iFlashTmp1) =CharTmp;
    iFlashTmp1++;
    }
    vDelayMS(15);
}


/////////////////////////////////////////////////////////////
//使用到全局变量pFlashTmp1,FlashOneByte,FlashOneByteAdd
//使用到全局变量FlashNByteAdd,lashNByteLen,pFlashNByte
//填写FlashNByteAdd,flashNByteLen,pFlashNByte便可使用此函数
void Program_N_Byte ()
{
        CFLASH=0;
        EA=0;
        //A15=OPENFLASH;
        //计算页内偏移量
         cFlashTmp2=(uchar)FlashNByteAdd;
        //读取此页的内容出来
        FlashAddTmp=FlashNByteAdd;
        FlashAddTmp>>=8;
        FlashAddTmp<<=8;
    Read_N_Byte (FlashAddTmp,SECTORLEN,SectorBuf);
        //擦除此扇区
    Erase_One_Sector();
    //重新组织此扇区的内容
    if(    FlashNByteLen>SECTORLEN-cFlashTmp2)
    {
         memcpy( (uchar *)&SectorBuf[cFlashTmp2], pFlashNByte, SECTORLEN-cFlashTmp2);
             //将此扇区的内容写回
             Program_One_Sector();
             //已经写了SECTORLEN-cFlashTmp2个字节  指针后移
         cFlashTmp2=SECTORLEN-cFlashTmp2;
         FlashNByteLen-=cFlashTmp2;
         pFlashNByte+=cFlashTmp2;
         FlashNByteAdd+=cFlashTmp2;
      }
    else
    {

             //memmove ( pFlashNByte,&SectorBuf[cFlashTmp2],  FlashNByteLen);
             while(FlashNByteLen)
             {
                   FlashNByteLen--;
                   SectorBuf[cFlashTmp2++]=*pFlashNByte++;
              }
             //将此扇区的内容写回
             Program_One_Sector();
         //FlashNByteLen=0;
             A15=CLOSEFLASH;
             EA=1;
             CFLASH=1;
             return;
    }


       //看有没有写完    (整页写)
       while(FlashNByteLen>=SECTORLEN)
       {
    //擦除此扇区
        Erase_One_Sector();
    //组织此扇区的内容
    memcpy(SectorBuf,pFlashNByte,SECTORLEN);
    //将内容写回此扇区
    Program_One_Sector();
    //已经写了SECTORLEN个字节  指针后移
    FlashNByteLen-=SECTORLEN;
    pFlashNByte+=SECTORLEN;
    FlashNByteAdd+=SECTORLEN;
    }
    //看有没有写完    (最后一页)
    if(FlashNByteLen>0)
    {
       //计算页内偏移量
            cFlashTmp2=FlashNByteAdd;
       cFlashTmp2&=0x80;
       //读取此页的内容出来
           FlashAddTmp=FlashNByteAdd;
           FlashNByteAdd>>=8;
           FlashNByteAdd<<=8;
          Read_N_Byte (FlashNByteAdd,SECTORLEN,SectorBuf);
           //擦除此扇区
       Erase_One_Sector();
           //重新组织此扇区的内容
       memcpy( SectorBuf, pFlashNByte, FlashNByteLen);
       //将此扇区的内容写回
           Program_One_Sector();
       }
       A15=CLOSEFLASH;
       EA=1;
       CFLASH=1;
}
/////////////////////////////////////////


共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]