Assigning a byte to port pins ?


I need to write a byte to some port pins (i would prefer digital pins 2 to 9)

I am using an Arduino Mega.

How do i take the value of byte n, and send the 8 bits to corresponding digital pins.

e.g. bit0 on pin2 bit1 on pin3 etc. etc.

There is the PortManipulation, but then the bits are spread all over, and this is not a possibility for my setup!

How do i write a small routine that splits the byte up and directs the bits to the correct port pins ?


// Per.

Try using the digitalWriteFast library? You'll need to use constant pin numbers (no variables), but it should work...

(google for the name)

You can extract the various bits in a byte using bitRead(). For each bit in the byte, get its value using bitRead() and set the appropriate pin (it's easy to do this if the pin numbers are in an array in the same order as the bits in the byte).