IAR STM8 1.2 直接使用__eeprom关键字访问EEPROM

2019-12-23 18:36发布

IAR STM8 1.2 直接使用__eeprom关键字访问EEPROM

STM8L15X:
#include "stm8l15x_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);  
  
}



STM8S:
#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);

}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
42条回答
stewen
1楼-- · 2019-12-27 14:43
 精彩回答 2  元偷偷看……
to_xyr
2楼-- · 2019-12-27 15:09
回复【10楼】bluefeel 郁闷感觉
-----------------------------------------------------------------------

我COPY怎么会出现Error[Pe137]: expression must be a modifiable lvalue
Orpheus
3楼-- · 2019-12-27 18:46
我总卡死在这里“EEPROM_WaitForLastOperation(); “
郁闷
wsm80828
4楼-- · 2019-12-27 21:05
可以有
zmh169
5楼-- · 2019-12-27 22:45
正准备用STM8的EEPROM,学习一下
feiyue
6楼-- · 2019-12-27 23:39
 精彩回答 2  元偷偷看……

一周热门 更多>