SPI Hello World

Fork of SPI_HelloWorld_Mbed by Mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Example Program
00002  * Copyright (c) 2006-2014 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "mbed.h"
00017  
00018 SPI spi(D11, D12, D13); // mosi, miso, sclk
00019 DigitalOut cs(D0);
00020  
00021 int main() {
00022     // Chip must be deselected
00023     cs = 1;
00024 
00025     // Setup the spi for 8 bit data, high steady state clock,
00026     // second edge capture, with a 1MHz clock rate
00027     spi.format(8,3);
00028     spi.frequency(1000000);
00029  
00030     // Select the device by seting chip select low
00031     cs = 0;
00032  
00033     // Send 0x8f, the command to read the WHOAMI register
00034     spi.write(0x8F);
00035  
00036     // Send a dummy byte to receive the contents of the WHOAMI register
00037     int whoami = spi.write(0x00);
00038     printf("WHOAMI register = 0x%X\n", whoami);
00039  
00040     // Deselect the device
00041     cs = 1;
00042 }