码迷,mamicode.com
首页 > 其他好文 > 详细

STM8S EEPROM 操作

时间:2014-05-18 03:49:33      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:blog   class   code   c   tar   int   

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);//将首地址的数据读出来


STM8S EEPROM 操作,布布扣,bubuko.com

STM8S EEPROM 操作

标签:blog   class   code   c   tar   int   

原文地址:http://blog.csdn.net/chuangwu2009/article/details/26049877

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!