Help using basic port manipulation

Im trying to understand how to use basic port manipulation from the arduino PortManipulation tutorial (http://www.arduino.cc/en/Reference/PortManipulation). Not to mention get the hang of my whole arduino :)

It doesn't mention how to read the current port configurations, though it says "DDR and PORT registers may be both written to, and read" What is the command to read the current PORTB (or DDR or PIN) configuration? I would guess the best way to obtain it would be to do byte x = PORTB | 0?

Thanks

If PORTB is memory-mapped at address 0x0025, then you should be able to read it by coding

byte x = *(unsigned char *)0x0025;

This makes for pretty unreadable code so you could

#define PINB  *(unsigned char *)0x0023   /* Input pins              */
#define DDRB  *(unsigned char *)0x0024   /* Data direction register */
#define PORTB *(unsigned char *)0x0025   /* Data register           */

x = PORTB; /* etc */

just use byte x = PORTB;

all right

also small trivial question that i don't think deserves its own thread:

if i have a signed int val and total is an unsigned long and do if (total - (val*4939)) {etc}

what happens if val*4939 is greater than 65536? Does it not matter because it's happening in the proc not in the memory location? Also what happens if the val*4939 comes out negative but total is unsigned?

Thanks

The solution to above seems so simple now :)

If PORTB is memory-mapped at address 0x0025, then you should be able to read it by coding

Code: byte x = *0x0025;

The problem I can see there is the missing cast. I think the dereference operator should be set to the default int datatype, so the access should be 16 bits wide, not the desired 8. This may have undesireable side-effects.

Good catch! Repaired via edit.

Recently did an exercise in port D manipulation. Read input pins using PIND.

PORTD maps to Arduino digital pins 0 to 7

DDRD - The Port D Data Direction Register - read/write PORTD - The Port D Data Register - read/write PIND - The Port D Input Pins Register - read only

PORTB maps to Arduino digital pins 8 to 13 The two high bits (6 & 7) map to the crystal pins and are not usable

DDRB - The Port B Data Direction Register - read/write PORTB - The Port B Data Register - read/write PINB - The Port B Input Pins Register - read only

PORTC maps to Arduino analog pins 0 to 5. Pins 6 & 7 are only accessible on the Arduino Mini

DDRC - The Port C Data Direction Register - read/write PORTC - The Port C Data Register - read/write PINC - The Port C Input Pins Register - read only

I also have a question about port manipulation.
I read examples, but they only set 8 ports all together. I need to manipulate one port only.
If I want to set Arduino Mega1280 digital pin 3 as output

I have usualy written:
pinMode(3, OUTPUT);
Is it same as:
DDRD = (1<<PD3);

And
digitalWrite(3, HIGH);
Same as:
PORTD = (1<<PD3);

And
digitalWrite(3, LOW);
Same as:
PORTD = (0<<PD3);

You need to get the pin map to find the ports. I have for UNO but not Mega.

Port D (PD7…PD0)
Port D is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). The Port D output buffers have symmetrical drive characteristics with both high sink and source capability. As inputs, Port D pins that are externally pulled low will source current if the pull-up resistors are activated. The Port D pins are tri-stated when a reset condition becomes active, even if the clock is not running.

Pins 0…7 should be on PORTD.

Is there also a way to directly attach interrupt for some pin?

Ragnar:
I also have a question about port manipulation.
I read examples, but they only set 8 ports all together. I need to manipulate one port only.
If I want to set Arduino Mega1280 digital pin 3 as output

I have usualy written:
pinMode(3, OUTPUT);
Is it same as:
DDRD = (1<<PD3);

And
digitalWrite(3, HIGH);
Same as:
PORTD = (1<<PD3);

And
digitalWrite(3, LOW);
Same as:
PORTD = (0<<PD3);

Those would affect the other pins on the port. For example, the middle one would turn pin 3 HIGH and all the others LOW.

To change just the one pin use this.

digitalWrite(3, HIGH);
is the same as:
PORTD |= (1<<PD3); // You are doing an OR operation so that the other pins stay where they were.

and

digitalWrite(3, LOW);
is the same as:
PORTD &= ~(1<<PD3); // Doing an AND with the inverse of (1<<PD3). So that’s AND (11111011) which turns off only pin 3 and leaves the others where they were.