EEPROM写函数:
void Function_GetEepromData(){
Function_EepromRead(4, BUFF);
OLED_ShowChar(0, 0, BUFF[0] + '0');
OLED_ShowChar(0, 2, BUFF[1] + '0');
BUFF[0] ++;
BUFF[1] ++;
HAL_FLASHEx_DATAEEPROM_Unlock();
HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_WORD, 4 + DATA_EEPROM_BASE, *BUFF);
HAL_FLASHEx_DATAEEPROM_Lock();
}
流程很简单其实:就是一个开锁写或者读再上锁的过程
HAL_FLASHEx_DATAEEPROM_Unlock();
从函数名可以看出叫FLASH扩展函数,EPPROM数据区开锁
HAL_FLASHEx_DATAEEPROM_Program
program指的是烧录,意思是将数据烧录到EEPROM数据区域
FLASH_TYPEPROGRAMDATA_WORD
按整个字烧录
烧录函数一次只能写一个数据进去
读取函数:
void Function_EepromRead(uint32_t address, uint32_t* buff){
uint32_t* TrueValue = (uint32_t* )(DATA_EEPROM_BASE + address);
HAL_FLASHEx_DATAEEPROM_Unlock();
I = 2;
while(I --){
*buff ++ = *TrueValue ++;
}
HAL_FLASHEx_DATAEEPROM_Lock();
}
DATA_EEPROM_BASE就是获取EEPROM数据区地址,然后开锁,读取关锁
对于*buff ++ = *TrueValue ++
这部分代码我又额外分析了一下
#include <stdio.h>
#include <stdint.h>
uint32_t DATA[5] = {1, 2, 3};
uint32_t* ADDRESS = DATA;
int main(){
printf("ADDRESS地址:%p\n", ADDRESS);
printf("*ADDRESS的值:%d\n", *ADDRESS);
uint32_t temp = *ADDRESS ++;
printf("temp的值:%d *ADDRESS操作后的值:%d\n", temp, *ADDRESS);
printf("ADDRESS操作后的值:%p", ADDRESS);
}
通过输出不难看出++操作的优先级是比*的优先级要低的,且uint32_t每个占空间4字结,这也是为什么address要是4的倍数否者就读不出来了
有意思的是:
#include <stdio.h>
#include <stdint.h>
char a = 1;
uint32_t b = 3;
char* q = &a;
uint32_t* d = &b;
int main(){
printf("q: %p, d: %p\n", q, d);
q = q + 1;
d = d + 1;
printf("(q + 1) : %p, (d + 1) : %p\n", q, d);
}
对于char类型占1字节地址+ 1那么地址真是增加1,而对于uint32_t即32位的来说+1操作是地址向后挪4位刚好一个uint32_t的距离