Projet_S5 / Mbed 2 deprecated Repo_Noeud_Mobile_refactor

Dependencies:   mbed-rtos mbed

Fork of Repo_Noeud_Mobile by Projet_S5

Committer:
groygirard
Date:
Thu Mar 05 19:36:48 2015 +0000
Revision:
8:51f6c8f59449
Parent:
6:fd1bf5563299
Child:
11:a9fbf205233a
Child:
12:ebb08773dbdb
Included accelerometer data acquisition in main.

Who changed what in which revision?

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