Reading digital pins

Hello,

Is there a way of reading 8 or 16 digital input or controlling outputs at the same time as a byte or a word. I am a long time user of the Parallax Basic Stamp and now have fully converted to Arduino.

In the stamp, you can read/write 16-bit variables called INS or OUTS which give/set the the state of all 16 digital pins. It is very handy when you are monitoring multiple input/outputs at the same time without writing a bunch of code.

Is there an equivalent method in Arduino?

Thanks,

Al

Yes, look into Port Manipulation.

http://playground.arduino.cc/Learning/PortManipulation

The paradigm here emphasizes the digitalRead(), speed, and building a function to gather all that up with.

Perfect, thanks. How does this work for the Mega, pins 22 - 51? This isn't mentioned in the link you provided and couldn't find a reference to it.

Al

Works the same, but you'll have to dig up and look at the datasheet

to see which pins are associated with which ports.

You cannot do 16 at a time with any AVR, but you can do 8.

Most smaller Arduinos don't allow 8 either because various pins are used for other stuff, but the Mega does, so for example if you look at the Mega schematics you will see that PORTA is broken out to pins 22-29 so there are 8 pins you can set in one instruction, for example

PORTA = 0x55;

after setting the pins to outputs.


Rob

Thank you. I looked up the datasheet and I can see all the ports. Excellent!