In previous part of USART tutorial we have discussed about simplest way of implementing USART transmitting and receiving routines. These are OK to use but in more intense and power critical applications they are not practical and efficient. Firs of all using loops to poll for transmitting buffer to be ready or wait for received byte consumes lots of processing power what also leads to more power consumption. In reception mode we cant predict when actually data will be received, so program has to check for received data indicating flag constantly and don’t miss it as next upcoming byte may clear it. So there is a better way of using USART – so called Interrupt Driven USART.
USART Interrupt sources
If you look into datasheet you will find that USART0 in Atmega328 has three interrupt sources:
TX Data Register Empty;
Probably a natural question comes out: Why there are two interrupts for transmission? Explanation is simple. Lets take TX Complete interrupt. It will occur when Transmit Shift Register has been shifted out and is empty. In this case we have empty transmit buffer UDR0 and Transmit Shift Register. After this interrupt there is no physical transmission on pins. This feature is usually used in half duplex transmission mode when transmitter and receiver share same line like in RS-485 standard.
So if we need to use single line and be able to transmit and receive data – use Half Duplex mode and TX complete interrupts. If we need to transmit data in one direction only, for instance serial LCD using TX Complete interrupt is impractical because we get gaps between transmitted frames and loose transmission rate. In such case it is more practical to use Tx Data Register Empty interrupt. This interrupt occurs just after data from transmit buffer is sent to transmit shift register. In this case UDR0 is ready to receive data while previous is being transmitted. In this case we get “chain” like transmission without gaps between frames. Read more »