1
\$\begingroup\$

I am using a Tinkerkit button as input to Arduino PORTD pin0 and expect an output on PORTC, pin0 as output.

Code:

void wait_for_button()
{

  if( (PORTD & (1<<PD0)) )
      PORTC|=(1<<PC0);
  else
      PORTC|=~(1<<PC0);

}
int main (void)
{

  DDRD=0x00;   //PORTD pin 0 as input
  PORTD=0x00;
  DDRC=0xFF;   //PORTC as output
  PORTC=0x00;

   while(1)
   {
     _delay_ms(200);
     wait_for_button();
   }

}

HW Setup: http://ibb.co/ek6R7a

TinkerKit: http://www.mouser.com/catalog/specsheets/TinkerKitPushButton.pdf

LED does not lightup on button push. what am i doing wrong?

\$\endgroup\$
9
  • \$\begingroup\$ have you confirmed your hardware setup by running the example code given in your TinkerKit link? \$\endgroup\$
    – Makoto
    Commented Jun 4, 2017 at 18:15
  • 1
    \$\begingroup\$ 1st thing you're doing wrong is not including all of the relevant detail in the body of your question. \$\endgroup\$
    – brhans
    Commented Jun 4, 2017 at 18:19
  • \$\begingroup\$ @brhans What exactly is not detailed? Its pretty straight forward right? \$\endgroup\$
    – Abel Tom
    Commented Jun 4, 2017 at 18:21
  • \$\begingroup\$ All I see is a bunch of links I'm not clicking on. \$\endgroup\$
    – brhans
    Commented Jun 4, 2017 at 18:34
  • 2
    \$\begingroup\$ Your line "PORTC|=~(1<<PC0);" should probably be "PORTC&=~(1<<PC0);" instead. \$\endgroup\$
    – brhans
    Commented Jun 4, 2017 at 18:54

2 Answers 2

3
\$\begingroup\$

The problem is in the code that detects a button press. You wrote:

if( (PORTD & (1<<PD0)) )

However, you should write this instead:

if(PIND & (1 << PD0))

This is because PORTD is the output register. It will only reflect values that you write to the pin, not an external voltage. PIND is the input register, which reflects the voltage read at the pin.

And as brhans pointed out, PORTC |= ~(1 << PC0); should be PORTC &= ~(1 << PC0); instead.

\$\endgroup\$
4
  • \$\begingroup\$ Thanks, I did not know that! That did it! \$\endgroup\$
    – Abel Tom
    Commented Jun 4, 2017 at 19:43
  • \$\begingroup\$ I am glad I could help. I hope you have successful tinkering! \$\endgroup\$ Commented Jun 4, 2017 at 19:46
  • \$\begingroup\$ Yea sure! So if i understood correctly PINx is mainly used to enquire the state of relevant PIN (1 or 0) , while using peripheral like ADC it is irrelevant, right? \$\endgroup\$
    – Abel Tom
    Commented Jun 5, 2017 at 5:16
  • 1
    \$\begingroup\$ Yes. PINx is regular register, so it can only hold 1 or 0. Thus, there is a particular cut-off voltage (I think around 3V), above which PINx will hold a 1, otherwise a 0. The ADC is the only way to get a higher voltage resolution. \$\endgroup\$ Commented Jun 6, 2017 at 0:10
1
\$\begingroup\$

As I am new to stackexchange I have to write an answer.

Some things I would do:

  1. Check that no internal pull-ups on the input pins are activated.
  2. Measure the button output voltage when it is pressed.
  3. Use a debugger to check the register states when button is pressed or released.
  4. Make sure that UART is not enabled on the "arduino" RX pin.
\$\endgroup\$
2
  • \$\begingroup\$ He ensured that internal pull-ups are not enabled by writing a 0 to both DDRD and PORTD. \$\endgroup\$ Commented Jun 4, 2017 at 19:38
  • \$\begingroup\$ My answer was more of a general nature. I wasn't particulary thinking about how the AVR ISA works. But thanks, I will try to make my answers more concrete for the architecture being used. \$\endgroup\$ Commented Jun 5, 2017 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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