Guide

1/12/2018 mbed Starter Kit Experiment Guide - learn.sparkfun.com
https://learn.sparkfun.com/tutorials/mbed-starter-kit-experiment-guide/all 28/65
// I2C address are bits [6..1] in the transmitted byte, so we shift by 1
dev_addr = m_addr << 1;
// Write device address with a trailing 'write' bit
m_i2c.start();
m_i2c.write(dev_addr & 0xFE);
// Write register address
m_i2c.write(reg);
// Write the data to the register
m_i2c.write(data);
m_i2c.stop();
}
Click “Save”.
And that’s it! We just created our very first library in mbed. Because the library is contained within our project, everything is automatically linked at compile time.
We just need to write #include “MMA8452Q.h” in our main program to use the MMA8452Q accerlerometer functions.
Program
Click on “main.cpp” under our “ulcd-accel” project to open up our main program file. Because we selected the “Blinky” template, there will be some code in the
file already. Go ahead and delete everything in “main.cpp”. Copy and paste in the following code.