Newbie- Trouble getting LED to blink

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey guys, I am making the transition from Arduino to AVR but I am running into a problem in the first tutorial I am following. I am using a Sparkfun ATMega32U4 brekaout board, AtmelStudio6, and an AVRISPMK2 programmer. Here is my code:

#include     // Defines pins, ports, etc to make programs easier to read
#define F_CPU 100000UL	      // Sets up the default speed for delay.h
#include 

int main(){

	DDRB = 1;		      /* enable output on port B/
	
	while(1){

		PORTB = 0;
		_delay_ms(1000);

		PORTB = 1;
		_delay_ms(1000);

	}

	return(0);
}

As far as I understand, this should set all PORTB bits to output, then cycle between low and high on all PORTB pins. However using my multimeter I am unable to detect a change in voltage on any of the PORTB pins.

Thanks for the help!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Update: I misspoke when I said that there is no change in voltage on ANY of the PORTB pins. I discovered that pin PB0 functions correctly, but none of the other PB pins do. Shouldn't my code cycle all the PORTB pins on and off?

Thanks!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Welcome to the Forum.

The port has 8 bits: 7 6 5 4 3 2 1 0

DDRB = 1 assigns a value of 1 to the Port's Input/Output control register. The bits then are set up as:

DDRB: 0 0 0 0 0 0 0 1 in binary

Hence only the Least Significant Bit, Bit 0, was assigned output.

Try setting DDRB = 255 for making the entire port output, giving:

DDRB: 1 1 1 1 1 1 1 1 in binary.

JC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As DocJC said, you need to control each bit separately. Also, the first include should be, #include to match the comment. What you have just defines the interrupt vectors and the related macros. However, as it also includes io.h, everything works.

As you move ahead you'll want to wean yourself off of the _delay_x routines and define F_CPU in the build system ( AS6 or the makefile ) so that it is easier to change and have that apply to all files in a project.

That said... you look to be off to a good start.

Martin Jay McKee

As with most things in engineering, the answer is an unabashed, "It depends."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And you may want to set F_CPU to a sensible value.

And you should set it prior to including any files.

Sid

Life... is a state of mind

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yeah, one 0 is missing in your F_CPU