shiftOut question

I was looking at the wiring_shift.h file to see how the shiftOut function worked and I thought that the use of the digitalWrite function was kind of inefficient. I read some articles a while back that said the digitalWrite function took up to fifty-something cycles to execute while setting PORTx took something like ~2 cycles to execute.. Am I wrong in thinking that it would be pretty straight forward to implement PORTx and increase the speed of the function?

Here is the code from wiring_shift.h (copied without modification)

#include "wiring_private.h"

void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, byte val)
{
      int i;

      for (i = 0; i < 8; i++)  {
            if (bitOrder == LSBFIRST)
                  digitalWrite(dataPin, !!(val & (1 << i)));
            else      
                  digitalWrite(dataPin, !!(val & (1 << (7 - i))));
                  
            digitalWrite(clockPin, HIGH);
            digitalWrite(clockPin, LOW);            
      }
}

The reason I ask is because I am using a shift register to control a POV-LED display and with the addition of PWM and more LED's, I fear that the standard shiftOut function wouldn't be fast enough.

thanks in advance for any advice

Also, how do you use PORTx to set ONE pin at a time and not all eight? Something with cbi() and sbi() ?

You can best do it using and & or operations on the port. There is an example for port manipulation in the reference section:

Keep in mind that be using direct port access your code will not be portable across the Arduino supported processors, i.e. 328 chip Vs 1280 chip.

Lefty

As you already have taken a look into the standard libraries - which is a good thing, and everybody who is concerned with interface programming should do - you might also have a look at how digitalWrite is implemented.

You can duplicate and adapt that code and make a very efficient and portable shiftOut!!

It looks as if this had been "forgotten" during the last updates of Ardiono...

if your worried about spi speed, look at using the onboard hardware and pretty much ignore shiftOut()