STM32F4xx UART2 中断接收不定长串口数据 - C语言示例

以下是在 Keil 环境下使用 UART2 中断方法接收串口不定长数据并显示出来的 C 语言程序:

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "stm32f4xx.h"

#define UART_BUFFER_SIZE 100

volatile char uartBuffer[UART_BUFFER_SIZE];
volatile uint8_t uartIndex = 0;
volatile uint8_t uartIsReceiving = 0;

void USART2_IRQHandler(void) {
    if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) {
        uartBuffer[uartIndex] = USART_ReceiveData(USART2);
        uartIndex++;
    }
}

void UART2_Init(void) {
    GPIO_InitTypeDef GPIO_InitStruct;
    USART_InitTypeDef USART_InitStruct;
    NVIC_InitTypeDef NVIC_InitStruct;

    // Enable clock for GPIOA and USART2
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

    // Configure GPIO pins for USART2
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStruct);

    // Connect USART2 pins to AF2
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);

    // Configure USART2 parameters
    USART_InitStruct.USART_BaudRate = 9600;
    USART_InitStruct.USART_WordLength = USART_WordLength_8b;
    USART_InitStruct.USART_StopBits = USART_StopBits_1;
    USART_InitStruct.USART_Parity = USART_Parity_No;
    USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(USART2, &USART_InitStruct);

    // Enable USART2 receive interrupt
    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

    // Enable USART2 interrupt in NVIC
    NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStruct);

    // Enable USART2
    USART_Cmd(USART2, ENABLE);
}

void UART2_SendChar(char ch) {
    // Wait until transmit buffer is empty
    while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET) {}

    // Send a character
    USART_SendData(USART2, ch);
}

void UART2_SendString(char* str) {
    // Send a string
    while (*str) {
        UART2_SendChar(*str++);
    }
}

int main(void) {
    UART2_Init();

    while (1) {
        if (uartIsReceiving == 1) {
            uartIsReceiving = 0;
            uartIndex = 0;

            // Display received data
            UART2_SendString("\nReceived data: ");
            UART2_SendString(uartBuffer);
            UART2_SendChar('\n');
        }
    }
}

在这个程序中,我们使用了 USART2 和 GPIOA 的引脚 2 和 3 来进行串口通信。程序初始化了 UART2,并启用了 UART2 的接收中断。在中断处理函数中,我们将接收到的数据存储在 uartBuffer 数组中,并增加 uartIndex 变量的值。在主函数中,我们通过检查 uartIsReceiving 变量是否为 1 来判断是否接收到了完整的数据。如果接收到了完整的数据,我们将 uartIsReceiving 变量设为 0,uartIndex 变量设为 0,并通过 UART2 将接收到的数据发送出去。

**注意:**以上是一个简单的示例程序,实际应用中可能需要根据具体需求进行修改和扩展。

STM32F4xx UART2 中断接收不定长串口数据 - C语言示例

原文地址: https://www.cveoy.top/t/topic/nYlu 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录