找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 2295|回復(fù): 0
收起左側(cè)

STM32單片機(jī)串口接收一幀數(shù)據(jù)

[復(fù)制鏈接]
ID:302293 發(fā)表于 2021-6-24 17:08 | 顯示全部樓層 |閱讀模式
串口接收數(shù)據(jù)幀,可以做通信協(xié)議
  1. #include "sys.h"
  2. #include "usart.h"
  3. #define  len        5 //數(shù)組長(zhǎng)度
  4. #define a PCout(13)
  5. u8 rx_buff[8];//接收緩存
  6. u8 rx_done =0;//接收完成標(biāo)志
  7. u8 rx_cnt=0;//接收數(shù)據(jù)長(zhǎng)度

  8. u8 rx3_cont=0;
  9. u8 rx3_done=0;
  10. u8 rx_data[7];//復(fù)位與步進(jìn)接收緩沖區(qū)數(shù)據(jù)

  11. u8 stop_data[7];
  12. u8 num=0;     // 字節(jié)數(shù)
  13. u8 buf[5];        //接收緩沖,最大9個(gè)字節(jié).
  14. void  MAX485_Config(void)
  15. {

  16.   GPIO_InitTypeDef GPIO_InitStructure;
  17.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
  18. //        RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
  19.   GPIO_InitStructure.GPIO_Pin =GPIO_Pin_13;
  20.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  21.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  22.   GPIO_Init(GPIOC, &GPIO_InitStructure);
  23. a=1;
  24. }        


  25. void USART1_Config(void)
  26. {
  27. GPIO_InitTypeDef  GPIO_InitStructure;
  28. USART_InitTypeDef USART_InitStructure;
  29. NVIC_InitTypeDef NVIC_InitStructure;
  30. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//使能串口GPIO的時(shí)鐘
  31. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//使能串口外設(shè)的時(shí)鐘
  32. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//將USART1 Tx的GPIO配置為推挽復(fù)用模式
  33. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
  34. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  35. GPIO_Init(GPIOA,&GPIO_InitStructure);
  36. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//將USART1 Rx的GPIO配置為浮空輸入模式
  37. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
  38. GPIO_Init(GPIOA,&GPIO_InitStructure);
  39. USART_InitStructure.USART_BaudRate=115200;//配置波特率115200
  40. USART_InitStructure.USART_WordLength=USART_WordLength_8b;//配置數(shù)據(jù)字長(zhǎng)8bit
  41. USART_InitStructure.USART_StopBits=USART_StopBits_1;//配置停止位1bit
  42. USART_InitStructure.USART_Parity=USART_Parity_No;//校驗(yàn)位無(wú)
  43. USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;//硬件流控制無(wú)
  44. USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;//同時(shí)收發(fā)模式
  45. USART_Init(USART1,&USART_InitStructure);//完成串口的初始化配置

  46. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//嵌套向量中斷控制器組選擇
  47. NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;//配置USART為中斷源
  48. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;//搶斷優(yōu)先級(jí)
  49. NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;//子優(yōu)先級(jí)
  50. NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//使能中斷
  51. NVIC_Init(&NVIC_InitStructure);//初始化配置NVIC
  52. USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//使能串口接收中斷
  53. //USART_ITConfig(USART1,USART_IT_IDLE,ENABLE);//空閑中斷使能
  54. USART_Cmd(USART1,ENABLE);
  55. }

  56.         
  57.         
  58. void USART1_IRQHandler(void)
  59. {

  60.    u8 res;
  61. if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)//接收到一個(gè)字節(jié),進(jìn)入一次接收中斷
  62. {
  63. USART_ClearITPendingBit(USART1,USART_IT_RXNE);//清除接收中斷標(biāo)志
  64.          
  65.     res=USART_ReceiveData(USART1);//將接收的數(shù)據(jù)存入rx_buff中
  66. if(num>0)
  67.                                         {
  68.                                                 buf[num]=res;
  69.                                                 num++;
  70.                                         }
  71.                                         else if (res==0xc1)                // 包頭
  72.                                         {
  73.                                                 buf[0]=0xc1;//前面寫(xiě)進(jìn),后面才能讀
  74.                                                  num=1;
  75.                                         }
  76.                                         if(num>=len)               
  77.                                         {
  78.                                                 num=0;
  79.                                                 if(buf[(len-1)]==0x0d)         // 判斷包尾
  80.                                                 {
  81.                if(buf[0]==0xc1) a=0;
  82.                else
  83.                 a=1;                                                                 

  84.      if(buf[1]==5)
  85.                  {
  86.                    a=1;
  87.                  }


  88. }
  89. }
  90. }
  91. }

復(fù)制代碼


評(píng)分

參與人數(shù) 1黑幣 +20 收起 理由
admin + 20 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表