STM32下关于串口的固件库写得相当好了,以下只是本文对串口库的一点点封装:
/*HKY_uart.h*/ #ifndef _HKY_UART_H_ #define _HKY_UART_H_ #include "stm32f10x_lib.h" //#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_3 void 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; }
上一篇:基于51系列单片机控制步进电机调速闭环系统设计
下一篇:基于T89C51CC0X系列的程序下载器与通讯调试器设计
推荐阅读最新更新时间:2024-03-16 14:56