智能车制作

 找回密码
 注册

扫一扫,访问微社区

查看: 2175|回复: 2
打印 上一主题 下一主题

flash读写问题 复位丢失

[复制链接]

3

主题

8

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
302
QQ
威望
239
贡献
27
兑换币
0
注册时间
2010-12-3
在线时间
18 小时
跳转到指定楼层
1#
发表于 2011-9-24 15:36:16 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#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 */
}







这是写的程序 调试时可用 但是一旦把读操作注释掉 只执行写入时 数据就全没了 就解答 谢谢

3

主题

8

帖子

0

精华

中级会员

Rank: 3Rank: 3

积分
302
QQ
威望
239
贡献
27
兑换币
0
注册时间
2010-12-3
在线时间
18 小时
2#
 楼主| 发表于 2011-9-24 16:21:34 | 只看该作者
表达可能没清楚
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 */
}
回复 支持 反对

使用道具 举报

9

主题

48

帖子

0

精华

高级会员

Rank: 4

积分
951
威望
505
贡献
282
兑换币
306
注册时间
2013-9-2
在线时间
82 小时
毕业学校
成都大学
3#
发表于 2014-5-3 22:07:07 | 只看该作者
我的为什么最多只能储存13个数据的  
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

关于我们|联系我们|小黑屋|智能车制作 ( 黑ICP备2022002344号

GMT+8, 2024-6-2 14:26 , Processed in 0.112181 second(s), 30 queries , Gzip On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表