在不同的库中方法不同,如果是采样HAL库,则十分简单
/@@**
* @brief Return the unique device identifier (UID based on 96 bits)
* @param UID: pointer to 3 words array.
* @retval Device identifier
*/
void HAL_GetUID(uint32_t *UID) {
UID[0] = (uint32_t)(READ_REG(*((uint32_t *)UID_base)));
UID[1] = (uint32_t)(READ_REG(*((uint32_t *)(UID_base + 4U))));
UID[2] = (uint32_t)(READ_REG(*((uint32_t *)(UID_base + 8U))));
}
// 其中UID_base是由宏定义 设置的,不同的芯片不同,是定义在 stm32XXXXXX.h文件中的,其中XXXXXX就是具体芯片型号啦,一般项目中都会include的。
//比如下面就是对于SMT32F103C8T6为例的
#define UID_base 0x1FFFF7E8U /@@*!< Unique device ID register base address */
uint32_t uid[3];
HAL_GetUID(uid);
就这样就把UID读取到 uid数组中了。
如果采用标准库,就是去读取寄存器值,还是利用那个地址
uint32_t *id = (uint32_t *)0x1FFFF7E8; // 这个地址查手册获取这里也是SMT32F103C8T6为例
for(int8_t i = 0; i < 3; i++) {
printf("%x \r\n" id[i]);
}