no way to compare when less than two revisions

Differences

This shows you the differences between two versions of the page.


en:examples:communication:uart [2026/02/19 11:30] (current) – created - external edit 127.0.0.1
Line 1: Line 1:
 +<pagebreak>
 +====== Serial interface UART ======
 +
 +//Required knowledge: 
 +[HW] [[et:hardware:homelab:controller]], [HW] [[et:hardware:homelab:combo]], 
 +[AVR] [[et:avr:usart]], 
 +[LIB] [[et:software:homelab:library:usart]], \\
 +[LIB] [[et:software:homelab:library:module:lcd_graphic]] //
 +
 +===== Theory =====
 +
 +UART stands for "universal asynchronous receiver/transmitter". USART is almost the same, but with the difference that data is transferred together with a clock signal. UART can also be called a serial interface. A serial interface is a data transfer mechanism where each bit is transmitted one by one. For example, to transmit 1 byte, 8 bits are sent at a fixed time interval. Physically this means that on the serial line, which is a single microcontroller pin, the voltage level of that pin is changed high or low after a fixed time interval. A serial interface usually connects 2 devices: one transmits information (by changing the pin level) and the other receives it (by sampling the pin level). The transmitting pin is abbreviated TX, the receiving pin RX. Information on one line always flows in one direction. To send data in the other direction, a second line is used. If data is moved simultaneously on two lines, it is a full-duplex bus.
 +
 +[{{  :examples:communication:comm_uart_frame.png?319|UART frame, where S is start bit, 0-7 are data bits, P is parity bit (if used) and T is stop bit (or 2)}}]
 +
 +In the UART interface, data is transferred in frames. Depending on configuration, a frame has 5 to 9 data bits. The most common length is 8 bits, i.e., 1 byte. In addition to data bits, the frame includes extra bits that allow the receiver to recognize the start and end of the data. The first is the start bit, which is always 0, and the second is the stop bit (or bits), which is always 1. A parity bit can appear before the stop bit and is used to check data correctness. The parity bit indicates whether the number of ones among the data bits is even or odd. Which one it is depends on UART configuration. Today the parity bit is usually not used and can be disabled in the settings. Just like the parity bit, the number of data bits and stop bits can also be configured.
 +
 +Besides the frame structure, another important parameter is the baud rate, which specifies the number of symbols transmitted per second. A baud refers to a symbol. In UART, 1 baud equals 1 bit, which is why we speak about bits in the frame. In principle, any baud rate can be used for data transfer, but there are a number of common baud rates that are worth using, for example: 9600 bps, 19200 bps, 38400 bps, 57600 bps, 115200 bps.
 +
 +[{{ :et:examples:communication:rs232:usb-serial_tn.jpg?200|USB - RS-232 converter}}]
 +Almost all microcontrollers have one or more UART interfaces. The UART standard does not define signal voltages, so this depends on the microcontroller supply voltage, which is usually 3.3 V or 5.0 V. With a 5.0 V supply, the signals follow the TTL/CMOS standard. When UART is used to connect separate or distant devices, the physical standard RS-232 is used, which employs voltages up to -15 V and +15 V and defines the connector type DB-9. RS-232 is used mainly in computer serial ports, also known as COM ports. To connect a computer to a microcontroller, a level shifter is used that converts TTL/CMOS signals to RS-232 voltages and vice versa. Nowadays RS-232 has largely been replaced by USB, but due to its simplicity it is still used successfully in hobby applications, especially because USB-to-RS-232 converters exist. One advantage of RS-232 over USB is its much longer cable range.
 +
 +===== Practice =====
 +
 +On the HomeLab Combo module, the external UART interface (//ExtUART//) is available. To connect this port to a computer, you need a TTL UART<->RS232 or TTL UART<->USB adapter cable.
 +
 +To test the example program, you need a terminal-type application, e.g., HyperTerminal (Windows XP). On Linux you can use //minicom//. If the terminal is open when the microcontroller starts, a greeting message appears on the screen. Characters typed in the terminal are shown on the LCD screen. Pressing //Enter// changes the line on the LCD screen.
 +
 +<code c>
 +// HomeLab UART interface example program
 +// Text sent from a computer or another device is displayed on the LCD screen
 +#include <homelab/usart.h>
 +#include <homelab/module/lcd_gfx.h>
 +
 +// USART interface definition
 +usart port = USART(0);
 +
 +// Main program
 +int main(void)
 +{
 + char c;
 + unsigned char row = 1;
 +
 + // USART interface configuration
 + usart_init_async(port,
 + USART_DATABITS_8,
 + USART_STOPBITS_ONE,
 + USART_PARITY_NONE,
 + USART_BAUDRATE_ASYNC(9600));
 +
 + // LCD initialization and greeting message
 + lcd_gfx_init();
 + lcd_gfx_write_string("Ootan teadet");
 + lcd_gfx_goto_char_xy(0, row);
 + // Say hello to the computer
 + usart_send_string(port, "Tere, kirjuta midagi!\r\n");
 +
 + // Infinite loop
 + while (1)
 + {
 + if (usart_try_read_char(port, &c)) // Read a character from serial
 + {
 + if (c == '\r') // Is it a newline?
 + {
 + // Switch line
 + row = 1 - row;
 + // Clear the previous message from the line
 + lcd_gfx_clear_line(row);
 + }
 + else
 + {
 + // Print character directly to the screen
 + lcd_gfx_write_char(c);
 + }
 + }
 + }
 +}
 +</code>
 +
 +/*
 +[[et:examples:communication:rs232:alpha|Code example text to LCD (HomeLab ver 3.x and older)]]
 +*/
  
en/examples/communication/uart.txt · Last modified: by 127.0.0.1
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0