久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1900|回復: 3
打印 上一主題 下一主題
收起左側(cè)

stm32f103c8用串口2接受字符串,串口1發(fā)出對應數(shù)組的問題

[復制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:850421 發(fā)表于 2021-11-22 23:36 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
以下是我自己寫的,感覺串口配置出問題了,串口1發(fā)送沒問題;但是串口二的接收字符串后馬上串口1發(fā)送數(shù)組不行。有沒有大佬幫忙修改一下
  1. #include "usart.h"                 
  2. #include "led.h"
  3. #include "SysTick.h"
  4. #include "string.h"
  5. #include "DFPlayer.h"
  6. /*******************************************************************************
  7. * 函 數(shù) 名         : USART1_Init
  8. * 函數(shù)功能                   : USART1初始化函數(shù)
  9. * 輸    入         : bound:波特率
  10. * 輸    出         : 無
  11. *******************************************************************************/
  12. void USART1_Init(u32 bound)
  13. {
  14.    //GPIO端口設(shè)置
  15.         GPIO_InitTypeDef GPIO_InitStructure;
  16.         USART_InitTypeDef USART_InitStructure;
  17.         NVIC_InitTypeDef NVIC_InitStructure;
  18.       
  19.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
  20.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);

  21.       
  22.         /*  配置GPIO的模式和IO口 */
  23.         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX                           //串口輸出PA9
  24.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  25.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;            //復用推挽輸出
  26.         GPIO_Init(GPIOA,&GPIO_InitStructure);  /* 初始化串口輸入IO */
  27.         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX                         //串口輸入PA10
  28.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;                  //模擬輸入
  29.         GPIO_Init(GPIOA,&GPIO_InitStructure); /* 初始化GPIO */
  30.       


  31.    //USART1 初始化設(shè)置
  32.         USART_InitStructure.USART_BaudRate = bound;//波特率設(shè)置
  33.         USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字長為8位數(shù)據(jù)格式
  34.         USART_InitStructure.USART_StopBits = USART_StopBits_1;//一個停止位
  35.         USART_InitStructure.USART_Parity = USART_Parity_No;//無奇偶校驗位
  36.         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無硬件數(shù)據(jù)流控制
  37.         USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;        //收發(fā)模式
  38.         USART_Init(USART1, &USART_InitStructure); //初始化串口1
  39.       
  40.         USART_Cmd(USART1, ENABLE);  //使能串口1
  41.       
  42.         USART_ClearFlag(USART1, USART_FLAG_TC);
  43.                
  44.         USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//開啟相關(guān)中斷
  45.       
  46.         while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);         


  47.         //Usart1 NVIC 配置
  48.         NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//串口1中斷通道
  49.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//搶占優(yōu)先級3
  50.         NVIC_InitStructure.NVIC_IRQChannelSubPriority =3;                //子優(yōu)先級3
  51.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                        //IRQ通道使能
  52.         NVIC_Init(&NVIC_InitStructure);        //根據(jù)指定的參數(shù)初始化VIC寄存器、      
  53. }








  54. /*******************************************************************************
  55. * 函 數(shù) 名         : USART1_Send_Data
  56. * 函數(shù)功能                   : USART1發(fā)送函數(shù)
  57. * 輸    入         : buf
  58. * 輸    入         : len
  59. * 輸    出         : 無
  60. *******************************************************************************/
  61. void USART1_Send_Data(u8 *buf,uint16_t len)
  62. {
  63.                 u8 t=0;
  64.           for(t=0;t<len;t++)                //循環(huán)發(fā)送數(shù)據(jù)
  65.     {   
  66.                                 USART_SendData(USART1,buf[t] );  
  67.                                 while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);         
  68.     }               
  69. }



  70. /*******************************************************************************  
  71. * 函 數(shù) 名         : uart2_init  
  72. * 函數(shù)功能         : IO端口及串口2,時鐘初始化函數(shù)     A2,A3   
  73. * 輸    入         : 無  
  74. * 輸    出         : 無  
  75. *******************************************************************************/   
  76. void USART2_Init(u32 bt)   
  77. {   
  78.      USART_InitTypeDef USART_InitStructure;   
  79.   NVIC_InitTypeDef NVIC_InitStructure;     
  80.     GPIO_InitTypeDef GPIO_InitStructure;    //聲明一個結(jié)構(gòu)體變量,用來初始化GPIO   
  81.    //使能串口的RCC時鐘   
  82.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //使能UART3所在GPIOB的時鐘   
  83.    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);   


  84.    //串口使用的GPIO口配置   
  85.    // Configure USART2 Rx (PB.11) as input floating      
  86.    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;   
  87.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;   
  88.    GPIO_Init(GPIOA, &GPIO_InitStructure);   


  89.    // Configure USART2 Tx (PB.10) as alternate function push-pull   
  90.    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;   
  91.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   
  92.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;   
  93.    GPIO_Init(GPIOA, &GPIO_InitStructure);   


  94.    //配置串口   
  95.    USART_InitStructure.USART_BaudRate = bt;   
  96.    USART_InitStructure.USART_WordLength = USART_WordLength_8b;   
  97.    USART_InitStructure.USART_StopBits = USART_StopBits_1;   
  98.    USART_InitStructure.USART_Parity = USART_Parity_No;   
  99.    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;   
  100.    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;   




  101.    // Configure USART   
  102.    USART_Init(USART2, &USART_InitStructure);//配置串口


  103.   // Enable USART1 Receive interrupts 使能串口接收中斷   
  104.    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);   
  105.    //串口發(fā)送中斷在發(fā)送數(shù)據(jù)時開啟   
  106.    //USART_ITConfig(USART2, USART_IT_TXE, ENABLE);   


  107.    // Enable the USART3     
  108.    USART_Cmd(USART2, ENABLE);//使能串口   


  109.    //串口中斷配置   
  110.    //Configure the NVIC Preemption Priority Bits      
  111.    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);   


  112.    // Enable the USART Interrupt     
  113.    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;   
  114.    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;   
  115.    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;   
  116.    NVIC_Init(&NVIC_InitStructure);   
  117. }   


  118. char USART_ReceiveString[50];                                                                                                        //接收PC端發(fā)送過來的字符
  119. int Receive_Flag = 0;                                                                                                                        //接收消息標志位
  120. int Receive_sum = 0;                                                                                                                        //數(shù)組下標


  121. void USART2_IRQHandler(void)   //中斷
  122. {
  123.         if(USART_GetITStatus(USART2,USART_IT_RXNE) == 1)                                                        //USART_FLAG_RXNE判斷數(shù)據(jù),== 1則有數(shù)據(jù)
  124.         {
  125.                 if(Receive_sum > 49)                                                                                                        //數(shù)組能存放50個字節(jié)的數(shù)據(jù)                              
  126.                 {
  127.                         USART_ReceiveString[49] = '\0';                                                                                //數(shù)據(jù)字節(jié)超過50位時,將最后一位設(shè)置為\0      
  128.                         Receive_Flag = 1;                                                                                                        //接收標志位置1,停止接收數(shù)據(jù)
  129.                         Receive_sum = 0;                                                                                                        //數(shù)組下標置0
  130.                 }
  131.                
  132.                 if(Receive_Flag == 0)                                                                                                        //接收標志位等于0,開始接收數(shù)據(jù)
  133.                 {
  134.                         USART_ReceiveString[Receive_sum] = USART_ReceiveData(USART2);                //通過USART2串口接收字符
  135.                         Receive_sum++;                                                                                                                //數(shù)組下標++
  136.                 }
  137.                
  138.                 if(Receive_sum >= 2)                                                                                                        //數(shù)組下標大于2
  139.                 {
  140.                         if(USART_ReceiveString[Receive_sum-2] == '\r' && USART_ReceiveString[Receive_sum-1] == '\n' )
  141.                         {
  142.                                 USART_ReceiveString[Receive_sum-1] = '\0';                                               
  143.                                 USART_ReceiveString[Receive_sum-2] = '\0';
  144.                                 Receive_Flag = 1;                                                                                                //接收標志位置1,停止接收數(shù)據(jù)
  145.                                 Receive_sum = 0;                                                                                                //數(shù)組下標置0                              
  146.                                 if(strcmp(USART_ReceiveString,"batteriesHarmful") == 0)
  147.                                 {
  148.           led1=!led1;
  149.                                 }
  150.                                 if(strcmp(USART_ReceiveString,"cansRecyclable") == 0)
  151.                                 {
  152.                                         Uart_DFPlayer(0x03 , 0x02) ;
  153.                                 }
  154.                                 if(strcmp(USART_ReceiveString,"cartonRecyclable") == 0)
  155.                                 {
  156.                                         Uart_DFPlayer(0x03 , 0x03) ;
  157.                                 }
  158.                                 if(strcmp(USART_ReceiveString,"cigaretteDry") == 0)
  159.                                 {
  160.                                         Uart_DFPlayer(0x03 , 0x04) ;
  161.                                 }
  162.                                 if(strcmp(USART_ReceiveString,"tissueDry") == 0)
  163.                                 {
  164.                                         Uart_DFPlayer(0x03 , 0x05) ;
  165.                                 }
  166.                               
  167.                                 Receive_Flag = 0;                              
  168.                         }               
  169.                 }
  170.                 USART_ClearITPendingBit(USART2,USART_IT_RXNE);                                                        //接收后先清空標志位
  171.         }
  172.       
  173. }
復制代碼



分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

沙發(fā)
ID:301191 發(fā)表于 2021-11-24 03:32 | 只看該作者
頂一下
回復

使用道具 舉報

板凳
ID:752974 發(fā)表于 2021-11-24 17:28 | 只看該作者
發(fā)送數(shù)據(jù)需要時間,收到一個字節(jié)數(shù)據(jù)立即用另一個口發(fā)送,都是用中斷,或都是用查詢,就易出問題,主要是發(fā)送占用CPU時,收到一個字節(jié)數(shù)據(jù)沒有及時取走,就會被下一個接收的數(shù)據(jù)覆蓋,或接收口占用CPU,同樣會導致發(fā)送不能正常工作。
回復

使用道具 舉報

地板
ID:311903 發(fā)表于 2021-11-25 13:59 | 只看該作者
你這樣處理不好,對于接受,開一個乒乓緩存吧,fifo1接受一幀后,切換到fifo2接受,并把fifo1的數(shù)據(jù)發(fā)出去
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

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

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

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 亚洲国产高清在线 | 免费成人高清在线视频 | 男人天堂99 | 欧美涩| 亚洲一区二区精品视频 | 成年人免费网站 | 欧美日韩精品一区 | 久久亚洲欧美日韩精品专区 | 91亚洲精品在线 | 国产在线观看一区二区 | 国产精品成人一区二区 | 日本一区二区三区在线观看 | 国产一区二区久久 | 精品视频999 | 干一干操一操 | 国产精品一区二区视频 | 久草视| 亚洲人成在线播放 | 99久久婷婷国产综合精品电影 | 最新毛片网站 | 日韩欧美一区二区三区免费观看 | 91观看 | 日韩中文字幕在线观看视频 | 麻豆国产精品777777在线 | 暴草美女 | yeyeav| 国产真实精品久久二三区 | 亚洲一区二区三区观看 | 日韩在线免费看 | 精品久久一区二区 | 日本久久精| 亚洲欧美中文日韩在线v日本 | 黄色毛片网站在线观看 | 色av一区二区三区 | 国产精品一区二区无线 | 欧美日韩国产高清 | 国产一区精品 | 国产精品久久久爽爽爽麻豆色哟哟 | 精品国产不卡一区二区三区 | 久久亚洲国产精品 | 亚洲精品一区在线 |