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.

I2C

/media/uploads/mbedofficial/i2c_interfaces.png

The I2C interface provides I2C Master functionality.

This interface can be used for communication with a I2C devices, such as serial memories, sensors and other modules or integrated circuits.

Hello World!

Reading range data from the SRF05 Ultrasonic Range Finder

#include "mbed.h"

I2C i2c(p9, p10);        // sda, scl
Serial pc(USBTX, USBRX); // tx, rx

const int addr = 0x70; // define the I2C Address

int main() {
    char cmd[2];
    while(1) {
        cmd[0] = 0x0;            // pointer to command register
        cmd[1] = 0x51;           // Start ranging, results in cm
        i2c.write(addr, cmd, 2); // Send command string

        wait(0.07);              // Could also poll, 65ms is typical

        // Set pointer to location 2 (first echo)
        cmd[0] = 0x2;
        i2c.write(addr, cmd, 1);
        i2c.read(addr, cmd, 2); // read the two-byte echo result

        // print the ranging data to the screen
        float echo = 0.01 * ((cmd[0] << 8) + cmd[1]);
        pc.printf("Range = %.2f\n", echo);
        wait(0.1);
    }
}

API

API summary

I2CAn I2C Master, used for communicating with I2C slave devices
Functions
I2CCreate an I2C Master interface, connected to the specified pins
frequencySet the frequency of the I2C interface
readRead from an I2C slave
readRead a single byte from the I2C bus
writeWrite to an I2C slave
writeWrite single byte out on the I2C bus
startCreates a start condition on the I2C bus
stopCreates a stop condition on the I2C bus
class I2C : public Base
An I2C Master, used for communicating with I2C slave devices
I2C(PinName sda,  
PinName scl,  
const char *name =  NULL)
Create an I2C Master interface, connected to the specified pins
void frequency(int hz)
Set the frequency of the I2C interface
int read(int address,  
char *data,  
int length,  
bool repeated =  false)
Read from an I2C slave
int write(int address,  
const char *data,  
int length,  
bool repeated =  false)
Write to an I2C slave
void start(void)
Creates a start condition on the I2C bus
void stop(void)
Creates a stop condition on the I2C bus

Details

The I2C Interface can be used on mbed pins p9/p10 and p28/p27

I2C is a two wire serial protocol that allows an I2C Master exchange data with an I2C Slave. The I2C protocol support upto 127 devices per bus. The I2C interface can be used for writing data words out of the I2C port, returning the data recieved back from I2C slave. The I2C clock frequency can be configured.

All drivers on the I2C bus are required to be open collector, and so it is necessary for pull up resistors to be used on the two signals. A typical value for the pullup resistors is around 2.2k ohms.

References




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.