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.
main.cpp@0:b191e77dc856, 2012-02-27 (annotated)
- Committer:
- polytech01
- Date:
- Mon Feb 27 16:30:10 2012 +0000
- Revision:
- 0:b191e77dc856
Test SPI - Leonardo Wesseling
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| polytech01 | 0:b191e77dc856 | 1 | #include "mbed.h" |
| polytech01 | 0:b191e77dc856 | 2 | |
| polytech01 | 0:b191e77dc856 | 3 | |
| polytech01 | 0:b191e77dc856 | 4 | SPI spi(p11, p12, p13); // mosi, miso, sclk |
| polytech01 | 0:b191e77dc856 | 5 | DigitalOut cs(p14); |
| polytech01 | 0:b191e77dc856 | 6 | |
| polytech01 | 0:b191e77dc856 | 7 | Serial pc(USBTX, USBRX); // tx, rx |
| polytech01 | 0:b191e77dc856 | 8 | |
| polytech01 | 0:b191e77dc856 | 9 | // Not possible to use inline assembler language with this device, error 1044 |
| polytech01 | 0:b191e77dc856 | 10 | /*#pragma arm |
| polytech01 | 0:b191e77dc856 | 11 | void NOP() |
| polytech01 | 0:b191e77dc856 | 12 | { |
| polytech01 | 0:b191e77dc856 | 13 | |
| polytech01 | 0:b191e77dc856 | 14 | __asm |
| polytech01 | 0:b191e77dc856 | 15 | { |
| polytech01 | 0:b191e77dc856 | 16 | NOP |
| polytech01 | 0:b191e77dc856 | 17 | } |
| polytech01 | 0:b191e77dc856 | 18 | } |
| polytech01 | 0:b191e77dc856 | 19 | #pragma thumb*/ |
| polytech01 | 0:b191e77dc856 | 20 | |
| polytech01 | 0:b191e77dc856 | 21 | |
| polytech01 | 0:b191e77dc856 | 22 | |
| polytech01 | 0:b191e77dc856 | 23 | |
| polytech01 | 0:b191e77dc856 | 24 | |
| polytech01 | 0:b191e77dc856 | 25 | int main() { |
| polytech01 | 0:b191e77dc856 | 26 | // Setup the spi for 8 bit data, high steady state clock, |
| polytech01 | 0:b191e77dc856 | 27 | // second edge capture, with a 1MHz clock rate |
| polytech01 | 0:b191e77dc856 | 28 | spi.format(8,0); |
| polytech01 | 0:b191e77dc856 | 29 | spi.frequency(1000000); |
| polytech01 | 0:b191e77dc856 | 30 | int teste, result, b=0; |
| polytech01 | 0:b191e77dc856 | 31 | |
| polytech01 | 0:b191e77dc856 | 32 | |
| polytech01 | 0:b191e77dc856 | 33 | teste=0xAA; |
| polytech01 | 0:b191e77dc856 | 34 | |
| polytech01 | 0:b191e77dc856 | 35 | cs=0; |
| polytech01 | 0:b191e77dc856 | 36 | pc.printf("Answer before = %d\n\r", teste); |
| polytech01 | 0:b191e77dc856 | 37 | |
| polytech01 | 0:b191e77dc856 | 38 | |
| polytech01 | 0:b191e77dc856 | 39 | spi.write(teste); |
| polytech01 | 0:b191e77dc856 | 40 | |
| polytech01 | 0:b191e77dc856 | 41 | cs=1; // it's necessary to close and open the connection for the program to work, we believe that's necessary to exchange the buffer values |
| polytech01 | 0:b191e77dc856 | 42 | // not exactly sure how much must this delay be |
| polytech01 | 0:b191e77dc856 | 43 | |
| polytech01 | 0:b191e77dc856 | 44 | if(b<1){} // delay necessary between closing and opening a connection with the slave, we believe it's because of the high clock value of the mbed |
| polytech01 | 0:b191e77dc856 | 45 | |
| polytech01 | 0:b191e77dc856 | 46 | |
| polytech01 | 0:b191e77dc856 | 47 | |
| polytech01 | 0:b191e77dc856 | 48 | cs=0; |
| polytech01 | 0:b191e77dc856 | 49 | result=spi.write(0x00); // to read the value of the previous reply from the slave, it's necessary to make another write sending a dummy byte |
| polytech01 | 0:b191e77dc856 | 50 | cs=1; |
| polytech01 | 0:b191e77dc856 | 51 | |
| polytech01 | 0:b191e77dc856 | 52 | |
| polytech01 | 0:b191e77dc856 | 53 | pc.printf("Answer later= 0x%X\n\r", result); |
| polytech01 | 0:b191e77dc856 | 54 | // Maybe what is happening is that we need a bigger delay between the two writes so that it works the first time,maybe result is showing not the |
| polytech01 | 0:b191e77dc856 | 55 | //value of the previous write in this run of the program, but the value stored in the buffer in the previous run |
| polytech01 | 0:b191e77dc856 | 56 | |
| polytech01 | 0:b191e77dc856 | 57 | |
| polytech01 | 0:b191e77dc856 | 58 | |
| polytech01 | 0:b191e77dc856 | 59 | |
| polytech01 | 0:b191e77dc856 | 60 | } |