AVR/Examples/IO

From OmgaV Wiki

Jump to: navigation, search

Simple examples

Do-nothing

A basic C-program for an AVR can look like this:

  1. //Include the default AVR io-header
  2. //This file(avr/io.h) holds all of the information about the mcu's ports, setup-registers and so on
  3. #include <avr/io.h>
  4.  
  5. //The normal main-routine
  6. int main(void){
  7. //Our main-loop
  8. for(;;){
  9. //Do nothing
  10. }
  11. //The compiler requires us to return something, even though we will never get here...
  12. return 0;
  13. }

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

Reading & writing from ports

  1. //Include the default AVR io-header
  2. #include <avr/io.h>
  3.  
  4. //The normal main-routine
  5. int main(void){
  6.  
  7. //Set PORTB as output
  8. DDRB = 0xff;
  9. //Set PORTA as input
  10. DDRA = 0x00;
  11.  
  12. //Our main-loop
  13. for(;;){
  14. //Set PORTB as PORTA
  15. PORTB = PINA;
  16. //Observe that when reading values from a port you use the PINx-tag, when writing values, you use the PORTx-tag.
  17. }
  18. //The compiler requires us to return something, even though we will never get here...
  19. return 0;
  20. }

Phew, our first program that actually does something! Notice how much this example resembles the first example.

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

Personal tools