minitreintje:
I would like to understand but it's the first time I use shift registers and bytes so it's a little bit confusing for me.
I don't get it quiet well with the bytes how you save it and pass it.
Dylan
When the clock traverses from LOW to HIGH the interrupt triggers. At this point there is a valid data bit on the input. You need to read that data bit:
byte dataValue = digitalRead(DATA_LINE);
and shift it into a storage variable:
incomingByte = incomingByte << 1; // Make room for it
incomingByte = incomingByte | dataValue;
incomingByte is a global and "volatile" variable:
volatile byte incomingByte = 0;
So say incomingByte starts with 0. In binary that would be, as it's a byte variable, 00000000. Then let's toggle the clock once, with a HIGH on the data line. The shift instruction (<< 1) would make incomingByte move all its bits left one. The leftmost one drops off the end. As it's all 0 at the moment it'll stay all 0. Then we "OR" the incoming bit with it. 00000000 | 1 = 00000001.
Then we toggle the clock again, for the second bit. Let's say it's a LOW on the data line at this point. Shift incomingByte once left, so 00000001 becomes 00000010, then OR in the data line (0). So now the byte contains 00000010. Then again, we toggle the clock - let's have a HIGH again. incomingByte shifted becomes 00000100, then ORed with the data line, is 00000101. Etc.
So looking at a full transfer of the byte 10100111, we would have:
Clock 1: Before shift: 00000000 After shift: 00000000 Data: 1: After OR: 00000001
Clock 2: Before shift: 00000001 After shift: 00000010 Data: 0: After OR: 00000010
Clock 3: Before shift: 00000010 After shift: 00000100 Data: 1: After OR: 00000101
Clock 4: Before shift: 00000101 After shift: 00001010 Data: 0: After OR: 00001010
Clock 5: Before shift: 00001010 After shift: 00010100 Data: 0: After OR: 00010100
Clock 6: Before shift: 00010100 After shift: 00101000 Data: 1: After OR: 00101001
Clock 7: Before shift: 00101001 After shift: 01010010 Data: 1: After OR: 01010011
Clock 8: Before shift: 01010011 After shift: 10100110 Data: 1: After OR: 10100111
If you want outgoing data for daisy chaining of registers you want to know what the bit was that "fell off the end of the byte" and send that out on the low-going edge of the clock. So, just before you do your left shift, you should examine that top bit and save it in a variable:
topBit = incomingData >> 7 & 0x01
Then when the clock goes low you can write it out:
digitalWrite(DATA_OUT, topBit);
So, you might have something like this in your program, amongst other things, like setting up the IO ports and configuring the interrupts:
volatile byte incomingData = 0;
volatile byte topBit = 0;
void risingEdge() {
topBit = incomingData >> 7 & 0x01;
incomingData <<= 1;
incomingData |= digitalRead(DATA_IN);
}
void fallingEdge() {
digitalWrite(DATA_OUT, topBit);
}
Then you can look at your latch input to know when the contents of incomingData contain valid data, and do something with them.