This is simple demo program of reading button state, lighting LEDs, sending information via USART. 8 buttons are connected to Atmega16 port A, 8 LEDs to port B via current limiting resistors. While none of buttons arent pressed there is running light on LEDs performed, but when any of buttons is pressed then LEDs display current 8 buit counter value in binary format. Same value is sent via USART – you can see number in terminal if connected.
AVR microcontroller have EEPROM memory built in which adds a lots of abilities storing constant values, calibration data, and other data that may be read and changed at any time during program flow. Ion couple projects I have used EEPROM for storing last device configuration, eg. in TDA7313 volume, BASS, TREBLE input and output channel information is updated each time when it has been changed. After device si turned ON it reads last saved settings from EEPROM memory and restores last device state. But what happens when you turn device for the first time. It has to read initial values that has to be stored or it wil read 'FF' what may lead to crash or something else. One way is to prepare separate *.eep file with initial EEPROM data and then burn it separately to microcontrollers EEPROM. But this may not be always handy.
Simple USART routines arenot always practical because waiting transmitting buffer to be ready in a simple loop occupies processor. Especially if we don't know when data will be received. Another issue when multiple data bytes has to be sent/received. As microcontroller speciality is interrupts so it is better to use them and this way improve overall performance and energy saving.
In previous article we discussed simple USART implementation. Where microcontroller constantly has to check if there is data received in UDR register. Also for sending it has to check if send buffer is free. Such coding is ineffective as MCU have always to run at 100% performance checking the buffer. In such mode batteries are going down really fast. Why not to set up guardian which would wake the MCU if it have received a byte via USART. In other hand Interrupt mode allows to perform other tasks at full capacity while waiting for USART interrupt.
Lets modify our program to Interrupt driven USART mode
AVR USART module is is one of more complex modules in AVR microcontroller, but it gives ability to interface microcontroller to other hardware like PC or other devices with many flexible features. USART differs from other AVR communication modules by its communication protocol and because it doesn't use separate pin for synchronizing communication. Synchronisation is made within protocol itself. It has several benefits as there are less wires used but there can be communication errors due to incorrect clock settings or due to clock non-stability. In some AVRs there is ability to synchronize communication externally with separate clock pin and run USAR in synchronous mode.
USART interface specification
Most people knows USART as RS232 interface – which is basically in computers. We cannot AVR name USART module to RS232 as there are not RS232 conditions met. One of them are voltage levels. AVR microcontroller operates lets say at 5V level, while RS232 logic levels are different: “0” - from +3V to 25V while “1” - from -3V to -25V. To make USART interface as RS232 there is an adapter needed. It is not hard to build one. You can connect two AVR or other microcontrollers with same voltages without any adapter via USART, but if you are going to communicate to PC, then you must connect through TTL – RS232 converter.