Pages: [1]   Go Down
Author Topic: digital read many pins at a same time  (Read 3346 times)
0 Members and 1 Guest are viewing this topic.
Hyderabad , India
Offline Offline
God Member
*****
Karma: 6
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

i want to create a program like this
Code:
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 ? :-[
Logged

North Yorkshire, UK
Offline Offline
Faraday Member
**
Karma: 104
Posts: 5531
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

North Yorkshire, UK
Offline Offline
Faraday Member
**
Karma: 104
Posts: 5531
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Essex, UK
Offline Offline
Full Member
***
Karma: 4
Posts: 150
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Home of the Nokia QVGA TFT LCD hacks: http://andybrown.me.uk

0
Offline Offline
Faraday Member
**
Karma: 23
Posts: 3470
20 LEDs are enough
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Udo
Logged

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

Hyderabad , India
Offline Offline
God Member
*****
Karma: 6
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Minnesota USA
Offline Offline
Sr. Member
****
Karma: 1
Posts: 323
Made it mahself outta sand 'n wahr.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Faraday Member
**
Karma: 23
Posts: 3470
20 LEDs are enough
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

Hyderabad , India
Offline Offline
God Member
*****
Karma: 6
Posts: 621
can't help not to think arduinaizing something !
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

ill need the maxium possible speed  smiley udo
Logged

0
Offline Offline
Faraday Member
**
Karma: 23
Posts: 3470
20 LEDs are enough
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

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

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 124
Posts: 6653
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.)

Logged

Pages: [1]   Go Up
Jump to: