<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          STM32串口通訊 UART

          作者: 時間:2016-12-02 來源:網(wǎng)絡(luò) 收藏
          三種方式:查詢,中斷,DMA

          通用同步異步收發(fā)器(USART)提供了一種靈活的方法來與使用工業(yè)標(biāo)準(zhǔn)NR 異步串行數(shù)據(jù)格式的外部設(shè)備之間進(jìn)行全雙工數(shù)據(jù)交換。 USART利用分?jǐn)?shù)波特率發(fā)生器提供寬范圍的波特率選擇。
          它支持同步單向通信和半雙工單線通信。它也支持LIN(局部互連網(wǎng)),智能卡協(xié)議和IrDA(紅外數(shù)據(jù)組織)SIR ENDEC規(guī)范,以及調(diào)制解調(diào)器(CTS/RTS)操作。它還允許多處理器通信。用于多緩沖器配置的DMA方式,可以實現(xiàn)高速數(shù)據(jù)通信。

          本文引用地址:http://www.ex-cimer.com/article/201612/325022.htm

          主要特性:
          全雙工的,異步通信
          NR 標(biāo)準(zhǔn)格式
          分?jǐn)?shù)波特率發(fā)生器系統(tǒng)
          -發(fā)送和接收共用的可編程波特率,最高到4.5Mbits/s
          可編程數(shù)據(jù)字長度(8位或9位)
          可配置的停止位 -支持1或2個停止位
          LIN主發(fā)送同步斷開符的能力以及LIN從檢測斷開符的能力
          - 當(dāng)USART硬件配置成LIN時,生成13位斷開符;檢測10/11位斷開符
          發(fā)送方為同步傳輸提供時鐘
          IRDA SIR 編碼器解碼器
          - 在正常模式下支持3/16位的持續(xù)時間
          智能卡模擬功能
          - 智能卡接口支持ISO7816 -3標(biāo)準(zhǔn)里定義的異步協(xié)議智能卡
          - 智能卡用到的0.5和1.5個停止位
          單線半雙工通信
          使用DMA的可配置的多緩沖器通信
          - 在保留的SRAM里利用集中式DMA緩沖接收/發(fā)送字節(jié)
          單獨的發(fā)送器和接收器使能位
          檢測標(biāo)志
          - 接收緩沖器滿
          - 發(fā)送緩沖器空
          - 傳輸結(jié)束標(biāo)志
          校驗控制
          - 發(fā)送校驗位
          - 對接收數(shù)據(jù)進(jìn)行校驗
          四個錯誤檢測標(biāo)志
          - 溢出錯誤
          - 噪音錯誤
          - 幀錯誤
          - 校驗錯誤
          10個帶標(biāo)志的中斷源
          - CTS改變
          - LIN斷開符檢測
          - 發(fā)送數(shù)據(jù)寄存器
          - 發(fā)送完成
          - 接收數(shù)據(jù)寄存器
          - 檢測到總線為空
          - 溢出錯誤
          - 幀錯誤
          - 噪音錯誤
          - 校驗錯誤
          多處理器通信 - - 如果地址不匹配,則進(jìn)入靜默模式
          從靜默模式中喚醒(通過空閑總線檢測或地址標(biāo)志檢測)
          兩種喚醒接收器的方式
          - 地址位(MSB)
          - 空閑總線



          STM32的串口配置 也挺方便的

          首先是配置UART的GPIO口
          /*******************************************************************************
          * Name : UART1_GPIO_Configuration
          * Deion : Configures the uart1 GPIO ports.
          * Input : None
          * Output : None
          * Return : None
          *******************************************************************************/
          void UART1_GPIO_Configuration(void)
          {
          GPIO_InitTypeDef GPIO_InitStructure;
          // Configure USART1_Tx as alternate push-pull
          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
          GPIO_Init(GPIOA, &GPIO_InitStructure);

          // Configure USART1_Rx as input floating
          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
          GPIO_Init(GPIOA, &GPIO_InitStructure);
          }

          然后是配置串口參數(shù)


          /* 如果使用查詢的方式發(fā)送和接收數(shù)據(jù) 則不需要使用串口的中斷
          如果需要使用中斷的方式發(fā)送和接收數(shù)據(jù) 則需要使能串口中斷
          函數(shù)原形 void USART_ITConfig(USART_TypeDef* USARTx, u16 USART_IT, alState NewState)
          功能描述 使能或者失能指定的 USART 中斷

          USART_IT 描述
          USART_IT_PE 奇偶錯誤中斷
          USART_IT_TXE 發(fā)送中斷
          USART_IT_TC 傳輸完成中斷
          USART_IT_RXNE 接收中斷
          USART_IT_IDLE 空閑總線中斷
          USART_IT_LBD LIN中斷檢測中斷
          USART_IT_CTS CTS中斷
          USART_IT_ERR 錯誤中斷

          */


          /*******************************************************************************
          * Name : UART1_Configuration
          * Deion : Configures the uart1
          * Input : None
          * Output : None
          * Return : None
          *******************************************************************************/
          void UART1_Configuration(void)
          {

          USART_InitTypeDef USART_InitStructure;
          /* USART1 configured as follow:
          - BaudRate = 9600 baud
          - Word Length = 8 Bits
          - One Stop Bit
          - No parity
          - Hardware flow control disabled (RTS and CTS signals)
          - Receive and transmit enabled
          */
          USART_InitStructure.USART_BaudRate = 9600;
          USART_InitStructure.USART_WordLength = USART_WordLength_8b;
          USART_InitStructure.USART_StopBits = USART_StopBits_1;
          USART_InitStructure.USART_Parity = USART_Parity_No ;
          USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
          USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

          /* Configure the USART1*/
          USART_Init(USART1, &USART_InitStructure);

          /* Enable USART1 Receive and Transmit interrupts */
          USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);


          /* Enable the USART1 */
          USART_Cmd(USART1, ENABLE);
          }

          發(fā)送一個字符
          /*******************************************************************************
          * Name : Uart1_PutChar
          * Deion : printf a char to the uart.
          * Input : None
          * Output : None
          * Return : None
          *******************************************************************************/
          u8 Uart1_PutChar(u8 ch)
          {
          /* Write a character to the USART */
          USART_SendData(USART1, (u8) ch);
          while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
          {
          }
          return ch;
          }


          發(fā)送一個字符串
          /*******************************************************************************
          * Name : Uart1_PutString
          * Deion : print a string to the uart1
          * Input : buf為發(fā)送數(shù)據(jù)的地址 , len為發(fā)送字符的個數(shù)
          * Output : None
          * Return : None
          *******************************************************************************/
          void Uart1_PutString(u8* buf , u8 len)
          {
          for(u8 i=0;i{
          Uart1_PutChar(*buf++);
          }
          }

          如果UART使用中斷發(fā)送數(shù)據(jù) 則需要修改stm32f10x_it.c 中的串口中斷函數(shù) 并且需要修改void NVIC_Configuration(void)函數(shù)

          在中斷里面的處理 原則上是需要簡短和高效 下面的流程是 如果接收到255個字符或者接收到回車符 則關(guān)閉中斷 并且把標(biāo)志位UartHaveData 置1

          /*******************************************************************************
          * Name : USART1_IRQHandler
          * Deion : This handles USART1 global interrupt request.
          * Input : None
          * Output : None
          * Return : None
          *******************************************************************************/
          void USART1_IRQHandler(void)
          {
          if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
          {
          /* Read one byte from the receive data register */
          RxBuffer[ RxCounter ] = USART_ReceiveData(USART1);
          if( RxCounter == 0xfe || r == RxBuffer[ RxCounter ] )
          {
          /* Disable the USART1 Receive interrupt */
          USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
          RxBuffer[ RxCounter ] = 看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();