SPI Hello World
Fork of SPI_HelloWorld_Mbed by
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
main.cpp@4:3789302f33bc, 2017-06-27 (annotated)
- Committer:
- sarahmarshy
- Date:
- Tue Jun 27 11:28:26 2017 -0500
- Revision:
- 4:3789302f33bc
- Parent:
- 3:dd9e7d208cbd
"Update mbed-os"
Who changed what in which revision?
User | Revision | Line number | New 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 | |
mab5449 | 3:dd9e7d208cbd | 18 | SPI spi(D11, D12, D13); // mosi, miso, sclk |
mab5449 | 3:dd9e7d208cbd | 19 | DigitalOut cs(D0); |
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 | } |