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.
Dependencies: BLE_API mbed nRF51822 circular_buffer
main.cpp
- Committer:
- agufal
- Date:
- 2016-02-19
- Revision:
- 7:e4f89c858e61
- Parent:
- 6:fa74a43cae81
- Child:
- 8:e7cafda76315
File content as of revision 7:e4f89c858e61:
/*
* Esto es para la placa Xtrinsic-Sense Board de element14, mas
* concretamente para el acelerometro MMA8491Q
*/
#include "mbed.h"
I2C i2c(p30, p7);
Serial pc(p9, p11);
//LEDS
DigitalOut led2(LED2);
DigitalOut led1(LED1);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
/*****************************************************************************
* El acelerometro tiene una maquina de estados basandose en enable
* Pagina 11
* http://www.element14.com/community/servlet/JiveServlet/previewBody/54565-102-1-273580/Datasheet_MMA8491Q.pdf
*****************************************************************************/
DigitalOut EN(p16);
/*
* Direccion del acelerometro: 0x55 << 1 = 0xAA
*
* Segun el datasheet los pasos son:
* - Mandar un Start Condition, direccion 0x55 y bit de R/W a 0 para indicar escritura. El esclavo manda un ACK
* - Transmitir la direccion del registro a leer. El esclavo manda un ACK
* - Transmitir un Repeated Start Condition y luego "direccionar?" al acelerometro con bit de R/W a 1 (lectura del registro)
* - El esclavo manda un ACK y transmite los datos del registro indicado
* - Transmitir un NACK y la señal de Stop
*
* Los rangos de datos para cada eje son:
*
* | Range ±8g
* 14-bit Data | (1 mg/count)
* ___________________|_______________
* 01 1111 1111 1111 | +8.000g
* 01 1111 1111 1110 | +7.998g
* ... | ...
* 00 0000 0000 0000 | 0.000g
* 11 1111 1111 1111 | -0.001g
* ... | ...
* 10 0000 0000 0001 | -7.998g
* 10 0000 0000 0000 | -8.000g
*/
int main(){
const char ACCEL_ADDRESS = 0x55;
char data_write[1];
char data_read[7];
data_read[0] = 0;
data_read[1] = 0;
data_read[2] = 0;
data_read[3] = 0;
data_read[4] = 0;
data_read[5] = 0;
data_read[6] = 0;
led1 = 1;
led2 = 1;
led3 = 1;
led4 = 1;
i2c.frequency(100000); // Min: 0kHz, Max: 400kHz
EN = 0; // SHUTDOWN Mode
wait(0.1);
while(1){
pc.printf("-----------------------------------------\n");
EN = 1; // ACTIVE Mode -> STANDBY Mode
/*****************************************************************************
* Direccion del registro STATUS
*
* El registro status cambia diferentes bits para informar de si hay
* nuevos datos y estan disponibles (1 - Si, 0 - No):
*
* ZYXDR : Hay nuevos datos en alguno de los tres ejes
* ZDR : Hay datos para el eje Z
* YDR : Hay datos para el eje Y
* XDR : Hay datos para el eje X
*
* El formato del byte es:
* 0 0 0 0 ZYXDR ZDR YDR XDR
*****************************************************************************/
data_write[0] = 0x00;
i2c.start();
// Como con write ya mandamos la direccion, hacemos los dos primeros pasos a la vez.
i2c.write(ACCEL_ADDRESS << 1, data_write, 1, true);
i2c.read(ACCEL_ADDRESS << 1, data_read, 7, true);
if(data_read[0] == 0)
led1 = 0;
else
led1 = 1;
//########### X #############
unsigned int ch0 = data_read[1];
ch0 = (ch0 << 6) | (data_read[2] >> 2);
int x_l = ch0; // Necesitamos 32 bits
if (x_l >= 0x2000)
x_l -= 0x4000;
long x = (1000 * x_l + 512) >> 10;
pc.printf("X: ");
/*if (x > 0x80000000){
pc.printf("-");
x = ~x + 1;
}
else
pc.printf("+");*/
// Hola mantissa
int r = x % 1000;
char a = (char)(x / 1000);
char b = (char)(r / 100);
r %= 100;
char c = (char)(r / 10);
char d = (char)(r%10);
pc.printf("dataread[1]: %x, dataread[2]: %x, x: %d\n", data_read[1], data_read[2], x);
//########### Y #############
unsigned int ch1 = data_read[3];
ch1 = (ch1 << 6) | (data_read[4] >> 2);
int y_l = ch1;
if (y_l >= 0x2000)
y_l -= 0x4000;
long y = (1000 * y_l + 512) >> 10;
pc.printf("Y: long: %d int %d ",sizeof(long),sizeof(int));
/*
if (y > 0x80000000){
pc.printf("-");
y = ~y + 1;
}
else
pc.printf("+");
*/
// Hola mantissa
r = y % 1000;
a = (char)(y / 1000);
b = (char)(r / 100);
r %= 100;
c = (char)(r / 10);
d = (char)(r%10);
pc.printf("yl %d y: %d\n", y_l, y);
//########### Z #############
unsigned int ch2 = data_read[5];
ch2 = (ch2 << 6) | (data_read[6] >> 2);
long z_l = ch2;
if (z_l >= 0x2000)
z_l -= 0x4000;
long z = (1000 * z_l + 512) >> 10;
pc.printf("Z: ");
/* if (z > 0x80000000){
pc.printf("-");
z = ~z + 1;
}
else
pc.printf("+");*/
// Hola mantissa
r = z % 1000;
a = (char)(z / 1000);
b = (char)(r / 100);
r %= 100;
c = (char)(r / 10);
d = (char)(r%10);
pc.printf("dataread[5]: %x, dataread[6]: %x, z: %d\n", data_read[5], data_read[6], z);
/*
UNSIGNED CHAR
HACER TODO SIN SIGNO Y LUEGO COMPROBAR EL BIT 14 PARA VER SI ES NEGATIVO
SI ES NEGATIVO, HACER UNA OR CON FFFFFF
EXTENDER SIGNO!
Result = (1000 x data.DWord + 512) >> 10
0x3FFF -0.001g
0x2001 -7.998g
0x2000 -8.000g
0x1FFF 8.000g
0x1FFE 7.998g
0x0000 0.000g
*/
i2c.read(0); // Enviamos un NACK (Espero)
i2c.stop();
EN = 0; // SHUTDOWN Mode
wait(0.1);
}
}
