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

          新聞中心

          EEPW首頁 > 嵌入式系統 > 設計應用 > STM32下串口的使用

          STM32下串口的使用

          作者: 時間:2016-11-19 來源:網絡 收藏

          STM32下關于串口的固件庫寫得相當好了,以下只是本文對串口庫的一點點封裝

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

          /*HKY_uart.h*/#ifndef _HKY_UART_H_#define _HKY_UART_H_#include "stm32f10x_lib.h"http://#include "platform_config.h"#define  GPIO_RTSPin              GPIO_Pin_1#define  GPIO_CTSPin              GPIO_Pin_0#define  GPIO_TxPin               GPIO_Pin_2#define  GPIO_RxPin               GPIO_Pin_3void USART2_Configuration(void);int USART_sendBuf(USART_TypeDef* USARTx, u8 *buf, u32 len);int USART_recvBuf(USART_TypeDef* USARTx, u8 *buf, u32 len);#endif //_HKY_UART_H_

          /*HKY_uart.c*/#include "HKY_uart.h"/* --------------------------------------------------------------------------*// * @Brief:  USART2_Configuration *//* --------------------------------------------------------------------------*/void USART2_Configuration(void){USART_InitTypeDef USART_InitStructure;GPIO_InitTypeDef GPIO_InitStructure;//IO port----------------------------------------------------------------------/* Configure USART2 RTS and USART2 Tx as alternate function push-pull */GPIO_InitStructure.GPIO_Pin = GPIO_TxPin;//GPIO_RTSPin | GPIO_TxPin;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_Init(GPIOA, &GPIO_InitStructure);/* Configure USART2 CTS and USART2 Rx as input floating */GPIO_InitStructure.GPIO_Pin = GPIO_RxPin;//GPIO_CTSPin | GPIO_RxPin;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;GPIO_Init(GPIOA, &GPIO_InitStructure);/* USART2 configuration ------------------------------------------------------*//* USART2 configured as follow:- BaudRate = 115200 baud  - Word Length = 8 Bits- One Stop Bit- No parity- Hardware flow control enabled (RTS and CTS signals)- Receive and transmit enabled*/USART_InitStructure.USART_BaudRate = 115200;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_HardwareFlowControl_RTS_CTS;USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;USART_Init(USART2, &USART_InitStructure);/* Enable the USART2 */USART_Cmd(USART2, ENABLE);USART_ClearFlag(USART2, USART_FLAG_TC); // clear flag}/* --------------------------------------------------------------------------*// * @Brief:  USART_sendBuf * * @Param: USARTx, which uart port* @Param: buf, send buffer* @Param: len, buffer length* * @Returns:   send bytes count*//* --------------------------------------------------------------------------*/int USART_sendBuf(USART_TypeDef* USARTx, u8 *buf, u32 len){u32 busy_cnt = 0;u32 i = 0;while (i < len){USART_SendData(USARTx, buf[i++]);busy_cnt = 0;while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET){busy_cnt++; if (busy_cnt > 0xffff)return i; //send error, return sended bytes count}}return len; }/* --------------------------------------------------------------------------*// * @Brief:  USART_recvBuf * * @Param: USARTx, which uart port* @Param: buf, receive buffer* @Param: len, buffer length* * @Returns:  received bytes count*//* --------------------------------------------------------------------------*/int USART_recvBuf(USART_TypeDef* USARTx, u8 *buf, u32 len){u32 busy_cnt = 0;u32 i = 0;while (i < len){if(USART_GetFlagStatus(USARTx, USART_FLAG_RXNE) != RESET){buf[i] = USART_ReceiveData(USARTx) & 0xff;i++;}else{busy_cnt++;}if (busy_cnt > 0xffff)return i; //receive error, return received bytes count}return len;}


          關鍵詞: STM32串

          評論


          技術專區(qū)

          關閉
          看屁屁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); })();