-AVR-GCC Tutorial

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:

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.

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.

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 RSS - -AVR-GCC Tutorial