Tip on storing initial values in EEPROM

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.

Interrupt driven AVR USART communication

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

Programing AVR USART module


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.

AVR-GCC 4 bit and 8 bit LCD library

Standard alphanumeric LCD display controlled by 74HC164 LCD controller can accept 8 bit data bytes or 4 bit nibbles. Earlier my 4 bit and 8 bit LCD libraries were split in separate files as they were used in different projects. Now they are merged in to one library where simple logic is implemented to select 4 bit or 8 bit library just by modifying only three lines of code.

In the library header file there is line added:


//Uncomment this if LCD 4 bit interface isused


#define LCD_4bit



what allows to select different LCD modes by commenting and uncommenting this line. Also don't forget to select proper ports and pins where LCD is connected:

Avrwiz-New C code generator for WinAVR

Some good news for those who are tired of writing same program parts every time. Most time consumption is during initial code writing. The guy named tcg in avrfreaks have decided to write code generating tool which could robust most common tasks.

Current version of program is v0.009 (early beta stage of program) but it already support baud calculator, timer calculator, multitasking generator, interrupts, ports and more. But there are several thing to be done like TWI, USI. As Author states there is lots of testing to be done. Project is open for new ideas and suggestions.

AVR-GCC code compatibility

AVR-GCC compiler is changing during the time. There are so many things changed from recent versions of WinAVR to now. C language itself doesn't change much, but functions that deal with hardware all the time. Earlier even set up interrupt service routines would take some brain, now it is simplified to just one simple function ISR(). All necessary job is handled in macro level.

Another very important issue is operations with I/O bits. Functions with bits have changed since old versions of WinAVR. Compiler just throws errors that functions cannot be found. One solution is to write your own macros to support them or change the code with new syntax. Or use special Patch program which returns old functions to new WinAVR. You can download this patch from myrobot.ru. This patch returns functions that are not supported since WinAVR20050214. This is handy when you are trying to compile older source versions and do not want to change the code and do not want to download older WinAVR tools.

AVR 4-bit LCD interface library

Standard alphanumeric LCD display controlled by 74HC164 controlled can accept 8 bit data bytes or 4 bit nibbles. Using 4 bit interface may give few benefits like you can save 4 microcontroller pins and use them for different purposes, or use small pin count Microcontrollers to control LCD like AVR ATtiny series.

When using 4 bit mode, only four data-lines of LCD (D4...D7) are used. So first is high nibble sent and then lower nibble has to be sent in order to form single byte. This way there are two cycles used to form one data or command byte. Earlier I have been using LCD library from Procyon AVRLIB, but I wasn’t satisfied with it as it generated a lots of HEX code because of many functionality included.

AVR Comparator C programming example

Lets write simple example on using AVR comparator module. Program is written in C and compiled with AVR-GCC compiler(WinAVR). Program example illustrates the case when Positive Comparator input is connected to Internal voltage regulator 1.23V by setting ACGB bitin ACSR register. Also as negative comparator input is taken ADC3 input via Multiplexer. As indicator LED is connected to PORTD0 pin. If input voltage is lower than 0.23V then Diode is OFF and if voltage exceed 1.23V diode lights ON. Diode state is changed with ANA_COMP_vect interrupt service routine on ACO bit toggle in ACSR register.

Working program code:

AVR analog comparator review

Analog comparator is pretty simple peripheral, but it can give big benefits. Comparator module is included in all Mega series of AVR microcontrollers. Simply speaking Comparator allows to compare voltage values applied to two pins of microcontrollers. Result of comparison is a logical level which can be read by program(ACO- Analog Comparator Output bit). There is also ability to generate an interrupt which depends on comparator resulting level. And more- timer T1 can capture this level what allows measuring the signal length of analog signals.

In order to use pins as comparator inputs they have to be prepared and configured as input(DDRx=0) and pull-up resistors mus be disconnected (PORTx=0). In some models like Atmega48x/88x/168x, Atmega165x, Atmega325x,3250x,645x/6450x and Atmega640x/128x/1281x/2560x/2561x there is ability to disconnect digital buffers of AIN0 and AIN1pins that allows to use these pins only for analog signal input – this minimises overal power consumption.

Digital buffers can be disconnected by writing “1” where bits AIN0D and AIN1D are in register DIDR1:

Using standard Qsort function in AVR-GCC

Qsort function is a generic sorting function which allows to choose your own sorting criterion. Not often but sometimes you may need to have some sort of array of data. Dont go to much in to it what data type you will be storing in your embedded application but lets say it is a simple notebook based on AVR. Lets analyse simple program how data can be sorted by using function pointer.

Lets start with sample data. In my example I will use simple structure of four fields:

struct persons

	char First_Name[31];
	char Last_name[50];
	uint8_t Age;
	uint8_t Address[50];


Subscribe to WinAVR AVR-GCC Tutorial RSS