Pages: [1]   Go Down
Author Topic: shiftOut, but with less than 8 bits?  (Read 1943 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 5
Posts: 200
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Edison Member
*
Karma: 3
Posts: 1001
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);            
      }
}
« Last Edit: May 25, 2010, 01:52:33 am by borref » Logged

0
Offline Offline
Full Member
***
Karma: 5
Posts: 200
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ahh, thanks! I could easily use that.
Logged

Amsterdam
Offline Offline
Full Member
***
Karma: 0
Posts: 137
he's looking at you, kiddy...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

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

and where do you find this source code of internal functions?
« Last Edit: May 25, 2010, 02:15:27 pm by macsimski » Logged

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

0
Offline Offline
Full Member
***
Karma: 5
Posts: 200
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!  smiley-razz 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.
Logged

Offline Offline
Edison Member
*
Karma: 3
Posts: 1001
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Logged

Amsterdam
Offline Offline
Full Member
***
Karma: 0
Posts: 137
he's looking at you, kiddy...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Edison Member
*
Karma: 3
Posts: 1001
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

0
Offline Offline
Full Member
***
Karma: 5
Posts: 200
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Amsterdam
Offline Offline
Full Member
***
Karma: 0
Posts: 137
he's looking at you, kiddy...
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

to be exact:

.../arduino/hardware/arduino/cores/arduino/wiring_shift.c
 
smiley
Logged

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

Pages: [1]   Go Up
Jump to: