![](/media/cache/group/mbed_shields_1.png.50x50_q85.png)
Example program for SD card shield on SPI pins D10-D13
Dependencies: SDFileSystem mbed
The program uses the standard Arduino shields SPI pins:
Signal | Pin |
SCK | D13 |
MISO | D12 |
MOSI | D11 |
CS | D10 |
Note that this shield doesn't use the standard SPI pins, but relies on non-standard 6-pin SPI header located at the shield bottom. To make it work you will have to bridge those pins as shown on the image (the green SPI pins).
Signal | Pin 1 | Pin 2 |
MOSI | D11 | SPI4 |
MISO | D12 | SPI1 |
SCK | D13 | SPI3 |
CS | D10 | D4 |
main.cpp@4:0928d6e00f01, 2015-02-13 (annotated)
- Committer:
- screamer
- Date:
- Fri Feb 13 09:40:18 2015 +0000
- Revision:
- 4:0928d6e00f01
- Parent:
- 3:5edc67dee8b7
Update mbed library.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
screamer | 3:5edc67dee8b7 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
screamer | 3:5edc67dee8b7 | 2 | * |
screamer | 3:5edc67dee8b7 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
screamer | 3:5edc67dee8b7 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
screamer | 3:5edc67dee8b7 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
screamer | 3:5edc67dee8b7 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
screamer | 3:5edc67dee8b7 | 7 | * Software is furnished to do so, subject to the following conditions: |
screamer | 3:5edc67dee8b7 | 8 | * |
screamer | 3:5edc67dee8b7 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
screamer | 3:5edc67dee8b7 | 10 | * substantial portions of the Software. |
screamer | 3:5edc67dee8b7 | 11 | * |
screamer | 3:5edc67dee8b7 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
screamer | 3:5edc67dee8b7 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
screamer | 3:5edc67dee8b7 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
screamer | 3:5edc67dee8b7 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
screamer | 3:5edc67dee8b7 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
screamer | 3:5edc67dee8b7 | 17 | */ |
screamer | 3:5edc67dee8b7 | 18 | |
screamer | 0:525c842a3c89 | 19 | #include "mbed.h" |
screamer | 0:525c842a3c89 | 20 | #include "SDFileSystem.h" |
screamer | 0:525c842a3c89 | 21 | |
screamer | 2:f2f5e2324ad4 | 22 | Serial pc(USBTX, USBRX); |
screamer | 0:525c842a3c89 | 23 | SDFileSystem sd(D11, D12, D13, D10, "sd"); // MOSI, MISO, SCK, CS |
screamer | 0:525c842a3c89 | 24 | FILE *fp; |
screamer | 0:525c842a3c89 | 25 | |
screamer | 0:525c842a3c89 | 26 | int main() { |
screamer | 1:93d41c73ac7d | 27 | wait(2); |
screamer | 1:93d41c73ac7d | 28 | pc.printf("Initializing\r\n"); |
screamer | 0:525c842a3c89 | 29 | |
screamer | 0:525c842a3c89 | 30 | fp = fopen("/sd/hello.txt", "r"); |
screamer | 0:525c842a3c89 | 31 | if (fp != NULL) { |
screamer | 0:525c842a3c89 | 32 | fclose(fp); |
screamer | 0:525c842a3c89 | 33 | remove("/sd/hello.txt"); |
screamer | 1:93d41c73ac7d | 34 | pc.printf("Remove an existing file with the same name\r\n"); |
screamer | 0:525c842a3c89 | 35 | } |
screamer | 0:525c842a3c89 | 36 | |
screamer | 0:525c842a3c89 | 37 | fp = fopen("/sd/hello.txt", "w"); |
screamer | 0:525c842a3c89 | 38 | if (fp == NULL) { |
screamer | 1:93d41c73ac7d | 39 | pc.printf("Unable to write the file\r\n"); |
screamer | 0:525c842a3c89 | 40 | } else { |
screamer | 0:525c842a3c89 | 41 | fprintf(fp, "mbed SDCard application!"); |
screamer | 0:525c842a3c89 | 42 | fclose(fp); |
screamer | 1:93d41c73ac7d | 43 | pc.printf("File successfully written!\r\n"); |
screamer | 0:525c842a3c89 | 44 | } |
screamer | 0:525c842a3c89 | 45 | } |