port register internal pull up

Hi to All
Recently as i was searching to make one project of mine i find a code that could do what i wanted

at first was hard to me to understand what was going on with that code but after some research i found out that this is a method in oder the processor works faster

also i start understand more parts of the code (i am new in coding , just one year through youtube tutorials, some articles in web sites and of course with lot information from here )

My problem is that i want to use the internal pull up resistors for the inputs and i am not sure how to do it with this type of code syntax .

#define Limit_Left_Switch            (PINC & B01000000)       // PC6, Pin31

(i want to make some tests before i decide to design and produce the Pcb that will have the external pull up resistors )

is this the right method ? :

#define Limit_Left_Switch            (PINC & B01000000,INPUT_PULLUP)       // PC6, Pin31

Is any other method more appropriate when we are using this kind of syntax ?

Here is the port register code

  //***** Limit Switches & LEDs *****
#define LimitPort                    PORTC                    // PIN_31, 33, 35, 37 - inputs, PIN_30, 32, 34, 36 - outputs
#define LimitInit()                  DDRC=B10101010;\
  PORTC = B11111111        // PORT_C pullup // external pullup to +5 is desired through 1K resistors PIN_31, 33, 35, 37

#define Limit_Left_LED_On()          LimitPort &= ~(1<<7)     // PC7, Pin30 0
#define Limit_Left_LED_Off()         LimitPort |= (1<<7)      // PC7, Pin30 1
#define Limit_Right_LED_On()         LimitPort &= ~(1<<5)     // PC5, Pin32 0
#define Limit_Right_LED_Off()        LimitPort |= (1<<5)      // PC5, Pin32 1
#define Limit_Front_LED_On()         LimitPort &= ~(1<<3)     // PC3, Pin34 0
#define Limit_Front_LED_Off()        LimitPort |= (1<<3)      // PC3, Pin34 1
#define Limit_Rear_LED_On()          LimitPort &= ~(1<<1)     // PC1, Pin36 0
#define Limit_Rear_LED_Off()         LimitPort |= (1<<1)      // PC1, Pin36 1

#define Limit_Left_Switch            (PINC & B01000000)       // PC6, Pin31
#define Limit_Right_Switch           (PINC & B00010000)       // PC4, Pin33
#define Limit_Front_Switch           (PINC & B00000100)       // PC2, Pin35
#define Limit_Rear_Switch            (PINC & B00000001)       // PC0, Pin37

thanks in advance

No.

You get input pullup when the bit in the DDRx register is 0 (indicating it’s an input) while the same bit in PORTx is 1 (indicating high).

Set the pins to INPUT_PULLUP before hand with pinMode(), or via direct port manipulation:

DDRC&=B10101010; //Make sure that the pins aren’t set output - not needed if you know they haven’t been set output elsewhere in sketch

PORTC=B01010101; //PC0, PC2, PC4, PC6

or, possibly more clearly:

PORTC=(1<<PORTC0)|(1<<PORTC2)|(1<<PORTC4)|(1<<PORTC6); //set PC0, 2, 4, and 6 high - but they’re not outputs, so it just turns on the pullup.

Note that on ATTiny441,841,1634 and 828, you use PUEx not PORTx to control pullups.

Post your complete program.

What Arduino are you using?

Because you only need to define the pin names and set the pullup resistors once in the program there is no great advantage in improved performance if it makes the code harder to understand. Consequently I would just set the pins with the usual pinMode(pinName, INPUT_PULLUP);

...R

Robin2:
Post your complete program.

What Arduino are you using?

Because you only need to define the pin names and set the pullup resistors once in the program there is no great advantage in improved performance if it makes the code harder to understand. Consequently I would just set the pins with the usual pinMode(pinName, INPUT_PULLUP);

...R

Agree, except on small memory attinys. IIRC getting rid of pinmode loses a couple hundred bytes, more if you also don't have any digitalWrite()/digitalRead().

DrAzzy:
Agree, except on small memory attinys.

Apologies to all. I missed that bit.

...R