Go Down

Topic: digital read many pins at a same time (Read 3903 times) previous topic - next topic

jada

i want to create a program like this
Code: [Select]
digitalread (pin1,pin2,pin3,pin4)
if val of(pin1,pin2,pin3,pin4)==(0,1,0,1,0)
action one

and so on is it possible ? :-[

mowcius

if(digitalRead(pin1)==0 && digitalRead(pin2)==1 && digitalRead(pin3)==0 && digitalRead(pin4)==1){
//do something here
}

That help?

Bitwise and, the reference page has more info

Mowcius

mowcius

If not then you might be able to do port reads with direct port manipulation and then compare that to the values you want...

Not sure about that though.

Andy Brown

The reason for wanting to do this can only be speed. Firstly see digitalReadFast:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1267553811/0

If that's not fast enough then you'll need to directly access PINB, PINC and PIND directly depending on how you've assigned your pins.
Home of the Nokia QVGA TFT LCD hacks: http://andybrown.me.uk

Udo Klein

There is another reason: memory consumption. Direct port uses less flash.

Udo
Check out my experiments http://blog.blinkenlight.net

jada

yeah andy brown was right i wanted to do it for speed  :)
any more help  in that direction

jrraines

As Andy said digitalReadFast is about 30 times faster than digitalRead. For multiple pins at once port manipulation can be up to 8 times faster still, but with a steeper learning curve. http://www.arduino.cc/en/Reference/PortManipulation

Udo Klein

How much speed do you need and for what? Sometimes you can beat even direct port manipulation. But it depends on what you want to achieve.

Udo
Check out my experiments http://blog.blinkenlight.net

jada

ill need the maxium possible speed  :) udo

Udo Klein

Of course you need the maximum possible speed. But for what? Depending on what you are doing there may be means to be faster than with direct port manipulation.

If you really need maximum possible speed you can drop Arduino and go for a faster processor and/or an FPGA. Or you can replace the crystal by a 20 MHz crystal.

It's not only port access statements. The rest has an inpact on speed as well. You might need to consider using assembler. Still the question is why. Sometimes a faster solution is using just one or two additional logic chips.

Udo
Check out my experiments http://blog.blinkenlight.net

westfw

Input pins can be read up to 8 at a time (depending on which pins) using "direct port manipulation."  You can read pins 0 through 7 at PIND (bits 0-7), 8 through 13 at PINB (bits 0-5) and the analog pins (in digital) at PINC (bits 0 through 5)...   See https://spreadsheets.google.com/ccc?key=0AqdMB5dovDUZckNXSnNQa0FiWldNMFhWcHV1ODFzR0E&hl=en

So for example:
Code: [Select]
pins = PINB & 0x7;  // read pins 8/9/10
switch (pins) {
  case 0:
    // no switches set
    break;
  case 1:
    switch1_func();
    break;
  case 2:
    switch2_func();
    break;
  case 3:  // switches one and two.
    switch1_func();
    switch2_func();
  case 4:  // switch 3 makes 1&2 into "don't care"
  case 5:
  case 6:
  case 7:
    switch3_func();
    break;
  }
}

If this makes perfect sense to you, then you should be all set, and the rest of us should be remorseful about wasting your time with philosophical diversions...

If it doesn't make much sense, it's going to be difficult to explain how to do what you're asking "at maximum speed" until you've read up enough background material that it at least starts to make sense.  (This is not the only way to make decisions based on reading multiple pins, but it has all the basics in there.  You'll need to deal with bitmasks, for example.)


Go Up