In embedded C, pre defined libraries play very important role in compiling a program and significantly reduce the code size for the same. However, the optimization and the output file size are very much dependent upon the compiler. In AVR Studio predefined library is pretty big and it is a very complex one. To offer portability a huge amount of work has been made in its Header files. What’s so interesting is that, AVR guys has been successful to keep the rules for its embedded C (for Avr controllers) closer to that of the rules for ANSI C. They resolved the register addressing problem by defining macros for the corresponding registers. This offers an opportunity to use them as variables. Now users can concentrate better on the codes rather than the internal hardware configurations of micro controllers. Now let’s have a look on some of the important header files.

Avr gcc library

avr/io.h

Even a single line of code would require this particular header file. It resolves the problem of handling the registers and provides a convenient way to treat the registers as variables. This makes it simple to assign a values to them. E.g. to write data into port B data direction register, the register can be addressed using the variable ‘DDRB’. In code, you can write an expression like this one:

DDRB= 0xff; // 0x prefix stands for an integer represented in hexadecimal format in C

You can treat all other registers in  similar fashion. There are some exceptions and complexities in some cases. But such things are beyond the scope of this chapter. Lets discuss them later.

util/delay.h

In many microcontroller programs, delay loop is essential. This header file defines 2 delay loops.

_delay_us(DELAY_TIME)

                It is a basic function to create a delay of ‘DELAY_TIMEmicro seconds. They are implemented with basic stop and wait delay loop. Variables are not allowed to use as an argument,  instead you can use predefined constants.  And to use this loop, you need to specify the CPU operating frequency (use: #define F_CPU). Let me show you an example:

int main()

{ #define F_CPU 1000000

int DELAY=0;

DELAY=29;

_delay_us(DELAY);

//…

}

——————– OR—————–

int main()

{ #define F_CPU 1000000

int DELAY=0;

for(DELAY=35;DELAY>=0;DELAY–)

{ _delay_us(DELAY);

//…

}

}

Both the code will fail to get complied. But the code below is valid:

int main()

{ #define F_CPU 1000000

_delay_us(29);

//…

}

int main()

{ #define F_CPU 1000000

#define DELAY 35

_delay_ms(DELAY);

//…

}

_delay_ms(DELAY_TIME)

This loop is same as previous. Just one difference, it creates delay in milli seconds. Rest of the facts which are applicable for ‘_delay_us()’ ,  are applicable for ‘_delay_ms()’ too. E.g.

#include

#include

int main()

{ #define F_CPU 1000000

DDRB=0x0f;

PORTB=0x05;

while(1)

{ _delay_ms(1000);

PORTB ~=PORTB;

//…

}

}

avr/interrupt.h

A micro controller has several interrupt sources. Each of them has separate interrupt sub-routine. In ANSI C, there are no interrupt handling schemes. But for micro controllers, interrupts are a matter of special significance! Many programs are very much dependent upon it! So to help users to implement subroutine codes more easily, there is a header file avr/interrupt.h It defines some functions and macros described below.

sei()

This function enables the global interrupt by setting the global interrupt mask.

cli()

This function disables the global interrupt by resetting the global interrupt mask.

reti()

Enables interrupts by setting the global interrupt mask. This function compiles into a single line of assembly code.

 ISR (INTERRUPT_vect)

ISR stands for Interrupt Sub Routine.  Using this macro, users can write up interrupt sub routine associated with interrupts ‘INTERRUPT’. In the place of the argument of the macro, some symbols are supplied. Here, symbols are named after the interrupt vectors they are representing. For a particular micro controller, some specific symbols are valid. For them, look at the AVR GCC reference manual that comes with the AVR Studio. Let’s see an example:

#include

#include

ISR(INT0_vect)

{ PORTB ~=PORTB; }

void initInterrupt(void)

{ cli();

GICR=0x40;

MCUCR=0x03;

sei();

}

int main()

{ initInterrupt();

DDRB=0xff;

PORTB=0x55;

}

 stdio.h

The term ‘stdio’ stands for ‘Standard Input Output’. This library really reduces the code size. But the compiled HEX file grasps lots of space. It defines many functions with the same nomenclature of that ANSI C. The functions have similar objectives that of its ANSI C versions.

 I’m putting the introduction from AVR GCC Reference manual as it is:

“This file declares the standard IO facilities that are implemented in avr-libc. Due to the nature of the underlying hardware, only a limited subset of standard IO is implemented. There is no actual file implementation available, so only device IO can be performed. Since there’s no operating system, the application needs to provide enough details about their devices in order to make them usable by the standard IO facilities. Due to space constraints, some functionality has not been implemented at all (like some of the printf conversions that have been left out). Nevertheless, potential users of this implementation should be warned: the printf and scanf families of functions, although usually associated with presumably simple things like the famous “Hello, world!” program, are actually fairly complex which causes their inclusion to eat up a fair amount of code space. Also, they are not fast due to the nature of interpreting the format string at run-time. Whenever possible, resorting to the (sometimes non-standard) predetermined conversion facilities that are offered by avr-libc will usually cost much less in terms of speed and code size.”

string.h

The header file ‘string.h’ defines some functions to make operations over strings. Like comparing, joining two strings, copying two strings, moving one string to some other location and few more. These functions makes it easy to process strings. In many advanced programming tasks, these predefined functions makes the job easier.

math.h

If you are opting to use some mathematical functions, this header file will reduce the labor required. It defines mathematical functions like sin(), cos(), tan(), exp() and much more.

stlib.h

It include some standard library functions like exit(), calloc(),malloc(),qsort() {qsort stands for quick sorting}, realloc() and some uncommon functions(). These functions are supposed not to be used very frequently. But in special cases, they may turn helpful.

Author

Comments are closed.