stm32中分析UART中IDLE,RXNE,TC,TXE这些标志位的作用

下面将基于 STM32 标准库,结合之前提到的不同应用场景,给出使用 TXETCIDLERXNE 标志位的代码示例及分析。

1. 连续数据发送(使用 TXE

应用场景

向外部设备连续发送大量数据,如向显示屏发送显示数据、向传感器发送配置指令序列等。

代码示例
复制代码
#include "stm32f10x.h"

void USART1_Configuration(void) {
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;

    // 使能 USART1 和 GPIOA 时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);

    // 配置 PA9 为复用推挽输出(TX)
    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);

    // 配置 USART1
    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_InitStructure.USART_Mode = USART_Mode_Tx;
    USART_Init(USART1, &USART_InitStructure);

    // 使能 USART1
    USART_Cmd(USART1, ENABLE);
}

void USART1_SendString(const char* str) {
    while (*str) {
        // 等待发送数据寄存器为空
        while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
        // 写入新的数据
        USART_SendData(USART1, (uint8_t)*str++);
    }
}

int main(void) {
    USART1_Configuration();
    const char* message = "Hello, World!";
    USART1_SendString(message);

    while (1) {
        // 主循环
    }
}
代码分析
  • USART1_Configuration 函数:
    • 使能 USART1 和 GPIOA 的时钟。
    • 配置 PA9 为复用推挽输出,用于 USART1 的发送功能。
    • 初始化 USART1,设置波特率为 115200,数据位为 8 位,停止位为 1 位,无校验位,无硬件流控制,仅使能发送模式。
    • 使能 USART1。
  • USART1_SendString 函数:
    • 使用 USART_GetFlagStatus 函数检查 TXE 标志位,当该标志位为 SET 时,表示发送数据寄存器为空。
    • 使用 USART_SendData 函数将字符串中的字符依次发送出去。
  • main 函数:
    • 调用 USART1_Configuration 函数初始化 USART1。
    • 调用 USART1_SendString 函数发送字符串 "Hello, World!"。

2. 数据发送完成确认(使用 TC

应用场景

在对数据完整性要求较高的场景中,确保整个数据帧完整无误地发送到目标设备。

代码示例
复制代码
#include "stm32f10x.h"

void USART1_Configuration(void) {
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;

    // 使能 USART1 和 GPIOA 时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);

    // 配置 PA9 为复用推挽输出(TX)
    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);

    // 配置 USART1
    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_InitStructure.USART_Mode = USART_Mode_Tx;
    USART_Init(USART1, &USART_InitStructure);

    // 使能 USART1
    USART_Cmd(USART1, ENABLE);
}

void USART1_SendDataAndWaitComplete(uint8_t data) {
    // 等待发送数据寄存器为空
    while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
    // 写入数据
    USART_SendData(USART1, data);
    // 等待发送完成
    while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}

int main(void) {
    USART1_Configuration();
    uint8_t command = 0xAA;
    USART1_SendDataAndWaitComplete(command);

    while (1) {
        // 主循环
    }
}
代码分析
  • USART1_Configuration 函数:与连续数据发送场景中的配置函数相同。
  • USART1_SendDataAndWaitComplete 函数:
    • 先使用 USART_GetFlagStatus 函数检查 TXE 标志位,当该标志位为 SET 时,将数据写入 DR 寄存器。
    • 然后使用 USART_GetFlagStatus 函数检查 TC 标志位,当该标志位为 SET 时,表示整个数据帧发送完成。
  • main 函数:
    • 调用 USART1_Configuration 函数初始化 USART1。
    • 调用 USART1_SendDataAndWaitComplete 函数发送一个字节的命令 0xAA

3. 数据帧边界识别(使用 IDLE

应用场景

接收不定长的数据帧,如从传感器接收实时数据、从其他设备接收通信协议数据等。

代码示例
复制代码
#include "stm32f10x.h"
#include <stdio.h>

#define BUFFER_SIZE 256
uint8_t rx_buffer[BUFFER_SIZE];
uint8_t rx_index = 0;

void USART1_Configuration(void) {
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    // 使能 USART1 和 GPIOA 时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);

    // 配置 PA10 为浮空输入(RX)
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // 配置 USART1
    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_InitStructure.USART_Mode = USART_Mode_Rx;
    USART_Init(USART1, &USART_InitStructure);

    // 使能 USART1 的 IDLE 中断
    USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);

    // 配置 NVIC
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    // 使能 USART1
    USART_Cmd(USART1, ENABLE);
}

void USART1_IRQHandler(void) {
    if (USART_GetITStatus(USART1, USART_IT_IDLE) != RESET) {
        // 清除 IDLE 标志位
        USART_ClearITPendingBit(USART1, USART_IT_IDLE);
        uint8_t temp = USART_ReceiveData(USART1); // 读取数据以清除 IDLE 标志

        // 处理一帧数据接收完成事件
        // 这里简单打印接收到的数据长度
        printf("Received %d bytes of data.\n", rx_index);

        // 清空缓冲区和索引
        rx_index = 0;
    } else if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) {
        // 读取接收缓冲区的数据
        rx_buffer[rx_index++] = USART_ReceiveData(USART1);
        // 清除 RXNE 标志位
        USART_ClearITPendingBit(USART1, USART_IT_RXNE);
    }
}

int main(void) {
    USART1_Configuration();

    while (1) {
        // 主循环
    }
}
代码分析
  • USART1_Configuration 函数:
    • 使能 USART1 和 GPIOA 的时钟。
    • 配置 PA10 为浮空输入,用于 USART1 的接收功能。
    • 初始化 USART1,设置波特率为 115200,数据位为 8 位,停止位为 1 位,无校验位,无硬件流控制,仅使能接收模式。
    • 使能 USART1 的 IDLE 中断,并配置 NVIC。
    • 使能 USART1。
  • USART1_IRQHandler 函数:
    • IDLE 中断发生时,使用 USART_ClearITPendingBit 函数清除 IDLE 标志位,读取数据以彻底清除该标志。处理接收到的数据(这里简单打印数据长度),并清空缓冲区和索引。
    • RXNE 中断发生时,将接收到的数据存入缓冲区,并清除 RXNE 标志位。
  • main 函数:
    • 调用 USART1_Configuration 函数初始化 USART1。
    • 进入主循环等待中断。

4. 实时数据接收(使用 RXNE

应用场景

实时处理接收到的数据,如实时监测传感器数据、接收外部设备的控制指令等。

代码示例
复制代码
#include "stm32f10x.h"

void USART1_Configuration(void) {
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    // 使能 USART1 和 GPIOA 时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);

    // 配置 PA10 为浮空输入(RX)
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // 配置 USART1
    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_InitStructure.USART_Mode = USART_Mode_Rx;
    USART_Init(USART1, &USART_InitStructure);

    // 使能 USART1 的 RXNE 中断
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

    // 配置 NVIC
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    // 使能 USART1
    USART_Cmd(USART1, ENABLE);
}

void USART1_IRQHandler(void) {
    if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) {
        uint8_t data = USART_ReceiveData(USART1); // 读取接收缓冲区的数据
        // 简单处理接收到的数据,这里假设点亮一个 LED
        if (data == '1') {
            GPIO_SetBits(GPIOA, GPIO_Pin_0);  // 点亮 PA0
        } else if (data == '0') {
            GPIO_ResetBits(GPIOA, GPIO_Pin_0);  // 熄灭 PA0
        }
        // 清除 RXNE 标志位
        USART_ClearITPendingBit(USART1, USART_IT_RXNE);
    }
}

int main(void) {
    // 使能 GPIOA 时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    // 配置 PA0 为推挽输出
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    USART1_Configuration();

    while (1) {
        // 主循环
    }
}
代码分析
  • USART1_Configuration 函数:
    • 使能 USART1 和 GPIOA 的时钟。
    • 配置 PA10 为浮空输入,用于 USART1 的接收功能。
    • 初始化 USART1,设置波特率为 115200,数据位为 8 位,停止位为 1 位,无校验位,无硬件流控制,仅使能接收模式。
    • 使能 USART1 的 RXNE 中断,并配置 NVIC。
    • 使能 USART1。
  • USART1_IRQHandler 函数:
    • RXNE 中断发生时,使用 USART_ReceiveData 函数读取接收缓冲区的数据。
    • 根据接收到的数据内容,控制 PA0 引脚的电平,实现点亮或熄灭 LED 的功能。
    • 使用 USART_ClearITPendingBit 函数清除 RXNE 标志位。
  • main 函数:
    • 使能 GPIOA 时钟,配置 PA0 为推挽输出。
    • 调用 USART1_Configuration 函数初始化 USART1。
    • 进入主循环等待中断。
相关推荐
省四收割者21 小时前
一文详解信号完整性(1)
python·嵌入式硬件·数学建模·信息与通信·信号处理·智能硬件
崇山峻岭之间1 天前
单片机无感FOC驱动之ADC
单片机·嵌入式硬件
Ligocious1 天前
1.点亮一颗小小的LED
单片机·嵌入式硬件
KaifuZeng1 天前
通信与接口协议面试八、CAN通信
单片机·嵌入式硬件·面试·通信与接口协议
智源单片机设计1 天前
基于单片机的直流电机双闭环调速系统设计
单片机·嵌入式硬件
kebidaixu1 天前
两轮车换电 BMS 的模拟前端中颖 SH367306 AFE 芯片详解
stm32
kebidaixu1 天前
两轮车换电 BMS 的模拟前端中颖 SH367306 AFE 芯片调试实录:四个关键问题与解决方案
stm32
KaifuZeng2 天前
通信与接口协议面试一、基本概念
嵌入式硬件·通信与接口协议
wuyk5552 天前
24. C 语言模块化:不是拆几个.c 文件那么简单
c语言·开发语言·stm32·单片机