1
\$\begingroup\$

I'm trying to read a value from an avr pin but it doesn't work.

I'm trying to read a value that is coming from a push button and this button is connected to 5V DC cell.

When I press the button the 5V should go to the atmega32 and reads it, then the if statement becomes true and the led goes on.

However, when the value becomes true the led will turn on but its not.

bit 1 is the button bit 0 is the led

#define DDRA (*((volatile unsigned char *)0x3A))
#define PORTA (*((volatile unsigned char *)0x3B))
#define PINA (*((volatile unsigned char *)0x39))

int main(void) {
   DDRA |= 0b00000001;         // pin 0 output

    while (1) {
        if (PINA&0b00000010) {  // button pressed
            PORTA |= 0b00000001;   // turn led on
        }
    }
}

enter image description here

\$\endgroup\$
  • \$\begingroup\$ I didn't use avr yet but are you sure your DDR command is OK? Usually a 0 means output and a 1 means input. Plus, you'd have to put a pull down resistor between your input pin and ground (unless your avr has internally pull down resistors), somewhere around 10kohm would do the job. \$\endgroup\$ – Marcelo Espinoza Vargas Jun 7 '17 at 23:17
  • 1
    \$\begingroup\$ Something else, your battery is upside down, you're supplying -5V to your pin1 when the button is pressed. \$\endgroup\$ – Marcelo Espinoza Vargas Jun 7 '17 at 23:25
  • 1
    \$\begingroup\$ Thank you very much you made my day. i found the problem in the battery because it was upside down and because of the pull down resistor.but for your first line, 1 means output in avr and 0 means input. \$\endgroup\$ – Ahmed Rifaat Jun 7 '17 at 23:45
  • \$\begingroup\$ Perfect! Go on with your project and learning (as I did today, now I won't struggle with DDR commands when I start using AVR) \$\endgroup\$ – Marcelo Espinoza Vargas Jun 7 '17 at 23:46
  • \$\begingroup\$ @MarceloEspinozaVargas, I find the DDR convention on AVR mind-boggling, but it's strangely obvious for AVR nerds. "0" for Onput, and "1" for Iutput. I mean, duh? \$\endgroup\$ – Jay Carlson Jun 14 '17 at 5:36
0
\$\begingroup\$

As stated in the comment, the battery being reversed, causes the while statement to be true (because the button closure is "noisy"), but it fails to meet the next test (b00000010 mask), so the LED is not lit.
After the battery is corrected, you might still experience the same problem because the button is "noisy" (when you close the circuit, it will switch between 5v and ground several times, before "settling" on 5v, and when you break the circuit, the same thing will happen before settling on 0v.
If you want to prevent this, you need to "debounce" the switch.

\$\endgroup\$
  • \$\begingroup\$ This is substantially confused. The while condition is always true, and has nothing to do with bouncing or button closure. Classic contact bounce cannot cause a problem here, because it will not happen until the switch is pushed, and pushing sets the LED permanently on, so nothing that happens later matters. Even if the |= were changed to something else, bounce would likely only cause an imperceptible flicker of the LED - it only really becomes an issue when a circuit does something like toggle between states or count on each button push. \$\endgroup\$ – Chris Stratton Jun 10 '17 at 23:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.