This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:iot-open:embeddedcommunicationprotocols2:uart [2023/06/25 20:39] – ktokarz | en:iot-open:embeddedcommunicationprotocols2:uart [2024/05/27 11:26] (current) – ktokarz | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== UART ==== | + | ====== UART ====== |
+ | {{: | ||
+ | UART name is an abbreviation of Universal Asynchronous Receiver Transmitter. It is one of the most often used communication methods, traditionally named serial interface or serial port. In contrast to previously presented interfaces, UART uses direct point-to-point communication. | ||
+ | UART is the communication unit implemented in microcontrollers rather than the communication protocol. It sends the series of bits via the TxD pin and receives a stream of bits with the RxD pin (figure {{ref> | ||
- | UART name is an abbreviation of Universal Asynchronous Receiver Transmitter. It is one of the most often used communication methods, traditionally named serial interface or serial port. In opposite to previously presented interfaces, | + | <figure uart1> |
+ | {{ en: | ||
+ | < | ||
+ | </ | ||
- | ; UART, is a simplified version - universal asynchronous serial interface. The difference being that USART also uses a clock signal line to synchronize data, but UART only uses data lines. AVR's USART allows the use of full duplex communication, | ||
- | Every configuration option has corresponding registers, which are quite easy to configure using the datasheet. The baud rate, though, is a bit more difficult to set. The clock signal for data transmission | + | The transmission speed and bit duration must be the same at the transmitter and receiver |
+ | Start and stop bits are used to synchronise | ||
- | Since data transmission takes place independently of the processor and much slower, it is necessary to confirm that the interface is ready for the next word before transmitting. This can be done by keeping an eye on the transmit buffer' | + | <figure uart2> |
+ | {{ en: | ||
+ | < | ||
+ | </ | ||
- | The arrival of a word is signified also by a special status bit. In addition | + | UART, namely Serial Port, is used in many modern microcontrollers |
+ | < | ||
+ | Many modern PC computers (except industrial ones) do not have a serial port exposed, so USB to serial converters must be used. Some development boards have a USB-serial converter on board (e.g. Arduino Uno, NodeMCU, STM Nucleo, etc.) | ||
+ | </ | ||
+ | < | ||
+ | Even if a PC computer has a serial port, it is usually compatible with the RS-232 standard. It uses the same frame structure but different voltage levels (with opposite zero-one encoding, known as reverse logic). | ||
+ | </ | ||
- | The transmit and receive buffers are physically separate registers, but share the same memory address and name. When writing to the shared register, the data is stored in the transmit buffer, and when reading from it, the data is read from the receive buffer. When using 9-bit words, the 9th bit is transmitted and read using one of the configuration registers. | ||
- | < | ||
- | |||
- | <box 100% round # | ||
- | |||
- | Task: Configure an 8 MHz ATmega128' | ||
- | |||
- | <code c> | ||
- | #include < | ||
- | |||
- | int main() | ||
- | { | ||
- | // Set baud rate to 9600 bps. Formula: | ||
- | // | ||
- | // UBRR = 8000000 / 16 / 9600 - 1 = ~51 | ||
- | UBRR0H = 0; | ||
- | UBRR0L = 51; | ||
- | |||
- | // Allow transmitting | ||
- | UCSR0B = (1 << TXEN0); | ||
- | |||
- | // Configure asynchronous mode, set the word size to 8 bits | ||
- | // 1 stop bit, no parity bits. | ||
- | UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); | ||
- | |||
- | // Wait for the data buffer to empty (previous word to transmit) | ||
- | // In this example there is no need to wait, as the first symbol is yet | ||
- | // to be sent, but it should be done when transmitting more symbols. | ||
- | while (!(UCSR0A & (1 << UDRE))) continue; | ||
- | |||
- | // Write the symbol to the buffer for transmitting | ||
- | UDR0 = ' | ||
- | |||
- | // Endless loop | ||
- | while (1) continue; | ||
- | } | ||
- | </ | ||
- | |||
- | </ |