新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STM8L探索套件學(xué)習(xí)筆記-EEPROM(十一)

STM8L探索套件學(xué)習(xí)筆記-EEPROM(十一)

作者: 時(shí)間:2016-11-24 來源:網(wǎng)絡(luò) 收藏
上節(jié)將到官方例程使用EEPROM存儲(chǔ)外圍電路消耗的電流值,今天我們對(duì)STM8L的EEPROM介紹下。STM8L帶有的32K的FLASH和1K的EEPROM都可以編程和擦除,編程模式有1、字節(jié)byte,2、字word,3、BLOCK和4、編程和擦寫完中斷。

由于默認(rèn)是鎖定的,所以對(duì)FLASH和EEPROM編程首先要解鎖,解鎖的方式是寫如寄存器特定值,0X56 0XAE。在IAR編譯器當(dāng)中,__eeprom用于EEPROM存儲(chǔ)空間,控制數(shù)據(jù)存放,控制指針類型和存放。@用于變量的絕對(duì)地址定位。也可以用#pragma location命令。
EEPROM區(qū)域數(shù)據(jù)存儲(chǔ):

用關(guān)鍵字__eeprom控制來存放,__eeprom關(guān)鍵字寫在數(shù)據(jù)類型前后效果一樣。
__eeprom unsigned char a;//定義一個(gè)變量存放在EEPROM空間
unsigned char __eeprom a;//效果同上
__eeprom unsigned char p[];//定義一個(gè)數(shù)組存放在EEPROM空間
對(duì)于EEPROM空間的變量操作同SRAM數(shù)據(jù)空間的操作方法一樣,編譯器會(huì)自動(dòng)
調(diào)用__EEPUT(ADR,VAL),__EEGET(VAR, ADR)宏函數(shù)來對(duì)EEPROM變量的
操作。
EEPROM空間絕對(duì)地址定位:
__eeprom unsigned char a @ 0x8;//定義一個(gè)變量存放在EEPROM空間
0X08單元
__eeprom unsigned char p[] @ 0x22//定義一個(gè)數(shù)組存放在EEPROM空間,
開始地址為0X22單元
__eeprom unsigned char a @ 0x08=9;//定義一個(gè)常數(shù)存放在EEPROM空
間0X08單元
__eeprom unsigned char p[] @0x22={1,2,3,4,5,6,7,8};
//定義一個(gè)組常數(shù)存放在EEPROM空間開始地址為0X22單元
由于常數(shù)在EEPROM空間的地址是已經(jīng)分配的,讀取EEPROM空間值可以用
變量和地址。

需要用戶外加三個(gè)函數(shù)才能使得編譯器使用_eeprom變量自動(dòng)寫入EEPROM。
/*
* The user must implement the three extern-declared functions below
* in order for the compiler to be able to automatically write to the
* EEPROM memory when __eeprom variables are assigned to.
*/


/*
* Wait for the last data EEPROM operation to finish.Return 0 if the
* operation failed, otherwise non-zero.You may want to handle
* errors here, since the utility functions below simply ignore
* errors, aborting multi-write operations early.
*/
int __eeprom_wait_for_last_operation(void)
{
FLASH_Status_TypeDef status = FLASH_WaitForLastOperation(FLASH_MemType_Data);
return !!(status & (FLASH_Status_Successful_Operation));
}

/*
* Write one byte to the data EEPROM memory.
*/
void __eeprom_program_byte(unsigned char __near * dst, unsigned char v)
{
FLASH_ProgramByte((u32)dst, (u8)v);
}

/*
* Write one 4-byte long word to the data EEPROM memory.The address
* must be 4-byte aligned.
*/
void __eeprom_program_long(unsigned char __near * dst, unsigned long v)
{
FLASH_ProgramWord((u32)dst, (u32)v);
}

加入了這三個(gè)函數(shù)后,我們就可以很方便的使用_eeprom的定義變量了
void main(void)
{
uint8_t temp1=a;
//temp1=2;
//temp2=0;
/* Initialize I/Os in Output Mode */
GPIO_Init(LED3_PORT,LED3_PIN,GPIO_Mode_Out_PP_Low_Fast);
//輸出低電平-高速10M
GPIO_Init(LED4_PORT,LED4_PIN,GPIO_Mode_Out_PP_Low_Fast);
//輸出低電平-高速10M
FLASH_Unlock(FLASH_MemType_Data);
FLASH_ProgramByte(0x1001, temp1);//eeprom memory: address is 0x1001 =temp1
FLASH_WaitForLastOperation(FLASH_MemType_Data);
//temp2=FLASH_ReadByte(0x1000);
if(num==a)
GPIO_SetBits(LED3_PORT,LED3_PIN);
while(1);

}


評(píng)論


技術(shù)專區(qū)

關(guān)閉