shiftOut, but with less than 8 bits?

Is is possible to do the shiftOut() command with fewer than 8 bits? I am getting a digital pot working and it requires that I send it special groups of commands (1 bit then 7 bits then 7 bits). I want to do shiftOut to send these commands, as it does the whole clock pin thing. Is this possible? If I feed it only 1 byte will it only spit out 1 byte, or assume the rest are zeroes? Is there a suitable replacement command? Thanks!

Te shiftOut() function is quite simple. You may want to include a copy of its source in your sketch and then rename and modify it to match your requirements. The source is as follows:

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

Ahh, thanks! I could easily use that.

sorry to hook in to this post, but i was reading someting i can't find a explanation off:

digitalWrite(dataPin, !!(val & (1 << i)));

what does the double exclamations do? quite difficult to google it. :slight_smile:

and where do you find this source code of internal functions?

Simple! The !! casts it as a boolean, even if it's an integer. basically a zero returns a zero and anything else returns a one. So even if it's not the LSB that's a high value, the function will still return a 1. Otherwise you have to reverse bitshift to make the LSB a 1, and that's slower than !!. I am the google-fu master! :stuck_out_tongue: But yeah, where do you get the source code for functions? And I would imagine they would be in avr-c, so maybe the person who posted made up that code.

what does the double exclamations do? quite difficult to google it.

The exlamation point "!" is the C/C++ logical NOT operator. If you apply the operator twice (!!) - anything not zero will be equal to "1" and anyting zero will remain as zero. One alternative to using this trick with the double NOT operator would be something like:

if (val & (1 << i)) digitalWrite(dataPin,HIGH);
else digitalWrite(dataPin, LOW);

and where do you find this source code of internal functions?

The source is part of the Arduino download. Whenever you compile/upload a sketch - the Arduino core (such as the shiftOut function) will also be compiled and linked with your program.

yes. that i know, but on what path do you find them. like /Users/simon/foobar/blabla

If you're working on creating cores for new mcu's you should know where to find the source. :slight_smile:

The following is the path for the Arduino core.

...\arduino\hardware\arduino\cores\arduino

Additional cores (e.g. Tiny) should have a directory of its own under cores.

On a mac, you need to right-click the arduino app in finder, and click "show package contents", and then there are a few folders in there (I forget specifics) but you should be able to find stuff pretty quickly.

to be exact:

.../arduino/hardware/arduino/cores/arduino/wiring_shift.c

:slight_smile: