欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

標題: stm32單片機串口發送16進制數的方法分享含程序 [打印本頁]

作者: jinhua1916    時間: 2025-1-18 08:15
標題: stm32單片機串口發送16進制數的方法分享含程序
stm32單片機串口發送16進制數的方法分享含程序;按下按鈕 key0后程序輸出16進制數組。

  1. #include "led.h"
  2. #include "delay.h"
  3. #include "key.h"
  4. #include "sys.h"
  5. #include "usart.h"
  6. #include <stdio.h>


  7. void UART_Send_Hex(uint8_t hex)
  8.                         {
  9.                         int i;
  10.                         char buffer[2];
  11.                         sprintf(buffer,"%02X",hex);
  12.                                 for( i=0;i<2;i++)
  13.                                 {i++;
  14.                 USART_SendData(USART1,buffer[i]);
  15.                 while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==0);
  16.                                 }
  17.                                 
  18.                         }

  19. int main(void)
  20. {        
  21.          
  22.          uint8_t DATA[4]={0xFE,0x00,0x01,0XFF};
  23.          uint8_t hex_data=0XFE;
  24.          uint8_t hex_data1=0x00;
  25.          uint8_t hex_data2=0x01;
  26.          uint8_t hex_data3=0xFF;
  27.          
  28.          
  29.          u8 t=0;
  30.          u16 times=0;
  31.         LED_Init();
  32.         
  33.         delay_init();                     //延時函數初始化         
  34.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //設置NVIC中斷分組2:2位搶占優先級,2位響應優先級
  35.         uart_init(9600);         //串口初始化為115200
  36.          //LED_Init();                             //LED端口初始化
  37.           KEY_Init();          //初始化與按鍵連接的硬件接口

  38.         LED0=0;
  39.         
  40.         
  41.          while(1)
  42.         {
  43.                
  44.                
  45.                 LED0=0;
  46.                
  47.                 delay_us(1000);
  48.                 LED0=0;
  49.                 //KEY_Scan(0);
  50.         //        UART_Send_Hex(hex_data);               
  51.                
  52.                 t=KEY_Scan(0);                //得到鍵值
  53.                 switch(t)
  54.                         
  55.                 {               
  56. case KEY0_PRES:
  57.         

  58. {
  59.          int i;
  60.          delay_us(1000);
  61.          for(i=0;i<4;i++)
  62.          {
  63.           USART1->DR=DATA[i];
  64.    while((USART1->SR&0X40)==0);
  65.                
  66.                 LED0=1;
  67.         }
  68. }

  69.                
  70. }

  71. }
復制代碼


原理圖: 無
仿真: 無
代碼: 代碼.7z (183.89 KB, 下載次數: 0)

作者: jinhua1916    時間: 2025-1-18 08:18
串口波特率9600,8位,無校驗。程序里面可根據自己需要修改。按鈕使用的是PA8。
作者: jinhua1916    時間: 2025-1-24 18:41
發現一個問題;發送數組的時候第一個數據會丟失;在發送數據前需要先檢查發送是否為空;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;
        }
}




歡迎光臨 (http://www.raoushi.com/bbs/) Powered by Discuz! X3.1