{"id":217,"date":"2015-07-10T16:32:18","date_gmt":"2015-07-10T16:32:18","guid":{"rendered":"https:\/\/arduino-developer.com\/arduino\/?p=217"},"modified":"2020-02-26T16:24:34","modified_gmt":"2020-02-26T16:24:34","slug":"using-spi-library","status":"publish","type":"post","link":"https:\/\/arduino-developer.com\/arduino\/programming\/spi\/using-spi-library","title":{"rendered":"Using SPI Library"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Resources<\/h4>\n\n\n\n<p><a href=\"https:\/\/www.arduino.cc\/en\/reference\/SPI\">https:\/\/www.arduino.cc\/en\/reference\/SPI<\/a><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Arduino As SPI Master<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\n#include &lt;SPI.h>\n\n  SPI.begin();\n\n  SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0));   \/\/  Speed (e.g.15000000=15MHz), BitOrder (MSBFIRST), Mode (SPI_MODE0 - SPI_MODE3)\n\n  rx_data = SPI.transfer(0x01);\n  rx_data = SPI.transfer(0x02);\n \n  SPI.endTransaction();\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Arduino As SPI Slave<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;SPI.h>\n\nvolatile byte spi_receive_buffer [100];\nvolatile byte spi_receive_index;\n\n  \/\/----- CONFIGURE SPI SLAVE PORT -----\n  pinMode(SS,INPUT_PULLUP);\n  pinMode(MOSI,INPUT_PULLUP);\n  pinMode(SCK,INPUT);\n  pinMode(MISO,OUTPUT);\n  \n  \/\/Turn on SPI in Slave Mode using SPI Control Register\n  SPCR |= _BV(SPE);     \/\/SPI default mode 0 (CPOL=0 and CPHA=0), SPE = SPI enable\n\n  \/\/Turn on SPI interrupts\n  SPCR |= _BV(SPIE);\n\n  \/\/Turn ON interrupt for SPI communication\n  SPI.attachInterrupt();        \/\/If a data is received from master the Interrupt Routine is called and the received value is taken from SPDR (SPI data Register)\n\n  spi_receive_index = 0;\n  \n\n\n\/\/*******************************************\n\/\/*******************************************\n\/\/********** SPI RECEIVE INTERRUPT **********\n\/\/*******************************************\n\/\/*******************************************\nISR (SPI_STC_vect)\n{\n\n  if (spi_receive_index &lt; sizeof(spi_receive_buffer))\n  {\n    \/\/Get received byte\n    spi_receive_buffer[spi_receive_index] = SPDR;\n  \n    \/\/Set next byte to be transmitted (on next byte transfer)\n    SPDR = spi_receive_buffer[spi_receive_index] + 0x10;      \/\/Send a copy with 0x10 added for this test\n  }\n  else\n  {\n    SPDR = 0;;\n  }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Resources https:\/\/www.arduino.cc\/en\/reference\/SPI Arduino As SPI Master Arduino As SPI Slave<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"class_list":["post-217","post","type-post","status-publish","format-standard","hentry","category-spi"],"_links":{"self":[{"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/posts\/217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/comments?post=217"}],"version-history":[{"count":5,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/posts\/217\/revisions"}],"predecessor-version":[{"id":548,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/posts\/217\/revisions\/548"}],"wp:attachment":[{"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/media?parent=217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/categories?post=217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arduino-developer.com\/arduino\/wp-json\/wp\/v2\/tags?post=217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}