智能车制作

标题: flash读写问题 复位丢失 [打印本页]

作者: powerkoala    时间: 2011-9-24 15:36
标题: flash读写问题 复位丢失
#include <hidef.h>      /* common defines and macros */
#include <MC9S12XS128.h>     /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"
#define D_Addr 0x00
#define Program_Dflash  0x11
typedef unsigned char UINT8;
typedef unsigned int UINT16;
typedef  unsigned char      uchar;
typedef  unsigned int       uint;
unsigned char read[8];
uchar k;
void delay(uint j)
{

  uchar i;
  for(;j>0;j--)
  for(i=20;i>0;i--);
}
void Write_Command(uchar index,uint fcmd,uint addr)
{
  FCCOBIX = index;
//set the  conmand mode and parameter

  FCCOB = (fcmd<<8) | addr; //set the address and the data

}

void Pre_Start()
{

if(FCLKDIV_FDIVLD ==0)

FCLKDIV |= 0x0f;  //设置时钟分频,外部晶振为8Mhz
  


}

void Start_Command()
{
  FSTAT_CCIF = 1; //To start the command. clear the FSTAT_CCIF begin to write the lauch command
}

void Wait_Complete_Command()
{
  while(FSTAT_CCIF == 0);
//if the command is finish FSTAT_CCIF will be zero
}


void Write_D_Flash()
{

  uint des_addr = D_Addr;



     Pre_Start();

     if(FSTAT_CCIF)
     {

        if(FSTAT_ACCERR)
        {

          FSTAT_ACCERR = 1;
          delay(100);           //似乎此处必须加延时
        }
        if(FSTAT_FPVIOL)
        {

          FSTAT_FPVIOL = 1;
          delay(100);
        }
        Write_Command(0x00,Program_Dflash,0x10);

        Write_Command(0x01,0x01,des_addr);  //0X00

        Write_Command(0x02,1,2);
        Write_Command(0x03,4,8);
        Write_Command(0x04,16,32);
        Write_Command(0x05,64,128);



        Start_Command();

        Wait_Complete_Command();
      }

}

void ReadEeprom( uchar * srcAddr, uint destAddr, UINT16 size)
{
   
EPAGE=0X00;
/* current location */   


while(size != 0)

{

/* copy to destination */

  *(UINT16 *)srcAddr=*(UINT16 *)destAddr;

/* next address */

destAddr=destAddr+1;

/* next address */


srcAddr=srcAddr+1;

/* one byte less */


size--;

}
}

void main(void)
{
  /* put your own code here */




   DDRB=0XFF;
   Write_D_Flash();
   ReadEeprom(read,0x0900,0x08);


   for(;;)

   {



for(k=0;k<8;k++) {

PORTB=read[k];
   delay(50000);
   delay(50000);

}
   


   } /* wait forever */
  /* please make sure that you never leave this function */
}







这是写的程序 调试时可用 但是一旦把读操作注释掉 只执行写入时 数据就全没了 就解答 谢谢
作者: powerkoala    时间: 2011-9-24 16:21
表达可能没清楚
main函数里这样写可能会好一点
void main(void)
{
  /* put your own code here */
  

   
   uchar read1[8]={1,2,4,8,16,32,64,128};
   DDRB=0XFF;
   
   

   

   ReadEeprom(read,0x0800,0x08);
   k=read[1];

         for(;;) {
         
            
   if(k>=7)k=0;
   k++;
         PORTB=read1[k];
   delay(50000);
   delay(50000);
   Write_D_Flash(k);
         
   } /* wait forever */
  /* please make sure that you never leave this function */
}
作者: bubian    时间: 2014-5-3 22:07
我的为什么最多只能储存13个数据的  





欢迎光临 智能车制作 (http://111.231.132.190/) Powered by Discuz! X3.2