uC input button problem

Status
Not open for further replies.

skmdmasud

Member
Hi..
I am trying to get input from a button using the following code but its not work, it should be something simple, avr studio does not give any error

DDRD = 0b00001111 //only port 5,6,7 as input and other for different purpose
PORTD = 0b01110000 //enabling pull up resistor

main
while(1)
if(pind5 == 0) //detecting input from button
{
some function here like turn on leds

}

Physical connections in breadboard: ATMEGA8--pin7 vcc, pin8 ground, pin 11,12,13 to push button which is connected to ground, other pins to leds.
 
I see three things...
1) you set pins 4-7 as inputs, not 5-7
2) you're only turning on 4-6 input resistors (pin 5 should still work)
3) there are no brackets for your while routine. If the if statement is the next statement (and only statement that will work), then it should work. But if there is something between the while and if, then the if will not be executed.

sorry, I can't download a data sheet and I'm away from home right now so I can't check your pinouts, but I think you have analog ground hooked up, the vcc and ground should be on opposite sides of the chip... maybe I'm wrong... I check it tomorrow.
 
Hi Mike
I just typed it in so i did not give some brackets its to give an idea, in my AVR Studio 6 it is all set properly. As for the pins i can work it out, my pind5 is what i am working with. As i read that when pullup is activated then it gives a 1 when we do lets say i=pind5 and zero when we ground the pin using a switch. In my case its not working.
As for Atmega8 it has 2 grounds one below vcc pin and the other at the opposite side which is pin22. I connected pin 7vcc and 8gnd. Will it make any difference if i connect the ground with pin22 the opposite of vcc pin?
 
it shouldn't make a difference, but I would. are you using the internal rc oscillator?

first thing to do (and only thing) is just to flash an LED to make sure your processor is running.
 
Hi..
i finally figured it out. The code was ok its was a logical error, to turn the ports and pins i was using 0Bxxxxxxx. I had both input and output in the B port.

So I was turning on pullup by 0b01110000 then tunring on ports by 0b00000111 which was over writing it. I fixed it by using bit shift like portb |= 1<<pind5.

Thanks.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…