STM8L探索套件學(xué)習(xí)筆記-EEPROM(十一)
由于默認是鎖定的,所以對FLASH和EEPROM編程首先要解鎖,解鎖的方式是寫如寄存器特定值,0X56 0XAE。在IAR編譯器當(dāng)中,__eeprom用于EEPROM存儲空間,控制數(shù)據(jù)存放,控制指針類型和存放。@用于變量的絕對地址定位。也可以用#pragma location命令。
EEPROM區(qū)域數(shù)據(jù)存儲:
用關(guān)鍵字__eeprom控制來存放,__eeprom關(guān)鍵字寫在數(shù)據(jù)類型前后效果一樣。
__eeprom unsigned char a;//定義一個變量存放在EEPROM空間
unsigned char __eeprom a;//效果同上
__eeprom unsigned char p[];//定義一個數(shù)組存放在EEPROM空間
對于EEPROM空間的變量操作同SRAM數(shù)據(jù)空間的操作方法一樣,編譯器會自動
調(diào)用__EEPUT(ADR,VAL),__EEGET(VAR, ADR)宏函數(shù)來對EEPROM變量的
操作。
EEPROM空間絕對地址定位:
__eeprom unsigned char a @ 0x8;//定義一個變量存放在EEPROM空間
0X08單元
__eeprom unsigned char p[] @ 0x22//定義一個數(shù)組存放在EEPROM空間,
開始地址為0X22單元
__eeprom unsigned char a @ 0x08=9;//定義一個常數(shù)存放在EEPROM空
間0X08單元
__eeprom unsigned char p[] @0x22={1,2,3,4,5,6,7,8};
//定義一個組常數(shù)存放在EEPROM空間開始地址為0X22單元
由于常數(shù)在EEPROM空間的地址是已經(jīng)分配的,讀取EEPROM空間值可以用
變量和地址。
需要用戶外加三個函數(shù)才能使得編譯器使用_eeprom變量自動寫入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);
}
加入了這三個函數(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);
}
評論