Piconomic Atmega128 development board

For this tutorial I have decided to add this Piconomic development board. You can purchase the board if you are from a South Africa.


Otherwise you can build this board by yourself. Piconomic allow to build a development board for non commercial personal use using their Protel 99 SE project files. Download Schematic to view it.

So I made one and now it is good chance to do some C programming by using it.


Program 16 bit AVR timer with WinAVR

AVR 16 bit timer is more advanced timer than 8 bit timer.  It has more features and this allows more accurate program execution timing. 16 bit timer is used when precise signal generation or signal timing measurement needed. As 8 bit timer counter can calculate up to 225 counts the 16 bit timer counter maximum value may reach 65535. In AVR microcontrollers 16 bit timer is Timer1. It contains a 16 bit input capture register (ICR1) and two 16 bit output compare registers (OCR1A and OCR1B). Of course the timer counters register (TCNT1) which is 16 bit long. When programming is ASM language there is special procedure for accessing it (refer to datasheet). While in C language it is done automatically we won’t get to deep into this. Timer1 is controlled by two timer counter control registers (TCCR1A/B). Signals are visible at timer interrupt flag register (TIFR) and interrupts can be individually masked in timer interrupt mask register (TIMSK).

Control AVR 8 bit Timer-Counter2 using WINAVR

AVR ATmaga8 Timer/Counter2 is little more advanced timer counter than Counter0 as it includes more features like:

  • Single Channel Counter

  • Clear Timer on Compare Match (Auto Reload)

  • Glitch-free, phase Correct Pulse Width Modulator (PWM)

  • Frequency Generator

  • 10-bit Clock Pre-scaler

  • Overflow and Compare Match Interrupt Sources (TOV2 and OCF2)

  • Allows Clocking from External 32 kHz Watch Crystal Independent of the I/O Clock

So we have there another register included OCR2, which is used to compare timer counter register TCNT2 register value and react on compare match. Wee see that there is Auto-reload ability what means that timer can be cleared automatically on compare match.

AVR microcontroller interrupts handling using WINAVR

Microcontrollers without interrupts are almost worthless. Interrupt is called what interrupts normal program flow. Interrupts are nothing more than subroutines that put on hold normal program flow while they are executed. After interrupt subroutines are finished they return to normal from last point. Those subroutines are called interrupt handlers that are called by some event like toggle external pin or some register value overfilled like timer overflow and so on.
Why interrupts are so important? For instance without interrupts you would have to do loops in order to check if one or another event occurred. This operation is called polling. But pooling has many disadvantages like program have do loops taking resources from other task may be done. This is why microcontroller comes with multiple interrupt sources. Instead of checking events microcontroller has ability to interrupt normal program flow on event and jump to interrupt service subroutine and then get back to normal program flow.

VMLAB is free virtual oscilloscope for AVR

VMLAB is one of well known free simulators that support AVR microcontrollers. You may get free version of VMLAB tools from amctools. The simulation is done not in real time as computer resources wouldn’t allow this, but all timings of processes viewed in virtual oscilloscope are tied to real world values.

VMLAB is a project workspace where special scripting language describes the initial conditionas and circuit showing to virtual simulator connections between hardware and microcontroller. Few pre-built examples you may find in folders C:\VMLAB\AVR_demo and C:\VMLAB\ WinAVRdemo (for default installation in C:\VMLAB\ folder).

VMLAB quite rich in its hardware support: Resistors, Grounded capacitors, Interactive switches / keys, LED diodes, Pulsed voltage source, Sine wave voltage source, Slider dependent voltage source (interactive), Non-ruturn-to-zero (NRZ) generator (interactive), Operational amplifier, Comparator, 2 inputs NAND gate, 8 bits D to A converter, RS232 based TTY (interactive), LCD module, I2C monitor (interactive), Interactive keypad 4x4 Multiprocess-dedicated: External Input, External Output. Actually you can do a wide range of simulations with your virtual embedded system before taking it to the real world. VMLAB also has a powerful scope feature where you can view voltages on pins or even some internal microcontroller register values like ACO, TIMOVF.

Save constants to AVR EEPROM using WinAVR

AVR microcontrollers like many any other Harvard architecture MCU's are ships with some amount of EEPROM (Electronically Erasable Read-Only memory ) memory. This type of memory allows developers to store program parameters, constants, menu strings etc. EEPROM memory is good that you can read like one byte and store modified, while FLASH memory usually is written in pages.

In this article I am going to show how to store data to EEPROM by defining a particular variable types.

Working with AVR microcontroller FLASH memory using WinAVR GCC

Because AVR microcontrollers are Harvard architecture they have separate address spaces for different memory types: SRAM, FLASH and EEPROM. Each of these memories have their own purposes. If data will not change during all time of program execution, then it should be stored as one type of memory like EEPROM otherwise if data will be used frequently and will change during program execution, then it should be stored in SRAM memory. Where to store data decides program developer. One thing you should note, that working with different types of memories require different operations how they are accessed.

Usually when define variables in C language like int a – compiler automatically stores it in to the SRAM. But if you want you can place constants in EEPROM or even FLASH memories.

As I mentioned no specific operations aren't needed to work with variables sored in SRAM. Lets go through other two memory types FLASH and EEPROM.

Typecasting in AVR-GCC

I am sure- typecasting is one of common practices in embedded C when converting one variable to another. As AVR microcontrollers are 8 bit systems and sometimes we operate with 16, 32 or even 64 bit length variables we have an ability to change like 16 bit integer value to 8 bit char and so on. This operations is so called typecasting. So it is important to understand this operation in order to use it properly and get desired result.

Enumeration of variables

This is similar to #define preprocessor where you can describe a set of constatnts. Using preprocessor we use:

#define zero 0

#define one 1

#define two 3

But there is an alternative of using enumerating using keyword enum:

enum (zero=0,one, two); //zero=0, one=1; two=2

By default enumeration assigns values from 0 and up.

Now you can use enumeration like in following example:

More about C types in AVR-GCC

Char types

As it is important to understand variable types in AVR-GCC lest take a deeper look at this.

Choosing proper variable type sometimes may be crucial in designing embedded software as microcontroller resources are limited. Choice of proper variable type may save lots of time and space of your program.

What is common variable type in 8 bit system? Of course byte or so called char type. Char name comes from word “character” because char type usually is used to store character symbols (ASCII). Char type can have values from -128 to +127 while unsigned char may have values from 0 to 255 256. Some compilers may use byte type instead of unsigned char.


Subscribe to WinAVR AVR-GCC Tutorial RSS