STM32学习笔记(二)USART配置、接收和发送

USART初始化示例

void UART1_Init(uint32_t BaudRate)
{
	USART_InitTypeDef USART_InitStructure;
	GPIO_InitTypeDef GPIO_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);    
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);	
	
	USART_InitStructure.USART_BaudRate = 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_Mode = USART_Mode_Rx | USART_Mode_Tx;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_Init(USART1, &USART_InitStructure);
	
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	USART_ITConfig(USART1,USART_IT_RXNE, ENABLE);
	USART_ITConfig(USART1,USART_IT_TXE, DISABLE);
	
	USART_Cmd(USART1,ENABLE);
	
	USART_ClearFlag(USART1, USART_FLAG_TC);
	
}

串口应用

中断接收

#define READMAX 128

extern uint8_t readBuf[READMAX];
extern uint16_t readReg;

uint8_t readBuf[READMAX];
uint16_t readReg = 0;

void USART1_IRQHandler(void)
{
	 uint8_t data;
	
	if (USART_GetITStatus(USART1,USART_IT_RXNE) == SET) {

		data = USART_ReceiveData(USART1);
		
		if ((readReg & 0X8000) == 0) {
			if((readReg & 0X4000)){
			  if (data == '\n'){
					readReg |= 0X8000;					
				}else {
					readReg = 0;	
				}				
			}else {
				if (data == '\r') {
					readReg |= 0X4000;				
				}else {
					readBuf[(readReg & 0X3fff)] = data;
					readReg++;					
					if ((readReg & 0X3fff) > READMAX-1) {
						readReg = 0;						
					}
				}			
			}		
		}	
	}
}

发送字符串

void UART1_SendString(uint8_t *string)
{
	while (*string) {

		
		USART_ClearFlag(USART1, USART_FLAG_TC);
		USART_SendData(USART1, *string);
		while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {

		}
	
		string++;
	}
}

USART相关函数

USART_Init

void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct);

USARTx:x 可以是 1,2 或者 3,来选择 USART 外设

typedef struct
{
  uint32_t USART_BaudRate;            /*!< This member configures the USART communication baud rate.
                                           The baud rate is computed using the following formula:
                                            - IntegerDivider = ((PCLKx) / (16 * (USART_InitStruct->USART_BaudRate)))
                                            - FractionalDivider = ((IntegerDivider - ((u32) IntegerDivider)) * 16) + 0.5 */

  uint16_t USART_WordLength;          /*!< Specifies the number of data bits transmitted or received in a frame.
                                           This parameter can be a value of @ref USART_Word_Length */

  uint16_t USART_StopBits;            /*!< Specifies the number of stop bits transmitted.
                                           This parameter can be a value of @ref USART_Stop_Bits */

  uint16_t USART_Parity;              /*!< Specifies the parity mode.
                                           This parameter can be a value of @ref USART_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). */
 
  uint16_t USART_Mode;                /*!< Specifies wether the Receive or Transmit mode is enabled or disabled.
                                           This parameter can be a value of @ref USART_Mode */

  uint16_t USART_HardwareFlowControl; /*!< Specifies wether the hardware flow control mode is enabled
                                           or disabled.
                                           This parameter can be a value of @ref USART_Hardware_Flow_Control */
} USART_InitTypeDef;
#define USART_WordLength_8b                  ((uint16_t)0x0000)
#define USART_WordLength_9b                  ((uint16_t)0x1000)
#define USART_StopBits_1                     ((uint16_t)0x0000)
#define USART_StopBits_0_5                   ((uint16_t)0x1000)
#define USART_StopBits_2                     ((uint16_t)0x2000)
#define USART_StopBits_1_5                   ((uint16_t)0x3000)
  
#define USART_Parity_No                      ((uint16_t)0x0000)
#define USART_Parity_Even                    ((uint16_t)0x0400)
#define USART_Parity_Odd                     ((uint16_t)0x0600) 
#define USART_Mode_Rx                        ((uint16_t)0x0004)
#define USART_Mode_Tx                        ((uint16_t)0x0008)
#define USART_HardwareFlowControl_None       ((uint16_t)0x0000)
#define USART_HardwareFlowControl_RTS        ((uint16_t)0x0100)
#define USART_HardwareFlowControl_CTS        ((uint16_t)0x0200)
#define USART_HardwareFlowControl_RTS_CTS    ((uint16_t)0x0300)

USART_ITConfig

void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState);

使能或者失能指定的 USART 中断

#define USART_IT_PE                          ((uint16_t)0x0028) //奇偶错误中断
#define USART_IT_TXE                         ((uint16_t)0x0727) //发送中断
#define USART_IT_TC                          ((uint16_t)0x0626) //传输完成中断
#define USART_IT_RXNE                        ((uint16_t)0x0525) //接收中断
#define USART_IT_IDLE                        ((uint16_t)0x0424) //空闲总线中断
#define USART_IT_LBD                         ((uint16_t)0x0846) //LIN 中断检测中断
#define USART_IT_CTS                         ((uint16_t)0x096A) //CTS 中断
#define USART_IT_ERR                         ((uint16_t)0x0060) //错误中断
#define USART_IT_ORE                         ((uint16_t)0x0360) //
#define USART_IT_NE                          ((uint16_t)0x0260) //
#define USART_IT_FE                          ((uint16_t)0x0160) //

在这里插入图片描述

typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;

USART_GetITStatus

ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT);

检查指定的 USART 中断发生与否

USART_Cmd

void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState);

使能或者失能USART外设

USART_ClearFlag

void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG);

清除 USARTx 的待处理标志位

#define USART_FLAG_CTS                       ((uint16_t)0x0200)
#define USART_FLAG_LBD                       ((uint16_t)0x0100)
#define USART_FLAG_TXE                       ((uint16_t)0x0080)
#define USART_FLAG_TC                        ((uint16_t)0x0040)
#define USART_FLAG_RXNE                      ((uint16_t)0x0020)
#define USART_FLAG_IDLE                      ((uint16_t)0x0010)
#define USART_FLAG_ORE                       ((uint16_t)0x0008)
#define USART_FLAG_NE                        ((uint16_t)0x0004)
#define USART_FLAG_FE                        ((uint16_t)0x0002)
#define USART_FLAG_PE                        ((uint16_t)0x0001)

在这里插入图片描述

USART_GetFlagStatus

FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG);

检查指定的 USART 标志位设置与否

USART_SendData

void USART_SendData(USART_TypeDef* USARTx, uint16_t Data);

USART_ReceiveData

uint16_t USART_ReceiveData(USART_TypeDef* USARTx);

串口中断补充

NVIC_IRQChannel,中断源,板子不同USART中断个数不一样

  USART1_IRQn                 = 37,     /*!< USART1 global Interrupt                              */
  USART2_IRQn                 = 38,     /*!< USART2 global Interrupt                              */

中断函数名

                EXPORT  USART1_IRQHandler          [WEAK]
                EXPORT  USART2_IRQHandler          [WEAK]
                EXPORT  USART3_IRQHandler          [WEAK]
STM32CubeIDE中配置USART串口需要进行以下步骤: 1. 打开STM32CubeIDE,并创建一个新的工程。 2. 在工程导航器中,右键单击"Core"文件夹,选择"Add Component",然后选择"USART"组件。这将在工程中添加USART相关的文件和代码。 3. 在"Pinout & Configuration"选项卡中,选择你要使用的USART引脚。 4. 在"Configuration"选项卡中,配置USART的参数,包括波特率、数据位数、停止位等等。可以根据具体需求进行配置。 5. 在代码中,使用HAL库函数来初始化和配置USART,例如使用`HAL_UART_Init()`函数来初始化USART,使用`HAL_UART_MspInit()`函数来配置USART引脚等等。 6. 在代码中,根据需求来实现USART发送接收功能。可以使用轮询方式或中断方式来发送接收数据。如果使用中断方式,需要编写相应的中断回调函数,例如使用`HAL_UART_RxCpltCallback()`函数来处理接收完成中断。 在以上步骤中,引用提供了一些关键词,如"STM32F407VE"、"FreeRTOS"、"DMA"等,可以参考这些关键词在STM32CubeIDE中进行配置。同时,引用和引用提供了一些代码示例,可以参考这些示例来实现USART配置和功能。 请注意,具体的配置和代码实现可能会因具体的硬件平台和需求而有所不同。建议查阅官方的文档和资料来获取更详细和准确的配置步骤。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [STM32F407VE+FreeRTOS+STM32CubeIDE+串口DMA+Freemodbus移植代码](https://download.csdn.net/download/qq_31272725/83163651)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [STM32CubeIDE学习笔记——USART(三种收发方式,printf重定向,工程配置)](https://blog.csdn.net/Reasally/article/details/126751127)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值