Programming AVR USART with AVR-GCC. Part 2

In the last part of the USART tutorial, we discussed the most straightforward 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. First 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 can’t predict when actual data will be received, so the 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 the datasheet, you will find that USART0 in Atmega328 has three interrupt sources: Probably a natural question comes out: Why there are two interrupts for transmission? The explanation is simple. Let’s take TX Complete interrupt. It will occur when Transmit Shift Register has been shifted out and is empty. We have empty transmit buffer UDR0…

Continue reading

Basic understanding of microcontroller interrupts

This tutorial is based on Atmega328 microcontroller, which is popular in Arduino boards. So you’ll be able to test all code examples on Arduino as it can serve as general purpose AVR test board with no problem. Understanding Interrupts Probably you won’t be able to find a microcontroller without interrupt capability. These are essential attributes of any modern microcontroller or processor. They may seem confusing and tricky at first glance, but during the time, you will find out that regular MCU operation is impossible without interrupts. Interrupts can be compared to real-life events. Look around – all your activities are full of them. For instance, you are reading this tutorial and find it interesting, so you are all in it. But suddenly, your cell phone rings. What do you do? You remember the last stroke you’ve read and answered the phone. One phone conversation is over; you return to your reading as if nothing happened. Well, this is only one example of interrupt to give some visual clue what interrupts are.

Continue reading