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.

Pointers to structures in embedded C

Sometimes you might like to manipulate the members of structures in more generalized way. This can be done by using pointer reference to structure. Sometimes it is easier to pass pointer to structure than passing entire structure to function.

Structure pointer can be declared very easily:

struct structure_tag_name *variable;

For instance:

AVR GCC Structures

Basically Structures are nothing more than collection of variables so called members. Structures allows to reference all members by single name. Variables within a structure doesn't have to be the same type. General structure declaration:

struct structure_tag_name{
	type member1;
	type member2;
	type memberX


struct structure_tag_name{
	type member1;
	type member2;
	type memberX
} structure_variable_name;

Chip45boot-free AVR bootloader


Why this bootloader is different? This is based on standard Motorola s-record compatible file which can be easily generated from Intel HEX. Firmware can be transferred via standard UART of any AVR microcontroller. File can be transferred using any Hyper terminal program or your own program. Just note, that it is necessary to have a delay of 250ms after each line transmitted. This can be set with almost all Hyper terminal program.

Download process can be automatized by using command line tool, which can be called from Makefile or another application.

About GNU Compiler Collection-GCC

GNU Compiler Collection or GCC is free and have been around for a very long time. GCC is supported bu volunteers. This means there is a big community where you can find help for free and from different sources – but response can take longer or you want get at all.

But there is several significant advantages of GCC compiler collection:

  • GCC is available for many different processors like x86, AVR, ARM;

  • Code is very mature and works well. It is also well documented.

Program Flow In Embedded C

Program Flow and control is a control method of your program. For example Loop constructions control repeated execution of repeated program segments where control is taken by control parameter. In this article we will go through if/else switch/case statements and loop sentences like while, do while, for.

While statement

As I mentioned three looping sentences available in C language one of them is While sentence. Lets take an example:

Run WinAVR from USB media


This is good news for those who more than one working places. It is not convenient to install separate WinAVR development toolsets in separate computers and care about them. There is a solution. Now you can run your WinAVR suite directly from USB memory stick or other portable media without prior installation. You can download it from here Portable WinAVR its Free.

Whats included in package:

C language Operators and expressions

The main thing what microcontrollers does is operates with data. There are four main operations that microcontrollers does: adds, abstracts, multiplies and divides (+,-,*,/). division can be split in division “/” and modulus operation “%”. For instance i1/i2 is integer division.

Other part of operators is Relation operators. They are used for boolean conditions and expressions. Expressions with these operators return true or false values. Zero is taken as false and non zero value as true. Operators may be as follows: <, <=, > >=, ==, !=.

The priority of the first four operators is higher than that of the later two operators. These operators are used in relational expressions such as:

Constants in C language

Constant value is understandable as non changeable value like PI=3.141592... value in math. Usually you use constants in your programs, but don't realize that they are constants. For instance:

x=x+3; The number 3 is a constant which will be compiled directly in addition operation. Constants can be character or string. Like in function printf(“Hello World\n”); “Hello World” is a string constant which is placed in program memory and will never changes.

It is usually recommended to declare constants by using identifier with reserved word const:

const int No=44;


Subscribe to WinAVR AVR-GCC Tutorial RSS