{"id":1929,"date":"2024-07-12T13:05:03","date_gmt":"2024-07-12T13:05:03","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=1929"},"modified":"2024-08-11T17:21:16","modified_gmt":"2024-08-11T17:21:16","slug":"spi-serial-peripheral-interface-basic","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=1929","title":{"rendered":"SPI (Serial Peripheral Interface) Basic"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-1 wp-block-paragraph\" style=\"color:#5c5c5c\">\u00b5C support various wire communication protocols i.e. UART or UASRT communication, SPI, TWI etc. SPI is the simplest of all communication system. In SPI data communication take place between two devices, commonly known as Master and slave. The device or devices that control the operation inside the network is known as Master and the device or devices that attached known as Slave. In AVR the SPI communication uses four pins-<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"672\" height=\"154\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-6.jpg\" alt=\"\" class=\"wp-image-1933\" style=\"width:284px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-6.jpg 672w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-6-300x69.jpg 300w\" sizes=\"(max-width: 672px) 100vw, 672px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-2 wp-block-paragraph\" style=\"color:#5c5c5c\"><\/p>\n\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\">\n<p class=\"has-text-color has-link-color wp-elements-3 wp-block-paragraph\" style=\"color:#5c5c5c\">Thus pins have many alternative names. Some are listed bellow-<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"748\" height=\"198\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-7.jpg\" alt=\"\" class=\"wp-image-1939\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-7.jpg 748w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-7-300x79.jpg 300w\" sizes=\"(max-width: 748px) 100vw, 748px\" \/><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"590\" height=\"320\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/SPIprotocol.png\" alt=\"\" class=\"wp-image-1940\" style=\"width:348px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/SPIprotocol.png 590w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/SPIprotocol-300x163.png 300w\" sizes=\"(max-width: 590px) 100vw, 590px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-4 wp-block-paragraph\" style=\"color:#5c5c5c\">Atmel microcontroller can be configured as both Master and Slave. Before we discuss let looks at the SPI registers available and there uses-<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"590\" height=\"806\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/SPI.png\" alt=\"\" class=\"wp-image-1944\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/SPI.png 590w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/SPI-220x300.png 220w\" sizes=\"(max-width: 590px) 100vw, 590px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"590\" height=\"88\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/spia.png\" alt=\"\" class=\"wp-image-1950\" style=\"width:429px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/spia.png 590w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/spia-300x45.png 300w\" sizes=\"(max-width: 590px) 100vw, 590px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"606\" height=\"237\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-9.jpg\" alt=\"\" class=\"wp-image-1952\" style=\"width:327px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-9.jpg 606w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-9-300x117.jpg 300w\" sizes=\"(max-width: 606px) 100vw, 606px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-5 wp-block-paragraph\" style=\"color:#5c5c5c\">The coding for configure as Master 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#define SPI_DDR \t           DDRx\n#define SPI_ MOSI \t           DDx\n#define SPI_ SCK \t           DDx\n#define SPI_SS                 DDx\n#define SPI_ MISO \t           DDx\n#define SPI_PORT \t           PORTx\n#define SPI_CS\t\t           PINx\nSPI_DDR|=(1&lt;&lt; SPI_ MOSI)|(1&lt;&lt; SPI_ SCK)|(1&lt;&lt; SPI_SS);\nSPI_DDR&amp;=~(1&lt;&lt; SPI_ MISO);\nSPI_PORT&amp;=~(1&lt;&lt;SPI_CS);               \/\/Chip selection\nSPCR|=(1&lt;&lt;SPE);                       \/\/SPI Enable   \nSPCR|=(1&lt;&lt;MSTR);                      \/\/Set as Master\nSPCR|=(1&lt;&lt;SPR1)|(1&lt;&lt;SPR0);            \/\/Clock select\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"590\" height=\"88\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/spib.png\" alt=\"\" class=\"wp-image-1958\" style=\"width:449px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/spib.png 590w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/spib-300x45.png 300w\" sizes=\"(max-width: 590px) 100vw, 590px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"192\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-10.jpg\" alt=\"\" class=\"wp-image-1960\" style=\"width:335px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-10.jpg 624w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/07\/function-10-300x92.jpg 300w\" sizes=\"(max-width: 624px) 100vw, 624px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-6 wp-block-paragraph\" style=\"color:#5c5c5c\">The coding for configuring as Slave 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#define SPI_DDR \t           DDRx\n#define SPI_ MOSI \t           DDx\n#define SPI_ SCK \t           DDx\n#define SPI_SS \t               DDx\n#define SPI_ MISO \t           DDx\n#define SPI_PORT \t           PORTx\n#define SPI_CS\t\t           PINx\nSPI_DDR&amp;=~((1&lt;&lt; SPI_ MOSI)|(1&lt;&lt; SPI_ SCK)|(1&lt;&lt; SPI_SS);\nSPI_DDR|=(1&lt;&lt; SPI_ MISO);\nSPCR|=(1&lt;&lt;SPE);                       \/\/SPI Enable   \nSPCR&amp;=~(1&lt;&lt;MSTR);                     \/\/Set as Slave\nSPCR|=(1&lt;&lt;SPR1)|(1&lt;&lt;SPR0);            \/\/Clock select\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-7 wp-block-paragraph\" style=\"color:#5c5c5c\">After selecting AVR Master or Slave, it is time to communicate between master and slave. For data communication SPDR register is used which is full duplex. Means that data can be both transmit and receive at the same time. So that data can be transmit by writing data to SPDR register, that-<\/p>\n\n\n\n<ul style=\"color:#6c8a97\" class=\"wp-block-list has-text-color has-link-color wp-elements-8\">\n<li class=\"has-upper-heading-font-size\">SPDR=data<\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-9 wp-block-paragraph\" style=\"color:#5c5c5c\">And data can be received by receiving the SPDR register that<\/p>\n\n\n\n<ul style=\"color:#6c8a97\" class=\"wp-block-list has-text-color has-link-color has-upper-heading-font-size wp-elements-10\">\n<li>data=SPDR<\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-11 wp-block-paragraph\" style=\"color:#5c5c5c\">So it is very easy to transmit and receive data through SPDR register, but it should be configure first if SPDR register is ready to transmit\/receive data. It is interesting that SPIF bit of SPSR register is set whenever the data transmit is complete even if interrupts are not enable. So we use the flag register for our safety and error free programming. So we separate SPI read and SPI write operation.<\/p>\n\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\">\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-12 wp-block-paragraph\" style=\"color:#6c8a97\"><strong>SPI Write<\/strong><\/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(unsigned char datain)\n{\n\tSPDR = datain; \/\/ send the data\nwhile(!(SPSR &amp; (1&lt;&lt;SPIF)))\n\t{\n\t; \/\/ wait until transmission is complete\n\t}\n}\n<\/pre><\/div><\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-13 wp-block-paragraph\" style=\"color:#6c8a97\"><strong>SPI Read<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nunsigned char SPIRead(void)\n{\n\twhile(!(SPSR &amp; (1&lt;&lt;SPIF)))\n\t{\n\t\t; \/\/ wait until all data is received\n\t}\nreturn SPDR;\n}\n<\/pre><\/div><\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-14 wp-block-paragraph\" style=\"color:#5c5c5c\">For your better understand we give a program in which ATmega32 as Master and ATmega8 as Slave. We will discuss more about SPI and communication between AVR with other peripherals in the next article. Download the program and understand the logic-<\/p>\n\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\/Test-data.rar\" style=\"border-style:none;border-width:0px;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-15 wp-block-paragraph\" style=\"color:#6c8a97\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Test-data.rar\">Test data.rar<\/a><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u00b5C support various wire communication protocols i.e. UART or UASRT communication, SPI, TWI etc. SPI is the simplest of all communication system. In SPI data communication take place between two devices, commonly known as Master and slave. The device or devices that control the operation inside the network is known as Master and the device [&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-1929","post","type-post","status-publish","format-standard","hentry","category-spi"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1929","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=1929"}],"version-history":[{"count":19,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1929\/revisions"}],"predecessor-version":[{"id":2629,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1929\/revisions\/2629"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}