Go Down

Topic: shiftOut, but with less than 8 bits? (Read 2755 times) previous topic - next topic

wyager

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!

BenF

#1
May 25, 2010, 08:46 am Last Edit: May 25, 2010, 08:52 am by borref Reason: 1
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:

Code: [Select]
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);            
     }
}


wyager

Ahh, thanks! I could easily use that.

macsimski

#3
May 25, 2010, 09:12 pm Last Edit: May 25, 2010, 09:15 pm by macsimski Reason: 1
sorry to hook in to this post, but i was reading someting i can't find a explanation off:

Code: [Select]
digitalWrite(dataPin, !!(val & (1 << i)));


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

and where do you find this source code of internal functions?
--
"We're all in this together..."

wyager

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!  :P 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.

BenF

Quote
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:

Code: [Select]
if (val & (1 << i)) digitalWrite(dataPin,HIGH);
else digitalWrite(dataPin, LOW);


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



macsimski

yes. that i know, but on what path do you find them. like /Users/simon/foobar/blabla
--
"We're all in this together..."

BenF

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

The following is the path for the Arduino core.

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


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

wyager

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.

macsimski

to be exact:

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

:)
--
"We're all in this together..."

Go Up