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 » Issue with PC6/PD0 - cannot set for output

February 23, 2010
by yanzickp
yanzickp's Avatar

Hello,

I'm relatively new at this, so any suggestions would be helpful!

I have wired up a 4 digit 7-segment display to the Atmega 168 that came with the kit, and I am trying to turn on all of the segments of the digits on. I am using 4 digital I/O pins to select the digit, and then I turn on the pin for the segment I want to shut off (a bit backwards it seems, but that is the way it seems to work - it is a common anode display).

When I fire up my test program, all of the segments turn on, EXCEPT G (the LED that turns a 0 to an 8). If I take the wire connected to the pin that G is attached to (tried PC6 and PD0 so far) and connect it to any of the other PC pins, it illuminates, so it appears the pin isn't setting. Is it because all of the other pins I'm using for the segments can be used for ADC pins and I have something misconfigured? I am able to change the digits on the display without issue as well...

One other thing to note... when I program the processor with the programmer that comes with the kit, the segment will illuminate due to the data being sent to the processor (when on PD0) so the path to that segment appears to be good.

Below is the code that I have for the main function. Right now I get a '0' on the very first digit in the display. I used the led_blink as the framework for the code, so it may look a bit familiar... I know there are more elegant ways to code this, but I am trying to get as atomic as I can right now, trying to figure out the problem...

#define F_CPU 14745600

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

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

// PIN DEFINITIONS:
//
// PC0 - Segment A
// PC1 - Segment B
// PC2 - Segment C
// PC3 - Segment D
// PC4 - Segment E
// PC5 - Segment F
// PD0 - Segment G
// PD1 - Digit 1
// PD2 - Digit 2
// PD3 - Digit 3
// PD4 - Digit 4

int main() {
    // Set the individual pins for output

    // The Segment pins
    DDRC |= (1<<PC0);
    DDRC |= (1<<PC1);
    DDRC |= (1<<PC2);
    DDRC |= (1<<PC3);
    DDRC |= (1<<PC4);
    DDRC |= (1<<PC5);
    DDRD |= (1<<PD0);

    // The digit selection pins
    DDRD != (1<<PD1);
    DDRD |= (1<<PD2);
    DDRD |= (1<<PD3);
    DDRD |= (1<<PD4);

    // Select first digit
    PORTD |= (1<<PD1);

  return 0;
}
February 24, 2010
by Rick_S
Rick_S's Avatar

PC6 is the reset line of your micro-controller and is typically a last resort to turn into an I/O pin. If you can find an alternate pin to use do so. Once the reset pin is turned into and I/O pin, you may not be able to program your chip again without a special programmer.

PD0 and PD1 are part of the Serial I/O pins. While you aren't using them in your program for serial communications, they are still "Turned on" by the bootloader. You can use them but you have to take control of them first. See This Thread which discusses just that.

Good luck with your project,

Rick

February 24, 2010
by yanzickp
yanzickp's Avatar

Hello Rick_S,

Thank you for the information! I was hoping to use PC6 initially because that way I can just flip the bits for a single port (assigning a single value) instead of having to expand to 2, making it a little bit easier in the code.

I added the line suggested in the thread (UCSR0B = 0;), first line that executes in the main() function, however I am now no longer able to turn the first digit on (PD1), and I am still unable to get segment G (PD0) to work. Turns out I was not turning the first digit on in the first place; without the line it was on to begin with, which makes sense knowing what the other pin is for. Also, by adding that line and trying to set PD0 all of the other LEDs on the digit would illuminate dimly. I've moved segment G to PD6 for now, and that is working. I can probably move the first digit select pin as well, but for organizational purposes I would have rather not.

Is there anything else I should be aware of when trying to use PD0/PD1?

Thanks, Paul

February 24, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi yanzickp,

Are you removing the programming cable when running your program? That might explain the dimness.

Also, in general I would suggest you leave PD0 and PD1 for the UART module unless you are otherwise out of pins. Moving things down to other pins and making little code modifications (even though they might not be as pretty) is definitely worth the ability to send data to the MCU from your computer. This is useful for debugging, or just adding extra functionality that you think of later on down the line.

Humberto

February 24, 2010
by yanzickp
yanzickp's Avatar

Hello,

Well, I've made a few changes... I am now using PC0-PC5 and PD6 for turning the segments on and off, and am using PD2-PD5 to select the digit, but I am still running into the dimming effect, but ONLY with PD2. If I connect the same wire going to the LED panel to PD3 and turn it on, it is nice and bright. If I reconnect it to PD2 and activate PD2 and PD3, PD2 doesn't light up at all.

I tried moving from PD2 to PD7, and I had the same problem with the dim segments. The only explanation I can think of is because the pins are not set correctly, but the code is pretty much the same as I had originally posted, just a few additional pins set up and activated.

Thank you for the explanation on PD0 and PD1... I will certainly go by your recommendations to leave them alone as well as PC6, but I don't understand why they are not working for me. I will have enough I/O to set this project up (it is really just a kitchen timer when it is all said and done with a few switches thrown in to set the timer) but I am wanting to get into more involved projects soon... plus things like this bug me when I can't find a solution... :)

Paul

February 24, 2010
by bretm
bretm's Avatar

How are you connecting the LEDs? Are you using current-limiting resistors? You shouldn't try to supply more than 20ma of current from a single pin. Also, you should not exceed a total of 100ma combined for the following groups of pins (see page 305 of datasheet):

C0 + C1 + C2 + C3 + C4 + C5 + ADC6 + ADC7

B0 + B1 + B2 + B3 + B4 + B5 + D5 + D6 + D7 + XTAL1 + XTAL2

D0 + D1 + D2 + D3 + D4 + RESET

For the common-cathode situation, where you're pulling the pins high instead of low, you can go up to 150ma.

I'm not sure what symptoms you'd see if you exceed these limits, or whether it explains your problem. But when you said "nice and bright" it made me think of this.

February 24, 2010
by yanzickp
yanzickp's Avatar

The LEDs are connected via 470 ohm resisters. The display is a common anode display. The PDn series of pins, even when trying to use 1 (depending on the pin) has the same effect.

I changed my digit selection pins to PD3 - PD6, and it is working. I don't know why I can't use PD0 - PD2, even after disabling the serial communication. Maybe it is related to the power pull, but if I disconnect all of them on the PDn set except PD2, it is dim (i.e. barely illuminated), but if I move that to PD3, it is a normal brightness.

February 25, 2010
by yanzickp
yanzickp's Avatar

Hello all,

Well, I'm still working with this... I have all of the segments going, but I thought that I would try to connect up the colon on the display, using one of the B pins... but none of them seem to want to work. ARGH, this is frustrating! I've enabled them the same way as I did C, but no go. Any suggestions would be very helpful!

February 25, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi Yanzickp,

Can you provide more details about this colon. Is it just another LED connected to the same common anode? Does the colon go on if you just connect it to your GND rail momentarily? Can you verify that the power connections to the right side of the breadboard are correct. Those power connections are for the ADC on the chip, so they might not affect the normal operation of the chip but can cause wonky electrical problems on the chip if they are not correct. Perhaps if you posted a picture of your setup, one of the many smart people here will be able to see something amiss. Plus we would also like to see your project!

Humberto

February 25, 2010
by yanzickp
yanzickp's Avatar

Hello,

The colon has both a cathode and an anode pin, unlike the other segments. The cathode is tied to GND, and the anode is tied to the pin through a resistor. I've used the base setup from the binary math example, just disconnected the LCD and the switch bundle. I do have the 2 power and 1 GND on that side of the chip set up... The image below shows the circuit... sorry for the poor picture, if need I will get a better one.

Oh, another note... If I take the line from the processor pin and plug it into power, it illuminates the colon.

Breadboard image

February 26, 2010
by hevans
(NerdKits Staff)

hevans's Avatar

Hi Yanzickp,

That is quite odd, I'm not sure what to tell you other than your colon should work when you plug it in the way you described. Did you check the little things? Are you sure you are setting the pin driving the colon as an output pin, made sure you set the DDRB register, not DDRC accidentally. Are you sure that output pin is set to high? That wasn't necessary with the other ones as you were driving them low because they were on the cathode of the LED. Keep us informed of your progress. Can't wait to see that thing working.

Humberto

February 27, 2010
by Noter
Noter's Avatar

I tried to drive a blue 7 segment display that looks similar to the one you have and finally discovered the mcu pins didn't have quite enough power to drive the display directly. I added transistors to power each segment and it all started working fine.

March 16, 2010
by yanzickp
yanzickp's Avatar

Hello all,

Sorry, it has been awhile since I have posted. I took a break from this just to clear my head and get a fresh perspective. Interestingly enough, I did get it working... YAY! I was making a boneheaded move of turning the colon on when I was clearing segments every time I moved a digit, so it would always stay on. DOH!

So, I have made progress. I was able to get the timer to work so I (more or less) have a timer that will count down from whatever time I put in code. The next step, which I am eager to hear your suggestions for, is to attach some buttons so I can set the time values. The problem I am going to have is that I won't have enough pins unless I repurpose the reset and/or programming pins unless someone has some ideas.

Here are the pins currently in use:

// PB1 - Colon Segment
// PC0 - Segment A
// PC1 - Segment B
// PC2 - Segment C
// PC3 - Segment D
// PC4 - Segment E
// PC5 - Segment F
// PD7 - Segment G
// PD3 - Digit 1
// PD4 - Digit 2
// PD5 - Digit 3
// PD6 - Digit 4

I have 5 buttons I want to attach, and I would like to hook up a speaker (probably the one that came in the kit) so I need to find 6 more pins.

I can use PB2-PB6 for 4 of the switches, but I am still short 2... suggestions?

2 of the buttons are to select the digit to adjust left or right, 2 to move the digit up or down, and 1 button to start/stop the timer. Any suggestions on the best way for me to handle this? Am I going to need to get an LED driver or something that requires fewer pins?

March 16, 2010
by JKITSON
JKITSON's Avatar

YOU COULD HARD WIRE THE COLON TO STAY ON. THAT WOULD FREE UP PB1 FOR YOU....

JIM

March 16, 2010
by Noter
Noter's Avatar

You could use a serial-in/parallel-out shift register like the 74HC595 and free up a bunch of pins on the MCU. Connect the 74HC595 to MOSI, SCK, and SS, then it's easy to shift data to it with the SPI write command. After writing, give SS a pulse to lock the serial data to the output on the 74HC595. Cascade a couple of them together and your whole display will take only 4 pins on the MCU.

March 17, 2010
by Rick_S
Rick_S's Avatar

Another option although a bit more involved programming wise is an SAA1064 7 segment I2C driver. This only uses two pins from the mcu to drive up to 4 segments. I've thought of getting one of these myself just to play with.

That would free up a lot of pins. Whatever you end up doing however, unless you have a high voltage programmer on hand such as available on an Atmel STK-500, DO NOT RE-PURPOSE THE RESET PIN. Without a high voltage programmer, you can not program the chip after the fuses have been set to re-purpose reset to something else.

Rick

Post a Reply

Please log in to post a reply.

Did you know that inductors try to keep their current constant over short periods of time? Learn more...