Using sprintf function for float numbers in AVR-GCC

Usually it is not recommended to use float numbers in while writing AVR programs,because AVR core doesn't have floating point arithmetics built in so floating point arithmetics is emulated by software. Sometimes you don't need to declare variables as float if operations are pretty simple like division or multiplying by 2, 4, 8... this can be replaced by byte shift operation (byte<<1)=byte*2. Of course it depends on level of programming and not always you can run away from floats. And you don't have to. If you code will fit to Program memory and execution speed isn't crucial then use floats or double number formats.

Lets see what happens when building simple program with float numbers:

KontrollerLab IDE development tools for AVR under Linux

Good news for Linux users. The KontrollerLab team is announcing the 0.7.0 version of KDE based microcontroller IDE. Besides some bug fixes, there are new features implemented, such as a LCD wizard, a wizard for painting user-defined characters on LCDs, and a seven segments display wizard. Additionally, there are binary packages for (K)Ubuntu and Fedora Core 5 and 6 available for download from the project's sourceforge website

KontrollerLab is GPL and fully-featured editor which includes serial terminal for debugging, uses AVR-GCC compiler, uisp for serial downloading and avrdude programming software.

Fast start with AVR-GCC In AVRStudio environment

AVRStudio4 is capable to integrate AVR-GCC compiler by using built in plug-in. AVRstudio is a powerful tool which has assembler compiler- debugger, programmer, etc. Integration of AVR-GCC to it makes this tool much more powerful and more complete playground for developer. Integration of AVR-GCC is done by plug-in automatically. Plugin detects AVR-GCC - you don’t have to care about it. After this wee get FREE convenient user interface, automatic makefile generation, visual debugging with processors register view, and chip flasher.


Setting up working environment is simple. First of all download WinAVR20060421 and install in your PC. Then download AVRStudio latest Version 4.12 and latest service pack Service Pack 3 and install it in your PC.

You are set. Lets create sample project. Open AVR Studio and select menu->Project Wizard->New project.


AVR USART explained

The Universal Synchronous and Asynchronous serial Receiver and Transmitter (USAR) is powerful and useful interface in many projects. It is usually used for code debugging and other user interaction while sending and receiving data form PC. Lets analyse how USART works on Atmega8. The main features of Atmega8 USART are:

  • Full Duplex Operation with Independent Serial Receive and Transmit Registers);

  • Asynchronous or Synchronous Operation;

  • Master or Slave Clocked Synchronous Operation;

  • High Resolution Baud Rate Generator;

  • Data OverRun Detection;

  • Framing Error Detection;

  • Supports Serial Frames with 5, 6, 7, 8, or 9 Databits and 1 or 2 Stop Bits;

  • Odd or Even Parity Generation and Parity Check Supported by Hardware;

  • Multi-processor Communication Mode;

  • Double Speed Asynchronous Communication Mode;

  • Noise Filtering Includes False Start Bit Detection and Digital Low Pass Filter;

  • Three Separate Interrupts on TX Complete, TX Data Register Empty and RX Complete;

As USART is pretty complex peripheral I will just go through it. Detailed information may be found on data-sheets!

USART is separated in three modules: Clock generator, Transmitter and Receiver.

Serial Peripheral interface-SPI of AVR microcontrollers

This is high speed synchronous data transfer between microcontrollers or other peripheral devices. AVR SPI interface:

  • uses three wires for synchronous data transfer;

  • can operate as Master or Slave;

  • allow LSB or MSB first bit transfer;

  • support multiple bit rates;

  • has end of transmission interrupt;

  • has write collision flag protection;

  • wakes up from idle mode;

  • supports double speed master SPI mode

Do not ignore C compiler preprocessor

You maybe don't know or probably didn't think about this but you are using program preprocessing before compiling it. As I said before compiling I mean, that each time you are compiling your project, C compiler prepares program file to be ready for compilation. Preprocessor includes other files to main file, defines symbol constants and macros, prepares conditional compilation of code. All preprocessor tasks are marked with ampersand symbol “#”. Lets go through most of directives of preprocessor:

Endless loops in embedded programs


A series of instructions that are constantly repeated. Also called an "infinite loop," it can be caused by an error in the program or be intentional. For example, a screen demo on continuous replay just keeps repeating itself.

Probably you have noticed that many programs have similar structure like:


4x4 keypad example using AVR-GCC C language

This is as simple routine how to read 4x4 keypad keys using AVR-GCC language. The keypad is connected to AVR microcontroller 8 bit port. In this example it is B port. You can change ports depending on your needs – this is only an example ant it is not the only way to this.


More ways to use AVR Flash memory using WinAVR

We've been talking about controlling AVR Flash memory in previous post. We talked how ease we can write and read from flash memory using PROGMEM atribute. But lets see more ways of usage Flash memory in your applications.

Sometimes when you write a code and in some places you are just sending strings to LCD or USART using simple expression like:

SendSTR(“This is a string”);

This seems very simple but not good way to use strings, because any way this string is being hardcoded in to flash memory, but compiler is forced to load this string to SRAM during microcontroller initialization before main() routine starts. And this string stays in RAM. This is ok for one or two strings but when you use significant amounts of strings this way then you really waste your RAM by holding all static unchanging data.

Example of using sections in WinAVR

Before you start reading this

it is recommended to read


described in avrlibc documentation

to get picture what it is all about.

In this article you will see how

to create projects by memory using sections

for solving many tasks before

main program starts. Let's say wee need: 

  1. To test SRAM after startup;
  2. Wait until external device will be ready;
  3. and then Prepare I/O ports;

Bellow is a programm with comments:


Subscribe to WinAVR AVR-GCC Tutorial RSS