1. Objectives
-
Introduction to serial communication protocols
-
Using the Serial Peripheral Interface (SPI) protocol
2. Parts List
-
LPC1769 LPCXpresso board
-
USB A-Type to Mini-B cable
-
Breadboard
-
Jumper wires
-
Pololu LSM303D 3D compass and accelerometer module
Figure 1. Pololu LSM303D 3D Compass and Accelerometer Module
3. Background
In this experiment, you will use one of the serial communication interfaces of the LPC1769 microcontroller, specifically the SPI interface (through the SSP controller), to interact with a digital accelerometer.
3.1. SPI Communication Using the LPC1769 Microcontroller
3.1.1. Serial vs. Parallel Communication
Serial communication is the process of sending data one bit at a time, sequentially. In contrast, parallel communication involves sending multiple bits at the same time, as illustrated in the Parallel vs. Serial Communication figure below.
Some of the main differences between serial and parallel communication are:
-
A parallel link requires more wires, occupying more space and resulting in higher cost.
-
To keep all wires in a parallel link synchronized, the link rate is limited. In contrast, serial links can sustain much higher clock rates.
-
Parallel links are more susceptible to crosstalk interference.
-
Parallel communication between ICs require more pins, increasing the IC cost.
-
Parallel communication is easier to implement because it does not require data serialization and deserialization.
Serial communication is becoming more common for transmitting data between a computer and a peripheral device or even another computer, as improved signal integrity and transmission speeds in newer serial technologies have begun to outweigh the parallel bus’s advantages.
3.1.2. Serial Communication Protocols
Serial communication standards include USB, FireWire, Serial ATA (SATA), PCI Express (PCIe), and Ethernet. Serial protocols commonly used in embedded systems include UART, I2C, and SPI.
Serial communication protocols can be synchronous or asynchronous. An asynchronous protocol sends a start signal prior to each code word, and a stop signal after each code word. UART is an asynchronous serial protocol supported by UART interfaces.
A synchronous serial protocol sends a clock signal on a dedicated wire. Additional wire(s) are required for data. I2C and SPI are synchronous serial protocols.
3.1.3. LPC1769 Serial Interfaces
The LPC1769 microcontroller provides the following serial interfaces (LPC1769 Manual):
-
Two Synchronous Serial Port (SSP) controllers, SSP0 and SSP1, with multi-protocol capabilities. They can operate as SPI, 4-wire TI SSI, or Microwire bus controllers.
-
A Serial Peripheral Interface (SPI) controller. SSP0 is intended to be used as an alternative for the SPI interface. SPI is included as a legacy peripheral.
-
Three enhanced Inter-Integrated Circuit (I2C) bus interfaces, one supporting the full I2C specification, and two with standard port pins. I2C is pronounced I-squared-C.
-
Four UARTs.
-
A two-channel CAN controller.
-
Ethernet MAC with RMII interface and dedicated DMA controller.
-
USB 2.0 full-speed controller that can be configured for either device, host, or OTG operation with an on-chip PHY for device and host functions and a dedicated DMA controller.
In this experiment, we will use the SSP interface configured for the SPI protocol.
3.1.4. Serial Peripheral Interface (SPI)
SPI is a four-wire, full-duplex, master-slave bus that was created by
Motorola. There can be only a single master. Multiple slaves are
allowed with individual slave select (SS
or SSEL
) lines. The
four wires are:
-
SCLK
: Serial Clock (output from master) -
MOSI
: Master Output, Slave Input (output from master) -
MISO
: Master Input, Slave Output (output from slave) -
SSEL
: Slave Select (active low, output from master) — one per slave
The microcontroller is usually the master. It uses the MOSI
pin to
send data, and the MISO
pin to read data. The SCLK
pin dictates
the transmission rate; a bit is sent/received every clock pulse. A
simple timing diagram for writing data is shown below.
The slave select (SSEL
) signal is used to select the slave in a
data transfer. SSEL
is active low: it must be low before the
transaction begins, and must stay low for the duration of the
transaction.
To connect multiple slaves, you need a dedicated SSEL
for each
slave. All slaves can share the remaining wires.
Even though the SSEL
signal is a part of the SPI protocol, it is not
uncommon to leave its control to the software instead of the SPI/SSP
controller. The LPC176x manual states that "This
signal is not directly driven by the master. It could be driven by a
simple general purpose I/O under software control." In the LPCXpresso
Base Board, SSEL
is connected to GPIO P2.2. It should be driven low
(by software) prior to placing data in the Data Register (DR
), and
then switched back to high.
3.1.5. Using SSP/SPI in LPC1769
The section describes how to use the SSP interface of the LPC1769 microcontroller as an SPI interface by listing the involved registers and their functions.
Data Register (DR
)
The data to be sent serially must be loaded into the SSP Data Register
(LPC_SSP1→DR
). The serial transfer rate is controlled by the SSP
clock as described below.
The To transmit the value stored in
Similarly, to receive a new value and store in
|
Every time you send data by writing to the Also, to be able to receive something from a slave, you need to trigger the two way communication by putting dummy data in the DR. |
SSP Control Registers
There are two control registers for the SSP1
interface (see
LPC17xx.h
):
-
SSP1CR0
: can be accessed asLPC_SSP1→CR0
-
SSP1CR1
: can be accessed asLPC_SSP1→CR1
The CR0
register has 5 fields:
-
Data size (bits 0-3): the number of bits transferred in each frame.
-
Frame Format (bits 4-5): the serial protocol to be used.
00 SPI
01 TI
10 Microwire
11 Not supported
-
Clock Out Polarity (bit 6): should be 0 in our application.
-
Clock Out Phase (bit 7): should be 0 in our application.
-
Serial Clock Rate (
SCR
) (bits 8-15): used with the Clock Prescale Register (CPSR
) to control the SSP clock. This is crucial when the SSP peripheral requires a specific value or range of frequencies.
The CR1
register has 4 fields, the most crucial of which is bit 1:
SSP enable.
In addition to CR0
and CR1
, there is the SSP Clock Prescale
Register (CPSR
). The CSPR
register contains a single field,
CPSDVSR
, in bits 0-7. Its remaining bits are reserved (unused).
The SSP clock frequency is calculated using the formula:
The SSP’s |
For details, see Tables 371, 372, and 375 in the LPC176x manual. |
3.2. Using the LSM303D Accelerometer
The LSM303D chip is a system-in-package featuring two devices: a 3D digital linear acceleration sensor, and a 3D digital magnetic sensor. It includes both I2C and SPI interfaces. It also can be configured to generate an interrupt signal for free fall, motion detection, and magnetic field detection. The magnetic and accelerometer parts can be enabled or put into power-down mode separately.
In this experiment, we will focus on the digital accelerometer. Nonetheless, the digital magnetic sensor, or compass, can be used by following similar procedures, as documented in the chip datasheet [lsm303d-manual].
To be able to conveniently use the LSM303D chip, we will be using the Pololu carrier module/board [lsm303d-pololu].
3.2.1. Accelerometers
An accelerometer is an electromechanical device that will measure acceleration forces. These forces may be static, like the constant force of gravity pulling at your feet, or they could be dynamic, caused by moving or vibrating the accelerometer.
An accelerometer can help your project understand its surroundings better. Is it driving uphill? Is it going to fall over when it takes another step? Is it flying horizontally? A good programmer can write code to answer all of these questions using the data provided by an accelerometer. An accelerometer can even help analyze problems in a car engine using vibration testing.
In the computing world, IBM and Apple have been using accelerometers in their laptops to protect hard drives from damage. If you accidentally drop the laptop, the accelerometer detects the sudden freefall, and switches the hard drive off so the heads don’t crash on the platters. In a similar fashion, high-g accelerometers are the industry standard way of detecting car crashes and deploying airbags at just the right time. [accelerometers]
3.2.2. The LSM303D SPI Interface
The LSM303D chip provides an SPI interface with the device acting as a
slave on the SPI bus. It allows writing and reading the registers of
the device. The serial interface interacts with the outside world
through 4 wires: CS
, SPC
, SDI
and SDO
.
Check the LSM303D datasheet. Read the ``SPI bus Interfaces'' section to find out how to read from and write to the registers of LSM303D. |
3.2.3. Using the LSM303D Accelerometer
The accelerometer measures acceleration along the three dimensions, and makes them available in the following registers:
OUT_X_L_A
(28h),OUT_X_H_A
(29h)-
X-axis acceleration data. The value is expressed in 16 bits as 2’s complement.
OUT_Y_L_A
(2Ah),OUT_X_H_A
(2Bh)-
Y-axis acceleration data. The value is expressed in 16 bits as 2’s complement.
OUT_X_L_A
(2Ch),OUT_X_H_A
(2Dh)-
Z-axis acceleration data. The value is expressed in 16 bits as 2’s complement.
A simple program that shows how to read data from the accelerometer is available in the AN3192 Application note document, page 10. |
The Directions of the Three Accelerometer Readings figure shows the directions corresponding to positive values along each of the three axes, relative to the chip.
You must configure the |
Reading data from the accelerometer device is completed in 16 clock pulses. Thus, in order to read the data correctly from the registers, you have 2 options: send multiple 8-bit data, or send 16-bit data. The description is as follows:
|
4. Tasks
-
Use the LPC1769’s SSP/SPI interface to read the accelerometer data from the LSM303D device.
-
Write a simple application to indicate different stationary positions. For example, indicate whether the device is tilted to the right or to the left, tilted forward or backward, and whether it’s facing upward or downward. Use some output device to reflect this data in real-time. The following table summarizes the readings corresponding to each of the six stationary positions.
Stationary Position Ax Ay Az Z down
0
0
-
Z up
0
0
+
Y down
0
-
0
Y up
0
+
0
X down
-
0
0
X up
+
0
0
5. Resources
- [base-board-manual]
-
Embedded Artists AB. 'LPCXpresso Base Board Rev B User’s Guide'. 2013-01-25.
http://www.embeddedartists.com/sites/default/files/support/xpr/base/LPCXpresso_BaseBoard_rev_B_Users_Guide.pdf - [lpc1769-manual]
-
NXP Semiconductors. 'UM10360 LPC176x/5x User manual'. Rev. 3.1. 2 April 2014.
http://www.nxp.com/documents/user_manual/UM10360.pdf - [accelerometers]
-
Dimension Engineering Inc. 'A Beginner’s Guide to Accelerometers'. Retrieved: 2015-11-7.
http://www.dimensionengineering.com/info/accelerometers - [lsm303d-manual]
-
STMicroelectronics. 'LSM303D: Ultra compact high performance e-Compass 3D accelerometer and 3D magnetometer module — Datasheet — preliminary data'. Doc ID 023312 Rev 1. June 2012.
https://www.pololu.com/file/0J703/LSM303D.pdf - [lsm303d-pololu]
-
Pololu Corporation. 'LSM303D 3D Compass and Accelerometer Carrier with Voltage Regulator'. Retrieved: 2015-11-7.
https://www.pololu.com/product/2127 - [lsm303d-appnote]
-
STMicroelectronics. 'AN3192 Application note: Using LSM303DLH for a tilt compensated electronic compass'. Doc ID 17353 Rev 1. August 2010.
https://www.pololu.com/file/0J434/LSM303DLH-compass-app-note.pdf
6. Grading Sheet
Task | Points |
---|---|
Operate a seven-segment display using the SSP/SPI interface |
7 |
Discussion |
3 |