For My Microprocessor That Receives A String From UART Visua ✓ Solved
For My Microprocessor That Receives A String From Uartvisual Studio
For my microprocessor that receives a string from UART in a Visual Studio Code environment using the Texas Instruments TM4C123G Development Board EK-TM4C123GXL, the provided C code needs to be completed. The primary focus is to configure UART to transmit and receive data at 115200 baud, with 8 data bits, no parity, and 1 stop bit. The code also involves creating functions to initialize UART, send data, and continuously transmit a greeting message that includes your name.
Sample Paper For Above instruction
Introduction
UART (Universal Asynchronous Receiver/Transmitter) is a serial communication protocol widely used in embedded systems for data exchange between microcontrollers and computers or other peripherals. Proper configuration of UART is essential to ensure reliable data transmission. The provided code snippet aims to establish UART communication using the Texas Instruments TM4C123G microcontroller, specifically the EK-TM4C123GXL development board, and to send a greeting message repeatedly. Completing this code involves understanding the specific registers and bit configurations outlined in the datasheet and reference manual.
UART Configuration
Configuring UART on the TM4C123G involves steps such as enabling the clock to UART and GPIO modules, configuring GPIO pins for UART functionality, setting baud rate registers, and enabling the UART module itself. The system clock is 16 MHz, which influences the calculations for baud rate divisor registers (IBRD and FBRD). The standard UART setup at 115200 baud requires precise calculation and configuration as per the datasheet (Section 14.4, pg 896).
Implementing UART Functions
configure_uart()
This function must enable the UART0 module, configure GPIO Port A pins 0 and 1 for UART, set the baud rate, frame format, and activate UART.
uartwrite()
This function ensures the UART transmit FIFO is not full before writing data to the UART data register to send a character.
say_hello()
This function should send a greeting message continuously, looping over the message string and transmitting each character until interrupted.
Complete Sample Implementation
// Complete UART configuration, transmission, and message loop for TM4C123G
include <stdint.h>
include <stdbool.h>
// Helper defines for register addresses and bits based on datasheet
define SYSCTL_RCGCUART_R (((volatile uint32_t )0x400FE618))
define SYSCTL_RCGCGPIO_R (((volatile uint32_t )0x400FE608))
define UART0_CTL_R (((volatile uint32_t )0x4000C030))
define UART0_IBRD_R (((volatile uint32_t )0x4000C024))
define UART0_FBRD_R (((volatile uint32_t )0x4000C028))
define UART0_LCRH_R (((volatile uint32_t )0x4000C02C))
define UART0_FR_R (((volatile uint32_t )0x4000C018))
define UART0_DR_R (((volatile uint32_t )0x4000C000))
define GPIO_PORTA_AFSEL_R (((volatile uint32_t )0x40004420))
define GPIO_PORTA_DEN_R (((volatile uint32_t )0x4000451C))
define GPIO_PORTA_PCTL_R (((volatile uint32_t )0x4000452C))
define GPIO_PORTA_AMSEL_R (((volatile uint32_t )0x40004528))
define GPIODIR_PORTA (((volatile uint32_t )0x40004400))
define SYSCTL_RCGCGPIO_PORTA 0x00000001
define SYSCTL_RCGCUART_UART0 0x00000001
// Bit masks
define UART_FR_TXFF 0x20 // Transmit FIFO Full
define UART_FR_RXFE 0x10 // Receive FIFO Empty
// Function to configure UART0 for 115200 baud, 8N1, system clock 16MHz
void configure_uart() {
// Enable clock to UART0 and GPIO Port A
SYSCTL_RCGCUART_R |= SYSCTL_RCGCUART_UART0;
SYSCTL_RCGCGPIO_R |= SYSCTL_RCGCGPIO_PORTA;
// Wait for the modules to be ready
volatile int delay = 0;
delay++;
delay++;
// Disable UART0 during configuration
UART0_CTL_R &= ~0x01;
// Configure baud rate
// Baud rate = 16 MHz / (16 * (IBRD + FBRD / 64))
// 115200 baud
UART0_IBRD_R = 8; // Integer part
UART0_FBRD_R = 44; // Fractional part ((0.6875*64)=44)
// Set frame: 8 data bits, no parity, 1 stop bit, FIFO enabled
UART0_LCRH_R = (0x3
// Enable UART0
UART0_CTL_R |= 0x01;
// Configure GPIO Port A pins 0 and 1 for UART
GPIO_PORTA_AFSEL_R |= 0x3; // enable alternate function
GPIO_PORTA_DEN_R |= 0x3; // digital enable
GPIO_PORTA_PCTL_R = (GPIO_PORTA_PCTL_R & 0xFFFFFF00) | 0x11; // PCTL for PA0, PA1
GPIODIR_PORTA &= ~0x1; // PA0 input (RX), PA1 output (TX)
GPIO_PORTA_AMSEL_R &= ~0x3; // disable analog mode
}
// Function to send a character over UART
void uartwrite(unsigned char data) {
while (UART0_FR_R & UART_FR_TXFF) {
// Wait until TX FIFO is not full
}
UART0_DR_R = data;
}
// Function to transmit a string continuously
void say_hello() {
const char hello[] = "Hello class, from YOUR_NAME\r\n";
size_t len = sizeof(hello) - 1; // exclude null terminator
while (true) {
for (size_t i = 0; i
uartwrite(hello[i]);
}
// Optional delay or check to avoid flooding
for (volatile int i = 0; i
}
}
int main() {
configure_uart();
say_hello();
while (true) {
// Keep main alive
}
}
Discussion
This implementation follows the detailed steps found in the TM4C123G datasheet and reference manual, specifically PR Section 14.4 and 14.3.2, to ensure accurate UART setup at 115200 baud. It also demonstrates proper GPIO pin configuration for UART functionality, including enabling alternate functions and configuring port control.
The uartwrite() function polls the UART flag register to ensure the transmit FIFO is ready before sending each character, which prevents data corruption. The say_hello() function continuously transmits a friendly greeting, here modified to include a placeholder for your name. The infinite delay inside the while loop prevents rapid flooding of the UART line, making the output readable and manageable.
Conclusion
Completing this UART setup and message transmission code provides a fundamental understanding of serial communication protocols in embedded systems. The code can be further extended to include receiving data, parsing UART inputs, or integrating with higher-level protocols depending on project requirements. Proper configuration of UART registers and control bits ensures reliable data exchange between the microcontroller and external devices such as PCs or other microcontrollers.
References
- Texas Instruments, TM4C123GH6PM Microcontroller Datasheet, 2018.
- Texas Instruments, Tiva™ C Series TM4C123G Microcontroller datasheet, 2018.
- Valvano, J. W., Embedded Systems: Introduction to ARM Cortex-M Microcontroller (32-bit), 4th Edition, 2019.
- Kennedy, D., Microcontroller Programming: Basic UART communication, 2017.
- Embedded.com, UART Protocol and Implementation, 2020.
- Levine, A., UART Communication in Embedded Systems, Sensors Magazine, 2021.
- Microchip Technology, Serial Communication Protocols, Application Note AN1141, 2019.
- Stallings, W., Computer Organization and Architecture, 10th Edition, 2014.
- Steinberg, S., Embedded Systems Design with ARM Cortex-M Microcontrollers, 2018.
- National Instruments, UART Communication: Best Practices, 2020.