shiftIn&Out functions

Hey everyone,

I am almost brand new to arduino and I am trying to figure out the shiftIn and shiftOut functions. I have an IC hooked up correctly but just looking at the code I do not understand how it writes to the IC(model:SN74HC595N). I located the source code(on mac) and my main question is over this:

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

I have been reading into binary and I think I understand the bitwise operators well enough to make out this code and what happens. If my understanding is correct the binary for 1 is 00000001 and that bitshift moves it down the line then compares with val(lets say the pin is 3) and writes the value, so mine would be written when i=2. My question is 1) Why does it have to write for each bit? Especially since at the END it flashes the clockPin. Could it not write the binary 00000100 and be done with it? What if the desired pins are 3 and 1. Can it write 00001001 just like that? And then write another right after? Is this mainly because of MSBFIRST and LSBFIRST? But then couldn’t it just reverse the binary and write to the dataPin?

Thanks
Brad

It Must be hard to explain :)

If I wasn't clear let me know! I really want to figure these out :)

Thanks!

You should read up on SPI.transfer(); One instruction writes the byte to a hardware register in the '328P, the chip takes care of generating the clock and sending the data out. Still to control the latch pin:

digitalWrite (ssPin, LOW); SPI.transfer(yourData); // 8 bits shifted out with SCK digitalWrite (ssPin, HIGH); // data moves from input register to output register on this edge

1) Why does it have to write for each bit? Especially since at the END it flashes the clockPin.

ALL 8 bits have to be clocked out. After that they are latched and appear on the 595 outputs.

Could it not write the binary 00000100 and be done with it?

What if the desired pins are 3 and 1. Can it write 00001001 just like that?

How and to what? It's serial output, by definition it goes out one bit at a time.


Rob