历史上的今天
今天是:2024年12月16日(星期一)
2020年12月16日 | STM32F7xx —— 串口通信
2020-12-16 来源:eefocus
一、串口初始化过程
1、时钟使能;
2、GPIO初始化;
3、串口波特率设置;
4、串口控制;
5、数据发送与接收
二、几个重要的串口函数
HAL_StatusTypeDef HAL_UART_Init(UART_HandleTypeDef *huart); // 串口初始化
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout); // 串口发送
HAL_StatusTypeDef HAL_UART_Receive(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout); // 串口接收
__HAL_UART_ENABLE_IT(__HANDLE__, __INTERRUPT__) // 串口中断使能
void HAL_NVIC_SetPriority(IRQn_Type IRQn, uint32_t PreemptPriority, uint32_t SubPriority); // 设置中断优先级
void HAL_NVIC_EnableIRQ(IRQn_Type IRQn); // 使能中断
三、几个重要的结构
// 串口初始化结构体 包含了串口句柄 波特率配置 发送接收缓存 dma等
// 我们只描述前两个基本功能,对效率要求极高可以使用DMA。
typedef struct
{
USART_TypeDef *Instance; /*!< UART registers base address */
UART_InitTypeDef Init; /*!< UART communication parameters */
UART_AdvFeatureInitTypeDef AdvancedInit; /*!< UART Advanced Features initialization parameters */
uint8_t *pTxBuffPtr; /*!< Pointer to UART Tx transfer Buffer */
uint16_t TxXferSize; /*!< UART Tx Transfer size */
uint16_t TxXferCount; /*!< UART Tx Transfer Counter */
uint8_t *pRxBuffPtr; /*!< Pointer to UART Rx transfer Buffer */
uint16_t RxXferSize; /*!< UART Rx Transfer size */
uint16_t RxXferCount; /*!< UART Rx Transfer Counter */
uint16_t Mask; /*!< UART Rx RDR register mask */
DMA_HandleTypeDef *hdmatx; /*!< UART Tx DMA Handle parameters */
DMA_HandleTypeDef *hdmarx; /*!< UART Rx DMA Handle parameters */
HAL_LockTypeDef Lock; /*!< Locking object */
__IO HAL_UART_StateTypeDef gState; /*!< UART state information related to global Handle management
and also related to Tx operations.
This parameter can be a value of @ref HAL_UART_StateTypeDef */
__IO HAL_UART_StateTypeDef RxState; /*!< UART state information related to Rx operations.
This parameter can be a value of @ref HAL_UART_StateTypeDef */
__IO uint32_t ErrorCode; /*!< UART Error code */
}UART_HandleTypeDef;
// 串口的操作句柄 如 USART1 USART2 USART3等
typedef struct
{
__IO uint32_t CR1; /*!< USART Control register 1, Address offset: 0x00 */
__IO uint32_t CR2; /*!< USART Control register 2, Address offset: 0x04 */
__IO uint32_t CR3; /*!< USART Control register 3, Address offset: 0x08 */
__IO uint32_t BRR; /*!< USART Baud rate register, Address offset: 0x0C */
__IO uint32_t GTPR; /*!< USART Guard time and prescaler register, Address offset: 0x10 */
__IO uint32_t RTOR; /*!< USART Receiver Time Out register, Address offset: 0x14 */
__IO uint32_t RQR; /*!< USART Request register, Address offset: 0x18 */
__IO uint32_t ISR; /*!< USART Interrupt and status register, Address offset: 0x1C */
__IO uint32_t ICR; /*!< USART Interrupt flag Clear register, Address offset: 0x20 */
__IO uint32_t RDR; /*!< USART Receive Data register, Address offset: 0x24 */
__IO uint32_t TDR; /*!< USART Transmit Data register, Address offset: 0x28 */
} USART_TypeDef;
// 设置串口的各个参数 波特率 字长 停止位 奇偶校验 收发模式 硬件流 过采样
// 字长:8位/9位
// 停止位:1位/2位
typedef struct
{
uint32_t BaudRate; /*!< This member configures the UART communication baud rate.
The baud rate register is computed using the following formula:
- If oversampling is 16 or in LIN mode,
Baud Rate Register = ((PCLKx) / ((huart->Init.BaudRate)))
- If oversampling is 8,
Baud Rate Register[15:4] = ((2 * PCLKx) / ((huart->Init.BaudRate)))[15:4]
Baud Rate Register[3] = 0
Baud Rate Register[2:0] = (((2 * PCLKx) / ((huart->Init.BaudRate)))[3:0]) >> 1 */
uint32_t WordLength; /*!< Specifies the number of data bits transmitted or received in a frame.
This parameter can be a value of @ref UARTEx_Word_Length */
uint32_t StopBits; /*!< Specifies the number of stop bits transmitted.
This parameter can be a value of @ref UART_Stop_Bits */
uint32_t Parity; /*!< Specifies the parity mode.
This parameter can be a value of @ref UART_Parity
@note When parity is enabled, the computed parity is inserted
at the MSB position of the transmitted data (9th bit when
the word length is set to 9 data bits; 8th bit when the
word length is set to 8 data bits). */
uint32_t Mode; /*!< Specifies whether the Receive or Transmit mode is enabled or disabled.
This parameter can be a value of @ref UART_Mode */
下一篇:STM32F7xx —— 输入
史海拾趣
|
摘要:采用PWM控制器和MOSFET功率开关一体化的集成控制芯片是新一代开关电源设计的重要特点和趋势。本文介绍美国功率集成公司(PowerIntegrationsInc.)于九十年代中期研制推出的三端PWM/MOSFET二合一集成控制器件TOPSwitch系列及其在开关电源设计 ...… 查看全部问答> |
|
如题? 是否必须给这个内核打yaffs2的补丁? 哪位大哥有yaffs2源码共享一下。 另外,从哪个内核开始能支持yaffs2而不需要再打补丁?… 查看全部问答> |
|
先给我的代码, CW DATA 0F9FCH ;写指令 CR DATA 0F9FEH ;读状态 DIW DATA 0F9FDH ;写数据 DIR DATA 0F9FFH &nb ...… 查看全部问答> |
|
用频谱仪测试读卡器产生的场强,显示幅度单位是mV.但据标准给出的是1.5A/m-7.5A/m才为有效的场强范围。 应如何进行换算?或是如何确定在有效范围内? 请各位指点… 查看全部问答> |
|
要做一个IAP的系统,将程序分成2部分:boot和AP,boot负责引导系统和更新AP部分的程序,AP部分是实际需要程序。boot程序相当于普通程序,编译后下载到0x8000000开始的地址,中断向量表从0开始。AP的开始地址要在boot部分的后面,比如从0x80080 ...… 查看全部问答> |
|
前不久,公司买了一套real6410的开发板,在调试过程中出现了一些问题,经过查找资料和实验,不断的排查,找了问题的所在。不过声明一下,我这里没有进行系统的测试,问题的真正原因有待深入排查,至少在我这里是解决了一些问题。 开发平台:Real ...… 查看全部问答> |
|
#include<avr/io.h>#include<avr/delay.h>#include SET_1(a,b) a|(1<<b) //置1#include CLE_0(a,b) a&~(1<<b) //清零 我在程序开始定义了“#include SET_1(a,b) a|(1<<b)”和“#include CLE_0(a,b) a&~(1<<b)”这两件 ...… 查看全部问答> |
|
1602液晶显示和DS18B20测温方面已经没问题,但小弟想设置上下限警报器,想各位教教我或帮我编写一下,万分感谢编写软件:ICCAVR 单片机芯片:MEGA16 ========================================= #include \"iom16v.h\"#include \"macros.h\" #define RS ...… 查看全部问答> |
|
最近在做一个项目,是用fpga配置ad9923a 来驱动 ccd ,型号是icx205al 。配置ad9923a的时序是串口方式配置,由于要配置的寄存器多达140个,每个由12位地址和28位数据总共40位组成 (不足位的补0),里面还有部分需要延时,附上时序图,有哪位 ...… 查看全部问答> |




