| Using The AVR LibC Library | |
|---|---|
| Description: The AVR LibC library provides a subset of the standard C library, for use while developing programs for Atmel AVR microcontrollers. This tutorial provides details on using the AVR LibC library in your projects. | |
|
|
| Using The AVR LibC Library | |
|
Copyright (C) 2003,2004,
Psychogenic Inc. All Rights Reserved.
The AVR LibC library provides a subset of the standard C library, for use while developing programs for Atmel AVR microcontrollers. This tutorial provides details on using the AVR LibC library in your projects. TOCAVR LibC SetupBefore you can use the AVR LibC library, it needs to be installed along with a few other programs (the compiler and assembler for AVR targets). The installation of all requirements is described in detail on the Free software AVR development toolchain page. AVR LibC Programming NotesLibrary FunctionsIn addition to the AVR specific functions (stuff like watchdog timer setup), a number of the standard C library functions are supported by AVR LibC: string functions like strcpy, strlen, etc. math functions like sin, cos, sqrt and pow, I/O functions like printf and scanf and more. You must remember though that the entire standard library isn't available--certain functions have yet to be implemented (perhaps you can help?) while others may not be possible or even make sense on the platform. Check the AVR LibC docs before you code. ROM StringsIf you are using many constant strings in your application, for instance while using an LCD display or speaking to a serial port through the UART, you'll want to save RAM by storing the constant strings (and leaving them) in the comparatively vast ROM. You can do this thanks to the AVR LibC Program Space String Utilities. By adding an #include <avr/pgmspace.h> to your C source file, you gain access to functions which allow access to data stored in the device's program space (the flash memory ROM). With the program space string utilities, you can declare and manipulate PGM_P variables: pointers to strings in program space. In addition to low level pgm_read_* functions, they provide a number of _P equivalent string manipulation functions, such as
Function Register AccessLots of the microcontroller programmer's work involves dealing with I/O and other function registers, often on a bit-by-bit basis. AVR-LibC provides two methods by which this may be accomplished. You may use specific IO instructions on IO address space, e.g. outb(PORTB, 0xFF); Or you can choose to use the symbolic address directly: PORTB = 0xFF; The former method may be preferable if you plan to compile the program using other AVR platform C compilers. The latter has the advantage of clarity. For instance, here are two equivalent instructions, taken from the AVR LibC documentation: outb(DDRD, inb(DDRD) & ~LCDBITS); DDRD &= ~LCDBITS; Interrupt APIThe vector table, described above, is set to point interrupt routines with predetermined names. The library provides default interrupt routines, which get used unless overridden. If you wish to provide an interrupt routine for a particular signal you must, in addition to any required AVR setup, create the function using one of the SIGNAL() or INTERRUPT() macros, along with the appropriate signal names. There are a number of preset signal names, such as
To create a interrupt routine, select a macro-signal combination and
#include <avr/signal.h>
INTERRUPT(SIG_OVERFLOW0)
{
/* Your overflow handler routine */
}
For the same overflow signal, you could have written
#include <avr/signal.h>
SIGNAL(SIG_OVERFLOW0)
{
/* Your overflow handler routine */
}
The difference between the two is the state of the global interrupts as your enter the function. In the case of INTERRUPT(), global interrupts are initally enabled whereas for SIGNAL() they are disabled (your routine can not be itself interrupted). Things to remember when using interrupts:
A number of constants and function registers are used, the MY... values are user defined while the others are set through the io.h header. The important thing here is to notice that it is often important to perform a good deal of initial setup before a peripheral and its interrupts may be used. #include <avr/interrupt.h> /* ... */ /* enable global interrupts */ sei(); |
|
| Level: | Article |
| Additional Article Data | |
| Level: | Article |
| Comments |
|---|
|
|
|
The comments are owned by the poster. We aren't responsible for their content.
|
| Jump to section |
|---|

