EEPROM read and write STM8L152 (using the firmware library)

STM8L MCU built-1K bytes of EEPROM, very easy to use.

EEPROM address is:

1.6.1 firmware using the library

EEPROM_Write_Byte void (uint16_t Addr, uint8_t the Data)
{
asm ( "the SIM"); // close the interrupt
FLASH_Unlock (FLASH_MemType_Data);
// the while (FLASH_IAPSR_DUL!); // if DUL is not set, indicating unlock unsuccessful, it is best to join timeout determination
// FLASH_EraseByte (EEPROM_STARTADDR + Addr); // erase write
// FLASH_WaitForLastOperation (FLASH_MemType_Data); // wait until the operation
FLASH_ProgramByte (Addr + EEPROM_STARTADDR, the Data);
FLASH_WaitForLastOperation (FLASH_MemType_Data);
FLASH_Lock (FLASH_MemType_Data);
ASM ( "rim"); // open the interruption
}

// Note that the writing of 4 bytes must be written to an integral multiple
void eeprom_write_word to (uint16_t Addr, the Data uint32_t)
{
ASM ( "SIM"); // interrupt disabled
FLASH_Unlock (FLASH_MemType_Data);
// FLASH_EraseByte (EEPROM_STARTADDR Addr +) ; // erase write
// FLASH_WaitForLastOperation (FLASH_MemType_Data); // wait until the operation
// FLASH_EraseByte (EEPROM_STARTADDR + Addr + 1 ); // erase write
// FLASH_WaitForLastOperation (FLASH_MemType_Data); // wait for the operation to complete
// FLASH_EraseByte (EEPROM_STARTADDR + Addr + 2 ); // erase write
// FLASH_WaitForLastOperation (FLASH_MemType_Data); // wait until the operation
// FLASH_EraseByte (EEPROM_STARTADDR + Addr + 3 ); // erase write
// FLASH_WaitForLastOperation (FLASH_MemType_Data); // wait until the operation
FLASH_ProgramWord (Addr + EEPROM_STARTADDR, the Data);
FLASH_WaitForLastOperation (FLASH_MemType_Data); // wait for the operation to complete
FLASH_Lock (FLASH_MemType_Data);
asm ( "RIM"); // open the interruption
}

uint8_t EEPROM_Read_Byte(uint16_t Addr)
{
uint8_t res;
res = FLASH_ReadByte(EEPROM_STARTADDR+Addr);
return res;
}

Before writing do not have to erase to erase, direct byte operation, easy not to do the

Guess you like

Origin www.cnblogs.com/yuanguanghui/p/Yuan_guanghui.html