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
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 )
{
}
-
2For 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.Gerben– Gerben2021-02-10 13:47:13 +00:00Commented Feb 10, 2021 at 13:47
-
1@Gerben thanks for the clarification (added a reference to your comment).Michel Keijzers– Michel Keijzers2021-02-10 14:45:52 +00:00Commented Feb 10, 2021 at 14:45