‘Main Site Blog Playground Forum Labs Stor
Signin or Register
Reference Language | Libraries | Comparison | Changes
SPI library
This ibrary allows you to communicate with SPI device, with the
Arduino a the master device
A Brief Introduction to the Serial Peripheral Interface
«spn
Serial Peripheral Interface (SPI) is asynchronous serial data protocol
‘used by microcontrollers for communicating with one or more peripheral
Aovices quickly over short distances. It ean also be used for
‘communication between two microcontrollers
Functions
+ begin
endo
% setBitOrder0
1 setCloekDivider)
* setDataModed)
“ transfer
“Due Extended SPI usageWith an SPI connection theres always one master device (usuallya Examples
microcontroller) whieh controlsthe peripheral devices Typically there g
ometricPressureSensor: Read ait
se three ines common tal the devices pressure and temperature froma sensor
“4 MISO (Master In Slave Out) ~The Slave line for sending data tothe
using SPI
master, ‘ SPIDigtalPot: Control a digital,
“‘ MOSI (Master Ovt Slave In)-"The Master line for sending datato the potentiometer using SPL
peripherals,
4 SCK Gerial Clock)-The clock pulses which synchronize data «See also
‘transmission generated by the master 4 shifcout
+ shifttnd
and one line specific for every device:
“# 88 (Slave Slect)-the pin on each deviee thatthe mast
enable and disable specific devices.
an use to
vith the master
‘When a device's Slave Select pin islow,iteommunieat
‘When it's high it ignores the master. This allows you to have multiple SPI
devices sharing the same MISO, MOSI, and CLK lines.
To write code for anew SPI device you need to note afew things:
“& Is data shifted in Most Significant Bit (SB) or Least Significant Bit
(LSB) first? This s controlled by the SPL.setBitOrder) function.
“ Is the data clock ile when high or low? Are samples on therising or
falling edge of clock pulses? These modes are controlled by the
SPI.setDataModeO function.
“# What speed isthe SPI running a? This is controlled by the
SPL.setClockDivider0 function.
The
diferent. This means you have to pay special attention tothe device's
standards loose and each device implements italittledatasheet when writing your code
Generally speaking, there are four modes of transmission. These modes
control whether datas shifted in and out onthe rising or falling edge of
the data clock signal alle the clock phase), and whether the clock is
fle wien high or low called the clock polarity). The four modes
combine polarity and phase aecording to ths table:
polarity and phase
_Bvery SPI device has a maximum allowed speed for SPI Bus, The
SPL.setClockDivider() allows you to change the clock speed to make your
Advice working properly (default is 43H).
Once you have your SPI parameters set correctly you just need to figure
‘which registers in your device control which functions, and you're good
ogo. This willbe explained in the datasheet for your device.
or more on SPI, see Wikipedia's page on SP.