Direct portmanipulation with the register adress

Hi,

I am currently trying to directly access and manipulate my Arduino Ports without the PORTB/PORTC/PORTD command. How can I manipulate the ports with the register adress for example the PD4 Pin is located at 0x6D Bit 4. Is there any possibility to access this pin just with the adress and set it high or low?

Best regards
Kevin

MrKK11:
Hi,

I am currently trying to directly access and manipulate my Arduino Ports without the PORTB/PORTC/PORTD command. How can I manipulate the ports with the register adress for example the PD4 Pin is located at 0x6D Bit 4. Is there any possibility to access this pin just with the adress and set it high or low?

Best regards
Kevin

Hi Kevin,

Here's a simple example using the blink sketch with register manipulation on the Arduino Uno:

// Blink sketch using register maniplulation: toggles pin D13 on Arduino Uno
void setup() 
{
  DDRB |= _BV(PORTB5);        // Set D13 (PORTB5) to an output
}

void loop()
{
  if (!(PINB & _BV(PORTB5)))  // Test if D13 is low
  {
    PORTB |= _BV(PORTB5);     // Set D13 high
  }
  delay(1000);                // Wait 1 second
  if (PINB & _BV(PORTB5))     // Test if D13 is high
  {
    PORTB &= ~_BV(PORTB5);    // Set D13 low
  }
  delay(1000);                // Wait 1 second
}

The _BV macro simply converts the port's pin number to port position. For example _BV(PORTB5) is converted to B00100000, a binary 1 shifted left 5 times.

Please note that the OP said

I am currently trying to directly access and manipulate my Arduino Ports without the PORTB/PORTC/PORTD command.

although he did not say why

You could look at the definitions of the PINx/PORTx macros.
In essence, they are simply casts of the register address to volatile pointers, and a dereference.

Please note that the OP said...

Oops. I should've read the question properly.

MrKK11:
I am currently trying to directly access and manipulate my Arduino Ports without the PORTB/PORTC/PORTD command.

You're mistake is in thinking they are "commands". Per @AWOL, if you trace your way through all the #define(s), you'll see (for example) that 'PORTB' is actually:

(*(volatile uint8_t *)(0x25))

So, it already does exactly what you want.

Thank you for your answers.

gfvalvo:
You're mistake is in thinking they are "commands". Per @AWOL, if you trace your way through all the #define(s), you'll see (for example) that 'PORTB' is actually:

(*(volatile uint8_t *)(0x25))

So, it already does exactly what you want.

This is the example I was looking for thanks. How can I search quickly for the definitioins? In which libary file should I look for them?

AWOL:
You could look at the definitions of the PINx/PORTx macros.
In essence, they are simply casts of the register address to volatile pointers, and a dereference.

And this helped as well.
The reason for my question is just too improve my knowledge about microcontrollers and how to controll others without a libary.

MrKK11:
Thank you for your answers. This is the example I was looking for thanks. How can I search quickly for the definitioins? In which libary file should I look for them?And this helped as well.
The reason for my question is just too improve my knowledge about microcontrollers and how to controll others without a libary.

First step to learning about how to do stuff without a library is to study a few examples of existing libraries and see what they do. If you just try to start by yourself with no examples you'll develop some bad habits or miss some techniques that you should know.

MrKK11:
How can I search quickly for the definitioins?

IMO, by far the best way is to use a Real development IDE like Eclipse / Sloeber. It allows you to trace just about anything back to its declaration - #define(s), classes, variables, functions, etc. It will take you into the proper .h, .c, and .cpp files. This includes into the board core files and library files. After using this IDE, I can’t see myself going back to Arduino for any serious development work. It’s only good for generating the most trivial, one-off test cases.

A caveat: there is a learning curve with this tool and it sometimes can be a little flakey.

Baring a different IDE, you need a really good search tool. The one that comes with MS Windows sucks. Take a look at Agent Ransack.