{"id":1996,"date":"2024-07-14T15:51:07","date_gmt":"2024-07-14T15:51:07","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=1996"},"modified":"2024-08-11T17:25:48","modified_gmt":"2024-08-11T17:25:48","slug":"8bit-shift-register-74hc595","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=1996","title":{"rendered":"8bit shift register (74HC595)"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-1 wp-block-paragraph\" style=\"color:#5c5c5c\">The 74HC595 device has 8bit serial in parallel out shift register that feeds directly to the 8bit D type storage register. The 8bit serial shift register has its own input clock name SH_CP. The D latch 8 bit registers use pin named ST_CP for latching the 8bit shift registers output to D latch output registers.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-2 wp-block-paragraph\" style=\"color:#5c5c5c\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; In normal port operation the connection diagram as follow<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"590\" height=\"266\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/74HC595.png\" alt=\"\" class=\"wp-image-2000\" style=\"width:800px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/74HC595.png 590w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/74HC595-300x135.png 300w\" sizes=\"(max-width: 590px) 100vw, 590px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-3 wp-block-paragraph\" style=\"color:#6c8a97\">Pin description<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"311\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-3-1024x311.jpg\" alt=\"\" class=\"wp-image-2002\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-3-1024x311.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-3-300x91.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-3-768x234.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-3.jpg 1286w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-4 wp-block-paragraph\" style=\"color:#5c5c5c\">Here slave selection bit SS use to trigger the ST_CP (for output latching). Data out through MOSI to DS and data in through Q7S to MISO. Here maximum output frequency 100MHz. Let look at the function available for this chip-<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"502\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-4-1024x502.jpg\" alt=\"\" class=\"wp-image-2006\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-4-1024x502.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-4-300x147.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-4-768x377.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-4.jpg 1284w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-5 wp-block-paragraph\" style=\"color:#5c5c5c\">Let interface the chip to AVR. Here AVR works as Master and Shift register works as Slave. One thing notes that the Master generates the clk and slave response to that clk. Since the maximum output frequency is 100MHz for the slave we select Mode0 with double SPI speed. Since from the connection diagram with MR = 1, OE = 0 and the control function it can be seen that we can easily use this shift register with-<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"82\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-13-1024x82.jpg\" alt=\"\" class=\"wp-image-2010\" style=\"width:537px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-13-1024x82.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-13-300x24.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-13-768x62.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-13.jpg 1318w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-6 wp-block-paragraph\" style=\"color:#5c5c5c\">Now let\u2019s build a SPI Read\/Write function for using this shift register. Since the microcontroller acts as Master so our first step is to initialize AVR as Master with Mode0 double SPI speed. Let\u2019s use ATmega8 for this purpose. SPI initialization function-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n#define SPI_DDR \tDDRB\n#define SPI_PORT\tPORTB\n#define SPI_MOSI    DDB3\n#define SPI_MISO\tDDB6\n#define SPI_SCK \tDDB5\n#define SPI_SS_DISPLAY   \tDDB2 \n#define SPI_CS_DISPLAY   \tPINB2\n#define SPI_SS_CONTROL   \tDDB1 \n#define SPI_CS_CONTROL   \tPINB1\nvoid init_spi(void)\n{\n\/**** Set MOSI,SS, SCK as output and MISO as input ****\/\nSPI_DDR =(1&lt;&lt;SPI_MOSI)|(1&lt;&lt;SPI_SCK)|(1&lt;&lt;SPI_SS_CONTROL)|(1&lt;&lt;SPI_SS_DISPLAY);\nSPI_DDR&amp;=~(1&lt;&lt;SPI_MISO);\n\/******************************************************\/\nSPI_PORT&amp;=~(1&lt;&lt;SPI_CS_DISPLAY);  \/\/enable latch dsplay\nSPI_PORT&amp;=~(1&lt;&lt;SPI_CS_CONTROL);  \/\/enable latch control\nSPCR|=(1&lt;&lt;SPE)|(1&lt;&lt;MSTR);\nSPSR|=(1&lt;&lt;SPI2X);\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-7 wp-block-paragraph\" style=\"color:#5c5c5c\">Now let\u2019s make a single Read\/Write function. The write procedure consist of following steps<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"162\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-15-1024x162.jpg\" alt=\"\" class=\"wp-image-2016\" style=\"width:487px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-15-1024x162.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-15-300x47.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-15-768x121.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-15.jpg 1157w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-8 wp-block-paragraph\" style=\"color:#5c5c5c\">Here 2 shift register is used, one is to control the seven segment display and another one is to display digits.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-9 wp-block-paragraph\" style=\"color:#6c8a97\">void SPIWrite_display(unsigned char datain) -&gt; display digits<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid SPIWrite_display(unsigned char datain)\n{\nSPDR=datain;\n\twhile(!(SPSR&amp;(1&lt;&lt;SPIF)))\n\t\t{\n\t\t;\/\/\/\/ Wait for transmission complete\n\t\t}\nSPI_PORT |= (1&lt;&lt;SPI_CS_DISPLAY);\n_delay_us(2); \/\/ Hold pulse\n\/\/ Disable Latch\nSPI_PORT &amp;= ~(1&lt;&lt;SPI_CS_DISPLAY);\n\/\/ Return Serial In Value in MISO\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-10 wp-block-paragraph\" style=\"color:#6c8a97\">void SPIWrite_control(unsigned char datain) -&gt; Control the display<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nvoid SPIWrite_control(unsigned char datain)\n{\nSPDR=datain;\n\twhile(!(SPSR&amp;(1&lt;&lt;SPIF)))\n\t\t{\n\t\t;\/\/\/\/ Wait for transmission complete\n\t\t}\nSPI_PORT |= (1&lt;&lt;SPI_CS_CONTROL);\n_delay_us(2); \/\/ Hold pulse\n\/\/ Disable Latch\nSPI_PORT &amp;= ~(1&lt;&lt;SPI_CS_CONTROL);\n\/\/ Return Serial In Value in MISO\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-11 wp-block-paragraph\" style=\"color:#5c5c5c\">We learn how to interface seven segment in I\/O section, we also learn how to display digits. Some changes are made to interface in SPI. Let\u2019s look-<\/p>\n\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-12 wp-block-paragraph\" style=\"color:#6c8a97\">void print(unsigned long int num) -&gt; main display part<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nunsigned long int digits&#x5B;MAX_DIGIT],i,digit&#x5B;MAX_DIGIT];\nuint8_t control;\nvoid print(unsigned long int num)\n{\nfor(i=0;num!=0;i++)\n\t{\n\tdigits&#x5B;i]=num%10;\n\tdigit&#x5B;i]=seven_segment(digits&#x5B;i]);\n\tnum=num\/10;\n\t}\n\t\/* main part display number*\/\n\tfor(i=0;i&lt;MAX_DIGIT;i++)\n\t{\t\n\t    control=pow(2,MAX_DIGIT)-1;\n\t    SPIWrite_control(control);\n\t\t_delay_ms(1);\n\t\tSPIWrite_display(digit&#x5B;i]);\n\t\tcontrol&amp;=~(1&lt;&lt;i);\n\t\tSPIWrite_control(control);\n\t\t_delay_ms(1);\n\t\tcontrol=pow(2,MAX_DIGIT)-1;\n\t\tSPIWrite_control(control);\n\t\t_delay_ms(1);\n\t\t\n\t}\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-13 wp-block-paragraph\" style=\"color:#5c5c5c\">Here MAX_DIGIT is the maximum number of seven segments we want to control. The main program as follow-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n#include&lt;avr\/io.h&gt;\n#include&lt;util\/delay.h&gt;\n#include&quot;spi.h&quot;\n#include&quot;segment.h&quot;\nint main(void)\n{\ninit_spi();\nwhile(1)\n\t{\n\tprint(3221);\n\t}\nreturn 0;\n}\n<\/pre><\/div>\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-3e41869c wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Shift-Register-with-7segment.rar\" style=\"padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--40)\">download<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-14 wp-block-paragraph\" style=\"color:#252525\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Shift-Register-with-7segment.rar\">Shift Register with 7segment.rar<\/a><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The 74HC595 device has 8bit serial in parallel out shift register that feeds directly to the 8bit D type storage register. The 8bit serial shift register has its own input clock name SH_CP. The D latch 8 bit registers use pin named ST_CP for latching the 8bit shift registers output to D latch output registers. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-1996","post","type-post","status-publish","format-standard","hentry","category-spi"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1996","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1996"}],"version-history":[{"count":16,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1996\/revisions"}],"predecessor-version":[{"id":2633,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1996\/revisions\/2633"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}