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.
Fork of Repo_Noeud_Mobile by
Accelerometer/MMA8452Q.cpp
- Committer:
- tduplaix
- Date:
- 2015-03-04
- Revision:
- 2:95cdf1b8f675
- Child:
- 6:fd1bf5563299
File content as of revision 2:95cdf1b8f675:
#include "MMA8452Q.h" Serial pc(USBTX, USBRX); I2C i2c(p28, p27); Accel::Accel() { nack = 1; } uint8_t Accel::get_WHO_AM_I() { char cmd = WHO_AM_I; char *data = 0x00; nack = i2c.write(W_ADDR, &cmd, 1, true); //send a request to WHO_AM_I register, we expect 0x2a, or 42, the answer to the universe. wait(SPEED); if (!nack) { nack = i2c.read(R_ADDR, data, 1); wait(SPEED); if (!nack) { if(*data == CTRL_REG1) { pc.printf("I2C communication succes: Module MMA8452 online\r\n"); } } else { pc.printf("I2C communication fail: Module MMA8452 offline\r\n"); pc.getc(); return 1; } } return 0; } uint8_t Accel::set_CTRL_REG1() { char cmd[2]; cmd[0] = CTRL_REG1; cmd[1] = 0x01; //byte to enable the device nack = i2c.write(W_ADDR, cmd, 2, true); //comamnd to enable the device wait(SPEED); if (!nack) { pc.printf("I2C communication success: CTRL_REG1 configured\r\n"); } else { pc.printf("I2C communication fail: Could not configure CTRL_REG1\r\n"); pc.getc(); return 1; } return 0; } uint8_t Accel::init_MMA8452() { Serial pc(USBTX, USBRX); pc.printf("\r\n\r\n================================================\r\n\r\n"); pc.printf("Accelerometer I2C interface: Initialization sequence starting...\r\n"); if(get_WHO_AM_I() || set_CTRL_REG1()) { return 1; } return 0; } uint8_t Accel::get_axis_values() { char cmd = X_OUT_MSB; char data[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; accel_t a_data; nack = i2c.write(W_ADDR, &cmd, 1, true); //request the data in the 6 out registers wait(SPEED); if (!nack) { nack = i2c.read(R_ADDR, data, 6); //receive the data from the 6 out registers wait(SPEED); a_data.x = (data[0] << 4) + (data[1] >> 4); a_data.y = (data[2] << 4) + (data[3] >> 4); a_data.z = (data[4] << 4) + (data[5] >> 4); if (!nack) { pc.printf("I2C Communication success: Data received %#X; %#X; %#X;\r\n", a_data.x, a_data.y, a_data.z); } } else { pc.printf("I2C communcation fail: Command %#X at address %#X\n", cmd, W_ADDR); pc.getc(); return 1; } return 0; }