STM32下串口的使用
来源:互联网 发布:王羲之兰亭序字帖知乎 编辑:程序博客网 时间:2024/05/22 00:41
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_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下串口的使用
- STM32 串口+DMA的使用
- stm32 串口的使用-转转
- STM32之串口的使用
- STM32下的串口重定向问题
- STM32学习之串口的使用
- STM32学习之串口的使用
- 使用STM32的串口进行大量数据传输
- STM32串口中断使用
- STM32串口中断使用
- STM32串口使用
- KEIL下STM32如何使用printf函数进行串口输出
- STM32的串口通信
- 关于stm32的串口
- STM32高级开发(13)-Ubuntu下的串口助手minicom
- 在STM32使用串口printf
- STM32 串口3使用代码
- STM32学习笔记(7):USART串口的使用
- 事务的回滚五种方法
- [回馈CSDN网友]贴个SQL小工具--移动数据库物理文件
- 实现基于Android的英文电子词典
- 根据CRM_CB里的系统参数定义,各行主题图片切换
- SqlServer 杂项
- STM32下串口的使用
- Apache + Tomcat 实现负载均衡(三)
- UIButton按钮控件
- Linux scp远程文件/目录传输 用ps和grep命令寻找僵尸进程
- windows绘图概述
- shell数组
- 如何复制windows命令行内容
- [jQuery]使用jQuery.Validate进行客户端验证(高级篇-上)——不使用微软验证控件的理由
- ffmpeg: ‘UINT64_C’ was not declared in this scope