STM32F042G HAL库读取96bit UID(唯一ID) VCP输出

代码记录:

/* USER CODE BEGIN WHILE */
char *buff = "\n"; //换行符
char *buff1 = "UID = "; //特定字符

char UID0[8] = ""; //UID缓存
char UID1[8] = "";
char UID2[8] = "";

while (1)
{
 if (HAL_GPIO_ReadPin(KEY_GPIO_Port, KEY_Pin) == GPIO_PIN_SET) //检测按键是否按下
 {
 HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET); //亮灯提示

 sprintf(UID0, "%08X", HAL_GetUIDw0()); //获取ID转成16进制存到UID缓存
 sprintf(UID1, "%08X", HAL_GetUIDw1());
 sprintf(UID2, "%08X", HAL_GetUIDw2());

 while (CDC_Transmit_FS((uint8_t*)buff1, 6)); //输出特定字符
 while (CDC_Transmit_FS((uint8_t*)UID0, sizeof(UID0))); //输出UID
 while (CDC_Transmit_FS((uint8_t*)UID1, sizeof(UID1)));
 while (CDC_Transmit_FS((uint8_t*)UID2, sizeof(UID2)));
 while (CDC_Transmit_FS((uint8_t*)buff, 1)); //换行
 while (HAL_GPIO_ReadPin(KEY_GPIO_Port, KEY_Pin) == GPIO_PIN_SET); //等待松开按键
 }
 HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET); //灭灯
 /* USER CODE END WHILE */
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注