{"id":158,"date":"2015-10-04T18:24:57","date_gmt":"2015-10-04T18:24:57","guid":{"rendered":"http:\/\/thedutchguys.com\/?p=158"},"modified":"2022-03-29T22:21:00","modified_gmt":"2022-03-29T20:21:00","slug":"158","status":"publish","type":"post","link":"https:\/\/thijsbekke.nl\/blog\/2015\/10\/04\/158\/","title":{"rendered":"Twee shift registers"},"content":{"rendered":"<span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Leestijd: <\/span> <span class=\"rt-time\">3<\/span> <span class=\"rt-label rt-postfix\">Minuten<\/span><\/span><p>In een&nbsp;<a href=\"https:\/\/thijsbekke.nl\/blog\/2015\/10\/04\/arduino-shift-register\/\">eerder<\/a>&nbsp;bericht legde ik uit hoe ik een enkele shift register aansloot. &nbsp;Hieronder het diagram hoe je meerdere shift registers aanstuurt met nog steeds maar drie draden. Je stuurt ipv. \u00e9\u00e9n byte twee bytes naar de shift register deze stuurt deze weer door naar je tweede shift register, of je derde, vierde enzovoort.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/thijsbekke.nl\/blog\/wp-content\/uploads\/2015\/10\/2-Shift-register.png\" rel=\"lightbox[158]\"><img loading=\"lazy\" class=\"wp-image-159 aligncenter\" src=\"https:\/\/thijsbekke.nl\/blog\/wp-content\/uploads\/2015\/10\/2-Shift-register.png\" alt=\"2 Shift register\" width=\"327\" height=\"300\"><\/a><\/p>\n<p>&nbsp;<\/p>\n<h2>Code<\/h2>\n<p>&nbsp;<\/p>\n<pre class=\"lang:arduino decode:true \">\/\/**************************************************************\/\/\n\/\/  Name    : shiftOutCode, Predefined Dual Array Style         \/\/\n\/\/  Author  : Carlyn Maw, Tom Igoe                              \/\/\n\/\/  Date    : 25 Oct, 2006                                      \/\/\n\/\/  Version : 1.0                                               \/\/\n\/\/  Notes   : Code for using a 74HC595 Shift Register           \/\/\n\/\/          : to count from 0 to 255                            \/\/\n\/\/****************************************************************\n\n\/\/Pin connected to ST_CP of 74HC595\nint latchPin = 8;\n\/\/Pin connected to SH_CP of 74HC595\nint clockPin = 12;\n\/\/\/\/Pin connected to DS of 74HC595\nint dataPin = 11;\n\n\/\/holders for infromation you're going to pass to shifting function\nbyte data;\n\nbyte dataArray[10];\n\n\nvoid setup() {\n  \/\/set pins to output because they are addressed in the main loop\n  pinMode(latchPin, OUTPUT);\n  Serial.begin(9600);\n\n  \/\/Arduino doesn't seem to have a way to write binary straight into the code \n  \/\/so these values are in HEX.  Decimal would have been fine, too. \n  dataArray[0] = 0x00; \/\/00000000\n  dataArray[1] = 0x01; \/\/00000001\n  dataArray[2] = 0x02; \/\/00000010\n  dataArray[3] = 0x04; \/\/00000100\n  dataArray[4] = 0x08; \/\/00001000\n  dataArray[5] = 0x10; \/\/00010000\n  dataArray[6] = 0x20; \/\/00100000\n  dataArray[7] = 0x40; \/\/01000000\n  dataArray[8] = 0x80; \/\/10000000\n\n\n}\n\nvoid loop() {\n\n\n  for (int j = 0; j &lt; 9; j++) {\n    \/\/load the light sequence you want from array\n    data = dataArray[j];\n\n    \/\/ground latchPin and hold low for as long as you are transmitting\n    digitalWrite(latchPin, 0);\n    \/\/move 'em out\n    shiftOut(dataPin, clockPin, 0x00); \n    shiftOut(dataPin, clockPin, data);\n        \n    digitalWrite(latchPin, 1);\n    delay(300);\n  }\n\n  for (int i = 1; i &lt; 9; i++) { data = dataArray[i]; \/\/ground latchPin and hold low for as long as you are transmitting digitalWrite(latchPin, 0); shiftOut(dataPin, clockPin, data); shiftOut(dataPin, clockPin, 0x00); digitalWrite(latchPin, 1); delay(300); } } \/\/ the heart of the program void shiftOut(int myDataPin, int myClockPin, byte myDataOut) { \/\/ This shifts 8 bits out MSB first, \/\/on the rising edge of the clock, \/\/clock idles low \/\/internal function setup int i=0; int pinState; pinMode(myClockPin, OUTPUT); pinMode(myDataPin, OUTPUT); \/\/clear everything out just in case to \/\/prepare shift register for bit shifting digitalWrite(myDataPin, 0); digitalWrite(myClockPin, 0); \/\/for each bit in the byte myDataOut\ufffd \/\/NOTICE THAT WE ARE COUNTING DOWN in our for loop \/\/This means that %00000001 or \"1\" will go through such \/\/that it will be pin Q0 that lights. for (i=7; i&gt;=0; i--)  {\n    digitalWrite(myClockPin, 0);\n\n    \/\/if the value passed to myDataOut and a bitmask result \n    \/\/ true then... so if we are at i=6 and our value is\n    \/\/ %11010100 it would the code compares it to %01000000 \n    \/\/ and proceeds to set pinState to 1.\n    if ( myDataOut &amp; (1&lt;&lt;i) ) {\n      pinState= 1;\n    }\n    else {  \n      pinState= 0;\n    }\n\n    \/\/Sets the pin to HIGH or LOW depending on pinState\n    digitalWrite(myDataPin, pinState);\n    \/\/register shifts bits on upstroke of clock pin  \n    digitalWrite(myClockPin, 1);\n    \/\/zero the data pin after shift to prevent bleed through\n    digitalWrite(myDataPin, 0);\n  }\n\n  \/\/stop shifting\n  digitalWrite(myClockPin, 0);\n}\n\n\n<\/pre>\n<p><iframe loading=\"lazy\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/gTe3KWIyl4c?feature=oembed\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Leestijd: <\/span> <span class=\"rt-time\">3<\/span> <span class=\"rt-label rt-postfix\">Minuten<\/span><\/span> In een&nbsp;eerder&nbsp;bericht legde ik uit hoe ik een enkele shift register aansloot. &nbsp;Hieronder het diagram hoe je meerdere shift registers aanstuurt met nog steeds maar drie draden. Je stuurt ipv. \u00e9\u00e9n byte twee bytes naar de shift register deze stuurt deze weer door naar je tweede shift register, of je derde, vierde enzovoort.<\/p>\n","protected":false},"author":1,"featured_media":185,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[3,4],"_links":{"self":[{"href":"https:\/\/thijsbekke.nl\/blog\/wp-json\/wp\/v2\/posts\/158"}],"collection":[{"href":"https:\/\/thijsbekke.nl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thijsbekke.nl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thijsbekke.nl\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thijsbekke.nl\/blog\/wp-json\/wp\/v2\/comments?post=158"}],"version-history":[{"count":8,"href":"https:\/\/thijsbekke.nl\/blog\/wp-json\/wp\/v2\/posts\/158\/revisions"}],"predecessor-version":[{"id":630,"href":"https:\/\/thijsbekke.nl\/blog\/wp-json\/wp\/v2\/posts\/158\/revisions\/630"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/thijsbekke.nl\/blog\/wp-json\/wp\/v2\/media\/185"}],"wp:attachment":[{"href":"https:\/\/thijsbekke.nl\/blog\/wp-json\/wp\/v2\/media?parent=158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thijsbekke.nl\/blog\/wp-json\/wp\/v2\/categories?post=158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thijsbekke.nl\/blog\/wp-json\/wp\/v2\/tags?post=158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}