Projet_S5 / Mbed 2 deprecated Repo_Noeud_Mobile_refactor

Dependencies:   mbed-rtos mbed

Fork of Repo_Noeud_Mobile by Projet_S5

Committer:
tduplaix
Date:
Wed Mar 04 20:43:18 2015 +0000
Revision:
2:95cdf1b8f675
Child:
6:fd1bf5563299
First implementation of accelerometer data acquisition.; Not tested.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tduplaix 2:95cdf1b8f675 1 #include "MMA8452Q.h"
tduplaix 2:95cdf1b8f675 2 Serial pc(USBTX, USBRX);
tduplaix 2:95cdf1b8f675 3 I2C i2c(p28, p27);
tduplaix 2:95cdf1b8f675 4
tduplaix 2:95cdf1b8f675 5 Accel::Accel()
tduplaix 2:95cdf1b8f675 6 {
tduplaix 2:95cdf1b8f675 7 nack = 1;
tduplaix 2:95cdf1b8f675 8 }
tduplaix 2:95cdf1b8f675 9
tduplaix 2:95cdf1b8f675 10 uint8_t Accel::get_WHO_AM_I()
tduplaix 2:95cdf1b8f675 11 {
tduplaix 2:95cdf1b8f675 12 char cmd = WHO_AM_I;
tduplaix 2:95cdf1b8f675 13 char *data = 0x00;
tduplaix 2:95cdf1b8f675 14
tduplaix 2:95cdf1b8f675 15 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.
tduplaix 2:95cdf1b8f675 16 wait(SPEED);
tduplaix 2:95cdf1b8f675 17 if (!nack) {
tduplaix 2:95cdf1b8f675 18 nack = i2c.read(R_ADDR, data, 1);
tduplaix 2:95cdf1b8f675 19 wait(SPEED);
tduplaix 2:95cdf1b8f675 20 if (!nack) {
tduplaix 2:95cdf1b8f675 21 if(*data == CTRL_REG1) {
tduplaix 2:95cdf1b8f675 22 pc.printf("I2C communication succes: Module MMA8452 online\r\n");
tduplaix 2:95cdf1b8f675 23 }
tduplaix 2:95cdf1b8f675 24 } else {
tduplaix 2:95cdf1b8f675 25 pc.printf("I2C communication fail: Module MMA8452 offline\r\n");
tduplaix 2:95cdf1b8f675 26 pc.getc();
tduplaix 2:95cdf1b8f675 27 return 1;
tduplaix 2:95cdf1b8f675 28 }
tduplaix 2:95cdf1b8f675 29 }
tduplaix 2:95cdf1b8f675 30 return 0;
tduplaix 2:95cdf1b8f675 31 }
tduplaix 2:95cdf1b8f675 32
tduplaix 2:95cdf1b8f675 33 uint8_t Accel::set_CTRL_REG1()
tduplaix 2:95cdf1b8f675 34 {
tduplaix 2:95cdf1b8f675 35 char cmd[2];
tduplaix 2:95cdf1b8f675 36 cmd[0] = CTRL_REG1;
tduplaix 2:95cdf1b8f675 37 cmd[1] = 0x01; //byte to enable the device
tduplaix 2:95cdf1b8f675 38
tduplaix 2:95cdf1b8f675 39 nack = i2c.write(W_ADDR, cmd, 2, true); //comamnd to enable the device
tduplaix 2:95cdf1b8f675 40 wait(SPEED);
tduplaix 2:95cdf1b8f675 41 if (!nack) {
tduplaix 2:95cdf1b8f675 42 pc.printf("I2C communication success: CTRL_REG1 configured\r\n");
tduplaix 2:95cdf1b8f675 43 } else {
tduplaix 2:95cdf1b8f675 44 pc.printf("I2C communication fail: Could not configure CTRL_REG1\r\n");
tduplaix 2:95cdf1b8f675 45 pc.getc();
tduplaix 2:95cdf1b8f675 46 return 1;
tduplaix 2:95cdf1b8f675 47 }
tduplaix 2:95cdf1b8f675 48 return 0;
tduplaix 2:95cdf1b8f675 49 }
tduplaix 2:95cdf1b8f675 50
tduplaix 2:95cdf1b8f675 51 uint8_t Accel::init_MMA8452()
tduplaix 2:95cdf1b8f675 52 {
tduplaix 2:95cdf1b8f675 53 Serial pc(USBTX, USBRX);
tduplaix 2:95cdf1b8f675 54 pc.printf("\r\n\r\n================================================\r\n\r\n");
tduplaix 2:95cdf1b8f675 55 pc.printf("Accelerometer I2C interface: Initialization sequence starting...\r\n");
tduplaix 2:95cdf1b8f675 56
tduplaix 2:95cdf1b8f675 57 if(get_WHO_AM_I() || set_CTRL_REG1()) {
tduplaix 2:95cdf1b8f675 58 return 1;
tduplaix 2:95cdf1b8f675 59 }
tduplaix 2:95cdf1b8f675 60 return 0;
tduplaix 2:95cdf1b8f675 61 }
tduplaix 2:95cdf1b8f675 62
tduplaix 2:95cdf1b8f675 63 uint8_t Accel::get_axis_values()
tduplaix 2:95cdf1b8f675 64 {
tduplaix 2:95cdf1b8f675 65 char cmd = X_OUT_MSB;
tduplaix 2:95cdf1b8f675 66 char data[6] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
tduplaix 2:95cdf1b8f675 67 accel_t a_data;
tduplaix 2:95cdf1b8f675 68
tduplaix 2:95cdf1b8f675 69 nack = i2c.write(W_ADDR, &cmd, 1, true); //request the data in the 6 out registers
tduplaix 2:95cdf1b8f675 70 wait(SPEED);
tduplaix 2:95cdf1b8f675 71 if (!nack) {
tduplaix 2:95cdf1b8f675 72 nack = i2c.read(R_ADDR, data, 6); //receive the data from the 6 out registers
tduplaix 2:95cdf1b8f675 73 wait(SPEED);
tduplaix 2:95cdf1b8f675 74 a_data.x = (data[0] << 4) + (data[1] >> 4);
tduplaix 2:95cdf1b8f675 75 a_data.y = (data[2] << 4) + (data[3] >> 4);
tduplaix 2:95cdf1b8f675 76 a_data.z = (data[4] << 4) + (data[5] >> 4);
tduplaix 2:95cdf1b8f675 77
tduplaix 2:95cdf1b8f675 78 if (!nack) {
tduplaix 2:95cdf1b8f675 79 pc.printf("I2C Communication success: Data received %#X; %#X; %#X;\r\n", a_data.x, a_data.y, a_data.z);
tduplaix 2:95cdf1b8f675 80 }
tduplaix 2:95cdf1b8f675 81 } else {
tduplaix 2:95cdf1b8f675 82 pc.printf("I2C communcation fail: Command %#X at address %#X\n", cmd, W_ADDR);
tduplaix 2:95cdf1b8f675 83 pc.getc();
tduplaix 2:95cdf1b8f675 84 return 1;
tduplaix 2:95cdf1b8f675 85 }
tduplaix 2:95cdf1b8f675 86 return 0;
tduplaix 2:95cdf1b8f675 87 }