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 » Problems understanding input buttons

December 21, 2010
by archee
archee's Avatar

Im trying to turn on one led(on port PC3) when key is pressed and another to turn on when the key is released(on port PC0) the button conects GND to port PD2... but somehow even when the button is not pressed the LED shows that it is pressed... and when I press the button the light stays the same... there is no way to switch between the LED`s....

int main() {
 DDRC = 63;
 DDRD &= ~(1 << PD2); // set PD2 as input
 PORTD |= (1 << PD2); //turn on internal pull up resistor

 while(1) {

if(PIND & (1<<PD2))
{
    PORTC = (1<<PC3);
} else {
    PORTC = (1<<PC0);
}   
  }
 return 0;
}
December 21, 2010
by bretm
bretm's Avatar

The code seems right. I'm unclear from your description which LED is lighting up and under what circumstances. Can you take a picture of the breadboard showing how the LEDs and button are hooked up? Are you sure the LEDs are oriented correctly?

December 22, 2010
by met_fredrik
met_fredrik's Avatar

If I have slept enough to think clear; When you start up your microcontroller the PC0 will stay high. And if you press the button once both would be high all the time. Because your not turning off the LED's anywhere in your code.

BTW: when you set DDRC to 63, this equals 0110011, so PC3 won't be set as out?

December 22, 2010
by archee
archee's Avatar

I`m not pro at MCU programming, but I think you havent slept enaught.. :D no offence

63 = 0+32+16+8+4+2+1 -> 0111111

This line sets PORTC to 0001000

PORTC = (1<<PC3);

This line sets PORTC to 0000001

PORTC = (1<<PC0);

If I would get any reaction from the button I would be happy enaugh, even this reaction is incorrect. But there is no reaction from the button... ;/

@bretm when I_ll get home I_ll put picture of breadbord..

December 22, 2010
by archee
archee's Avatar

@bretm

Sorry a bit blurry,

pic of my breadbord

is it possible, that there could be some wires connected under the breadbord unintentionaly...?

thanks for help in advance.. I realy appreciate it

December 22, 2010
by VictorsNerdery
VictorsNerdery's Avatar

Should the if statement say
if (PORTD & (1<<PD2)) ?

December 22, 2010
by bretm
bretm's Avatar

No, PIND is the right way to read the port state.

December 22, 2010
by bretm
bretm's Avatar

I would absolutely not skip using current-limiting resistors unless you're on battery power, and it looks like you might be on USB power. The guide has you do that for led_blink but it will most definitely overload the MCU if you supply enough current. Battery limits the problem.

Can you verify by some other means that the ports are still working at all?

December 22, 2010
by archee
archee's Avatar

I can verify that PC0 - PC6 works well.

but for PD2. I havent done anything with this port for a long long time. I probbably should check it...

and MCU is running on 9v battery power.

December 22, 2010
by mrobbins
(NerdKits Staff)

mrobbins's Avatar

Hi archee,

Just to rule out mechanical oddities, can you connect the wire from PD2 directly to +5V (red rail), and then switch it to connect directly to GND (blue rail)? In fact, you can try using one hand to touch the copper wire directly to the microcontroller pin just to rule out a breadboard issue.

That should tell us if the problem is on the circuit side or the code side.

Mike

December 28, 2010
by Hexorg
Hexorg's Avatar

Wait, guys, pay attention to his code. Particularly the if(PIND & (1<<PD2)) statement. If you button is pressed you turn other LED on, but you are forgetting to turn the first one off. It should be like this:

if(PIND & (1<<PD2))
{
    PORTC &= ~(1<<PC0); // turn first LED off
    PORTC |= (1<<PC3); // second LED on
} else {
    PORTC |= (1<<PC0); // turn first LED on
    PORTC &= !(1<<PC3); // second off
}

Also, try to make it a habbit of putting bitwise arythmetic into the assignment statements for registers ( &= or |= instead of = )

December 28, 2010
by Hexorg
Hexorg's Avatar

Wait, so sorry, sleepy me didn't notice it was on the same port. Archee, make sure in MCUCR register your PUD (pull-up disabled) is 0.

January 02, 2011
by archee
archee's Avatar

@ mrobbins

Hi Mike,

Thanks for advice. I found the problem and it was in the hardwere. The red switches pins was bearly touching the contacts under the breadbord. When I am using one hand to push its pins into breadbord and other hand to push the button everything works fine.

Thanks for help everybody.

Arturs

Post a Reply

Please log in to post a reply.

Did you know that the microcontroller's crystal oscillator can be used to keep accurate time? Learn more...