SPI Hello World

Fork of SPI_HelloWorld_Mbed by Mbed

Note

You will need to redefine the SPI pins to match the pins on your board to run this example. The pin mapping can be found on the board's Platform page.

API

API reference

Import librarymbed

No documentation found.
Committer:
mbedAustin
Date:
Fri Mar 27 20:16:45 2015 +0000
Revision:
2:34bd7b8d30f9
Parent:
0:466ad3f38b6b
Child:
3:dd9e7d208cbd
Added license to main.c file.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedAustin 2:34bd7b8d30f9 1 /* mbed Example Program
mbedAustin 2:34bd7b8d30f9 2 * Copyright (c) 2006-2014 ARM Limited
mbedAustin 2:34bd7b8d30f9 3 *
mbedAustin 2:34bd7b8d30f9 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbedAustin 2:34bd7b8d30f9 5 * you may not use this file except in compliance with the License.
mbedAustin 2:34bd7b8d30f9 6 * You may obtain a copy of the License at
mbedAustin 2:34bd7b8d30f9 7 *
mbedAustin 2:34bd7b8d30f9 8 * http://www.apache.org/licenses/LICENSE-2.0
mbedAustin 2:34bd7b8d30f9 9 *
mbedAustin 2:34bd7b8d30f9 10 * Unless required by applicable law or agreed to in writing, software
mbedAustin 2:34bd7b8d30f9 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbedAustin 2:34bd7b8d30f9 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbedAustin 2:34bd7b8d30f9 13 * See the License for the specific language governing permissions and
mbedAustin 2:34bd7b8d30f9 14 * limitations under the License.
mbedAustin 2:34bd7b8d30f9 15 */
mbed_official 0:466ad3f38b6b 16 #include "mbed.h"
mbed_official 0:466ad3f38b6b 17
mbed_official 0:466ad3f38b6b 18 SPI spi(p5, p6, p7); // mosi, miso, sclk
mbed_official 0:466ad3f38b6b 19 DigitalOut cs(p8);
mbed_official 0:466ad3f38b6b 20
mbed_official 0:466ad3f38b6b 21 int main() {
mbed_official 0:466ad3f38b6b 22 // Chip must be deselected
mbed_official 0:466ad3f38b6b 23 cs = 1;
mbed_official 0:466ad3f38b6b 24
mbed_official 0:466ad3f38b6b 25 // Setup the spi for 8 bit data, high steady state clock,
mbed_official 0:466ad3f38b6b 26 // second edge capture, with a 1MHz clock rate
mbed_official 0:466ad3f38b6b 27 spi.format(8,3);
mbed_official 0:466ad3f38b6b 28 spi.frequency(1000000);
mbed_official 0:466ad3f38b6b 29
mbed_official 0:466ad3f38b6b 30 // Select the device by seting chip select low
mbed_official 0:466ad3f38b6b 31 cs = 0;
mbed_official 0:466ad3f38b6b 32
mbed_official 0:466ad3f38b6b 33 // Send 0x8f, the command to read the WHOAMI register
mbed_official 0:466ad3f38b6b 34 spi.write(0x8F);
mbed_official 0:466ad3f38b6b 35
mbed_official 0:466ad3f38b6b 36 // Send a dummy byte to receive the contents of the WHOAMI register
mbed_official 0:466ad3f38b6b 37 int whoami = spi.write(0x00);
mbed_official 0:466ad3f38b6b 38 printf("WHOAMI register = 0x%X\n", whoami);
mbed_official 0:466ad3f38b6b 39
mbed_official 0:466ad3f38b6b 40 // Deselect the device
mbed_official 0:466ad3f38b6b 41 cs = 1;
mbed_official 0:466ad3f38b6b 42 }