Variables in embedded C language

What are variables in C language. Variables are simple keywords which are defined by the values. Values can be changed. Variables are like a boxes with some size where values like apples can be put in. So variables can be various forms and sizes so called variable types.

Variable type is defined by a reserved word which indicates the type and size of variable identifier:

unsigned char my_char;

long int all_my_numbers;

int number;

Why do we need variables? The basic answer is that memory is limited and compiler needs to know much space to reserve for each variable. So the programmer needs to specify the variable type and its size by using one of reserved words from the table:

The very basics of C

C language is function based programming language. C program itself is a function. Usually parameters to C function are passed as arguments. Function consists of a name followed by the parentheses enclosing arguments or an empty pair of parentheses if there are not arguments required. If there are several arguments, they are separated by commas.

The mandatory part in C program is main function. This function must be included in every program because this is a first function which is run after execution of program.

Lets take an example:

Why C instead ASM

ASM is a specific language as it is a low level programming language. It is mnemonics to a mashine codes. It takes tons of time to develop embedded programs in ASM language. Now even 8 bit microcontrollers are more capable as they were earlier. The program memories are going up to megabyte(s). Programs becoming more complex, functionality grows up. This is one reason to use higher level programming languages like C.

If using C language you do not have to go into details how processor works. You don't have to think about hardware logic. It is better to leave this work to C compiler which may help you to avoid bugs in silicon level.

AVR microcontroller memory map


AVR microcontrollers are Harvard architecture. This means, that in this architecture are separate memory types (program memory and data memory) connected with distinct buses. Such memory architecture allows processor to access program memory and data memory at the same time. This increases performance of MCU comparing to CISC architecture, where CPU uses same bus for accessing program memory and data memory.

Atmega series of AVR microcontrollers


As all AVR microcontrollers Atmega microcontrollers are 8 bit RISC microcontrollers. Their core are the same as tiny series, just they are more powerful and has more peripheral devices built in like:

Tiny series of AVR microcontrollers


AVR microcontrollers are 8-bit on chip systems. AVR Tiny are very impressive microcontrollers. They small from outside, but inside they are not so small. In side is the same AVR RISC architecture which is compatible to all AVR microcontrollers. They have In system Programming (ISP) Flash memory, internal EEPROM and RAM, timers, interrupt sources, analog comparator, ADC and more.

Development tools for Tiny microcontrollers are the same as for other AVR MCU.

The biggest advantage of tiny microcontrollers is that they are extremely economical.

General properties of AVR microcontrollers


This applies to all types of AVR microcontrollers because they all use same AVR core technology. So lets go through some of them:

  • Ability to work at 1MIPS/MHz (MIPS – Millions Instructions Per Second);

  • On chip Flash memory (now tiny series have up to 8kB, mega up to 256kB) with write cycle not less than 1000 times;

  • On chip RAM memory (tiny series up to 512B, mega up to 8kB);

  • On chip EEPROM memory (tiny series up to 512B, mega up to 4kB) with write cycle not less than 100000 times;

  • Working frequencies up to 20MHz;

  • Protection fuses from unintentional reading and modification of program and data memories;

  • Ability to program in system – directly on board using ISP cable;

  • Four types of clock generators: Internal generator, RC generator of clock cycles, Internal generator with external crystal resonator, and External synchronisation;

  • Two or three power down modes;

  • Some microcontroller can work at very low voltages like 1.8V;

Why AVR Microcontroller?


AVR microcontroller are 8-bit on chip system with RICS (Reduced Instruction Set Computer) command system. The most of commands are performed in one clock cycle.

In AVR microcontroller reading next command is done during execution of previous command. So the overal number of commands in 1second is almos equal to working frequency.

AVR microcontrollers are manufactured using high quality CMOS technology, contains EEPROM memory inside the chip and differs from other microcontrollers that they require less power in higher frequencies.

General Information On AVR microcontrollers


AVR is quite new product in microcontroller world. But they are spreading in light speed. From 1996 things really changed. Many of microcontrollers become obsolete like AT90 “classic” type. They reached end of line. They were substituted by other more advanced series. In 1999 AVR microcontrollers split in to three types: “tiny”, “classic” and “mega”. Now seems to be that “classic” type is disappearing – they are weather obsolete or reached EOL (end of line). They were changed by more powerful “tiny” or “mega” series like AT90S2313 were substituted by compatible Attiny2313.

Installing WINAVR

First of all download WinAVR. You can download newest stabile version. For all recent versions installation is mostly the same. Installing and Configuring WinAVR - Touches on everything, although 2 years out of date.


Subscribe to WinAVR AVR-GCC Tutorial RSS