-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.

Control AVR 8 bit Timer-Counter0 using AVR-GCC

Timers are very important part of embedded systems. Timers can make your life much easier if used properly. Basically one good thing is that timers runs independently to main program flow. You just set up the timer and let it do its job while your main program may run and do its job. Timers are really easy to use. I will go through Atmega8 timers. Other AVR microcontroller models have similar timers. In this thread we will go through AVR 8 bit timers.

Atmega8 has two 8 bit timers/counters and one 16-bit timer counter with various abilities.

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.

Accessing AVR microcontroller ports with WinAVR GCC

All AVR ports have Read-modify-write functionality when used as genera I/O ports. Direction of separate port pin can be changed. Each pin buffer has symmetric capability to drive and sink source. Pin driver is strong enough to drive LED directly , but it is not recommended. All port pins have selectable pull-up resistors. All pins have protection diodes to both VCC and GND.

Each port consists of three registers DDRx, PORTx and PINx (where x means port letter). DDRx register selects direction of port pins. If logic one is written to DDRx then port is configured to be as output. Zero means that port is configured as input. If DDRx is written zero and PORTx is written logic “1” then port is configured as input with internal pull-up resistor. Otherwise if PORTx is written to zero, then port is configured as input but pins are set to tri-state and you might need to connect external pull-up resistors.

If PORTx is written to logic “1” and DDRx is set to “1”, then port pin is driven high. And if PORTx=0, then port is driven low.

Pages

Subscribe to RSS - -AVR-GCC Tutorial