Arduino timer interrupt tutorial

Operating microcontrollers without interrupts is very inefficient. One of common used microcontrollers among hobbyists id AVR. Starters probably would go with Arduino board where AVR Atmega168/328 is used. It doesn’t matter weather you are programming in Arduino style or plain C it is advised to use interrupts. EngBlaze has written a tutorial on programming AVR timers using interrupts. tutorial thoroughly goes through all information you may need including: what is timer, types of timers and finally running them in various modes. Once timers are set most of work is done in hardware with short interrupt routines where changes can be made. If you interested more about timers we have written couple tutorials on programming them using C. Continue reading

Programming AVR USART with AVR-GCC. Part 2

In the previous part of the USART tutorial, we have 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. 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 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 datasheet you will find that USART0 in Atmega328 has three interrupt sources: TX Complete; TX Data Register Empty; RX Complete. 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… Continue reading

Basic understanding of microcontroller interrupts

Earlier I’ve made a mistake by referring this tutorial to first AVR family microcontrollers like Atmega8/16/32/64/128. But AVR is being continuously changed with various new tweaks and updates like enhanced pin control or different register names. Since now tutorial will be 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 normal 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.… Continue reading