Position: Index > Unclassified >

AVR GCC Library of AVR Studio

2018-01-30 05:03  
Declaration:We aim to transmit more information by carrying articles . We will delete it soon, if we are involved in the problems of article content ,copyright or other problems.

In embedded C, pre defined libraries play very important role to compile a program and significantly reduces the code size for particular. 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, there a huge amount of work has been made in its Header files. Plus, they have been successful to keep the rules for its Embedded C closer to the ANSI C. They resolved the register addressing problem by defining Macros with the same of corresponding registers. This offers an opportunity to use them as variables. Now user can concentrate better on the codes rather than internal hardware configurations of the micro controllers. Now let’s have a look to 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 value 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 other registers in the similar way. There are some exceptions and complexities in some cases. But for simplicity, it has not been discussed.

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_TIME’ micro seconds. They are implemented with basic stop and wait delay loop. Variables are not allowed to use as an argument, 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 delays in mille seconds. Rest of the facts whom are applicable for ‘_delay_us()’ , ?are applicable for ‘_delay_ms()’ too. E.g.

#include<avr/io.h>

#include<util/delay.h>

int main()

{??? #define F_CPU 1000000

???? DDRB=0x0f;

???? PORTB=0×05;

???? 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 is no interrupt handling schemes. But for micro controllers, interrupts are 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. 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 to interrupt ‘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<avr/io.h>

#include<avr/interrupt.h>

ISR(INT0_vect)

{??? PORTB ~=PORTB;? }

void initInterrupt(void)

{??? cli();

???? GICR=0×40;

???? MCUCR=0×03;

???? sei();

}

int main()

{??? initInterrupt();

???? DDRB=0xff;

PORTB=0×55;

}

?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 functionalities that of its ANSI C versions.

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

“This file declares the standard IO facilities that are implemented inavr-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 theprintfconversions that have been left out). Nevertheless, potential users of this implementation should be warned: theprintfandscanffamilies 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 easy to process strings. In many advance programming, these predefined functions turn the job easier.

math.h

If you are opting to use some mathematical functions, this header file will reduce 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 use very frequently. But in special cases, they may turn helpful.


Reprinted Url Of This Article:
http://www.circuitstoday.com/avr-gcc-library-of-avr-studio