STM8L051F3P6内部具有256字节的EEPROM,在内存中的起始地址为:0x1000。下面是EEPROM数据的读写。
写数据到EEPROM:
void FLASH_WriteNByte(u8* pBuffer, u32 WriteAddr, u8 nByte)
{
FLASH_Unlock(FLASH_MemType_Data);
disableInterrupts();
while(FLASH_GetFlagStatus(FLASH_FLAG_DUL) == RESET);
while(nByte--)
{
FLASH_ProgramByte(WriteAddr, *pBuffer);
WriteAddr++;
pBuffer++;
FLASH_WaitForLastOperation(FLASH_MemType_Data);
}
enableInterrupts();
FLASH_Lock(FLASH_MemType_Data);
}
把无线手柄的通信频道存入EEPROM:
FLASH_WriteNByte((u8*)"NF",4096,2);
FLASH_WriteNByte(&NRF24L01_RFCH,4098,1);
从EEPROM读数据:
void FLASH_ReadNByte(u8* pBuffer, u32 ReadAddr, u8 nByte)
{
while(nByte--)
{
*pBuffer = FLASH_ReadByte(ReadAddr);
ReadAddr++;
pBuffer++;
}
}
单片机初始化的时候读出NRF24L01的通信频道值:
FLASH_ReadNByte(tempBuf,4096,3);
if(memcmp(tempBuf,"NF",2)==0)
{
if(tempBuf[2]<=100)
{
NRF24L01_RFCH=tempBuf[2];
nrfNum=tempBuf[2]/10-1;
nrfNum1=nrfNum;
}
}
|