Actions

AVR Lesson: Output Pins I: Difference between revisions

From HacDC Wiki

Line 11: Line 11:


== The Code ==
== The Code ==
<pre><nowiki>


/* Blinker Demo */


#include <avr/io.h> /* Defines pins, ports, etc */
#define F_CPU 1000000UL   /* Sets up the chip speed for delay.h */
#include <util/delay.h> /* Functions to waste time */


#define LED PB4 /* Defines pin PB4 for the LED.  I
often include a bunch of the circuit
info in the code this way, which
makes porting the code to another
chip easier and reminds you of how to
hook it up. */
int main(void){
  DDRB = _BV(LED);       /* Data Direction Register B:
      writing a one to the bit
      enables output.  More on the
      _BV() macro in the next
      lesson.*/
  while(1){ /* the main loop, from which we never return */
    PORTB = _BV(LED); /* Turn on the LED bit/pin in PORTB */
    _delay_ms(400); /* wait */
    PORTB &= ~_BV(LED); /*  Turn off the LED bit/pin in PORTB */
  _delay_ms(400); /* wait */
  }
  return(0); /* never reached */
}
</nowiki></pre>


== Trouble? ==
== Trouble? ==

Revision as of 22:41, 28 May 2008

Goals

In this lesson, you'll program the micro-controller version of "hello world", letting you fire up your programmer, compiler, chip, LED, and test them all out. You'll learn how to setup and use pins for output, and along the way get some exposure to AVR-specific coding practice.


The Circuit

The Code


/* Blinker Demo */

#include <avr/io.h>		/* Defines pins, ports, etc */
#define F_CPU 1000000UL	   /* Sets up the chip speed for delay.h */
#include <util/delay.h>		/* Functions to waste time */

#define LED PB4			/* Defines pin PB4 for the LED.  I
				 often include a bunch of the circuit
				 info in the code this way, which
				 makes porting the code to another
				 chip easier and reminds you of how to
				 hook it up. */

int main(void){

  DDRB = _BV(LED);		      /* Data Direction Register B:
				       writing a one to the bit
				       enables output.  More on the
				       _BV() macro in the next
				       lesson.*/
	
  while(1){			/* the main loop, from which we never return */

    PORTB = _BV(LED); 		/* Turn on the LED bit/pin in PORTB */
    _delay_ms(400);		/* wait */

    PORTB &= ~_BV(LED);		/*  Turn off the LED bit/pin in PORTB */
   _delay_ms(400);		/* wait */

  }

  return(0);			/* never reached */
}

Trouble?

Programmer woes:

Is the LED in the correct polarity? (Positive to pin PB4, negative to GND).



Back to AVR Tutorial