Shift out syntax question

the shiftOut() command with dataPin, clockPin, bitOrder, value) makes a lot of sense but I do not understand in the "Hello World" example of shift out the clock is pulsed. Would pin 12 (clockPin) look like a clock if an oscope was hooked up to it? Does anyone have a timing diagram to illistrate this command?

Thanks

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);
        }
}

This is what shiftOut does, so I guess the clockpin would be LOW a little longer than it is HIGH when viewed on an oscilloscope.

That is EXACTLY what I was looking for

Thanks !

Any reason this wouldn't be faster and easier to read -

enum shift_direction_e  { MSBFIRST, LSBFIRST };

void shiftOut(uint8_t dataPin, uint8_t clockPin, shift_direction_e sd, byte val)
{
    if ( MSBFIRST == sd )
    {
        byte    mask = B10000000;
        do
        {
            digitalWrite(dataPin, ((mask & val) ? HIGH : LOW));
            
            digitalWrite(clockPin, HIGH);
            digitalWrite(clockPin, LOW);
        } while ( mask >>= 1 );
    }
    else
    {
        byte    mask = B00000001;
        do
        {
            digitalWrite(dataPin, ((mask & val) ? HIGH : LOW));
            
            digitalWrite(clockPin, HIGH);
            digitalWrite(clockPin, LOW);
        } while ( mask <<= 1 );
    }
}

Any reason this wouldn't be faster and easier to read -

The function quoted above is part of Arduino-0018. You normally don't need to read it to use it. (I would have written (128 >> i) rather than (1 << (7 - i)) though)
I don't think there would be any noticeable difference in speed, as it is the calls to digitalWrite and not the bit shifting that consumes the time.
the compiler's strength-reduce optimization will also most likely fix the code such that only one shift per loop is required in either case.

The current code is also smaller, which is important on a microcontroller.

If speed is important, SPI, direct port manipulation and inline assembler are better options.

haha

meixi - "haha"

Meaning?

drhex

Understood.

And given your post above something along the lines of this seems to me to fulfill both the need for readability and small code size.

void shiftOut(uint8_t dataPin, uint8_t clockPin, shift_direction_e sd, byte val)
{
    byte mask = ((MSBFIRST == sd) ? B10000000 : B00000001);
    
    do
    {
        digitalWrite(dataPin, !!(mask & val));
        
        digitalWrite(clockPin, HIGH);
        digitalWrite(clockPin, LOW);
    } while ( mask = ((MSBFIRST == sd) ? (mask >> 1) : (mask << 1)) );
}

But I realize what looks like simplified code may not generate smaller object code. Is there a tool that can be used to determine code size and or something to determine cycle counts for our code. Other than doing timing tests?

The Arduino SDK on your PC should tell you the size of the generated binary whenever you compile, look at the bottom of the window.