Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
main.cpp@0:241cea911735, 2018-11-19 (annotated)
- Committer:
- Keith_N
- Date:
- Mon Nov 19 05:26:08 2018 +0000
- Revision:
- 0:241cea911735
spi_master
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Keith_N | 0:241cea911735 | 1 | #include "mbed.h" |
| Keith_N | 0:241cea911735 | 2 | |
| Keith_N | 0:241cea911735 | 3 | SPI spi(PB_5, PB_4, PB_3); // mosi, miso, sclk |
| Keith_N | 0:241cea911735 | 4 | DigitalOut cs(PA_1); |
| Keith_N | 0:241cea911735 | 5 | Serial pc(USBTX, USBRX); |
| Keith_N | 0:241cea911735 | 6 | DigitalOut led(LED1); |
| Keith_N | 0:241cea911735 | 7 | |
| Keith_N | 0:241cea911735 | 8 | #define DTIME 0.005 |
| Keith_N | 0:241cea911735 | 9 | |
| Keith_N | 0:241cea911735 | 10 | int main() |
| Keith_N | 0:241cea911735 | 11 | { |
| Keith_N | 0:241cea911735 | 12 | cs = 1; |
| Keith_N | 0:241cea911735 | 13 | spi.format(8,0); |
| Keith_N | 0:241cea911735 | 14 | spi.frequency(1000000); |
| Keith_N | 0:241cea911735 | 15 | led = 0; |
| Keith_N | 0:241cea911735 | 16 | |
| Keith_N | 0:241cea911735 | 17 | while(1) { |
| Keith_N | 0:241cea911735 | 18 | led = 1; |
| Keith_N | 0:241cea911735 | 19 | cs=0; |
| Keith_N | 0:241cea911735 | 20 | wait(DTIME); |
| Keith_N | 0:241cea911735 | 21 | int byte_1 = spi.write(0x03); |
| Keith_N | 0:241cea911735 | 22 | pc.printf("byte_1: %d ",byte_1); |
| Keith_N | 0:241cea911735 | 23 | cs=1; |
| Keith_N | 0:241cea911735 | 24 | |
| Keith_N | 0:241cea911735 | 25 | cs=0; |
| Keith_N | 0:241cea911735 | 26 | wait(DTIME); |
| Keith_N | 0:241cea911735 | 27 | int byte_2 = spi.write(0xFF); |
| Keith_N | 0:241cea911735 | 28 | pc.printf("byte_2: %d ",byte_2); |
| Keith_N | 0:241cea911735 | 29 | cs=1; |
| Keith_N | 0:241cea911735 | 30 | |
| Keith_N | 0:241cea911735 | 31 | cs=0; |
| Keith_N | 0:241cea911735 | 32 | wait(DTIME); |
| Keith_N | 0:241cea911735 | 33 | int byte_waste = spi.write(0xFF); |
| Keith_N | 0:241cea911735 | 34 | cs=1; |
| Keith_N | 0:241cea911735 | 35 | |
| Keith_N | 0:241cea911735 | 36 | int rbyte = (byte_1<<8)+byte_2; |
| Keith_N | 0:241cea911735 | 37 | pc.printf("rbyte: %d\r\n",rbyte); |
| Keith_N | 0:241cea911735 | 38 | led = 0; |
| Keith_N | 0:241cea911735 | 39 | |
| Keith_N | 0:241cea911735 | 40 | wait(0.05); |
| Keith_N | 0:241cea911735 | 41 | } |
| Keith_N | 0:241cea911735 | 42 | |
| Keith_N | 0:241cea911735 | 43 | } |