NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Project Help and Ideas » NerdKits with ATmega168 Port pin assignment

February 22, 2011
by vgphotog
vgphotog's Avatar

Before I go any farther with my Nerdkit I would like to understand "pin assignment". I have tried to changed the pin used to flash the led. The code is from the kit project. I have tried changing the port form "C" to "B" in the attached code. The program will compile and load but the led on PORTB pin 4 will not flash. I copied the code and changed only the port letter. Why does it not work? I have also tried changing to a different pin on portc that does not work either. What am I missing?
The modified code follows with the original code commented out and the changed code following. Before I go any farther with my Nerdkit I would like to understand "pin assignment". I have tried to changed the pin used to flash the led. The code is from the kit project. I have tried changing the port form "C" to "B" in the attached code. The program will compile and load but the led on PORTB pin 4 will not flash. I copied the code and changed only the port letter. Why does it not work? I have also tried changing to a different pin on portc that does not work either. What am I missing?
The modified code follows with the original code commented out and the changed code following.
The program loads and works correctly as originally written.

// for NerdKits with ATmega168
// hevans@nerdkits.edu

#define F_CPU 14745600

#include <avr/io.h>
#include <inttypes.h>

#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"

// PIN DEFINITIONS:
//
// PC4 -- LED anode

int main() {
  // LED as output
  //DDRC |= (1<<PC4);
  DDRB |= (1<<PB4);

  // loop keeps looking forever
  while(1) {
    // turn on LED
    //PORTC |= (1<<PC4);
    PORTB |= (1<<PB4);

    //delay for 500 milliseconds to let the light stay on
    delay_ms(500);

    // turn off LED
    //PORTC &= ~(1<<PC4);
    PORTB &= ~(1<<PB4);

    //delay for 500 milliseconds to let the light stay off
    delay_ms(500);

  }

  return 0;
}
February 22, 2011
by SpaceGhost
SpaceGhost's Avatar

Is the anode (+) of your LED connected to pin 18, and the cathode (flat side) of the LED connected to the negative rail of your breadboard?

Your code as it is written, should blink an LED connected to pin 18 on your MCU.

February 22, 2011
by amartinez
amartinez's Avatar

It worked for me. Your code looks good. I used PB1.

February 22, 2011
by mongo
mongo's Avatar

An easy way to tell anode from cathode in an LED is to just look at the inner profile. The cathode would be the larger section of material on which the actual LED chip sits on. If you look really close, you might see the whisker that connects the anode lead to the actual anode of the LED. The anode lead is offset from the center and tha cathode is in the center of the package internally.

February 23, 2011
by vgphotog
vgphotog's Avatar

Thank you all for taking the time to respond to my question. I owe you all an apology. In spite of the number of times that I checked the polarity of the LED it was wrong. The program does work. Again thank you.

Post a Reply

Please log in to post a reply.

Did you know that a motor's no-load current at a given voltage is much less than it's resistance would suggest? Learn more...