2

I can set pin mode to input or output through the DDRx registers. How can I enable the internal pullup resister through a register?

1 Answer 1

2

Credits to GoForSmoke. Also see Gerben's comment below.

See: https://forum.arduino.cc/index.php?topic=286145.0

byte ddrcMask =       ~
(
        (1 << DDC0) |  // pinMode( 14, INPUT ); // Set to input
        (1 << DDC1) |  // pinMode( 15, INPUT ); // Set to input
        (1 << DDC2) |  // pinMode( 16, INPUT ); // Set to input
        (1 << DDC3)    // pinMode( 17, INPUT ); // Set to input
);


byte portcMask =
(
        (1 << PORTC0) |  // digitalWrite( 14, HIGH ); // Enable the pullup
        (1 << PORTC1) |  // digitalWrite( 15, HIGH ); // Enable the pullup
        (1 << PORTC2) |  // digitalWrite( 16, HIGH ); // Enable the pullup
        (1 << PORTC3)    // digitalWrite( 17, HIGH ); // Enable the pullup
);

byte pincMask = ( (1 << PINC0) | (1 << PINC1) | (1 << PINC2) | (1 << PINC3) );


void setup( void )
{
  // Configure the pins for input
  DDRC = DDRC & ddrcMask;

  // Enable the pullups
  PORTC = PORTC | portcMask;

  // Read all four inputs
  uint8_t Pdat = PINC & pincMask;
}

void loop( void )
{
}
2
  • 2
    For clarity. You are using the same register (PORTC) you would use to set an OUTPUT pin to HIGH. When the pin is an INPUT it will enable the pull-up resistor. So the function of the register depends on whether the pin is an INPUT or OUTPUT. Commented Feb 10, 2021 at 13:47
  • 1
    @Gerben thanks for the clarification (added a reference to your comment). Commented Feb 10, 2021 at 14:45

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.