AVR/Examples/IO
From OmgaV Wiki
Simple examples
Do-nothing
A basic C-program for an AVR can look like this:
//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 #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; }
This program doesn't really do much however, so lets simply create another program which does a little bit more:
Reading & writing from ports
//Include the default AVR io-header #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; }
Phew, our first program that actually does something! Notice how much this example resembles the first example.
--Oybo 06:06, 31 October 2007 (CET)
