Skip to content

Commit 58bd9c9

Browse files
mprseAmanda Butler
andauthored
Add SPI HelloWorld example (#104)
* Add SPI HelloWorld example * Edit README.md Edit file for missing articles and formatting. Co-authored-by: Amanda Butler <[email protected]>
1 parent 512bd86 commit 58bd9c9

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPI example
2+
3+
This example shows how to use the SPI interface to read the WHOAMI register of the slave device. The WHOAMI register is an ID register of the slave device.
4+
5+
**Note:** Please update your MISO, MOSI, SCLK and CS pins.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2006-2020 Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
#include "mbed.h"
6+
7+
SPI spi(D11, D12, D13); // mosi, miso, sclk
8+
DigitalOut cs(D0);
9+
10+
int main()
11+
{
12+
// Chip must be deselected
13+
cs = 1;
14+
15+
// Setup the spi for 8 bit data, high steady state clock,
16+
// second edge capture, with a 1MHz clock rate
17+
spi.format(8, 3);
18+
spi.frequency(1000000);
19+
20+
// Select the device by seting chip select low
21+
cs = 0;
22+
23+
// Send 0x8f, the command to read the WHOAMI register
24+
spi.write(0x8F);
25+
26+
// Send a dummy byte to receive the contents of the WHOAMI register
27+
int whoami = spi.write(0x00);
28+
printf("WHOAMI register = 0x%X\n", whoami);
29+
30+
// Deselect the device
31+
cs = 1;
32+
}

0 commit comments

Comments
 (0)