Port Manipulation

what happens if i added more than 8 bits to port manipulation for example:
PORTB = B0000000011101101 will it just use the first 8 bits which are 11101101 or will it just fail to execute the code(i just only need the first 8 bits) ? if it would fail another question how to remove the last "0" bits (they were earlier modified using bitwise operations)

Why would you try to write 16 bits (I did not count them) to an 8-bit register. Make it an 8 bit value before you try to write it.

What Arduino are you using? On an Uno, for example, you should not change all the bits of any Port. The Mega does have some Ports with all bits available but I can't recall if PortB is one of them.

...R

Robin2:
Why would you try to write 16 bits (I did not count them) to an 8-bit register. Make it an 8 bit value before you try to write it.

What Arduino are you using? On an Uno, for example, you should not change all the bits of any Port. The Mega does have some Ports with all bits available but I can't recall if PortB is one of them.

...R

yes those are 16 bits, i am using a pro mini, also i am writing 16 bits bec i got those from another arduino over serial but i want only the last 8 bits so by bitwise >> i will change the position of the 8 bits i want to be at the beginig to be used by PORTD, i cant figure how to delete the rest 8 bits bec they are changing not fixed values

If your data is currently in an "int", you can mask (& AND) the top 8 bits off then send the lower 8 to your 8 bit PORT.
Just remember not to overwrite any bits that you are not using.

If the top 8 bits are those you need to deal with, shift the bits to the right so they get into place.

.

But to answer the original question, any time you try to stuff 16 bits into an 8 bit hole, the 8 high bits get lopped off. So the port would only see the low 8 bits.

Diymaker:
yes those are 16 bits, i am using a pro mini, also i am writing 16 bits bec i got those from another arduino over serial

I dont understand this, this doesn't make much sense.
A) pro mini has an ATmega328 chip on it, so PORTB is still 8 bits
B) "B0000000011101101" was typed by you, it wasnt received by another arduino. Although you probably mean that that number was sent to you and you reinterpreted it. But even if you did, PORTB is still 8 bits and therefore the higher 8 bits are irrelevant to the sender and to the receiver.

Also, there is a file inside the core of arduino that defines B######## from 0-255 and no higher, 1-8 bits and no higher. So that shouldn't even compile.

But all that aside, if you want to take off the last 8 bits:
A) PORTB = (byte)bitMask; //force bitMask to be 8 bits
or
B) PORTB = bitMask & 255; //set the higher 8 bits to 0
or
PORTB = bitMask; //it automatically removes the higher 8 bits

But as warned before, you may not want to set all of your bits like this. PORTB bits 6 and 7 are used for the crystal oscillator, so you dont want to mess with them. If you want to toggle a pin, then look here.

PORTB |= _BV (5); // digitalWrite (13, HIGH); //_BV means bit value, so it just means the 5th bit is set
PORTB &= ~_BV (5); // digitalWrite (13, LOW);

Diymaker:
bec i got those from another arduino over serial

I suspect the reason you think you got 16 bits is because you have read the incoming byte into an int rather than into a byte

...R

Ps991:
I dont understand this, this doesn't make much sense.
A) pro mini has an ATmega328 chip on it, so PORTB is still 8 bits
B) "B0000000011101101" was typed by you, it wasnt received by another arduino. Although you probably mean that that number was sent to you and you reinterpreted it. But even if you did, PORTB is still 8 bits and therefore the higher 8 bits are irrelevant to the sender and to the receiver.

Also, there is a file inside the core of arduino that defines B######## from 0-255 and no higher, 1-8 bits and no higher. So that shouldn't even compile.

But all that aside, if you want to take off the last 8 bits:
A) PORTB = (byte)bitMask; //force bitMask to be 8 bits
or
B) PORTB = bitMask & 255; //set the higher 8 bits to 0
or
PORTB = bitMask; //it automatically removes the higher 8 bits

But as warned before, you may not want to set all of your bits like this. PORTB bits 6 and 7 are used for the crystal oscillator, so you dont want to mess with them. If you want to toggle a pin, then look here.

PORTB |= _BV (5); // digitalWrite (13, HIGH); //_BV means bit value, so it just means the 5th bit is set

PORTB &= ~_BV (5); // digitalWrite (13, LOW);

ok,now
first the 16 bits are sent by me by serial.print() from another arduino, so if i sent 8 bits it will be PORTB = Serial.read(); but i am making an example for just 2 arduinos by sending 16 bits, but i will be using 5 arduinos which is 40 bits and i need to assign 8 bits of this 40bit message for every arduino, so i will shift those 8 bits to the left to be the lower ones which can be used by PORTB
second:
for pins 6 and 7 how to avoid them? set them as inputs or output and when using PORT changing them to 1 or 0??

Diymaker:
ok,now
first the 16 bits are sent by me by serial.print() from another arduino
...SNIP....
but i will be using 5 arduinos which is 40 bits and i need to assign 8 bits of this 40bit message for every arduino,

Explain how the 16 bits or the 40 bits will be sent to the Arduino. They have to be sent as a series of bytes.

Every Serial.read() reads a single byte (8 bits).

If you receive 5 bytes (40 bits) it will be very easy to allocate each of them wherever you want.

I wonder if you are sending the 40 "bits" as 40 separate characters ?

...R