[STM8L]EEPROM操作讀與寫
STM8L帶有片上EEPROM,常用來保存參數(shù),事實(shí)上STM8L整個(gè)程序存儲(chǔ)區(qū)都可以用于作為EEPROM,只是默認(rèn)情況下被閉了。
不同型號(hào)的STM8L器件其內(nèi)部默認(rèn)劃分的EEPROM區(qū)域都是從0x1000地址開始,以下為STM8LXX的EEPROM讀寫例:
#defineEEPROM_STARTADDR0x1000
//EEPROM讀數(shù)據(jù)方法
uint8EEPROM_Read(uint16Addr,uint8*RxBuffer,uint8Length)
{
uint16T=0;
uint8*EEP;
EEP=(uint8*)(EEPROM_STARTADDR+Addr);
FLASH_DUKR=0xAE;
FLASH_DUKR=0x56;
FLASH_CR2=0x00;
while((!(FLASH_IAPSR&S3))&(T<0xFFFF))T++;
if(T==0xFFFF)return0;
while(Length--){
*RxBuffer++=*EEP++;
}
return1;
}
//EEPROM讀數(shù)據(jù)方法
uint8EEPROM_Write(uint16Addr,uint8*RxBuffer,uint8Lenth)
{
uint16T=0;
uint8*EEP;
EEP=(uint8*)(EEPROM_STARTADDR+Addr);
FLASH_DUKR=0xAE;
FLASH_DUKR=0x56;
FLASH_CR2=0x00;
while((!(FLASH_IAPSR&S3))&(T<0xFFFF))T++;
if(T==0xFFFF)return0;
while(Lenth--){
*EEP++=*RxBuffer++;
T=0;
while((!(FLASH_IAPSR&S2))&(T<0xFFFF))T++;
if(T==0xFFFF)return0;
}
FLASH_IAPSR&=~S3;
return1;
}
STM8LXX核對(duì)數(shù)據(jù),程序存儲(chǔ)器進(jìn)行統(tǒng)一編址,所以可以直接對(duì)EEPROM進(jìn)行讀寫,之間需要進(jìn)行解鎖操作,通過FLASH_DUKR完成,寫入特定序列的密碼即可,由于EEPROM的寫入速度較慢,所以需要進(jìn)行等待,判斷其上一步工作是否已完成。實(shí)測(cè)發(fā)現(xiàn),當(dāng)CPU主頻變化時(shí),其等待時(shí)間差別較大,使用時(shí)應(yīng)注意,并且需要加入等待超時(shí)處理。