AVR/Examples/IO

From OmegaV-wiki
Jump to: navigation, search

Simple examples

Do-nothing

A basic C-program for an AVR can look like this: <source lang=c> //Include the default AVR io-header //This file(avr/io.h) holds all of the information about the mcu's ports, setup-registers and so on

  1. include <avr/io.h>

//The normal main-routine int main(void){

  //Our main-loop
  for(;;){
     //Do nothing
  }
  //The compiler requires us to return something, even though we will never get here...
  return 0;

} </source>

This program doesn't really do much however, so lets simply create another program which does a little bit more:

Reading & writing from ports

<source lang=c> //Include the default AVR io-header

  1. include <avr/io.h>

//The normal main-routine int main(void){

  //Set PORTB as output
  DDRB = 0xff;
  //Set PORTA as input
  DDRA = 0x00;
  //Our main-loop
  for(;;){
     //Set PORTB as PORTA
     PORTB = PINA;
     //Observe that when reading values from a port you use the PINx-tag, when writing values, you use the PORTx-tag.         
  }
  //The compiler requires us to return something, even though we will never get here...
  return 0;

} </source> Phew, our first program that actually does something! Notice how much this example resembles the first example.

--Oybo 06:06, 31 October 2007 (CET)