Recently changed pages
mbed interface mbed interface
tag interface, mbed
Homepage Homepage
Compiler Tour Compiler Tour
Media Media
Serial Serial
Terminals Terminals
SerialPC SerialPC
From the mbed microcontroller Handbook.

SPI

/media/uploads/mbedofficial/spi_interfaces.png

The SPI Interface provides a "Serial Peripheral Interface" Master.

This interface can be used for communication with SPI slave devices, such as FLASH memory, LCD screens and other modules or integrated circuits.

Hello World!

Reading the WHOAMI register of an LIS302 SPI accelerometer

#include "mbed.h"

SPI spi(p5, p6, p7); // mosi, miso, sclk
DigitalOut cs(p8);

Serial pc(USBTX, USBRX); // tx, rx

int main() {
    // Setup the spi for 8 bit data, high steady state clock,
    // second edge capture, with a 1MHz clock rate
    spi.format(8,3);
    spi.frequency(1000000);

    // Select the device by seting chip select low
    cs = 0;

    // Send 0x8f, the command to read the WHOAMI register
    spi.write(0x8F);

    // Send a dummy byte to receive the contents of the WHOAMI register
    int whoami = spi.write(0x00);
    pc.printf("WHOAMI register = 0x%X\n", whoami);

    // Deselect the device
    cs = 1;
}

API

API summary

SPIA SPI Master, used for communicating with SPI slave devices
Functions
SPICreate a SPI master connected to the specified pins
formatConfigure the data transmission format
frequencySet the spi bus clock frequency
writeWrite to the SPI Slave and return the response
class SPI : public Base
A SPI Master, used for communicating with SPI slave devices
SPI(PinName mosi,  
PinName miso,  
PinName sclk,  
const char *name =  NULL)
Create a SPI master connected to the specified pins
void format(int bits,  
int mode =  0)
Configure the data transmission format
void frequency(int hz =  1000000)
Set the spi bus clock frequency
virtual int write(int value)
Write to the SPI Slave and return the response
class DigitalOut : public Base
A digital output, used for setting the state of a pin

Details

The SPI Interface can be used on mbed pins p5/p6/p7 and p11/p12/p13

The SPI Interface can be used to write data words out of the SPI port, returning the data received back from the SPI slave. The SPI clock frequency and format can also be configured. The format is set to data word length 8 to 16 bits, and the mode as per the table below:

ModePolarityPhase
000
101
210
311

The SPI master generates a clock to synchronously drive a serial bit stream slave. The slave returns a bit stream, also synchronous to the clock.

Reference




calendar Page history
Last modified 21 Jul 2010, by user avatar Dan Ros   tag No tags | 0 replies     Share: Digg Tweet This

Please login to post comments.