{"id":2027,"date":"2024-07-14T16:27:38","date_gmt":"2024-07-14T16:27:38","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=2027"},"modified":"2024-08-11T17:27:47","modified_gmt":"2024-08-11T17:27:47","slug":"increasing-i-o-port-mcp23s17","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=2027","title":{"rendered":"Increasing I\/O Port (MCP23s17)"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-1 wp-block-paragraph\" style=\"color:#5c5c5c\">In this article we increase I\/O port with the help of IC MCP23S17 which has 16-bit remote bidirectional I\/O port with High-speed SPI interface (10 MHz).<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"590\" height=\"228\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/MCP23S17.png\" alt=\"\" class=\"wp-image-2030\" style=\"width:813px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/MCP23S17.png 590w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/MCP23S17-300x116.png 300w\" sizes=\"(max-width: 590px) 100vw, 590px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"307\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/mcp-interface-1024x307.jpg\" alt=\"\" class=\"wp-image-2031\" style=\"width:800px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/mcp-interface-1024x307.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/mcp-interface-300x90.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/mcp-interface-768x230.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/mcp-interface.jpg 1431w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-2 wp-block-paragraph\" style=\"color:#5c5c5c\">Let\u2019s look at the Control register for addressing mode-<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"590\" height=\"504\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/Increasing.png\" alt=\"\" class=\"wp-image-2033\" style=\"width:634px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/Increasing.png 590w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/Increasing-300x256.png 300w\" sizes=\"(max-width: 590px) 100vw, 590px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-3 wp-block-paragraph\" style=\"color:#5c5c5c\">One register (IOCON) is shared between the two ports. The PORTA registers are identical to the PORTB registers, therefore, they will be referred to without differentiating between the port designation (i.e., they will not have the \u201cA\u201d or \u201cB\u201d designator assigned). So you can change either IOCONA or IOCONB.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-4 wp-block-paragraph\" style=\"color:#5c5c5c\">The MCP23S17 CONTROL REGISTER SUMMARY (IOCON.BANK = 0)<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"586\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-5-1024x586.jpg\" alt=\"\" class=\"wp-image-2038\" style=\"width:701px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-5-1024x586.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-5-300x172.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-5-768x440.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/table-5.jpg 1289w\" 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\">Here MCP23S17 works in SPI interface and MCP23017 works on I<sup>2<\/sup>C interface. The necessary steps of configuration as I\/O PORT is-<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"214\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-16-1024x214.jpg\" alt=\"\" class=\"wp-image-2042\" style=\"width:560px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-16-1024x214.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-16-300x63.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-16-768x160.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-16.jpg 1395w\" 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\">Let\u2019s look at the SPI protocol for this IC<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"343\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-17-1024x343.jpg\" alt=\"\" class=\"wp-image-2045\" style=\"width:699px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-17-1024x343.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-17-300x100.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-17-768x257.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-17.jpg 1384w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-7 wp-block-paragraph\" style=\"color:#5c5c5c\">Thus for Read\/Write operation<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-8 wp-block-paragraph\" style=\"color:#5c5c5c\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Read &nbsp;&#8212;&#8211;&gt;SPDR=ID|(ADDRESS&lt;&lt;1)|READ<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-9 wp-block-paragraph\" style=\"color:#5c5c5c\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Write &#8212;&#8211;&gt;SPDR=ID|(ADDRESS&lt;&lt;1)|WRITE<\/p>\n\n\n\n<ul style=\"color:#5c5c5c\" class=\"wp-block-list has-text-color has-link-color wp-elements-10\">\n<li>Byte 2<\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-11 wp-block-paragraph\" style=\"color:#5c5c5c\">Register Address \u2013 Select the desire register from IOCON.BANK = 0 table.<\/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\">Write Operation<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-13 wp-block-paragraph\" style=\"color:#5c5c5c\">For write operation we need three steps. First we send Byte 1 that content Device ID, Address and Read operation. Then we send the Register address and after that we send register data. The control code for write operation as follow-<\/p>\n\n\n\n<ul style=\"color:#6c8a97\" class=\"wp-block-list has-text-color has-link-color wp-elements-14\">\n<li>void SPIWrite(uint8_t add,uint8_t data) -&gt; Write data to the register<\/li>\n<\/ul>\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_SLAVE_ID\t0x40\n#define SPI_SLAVE_ADD\t0x00\n#define SPI_SLAVE_WRITE\t0x00\nvoid SPIWrite(uint8_t add,uint8_t data)\n{\nSPI_PORT &amp;= ~(1&lt;&lt;SPI_CS);\/\/slave activated\n\/\/initilaze salve device\nSPDR=SPI_SLAVE_ID|(SPI_SLAVE_ADD&lt;&lt;1)|SPI_SLAVE_WRITE;\nwhile(!(SPSR&amp;(1&lt;&lt;SPIF)));\nSPDR=add;\nwhile(!(SPSR&amp;(1&lt;&lt;SPIF)));\nSPDR=data;\nwhile(!(SPSR&amp;(1&lt;&lt;SPIF)));\nSPI_PORT |= (1&lt;&lt;SPI_CS);\/\/slave deactivate\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-15 wp-block-paragraph\" style=\"color:#5c5c5c\">i.e. SPIWrite(IOCONA,0x28); \/\/ I\/O Control Register: BANK=0, SEQOP=1, HAEN=1<\/p>\n\n\n\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-16 wp-block-paragraph\" style=\"color:#6c8a97\">Read Operation<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-17 wp-block-paragraph\" style=\"color:#5c5c5c\">For read operation the first and second steps are identical. It is interesting that the MCP23S17 does not return any value until we write some value to it. The value should be any value.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nuint8_t SPIRead(uint8_t add)\n{\nSPI_PORT &amp;= ~(1&lt;&lt;SPI_CS);\/\/slave activated\n\/\/initilaze salve device\nSPDR=SPI_SLAVE_ID|(SPI_SLAVE_ADD&lt;&lt;1)|SPI_SLAVE_READ;\nwhile(!(SPSR&amp;(1&lt;&lt;SPIF)));\nSPDR=add;\nwhile(!(SPSR&amp;(1&lt;&lt;SPIF)));\n\/\/ Send Dummy transmission for reading the data\nSPDR = 0x00;\n\/\/ Wait for transmission complete\nwhile(!(SPSR&amp;(1&lt;&lt;SPIF)));\nSPI_PORT |= (1&lt;&lt;SPI_CS);\/\/slave deactivate\nreturn (SPDR);\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-18 wp-block-paragraph\" style=\"color:#5c5c5c\">i.e. PORTD=SPIRead(GPIOB); \/\/ PORTD is equal to GPIOB of MCP23S17<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-19 wp-block-paragraph\" style=\"color:#5c5c5c\">Let\u2019s make a program in which we use GPB Port as input with internal pull register active and GPA as output. In this program GPA bits are set from up to down and if GPB2 pin press then down to up. Download the spi.c and spi.h files. The man 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\/***************************************************************\/\n\/***************************************************************\/\n\/********      Subeer Kumar Sarkar                **************\/\n\/********      Electrical &amp; Electronic Engineer   **************\/\n\/***************************************************************\/\n\/***************************************************************\/\n#include&lt;avr\/io.h&gt;\n#include&lt;util\/delay.h&gt;\n#include&quot;spi.h&quot;\nint main(void)\n{\nSPIInit();\nuint8_t pushButton,shift=1;\n\/******************** initialize MCP23S17 *******************\/\n\/\/ I\/O Control Register: BANK=0, SEQOP=1, HAEN=1(Enable Addressing)\nSPIWrite(IOCONA,0x28);\nSPIWrite(IODIRA,0x00); \/\/GPIOA as output\nSPIWrite(IODIRB,0xff); \/\/GPIOB as input\nSPIWrite(GPPUB,0xff);  \/\/enable pull-up resister for GPB\n_delay_ms(5);\n\/*************************************************************\/\nwhile(1)\n{\n\tpushButton=SPIRead(GPIOB);\n\tif(pushButton == 0b11111011)\n\t{\n\t\tfor(int i=7;i&gt;=0;i--)\n\t\t{\n\t\t\tSPIWrite(GPIOA,(1&lt;&lt;i));\n\t\t\t_delay_ms(50);\n\t\t}\n\t}\n\telse \n\t\tSPIWrite(GPIOA,shift);\n\t\t_delay_ms(50);\n\t\tshift=(shift&lt;&lt;1);\n\t\tif(shift == 0)\n\t\tshift=1;\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\/IO-Expender.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-20 wp-block-paragraph\" style=\"color:#252525\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/IO-Expender.rar\">IO Expender.rar<\/a><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this article we increase I\/O port with the help of IC MCP23S17 which has 16-bit remote bidirectional I\/O port with High-speed SPI interface (10 MHz). Let\u2019s look at the Control register for addressing mode- One register (IOCON) is shared between the two ports. The PORTA registers are identical to the PORTB registers, therefore, they [&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-2027","post","type-post","status-publish","format-standard","hentry","category-spi"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2027","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=2027"}],"version-history":[{"count":17,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2027\/revisions"}],"predecessor-version":[{"id":2635,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/2027\/revisions\/2635"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}