首页 > 代码库 > STM8S EEPROM 操作

STM8S EEPROM 操作

STM8S 内置EEPROM,对于很大需要带记忆的产品来说,是个很好的资源,以下是我个人摸索出来的,并且验证OK,大家如需要可放心使用。


 #define EEPROMADDR0X000  ((u32)(FLASH_DATA_START_PHYSICAL_ADDRESS))
 #define EEPROMADDR0X001  ((u32)(FLASH_DATA_START_PHYSICAL_ADDRESS+1))
 #define EEPROMADDR0X002  ((u32)(FLASH_DATA_START_PHYSICAL_ADDRESS+2))
 #define EEPROMADDR0X003  ((u32)(FLASH_DATA_START_PHYSICAL_ADDRESS+3))

void FLASH_Unlock(FLASH_MemType_TypeDef MemType)
{
    /* Check parameter */
    assert_param(IS_MEMORY_TYPE_OK(MemType));

    /* Unlock program memory */
    if (MemType == FLASH_MEMTYPE_PROG)
    {
        FLASH->PUKR = FLASH_RASS_KEY1;
        FLASH->PUKR = FLASH_RASS_KEY2;
    }

    /* Unlock data memory */
    else
    {
        FLASH->DUKR = FLASH_RASS_KEY2; /* Warning: keys are reversed on 

data memory !!! */
        FLASH->DUKR = FLASH_RASS_KEY1;
    }
}

void FLASH_DeInit(void)
{
    u8 temp = 0;
    FLASH->CR1 = FLASH_CR1_RESET_VALUE;
    FLASH->CR2 = FLASH_CR2_RESET_VALUE;
    FLASH->NCR2 = FLASH_NCR2_RESET_VALUE;
    FLASH->IAPSR &= (u8)(~FLASH_IAPSR_DUL);
    FLASH->IAPSR &= (u8)(~FLASH_IAPSR_PUL);
    temp = FLASH->IAPSR; 
    /* Reading of this register causes the clearing of status flags */
}

void FLASH_ProgramByte(u32 Address, u8 Data)
{
    /* Check parameters */
    assert_param(IS_FLASH_ADDRESS_OK(Address));
    *((PointerAttr u8*) Address) = Data;
}

u8 FLASH_ReadByte(u32 Address)
{
    /* Check parameter */
    assert_param(IS_FLASH_ADDRESS_OK(Address));
    return(*((PointerAttr u8*) Address)); /* Read byte */

}

  FLASH_Unlock(FLASH_MEMTYPE_DATA);//解密EEPROM 可编程
  FLASH_DeInit();//EEPROM寄存器复位

  FLASH_Unlock(FLASH_MEMTYPE_DATA);//解密EEPROM 可读写数据
  FLASH_ProgramByte(EEPROMADDR0X000,0X25);//在首地址写入数据0x25
  FLASH_ReadByte(EEPROMADDR0X000);//将首地址的数据读出来