STM32 USART Communication with Button-Triggered Data Transmission

USART Serial Communication Fundamentals

Communication Concepts

Communication refers to the process of information exchange between computers and external devices. Communication protocols define the rules that both parties must follow during data transmission.

Common Communication Methods

Parallel Communication

All binary data bits are transmitted or received simultaneously. Advantages include high communication speed, while disadvantages include requiring multiple data lines and higher communication costs. Suitable for short-distance applications.

Serial Communication

Binary data bits are transmitted or received sequentially one by one. Advantages include requiring only 1-2 data lines and lower communication costs. Disadvantages include slower communication speed. Suitable for long-distance applications.

Serial Communication Types

Asynchronous Communication

Uses different clock sources for transmitter and receiver with out shared clock line control. Data is transmitted in frames consisting of start bit, data bits, parity bit, and stop bit. Transmission follows LSB-first order.

Synchronous Communication

Both transmitter and receiver share the same clock source with equal frequency. Data transmission begins with synchronization characters followed by data streams of variable length.

Data Transmission Speed

Baud rate (BPS) measures the number of data bits transmitted per second. For example: 120 characters/second × (1 start + 8 data + 1 stop) = 1200 BPS.

Communication Modes

  • Simplex: Single data line, unidirectional transmission
  • Half-duplex: Single data line, bidirectional transmission but not simultaneous
  • Full-duplex: Two data lines, simultaneous bidirectional transmission

STM32 USART Module Overview

STM32F40x USART modules provide flexible full-duplex data exchange with external devices. The chip contains six independent USART modules (USART1-6), with USART1, 2, 3, 6 supporting synchronous/asynchronous modes and UART4, 5 supporting only asynchronous mode.

Data Transmission Process

Transmission Sequence

  1. MCU defines data to transmit
  2. Data written to transmit data register via data bus
  3. Data parallel transferred to transmit shift register
  4. Data serial transmitted via TX pin according to baud rate
  5. Data sent to PC via USB-to-serial converter

Reception Sequence

  1. PC sends data via USB to serial converter to RX pin
  2. Data reecived into receive shift register according to baud rate
  3. Data parallel transferred to receive data register when full
  4. CPU reads data from receive data register

USART Configuration Implementation

GPIO Pin Configuration

// Enable GPIOA clock
RCC->AHB1ENR |= 1 << 0;

// Configure PA9 as alternate function
GPIOA->MODER &= ~(3 << 18);
GPIOA->MODER |= 2 << 18;
GPIOA->AFR[1] |= 7 << 4;

// Configure PA10 as alternate function  
GPIOA->MODER &= ~(3 << 20);
GPIOA->MODER |= 2 << 20;
GPIOA->AFR[1] |= 7 << 8;

USART Initialization Function

void USART1_Configuration(uint32_t baud_rate)
{
    float divider_value;
    uint32_t clock_freq = 84000000;
    uint16_t int_part, frac_part;
    
    // Enable USART1 clock
    RCC->APB2ENR |= 1 << 4;
    
    // Configure 16x oversampling, 8 data bits
    USART1->CR1 &= ~(1 << 15);
    USART1->CR1 &= ~(1 << 12);
    
    // 1 stop bit
    USART1->CR2 &= ~(3 << 12);
    
    // Calculate baud rate divisor
    divider_value = clock_freq / (8 * (2 - 0) * baud_rate);
    int_part = divider_value;
    frac_part = (divider_value - int_part) * 16;
    USART1->BRR = int_part << 4 | frac_part;
    
    // Enable transmitter, receiver and USART
    USART1->CR1 |= 1 << 3;
    USART1->CR1 |= 1 << 2;  
    USART1->CR1 |= 1 << 13;
}

Data Transmission Functions

// Redirect printf to USART
int fputc(int character, FILE* stream)
{
    while(!(USART1->SR & 1 << 6)) {}
    USART1->DR = character;
    return character;
}

// Send string via USART
void Send_String(uint8_t* string_data)
{
    uint8_t current_char;
    uint32_t index = 0;
    
    while(string_data[index] != '\0')
    {
        current_char = string_data[index];
        while(!(USART1->SR & 1 << 6)) {}
        USART1->DR = current_char;
        index++;
    }
}

Button Control Implementation

// Button initialization
void Button_Initialize(void)
{
    RCC->AHB1ENR |= 1 << 0;
    GPIOA->MODER &= ~(3 << 0);
    GPIOA->PUPDR &= ~(3 << 0);
    GPIOA->PUPDR |= 2 << 0;
}

// Button scanning with debounce
uint8_t Button_Check(void)
{
    static uint8_t button_state = 0;
    
    if((button_state == 0) && (GPIOA->IDR & 1 << 0))
    {
        button_state = 1;
        Delay_Milliseconds(500);
        if(GPIOA->IDR & 1 << 0)
        {
            return 1;
        }
    }
    else if((button_state == 1) && !(GPIOA->IDR & 1 << 0))
    {
        button_state = 0;
    }
    return 0;
}

Main Application

#define DATA_STRING "qwertyuiop"

int main(void)
{
    uint8_t button_status;
    LED_Initialize();
    Button_Initialize();
    USART1_Configuration(115200);
    
    printf("Press button to transmit data:\r\n");
    
    while(1)
    {
        button_status = Button_Check();
        if(button_status == 1)
        {
            Send_String(DATA_STRING);
            LED_Toggle();
            printf("Button pressed - data transmitted:\r\n");
        }
    }
}

Tags: STM32 USART SerialCommunication gpio microcontroller

Posted on Sat, 30 May 2026 00:02:15 +0000 by adityakonda