打印

stm8s,想保存几个字节的数据到flash或者eeprom,改怎么做?

[复制链接]
7628|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
donkey89|  楼主 | 2011-8-8 21:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
如题,芯片是stm8s的芯片,需要用8个字节的flash或者eeprom保存运行的配置,应该怎么操作?操作越简单越好,代码越少越好.
我用的编译器是iar
沙发
上官金虹| | 2011-8-8 21:26 | 只看该作者
本帖最后由 上官金虹 于 2011-8-8 21:30 编辑

#include "stm8s_conf.h"

void __eeprom_write_8(unsigned short addr_eep,unsigned char data)
{
  FLASH_WaitForLastOperation(FLASH_MEMTYPE_DATA);   
  FLASH_Unlock(FLASH_MEMTYPE_DATA);
   
  FLASH_ProgramByte(addr_eep, data);  
   
  FLASH_WaitForLastOperation(FLASH_MEMTYPE_DATA);  
  FLASH_Lock(FLASH_MEMTYPE_DATA);   
}

void __eeprom_write_16(unsigned short addr_eep,unsigned short data)
{
  FLASH_WaitForLastOperation(FLASH_MEMTYPE_DATA);   
  FLASH_Unlock(FLASH_MEMTYPE_DATA);
   
  FLASH_ProgramByte(addr_eep,   data/256);  
  FLASH_WaitForLastOperation(FLASH_MEMTYPE_DATA);  
   
  FLASH_ProgramByte(addr_eep+1, data%256);
  FLASH_WaitForLastOperation(FLASH_MEMTYPE_DATA);  
   
  FLASH_Lock(FLASH_MEMTYPE_DATA);   
}

void __eeprom_write_32(unsigned short addr_eep,unsigned long data)
{
  FLASH_WaitForLastOperation(FLASH_MEMTYPE_DATA);   
  FLASH_Unlock(FLASH_MEMTYPE_DATA);
   
  FLASH_ProgramByte(addr_eep,   (unsigned char)(data>>24));  
  FLASH_WaitForLastOperation(FLASH_MEMTYPE_DATA);  
   
  FLASH_ProgramByte(addr_eep+1, (unsigned char)(data>>16));
  FLASH_WaitForLastOperation(FLASH_MEMTYPE_DATA);  
   
  FLASH_ProgramByte(addr_eep+2,   (unsigned char)(data>>8));  
  FLASH_WaitForLastOperation(FLASH_MEMTYPE_DATA);  
   
  FLASH_ProgramByte(addr_eep+3, (unsigned char)(data>>0));
  FLASH_WaitForLastOperation(FLASH_MEMTYPE_DATA);
   
  FLASH_Lock(FLASH_MEMTYPE_DATA);   
}


void __eeprom_write_many(unsigned short addr_eep,unsigned short size,unsigned short dummy,unsigned short addr_ram)
{
   
  FLASH_WaitForLastOperation(FLASH_MEMTYPE_DATA);   
  FLASH_Unlock(FLASH_MEMTYPE_DATA);
   
  for(unsigned short i=0;i<size;i++)
  {
     FLASH_ProgramByte(addr_eep+i,   *((unsigned char *)(addr_ram)+i));  
     FLASH_WaitForLastOperation(FLASH_MEMTYPE_DATA);  
  }
   
  FLASH_Lock(FLASH_MEMTYPE_DATA);   
}
volatile __eeprom __no_init char  eep_u8; 
volatile __eeprom __no_init short eep_u16;
volatile __eeprom __no_init long  eep_u32;

void main( void )
{
  eep_u8=123;
  eep_u16=12345;
  eep_u32=123456789;
     
  while(1);
}

使用特权

评论回复
板凳
上官金虹| | 2011-8-8 21:31 | 只看该作者
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:数显ESR电容等效内阻测量仪 http://cn.digit-esr.com

241

主题

1609

帖子

1

粉丝