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.

Microcontroller Programming » LED circuit not producing expected results

February 11, 2012
by dvdsnyd
dvdsnyd's Avatar

Hi guys, Just some background. I have been trying to see if I can do the traffic light controller project. I was looking at the code that the Nerdkits staff provided and have been somewhat confused as to how they set the bits on the registers, namely;

DDRC |= (0x07 << PC1);

So, I decided to set up a simple test circuit with 7 LEDs and started playing around with it. I was able to find somewhere on the forums here that 0x07 = 0b00000111, therefore I THINK(please correct me if I am wrong) the above code shifts it over 1 bit....???? = 0b0001110 ????? If someone could please explain this a bit more...also does the x mean hexadecimal? I understand b is binary?

Anyways, it seemed as though everything was working correctly, the 3 LEDS were lighting up, but also the LED in the PINC6 position was as well(which is pin 1 on the MCU). Even if I set PORTC = 0, it would still light up. I decided to run some VERY simple code to see if it would show me anything. Here it is. The goofy thing is, PINC6 stays on consistently, no matter what I do. Could someone please explain what is going on? Can I not use Pin 1 as I am?

Here is the code as promised...nothing too enlightening, but may show some of my thought process.

Any help is greatly appreciated! Thanks a lot


// Traffic Light Controller
// main.c
// David Snyder
// Last Modified: 02/11/12

// Define Statements

#define F_CPU 14745600

// Include Files
#include <stdio.h>
#include <stdlib.h>

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

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

// Pin Definitions
// PC0 = side street red
// PC1 = side street yellow
// PC2 = side street green
// PC3 = main street green
// PC4 = main street yellow
// PC5 = main street red
// PC6 = walk - green

int main(){
//DDRC |= (0x07 << PC1);
DDRC =  0b01111111;
    PORTC &= ~(1 << PC6);

    PORTC |= (0x07 << PC2);
    PORTC = 0b01111111;
    PORTC =  0b00001111;
    PORTC = 0b00011100;
    PORTC = 0b00000001;
    PORTC = 0b00000010;
    PORTC = 0b00000100;
    PORTC = 0b00001000;
    PORTC = 0b00010000;
    PORTC = 0b00100000;
    PORTC = 0b01000000;
    PORTC = 0b00000000;

February 12, 2012
by dvdsnyd
dvdsnyd's Avatar

I think I found the answer to my question. In short, No. For anyone who is also interested in this, I direct you to this post:

Reset Pin programming


January 05, 2013
by Vreni
Vreni's Avatar

Hello, unfortunately i couldnt find the answer to ur first question in the other thread. So i am still struggling what: DDRC |= 0x07 << PC1; means? is it the same as DDRC|=(1<<PC1) ?? or is it DDRC|= 0b00000111 << PC1 like u suggested but this does not make sense to me?? In another thread someone said that 0x07 is the same as DDRC (i checked and in the datasheet really is DDRC 0x07(0x27)) but then the 0x07 is also used in PORTB for example: PORTB &= ~(0x07 << PB1); PORTB |= ((lights >> 4) & (0x07)) << PB1; I really dont know what this means. I initially thought i understood the concept of left and right shift and as long as only 1s are used it is great but know i am completely messed up. It would be nice if someone could help me out. Best regards, Verena

January 05, 2013
by Noter
Noter's Avatar

Only 1s are used when a byte is shifted. First change the hex 7 to binary - 00000111, then turn PC1 into a value - 1, then shift and 00001110 is the result that gets or'ed with whatever value is already stored at the DDRC location (x27) in memory.

January 05, 2013
by Noter
Noter's Avatar

Forgot to say that you should search the web on bitwise operations and you will find a ton of reference and example.

Post a Reply

Please log in to post a reply.

Did you know that interrupts can cause problems if you're not careful about timing and memory access? Learn more...