stm32單片機串口發送16進制數的方法分享含程序;按下按鈕 key0后程序輸出16進制數組。
- #include "led.h"
- #include "delay.h"
- #include "key.h"
- #include "sys.h"
- #include "usart.h"
- #include <stdio.h>
-
- void UART_Send_Hex(uint8_t hex)
- {
- int i;
- char buffer[2];
- sprintf(buffer,"%02X",hex);
- for( i=0;i<2;i++)
- {i++;
- USART_SendData(USART1,buffer[i]);
- while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==0);
- }
-
- }
- int main(void)
- {
-
- uint8_t DATA[4]={0xFE,0x00,0x01,0XFF};
- uint8_t hex_data=0XFE;
- uint8_t hex_data1=0x00;
- uint8_t hex_data2=0x01;
- uint8_t hex_data3=0xFF;
-
-
- u8 t=0;
- u16 times=0;
- LED_Init();
-
- delay_init(); //延時函數初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設置NVIC中斷分組2:2位搶占優先級,2位響應優先級
- uart_init(9600); //串口初始化為115200
- //LED_Init(); //LED端口初始化
- KEY_Init(); //初始化與按鍵連接的硬件接口
- LED0=0;
-
-
- while(1)
- {
-
-
- LED0=0;
-
- delay_us(1000);
- LED0=0;
- //KEY_Scan(0);
- // UART_Send_Hex(hex_data);
-
- t=KEY_Scan(0); //得到鍵值
- switch(t)
-
- {
- case KEY0_PRES:
-
- {
- int i;
- delay_us(1000);
- for(i=0;i<4;i++)
- {
- USART1->DR=DATA[i];
- while((USART1->SR&0X40)==0);
-
- LED0=1;
- }
- }
-
- }
- }
復制代碼
原理圖: 無
仿真: 無
代碼:
代碼.7z
(183.89 KB, 下載次數: 0)
2025-1-18 14:45 上傳
點擊文件名下載附件
stm32單片機串口發送16進制數的方 閱讀權限: 50 下載積分: 黑幣 -5
|