适用于新手学习串口通信,运行时用的是串口调试助手“ v4.11绿色版”来接收数据。。。
- #include "bsp_usart.h"
- /**
- * @brief USART GPIO 配置,工作参数配置
- * @param 无
- * @retval 无
- */
- void USART_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- // 打开串口GPIO的时钟
- DEBUG_USART_GPIO_APBxClkCmd(DEBUG_USART_GPIO_CLK, ENABLE);
-
- // 打开串口外设的时钟
- DEBUG_USART_APBxClkCmd(DEBUG_USART_CLK, ENABLE);
- // 将USART Tx的GPIO配置为推挽复用模式
- GPIO_InitStructure.GPIO_Pin = DEBUG_USART_TX_GPIO_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(DEBUG_USART_TX_GPIO_PORT, &GPIO_InitStructure);
- // 将USART Rx的GPIO配置为浮空输入模式
- GPIO_InitStructure.GPIO_Pin = DEBUG_USART_RX_GPIO_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(DEBUG_USART_RX_GPIO_PORT, &GPIO_InitStructure);
-
- // 配置串口的工作参数
- // 配置波特率
- USART_InitStructure.USART_BaudRate = DEBUG_USART_BAUDRATE;
- // 配置 针数据字长
- 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;
- // 完成串口的初始化配置
- USART_Init(DEBUG_USARTx, &USART_InitStructure);
-
- // 使能串口
- USART_Cmd(DEBUG_USARTx, ENABLE);
- }
- /***************** 发送一个字符 **********************/
- void Usart_SendByte( USART_TypeDef * pUSARTx, uint8_t ch)
- {
- /* 发送一个字节数据到USART */
- USART_SendData(pUSARTx,ch);
-
- /* 等待发送数据寄存器为空 */
- while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
- }
- /***************** 发送字符串 **********************/
- void Usart_SendString( USART_TypeDef * pUSARTx, char *str)
- {
- unsigned int k=0;
- do
- {
- Usart_SendByte( pUSARTx, *(str + k) );
- k++;
- } while(*(str + k)!='\0');
-
- /* 等待发送完成 */
- while(USART_GetFlagStatus(pUSARTx,USART_FLAG_TC)==RESET)
- {}
- }
- /***************** 发送一个16位数 **********************/
- void Usart_SendHalfWord( USART_TypeDef * pUSARTx, uint16_t ch)
- {
- uint8_t temp_h, temp_l;
-
- /* 取出高八位 */
- temp_h = (ch&0XFF00)>>8;
- /* 取出低八位 */
- temp_l = ch&0XFF;
-
- /* 发送高八位 */
- USART_SendData(pUSARTx,temp_h);
- while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
-
- /* 发送低八位 */
- USART_SendData(pUSARTx,temp_l);
- while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
- }
- ///重定向c库函数printf到串口,重定向后可使用printf函数
- int fputc(int ch, FILE *f)
- {
- /* 发送一个字节数据到串口 */
- USART_SendData(DEBUG_USARTx, (uint8_t) ch);
-
- /* 等待发送完毕 */
- while (USART_GetFlagStatus(DEBUG_USARTx, USART_FLAG_TXE) == RESET);
-
- return (ch);
- }
- ///重定向c库函数scanf到串口,重写向后可使用scanf、getchar等函数
- int fgetc(FILE *f)
- {
- /* 等待串口输入数据 */
- while (USART_GetFlagStatus(DEBUG_USARTx, USART_FLAG_RXNE) == RESET);
- return (int)USART_ReceiveData(DEBUG_USARTx);
- }
复制代码
全部资料51hei下载地址:
USART发送字符和字符串.rar
(293.81 KB, 下载次数: 78)
|