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:49] – 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> |
- | UART is the communication unit implemented in microcontrollers rather than the communication protocol. It sends the series of bits via TxD pin and receives a stream of bits with RxD pin. It is important to remember that pin TxD from one device should be connected to pin RxD in another device. This is a general rule but please always check the documentation for some non-standard markings. | + | {{ en: |
+ | < | ||
+ | </ | ||
- | To properly transmit data the speed of transmission, otherwise speaking the duration of bit, must be the same at the transmitter and receiver. In UART data is sent in frames | + | The transmission |
+ | Start and stop bits are used to synchronise the receiver and transmitter. Sample transmission flow is present in figure {{ref> | ||
- | ; UART, is a simplified version | + | <figure uart2> |
+ | {{ en:iot-open: | ||
+ | < | ||
+ | </ | ||
- | Every configuration option has corresponding registers, which are quite easy to configure using the datasheet. The baud rate, though, is a bit more difficult | + | 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 | ||
+ | </ | ||
- | 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' | ||
- | The arrival of a word is signified also by a special status bit. In addition to that, there are status bits to signify framing errors, parity errors and receive buffer overflows. Buffer overflow can occur when the last word is yet to be read from the buffer while a new one arrives - this is why it is always important to read the incoming words to the program as soon as possible, for example, by using an interrupt. There are three possible interrupt reasons: transmit buffer ready, transmit successful and receive successful. | ||
- | |||
- | 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; | ||
- | } | ||
- | </ | ||
- | |||
- | </ |