Full Version : How to Program AVR with AVRGCC & AVRLib - Stanford
avr >>C PROGRAMMING (AVR) >>How to Program AVR with AVRGCC & AVRLib - Stanford


AVR_Admin- 05-17-2006
How to Program the AVR with avr-gcc and AVRLib
Center for Computer Research in Music and Acoustics (CCRMA),
Stanford University

QUOTE
How to Program the AVR with avr-gcc and AVRLib
Physical Interaction Design for Music - CCRMA 2004

- Anatomy of a C program for AVR
- C syntax
- Comments
- Variables
- Arrays
- Conditionals
- Looping
- while Loops
- for Loops
- Functions
- Scope

- AVR-Specific Commands
- AVR Registers
- The DDRx Register
- The PORTx Register
- The PINx Register
- Other Registers
- AVRLib
- rprintf
- Program Memory Directives
- Makefiles

Anatomy of a C program for AVR
CCRMA Physical Interaction Design 2004


Link: http://ccrma.stanford.edu/workshops/pid200...ming/index.html

QUOTE ("Cliff Lawson @ May 2006")

Just a note for anyone reading those tutorials that they use deprecated macros such as outp() and cbi() etc. To make the programs work in the modern version of avr-gcc/avr-libc you will either need to:

1) # include <compat/deprecated.h>

or

2) remove the deprecated macros so that:

outp(port, val) simply becomes port = val
x = inp(port) simply becomes x = port
sbi(port, bit) becomes port |= (1<<bit)
cbi(port, bit) becomes port &= ~(1<<bit)


Anatomy of a C program for AVR

The following presents a rough overview and breakdown of a demo program from the avrlib-demos. The code is in demo2 "button.c". Details of C syntax and AVR-specific commands will follow.

The first portion of a C program is usually a bunch of comments that describe the file. Usually this includes the name, author, and date of the file.

CODE
//***************************************
//***************************************
//  AVRLIB-DEMO
//  For avrlib and avrmini development board.
//
//  File:     button.c
//  Author:   Michael Gurevich
//  Date:     Sept. 22, 2002
//  Modified: June 9, 2004


Next are #includes. They tell the compiler where to look for other bits of code you are using that do not reside in this file. They are normally ".h" or header files that contain macros and function prototypes. Before a the .c file is compiled, the contents of the #includes are literally copied into the file.

CODE
#include <avr/io.h>
#include <avr/signal.h>
                                                                               
#include "global.h"
#include "timer.h"


#defines are "macros". Before the program is compiled, the argument of the #define is simply substituted everywhere the name is used. They are handy for giving meaningful names to numbers and for changing a single value that may be used numerous times in a program without having to change every instance.

CODE
#define DEBOUNCE_THRESHOLD 1000


Function prototypes give the name, arguments and return type of functions that will be used later in the program. They can be included in header files or in the .c file before the function is defined.
void checkButton(void);


The main function is literally the main part of the code. There can only be one main function in the final compiled program. The code inside the main function is executed sequentially, one line at a time.
CODE

int main(void)
{
       // set led pins (low 4 bits) as outputs,
       // set button pins (high 4 bits) as inputs
       outb(DDRD, 0x0F);
                                                                               
       // Turn off LEDs by setting the low 4 bits
       outb(PORTD, 0x0F);
                                                                               
       // loop forever
       while(1) {
         // call the checkButton function
         checkButton();
       }
                                                                               
       return 0;
}


Finally, there are function definitions. These are pieces of code that are called from the main function or from other functions, that have a specific functionality that may want to be used over and over.
void checkButton(void) {

CODE
 static u16 buttonDownCounter;
      if (! bit_is_set(PIND,4)) {
         if (buttonDownCounter++ == DEBOUNCE_THRESHOLD) {
           cbi(PORTD,0);
         }
       }                                                                          
       else { //button is not pressed
         buttonDownCounter = 0;
         sbi(PORTD,0);
       }
}


Link to More: http://ccrma.stanford.edu/workshops/pid200...ming/index.html


Forumer™ is Voted #1 Free Forum Hosting provider
Build your own community today with the largest message board hosting company.