{"id":8261,"date":"2022-03-28T15:53:29","date_gmt":"2022-03-28T15:53:29","guid":{"rendered":"https:\/\/www.qec.tw\/?p=8261"},"modified":"2025-11-11T13:33:05","modified_gmt":"2025-11-11T13:33:05","slug":"spi-library","status":"publish","type":"post","link":"https:\/\/www.qec.tw\/zh\/86duino-library\/spi-library\/","title":{"rendered":"SPI library"},"content":{"rendered":"<p class=\"wp-block-paragraph\">This library allows you to communicate with SPI devices, with the 86Duino as the master device.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To use this library<br><code>#include &lt;SPI.h&gt;<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">A Brief Introduction to the Serial Peripheral Interface (SPI)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Serial Peripheral Interface (SPI) is a synchronous serial data protocol used by microcontrollers for communicating with one or more peripheral devices quickly over short distances. It can also be used for communication between two microcontrollers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With an SPI connection there is always one master device (usually a microcontroller) which controls the peripheral devices. Typically there are three lines common to all the devices:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"translation-block\"><strong>MISO<\/strong>&nbsp;(Master In Slave Out) \u2013 The Slave line for sending data to the master<\/li>\n\n\n\n<li class=\"translation-block\"><strong>MOSI<\/strong>&nbsp;(Master Out Slave In) \u2013 The Master line for sending data to the peripherals<\/li>\n\n\n\n<li><strong>SCK<\/strong>&nbsp;(Serial Clock) \u2013 The clock pulses which synchronize data transmission generated by the master<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">and one line specific for every device:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"translation-block\"><strong>SS<\/strong>&nbsp;(Slave Select) \u2013 the pin on each device that the master can use to enable and disable specific devices.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">When a device\u2019s Slave Select pin is low, it communicates with the master. When it\u2019s high, it ignores the master. This allows you to have multiple SPI devices sharing the same MISO, MOSI, and SCK lines.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To write code for a new SPI device you need to note a few things:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"translation-block\">Is data shifted in Most Significant Bit (MSB) or Least Significant Bit (LSB) first? <br>This is controlled by the&nbsp;<code>SPI.setBitOrder()<\/code>&nbsp;function.<\/li>\n\n\n\n<li class=\"translation-block\">Is the data clock idle when high or low? <br>Are samples on the rising or falling edge of clock pulses? These modes are controlled by the&nbsp;<code>SPI.setDataMode()<\/code>&nbsp;function.<\/li>\n\n\n\n<li class=\"translation-block\">What speed is the SPI running at? <br>This is controlled by the&nbsp;<code>SPI.setClockDivider()<\/code>&nbsp;function.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The SPI standard is loose and each device implements it a little differently. This means you have to pay special attention to the device\u2019s datasheet when writing your code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph translation-block\">Generally speaking, there are four modes of transmission. These modes control whether data is shifted in and out on the rising or falling edge of the data clock signal (called the clock&nbsp;<strong>phase<\/strong>) and whether the clock is idle when high or low (called the clock&nbsp;<strong>polarity<\/strong>). The four modes combine polarity and phase according to this table:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Mode<\/strong><\/td><td><strong>Clock Polarity (CPOL)<\/strong><\/td><td><strong>Clock Phase (CPHA)<\/strong><\/td><\/tr><tr><td><code>SPI_MODE0<\/code><\/td><td>0<\/td><td>0<\/td><\/tr><tr><td><code>SPI_MODE1<\/code><\/td><td>0<\/td><td>1<\/td><\/tr><tr><td><code>SPI_MODE2<\/code><\/td><td>1<\/td><td>0<\/td><\/tr><tr><td><code>SPI_MODE3<\/code><\/td><td>1<\/td><td>1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph translation-block\">The&nbsp;<code>SPI.setDataMode()<\/code>&nbsp;function lets you set the mode to control clock polarity and phase.<\/p>\n\n\n\n<p class=\"wp-block-paragraph translation-block\">Every SPI device has a maximum allowed speed for SPI Bus. The&nbsp;<code>SPI.setClockDivider()<\/code>&nbsp;allows you to change the clock speed to make your device working properly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once you have your SPI parameters set correctly you just need to figure which registers in your device control which functions, and you\u2019re good to go. This will be explained in the datasheet for your device.<\/p>\n\n\n\n<p class=\"wp-block-paragraph translation-block\">For more on SPI, see&nbsp;<a href=\"http:\/\/en.wikipedia.org\/wiki\/Serial_Peripheral_Interface_Bus\" target=\"_self\">Wikipedia\u2019s page on SPI.<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Connections<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The following table display which pins the SPI lines are broken out on the different 86Duino boards:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>86Duino Board<\/strong><\/td><td><strong>MOSI<\/strong><\/td><td><strong>MISO<\/strong><\/td><td><strong>SCK<\/strong><\/td><td><strong>SS<\/strong><\/td><\/tr><tr><td>Zero<\/td><td>SPIDO<\/td><td>SPIDI<\/td><td>SPICLK<\/td><td>SPICS<\/td><\/tr><tr><td>One<\/td><td>SPIDO<\/td><td>SPIDI<\/td><td>SPICLK<\/td><td>SPICS<\/td><\/tr><tr><td>Educake<\/td><td>\u2013<\/td><td>\u2013<\/td><td>\u2013<\/td><td>\u2013<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Note that MISO, MOSI, SCK, and SS are available in a consistent physical location on the SPI header; this is useful, for example, in designing a shield that works on every board.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter\"><a href=\"http:\/\/www.86duino.com\/wp-content\/uploads\/2014\/01\/86duino_spi.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"http:\/\/www.86duino.com\/wp-content\/uploads\/2014\/01\/86duino_spi-300x212.png\" alt=\"86Duino SPI Connector\" class=\"wp-image-6178\"\/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u529f\u80fd<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.qec.tw\/zh\/86duino-library\/spi\/spi-begin\/\">begin<\/a>()<\/li>\n\n\n\n<li><a href=\"https:\/\/www.qec.tw\/zh\/86duino-library\/spi\/spi-end\/\">end<\/a>()<\/li>\n\n\n\n<li><a href=\"https:\/\/www.qec.tw\/zh\/86duino-library\/spi\/spi-setbitorder\/\">setBitOrder<\/a>()<\/li>\n\n\n\n<li><a href=\"https:\/\/www.qec.tw\/zh\/86duino-library\/spi\/spi-setclockdivider\/\">setClockDivider<\/a>()<\/li>\n\n\n\n<li><a href=\"https:\/\/www.qec.tw\/zh\/86duino-library\/spi\/spi-setdatamode\/\">setDataMode<\/a>()<\/li>\n\n\n\n<li><a href=\"https:\/\/www.qec.tw\/zh\/86duino-library\/spi\/spi-setss\/\">setSS<\/a>()<\/li>\n\n\n\n<li><a href=\"https:\/\/www.qec.tw\/zh\/86duino-library\/spi\/spi-transfer\/\">transfer<\/a>()<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u7bc4\u4f8b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph translation-block\">The following are examples of the SPI library from the&nbsp;<a href=\"http:\/\/arduino.cc\/en\/Tutorial\/HomePage\" target=\"_self\">Arduino Tutorial<\/a>&nbsp;that can work on the 86Duino boards (but, when you refer to the circuits in these examples, note that the location of 86Duino\u2019s SPI pins is different from the Arduino boards adopted in these examples):<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"translation-block\"><a href=\"http:\/\/arduino.cc\/en\/Tutorial\/BarometricPressureSensor\" target=\"_self\">BarometricPressureSensor<\/a>: Read air pressure and temperature from a sensor using SPI<\/li>\n\n\n\n<li class=\"translation-block\"><a href=\"http:\/\/arduino.cc\/en\/Tutorial\/SPIDigitalPot\" target=\"_self\">SPIDigitalPot<\/a>: Control a digital potentiometer using SPI<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u53c3\u8003<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"http:\/\/www.86duino.com\/?p=1651\" target=\"_blank\" rel=\"noopener\">shiftOut()<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/www.86duino.com\/?p=1652\" target=\"_blank\" rel=\"noopener\">shiftIn()<\/a><\/li>\n<\/ul>\n\n\n<hr>\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/www.qec.tw\/zh\/86duino\/libraries\/\">\u51fd\u5f0f\u5eab\u53c3\u8003\u4e3b\u9801\u9762<\/a><\/h3>\n\n\n\n<p class=\"wp-block-paragraph translation-block\"><em>86Duino \u53c3\u8003\u7684\u6587\u672c\u662f\u6839\u64da <a href=\"https:\/\/creativecommons.org\/licenses\/by-sa\/3.0\/\" target=\"_self\">\u77e5\u8b58\u5171\u4eab\u7f72\u540d-\u76f8\u540c\u65b9\u5f0f\u5206\u4eab 3.0 \u8a31\u53ef\u8b49<\/a>\uff0c\u90e8\u5206\u6587\u672c\u662f\u5f9e <a href=\"https:\/\/arduino.cc\/en\/Reference\/HomePage\" target=\"_self\">Arduino \u53c3\u8003<\/a> \u4fee\u6539\u7684\u3002 \u53c3\u8003\u4e2d\u7684\u4ee3\u78bc\u793a\u4f8b\u5df2\u767c\u4f48\u5230\u516c\u5171\u9818\u57df\u3002<\/em><\/p>","protected":false},"excerpt":{"rendered":"<p>This library allows you to communicate with SPI devices, with the 86Duino as the master device. To use this library#include &lt;SPI.h&gt; A Brief Introduction to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8262,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[198,203],"tags":[162,314],"class_list":["post-8261","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-86duino-library","category-spi","tag-86duino","tag-lib"],"_links":{"self":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts\/8261","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/comments?post=8261"}],"version-history":[{"count":1,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts\/8261\/revisions"}],"predecessor-version":[{"id":16987,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/posts\/8261\/revisions\/16987"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/media\/8262"}],"wp:attachment":[{"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/media?parent=8261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/categories?post=8261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.qec.tw\/zh\/wp-json\/wp\/v2\/tags?post=8261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}