STM32读写Flash
byte:字节(8位)
halfword:半字(16位)
word:字(32位)
//写Flash时,将数据转换为u32类型的指针,再对指针取值。
FLASH_Status FLASH_ProgramWord(uint32_t Address, uint32_t Data);
FLASH_Status FLASH_ProgramHalfWord(uint32_t Address, uint16_t Data);
FLASH_Status FLASH_ProgramOptionByteData(uint32_t Address, uint8_t Data);
//例如:
FLASH_ProgramOptionByteData(0x0800f800, *((volatile uint8_t *)&writedata));
FLASH_ProgramHalfWord(0x0800f800, *((volatile uint16_t *)&writedata));
FLASH_ProgramWord(0x0800f800, *((volatile uint32_t *)&writedata));
//读Flash时,将地址中的数据强转成需要的类型数据。
readdata = *(volatile float *)0x0800f800;
readdata = *(volatile int *)0x0800f800;
readdata = *(volatile u16 *)0x0800f800;
//向flash写入1字节数据(写入1字节)
u8 Data;
*(__IO uint16_t*)Address = Data;
//向flash写入2字节数据(写入半字)
u16 Data;
*(__IO uint16_t*)Address = Data;
//向flash写入4字节数据(写入字)
//其实是分两次进行写入,先写2字节,再写2字节。
u32 Data;
*(__IO uint16_t*)Address = (uint16_t)Data;
Address += 2;
*(__IO uint16_t*)Address = Data >> 16;
数据的强制类型转换
u32 data32;
u16 data1;
u16 data2;
//获取的是data32的低16位
data1 = (u16)data32;
//获取的是data32的高16位
data2 = (u16)(data32 >> 16);