新聞中心

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

        STM32下串口的使用

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

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

        本文引用地址:http://www.104case.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串

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 潞城市| 黎城县| 乌什县| 鱼台县| 南陵县| 湄潭县| 铜鼓县| 昆明市| 宜春市| 古田县| 青神县| 祁连县| 修文县| 高要市| 军事| 南投县| 维西| 青龙| 斗六市| 麻栗坡县| 财经| 个旧市| 沙洋县| 开封市| 呼伦贝尔市| 南通市| 嘉黎县| 常熟市| 南宁市| 本溪市| 云安县| 岫岩| 柘城县| 西平县| 噶尔县| 阜康市| 南安市| 万宁市| 威远县| 金塔县| 肃北|