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
Diff: main.cpp
- Revision:
- 5:d5dba5370918
- Parent:
- 4:a743917cd04c
- Child:
- 6:fa74a43cae81
diff -r a743917cd04c -r d5dba5370918 main.cpp
--- a/main.cpp Wed Jan 20 10:48:50 2016 +0000
+++ b/main.cpp Wed Jan 20 11:36:30 2016 +0000
@@ -8,25 +8,40 @@
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
- * Como la direccion ocupa 7 bits + bit de R/W, en teoria seria 0xAA o 0xAB,
- * pero no estamos seguros de si esa transformacion se hace directamente al
- * llamar a write() y read() o lo tenemos que poner nosotros a mano.
+ * 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 la señal de Stop
+ * - 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 WRITE_ADDRESS = 0xAA; // 0xAA
- const char READ_ADDRESS = 0xAA; // 0xAB, 0x1D
+ const char ACCEL_ADDRESS = 0xAA;
char data_write[2];
char data_read[7];
@@ -47,11 +62,7 @@
pc.printf("Starting\n");
i2c.frequency(100000); // Min: 0kHz, Max: 400kHz
-
- /* 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
- */
+
EN = 0; // SHUTDOWN Mode
wait(0.1);
@@ -61,14 +72,28 @@
EN = 1; // ACTIVE Mode -> STANDBY Mode
- data_write[0] = 0x00; // Direccion del registro STATUS
+/*****************************************************************************
+ * 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(WRITE_ADDRESS, data_write, 1, true);
+ i2c.write(ACCEL_ADDRESS, data_write, 1, true);
- i2c.read(READ_ADDRESS, data_read, 7, true); // El true indica repeated start condition
+ i2c.read(ACCEL_ADDRESS, data_read, 7, true);
pc.printf("Status: %i\n", data_read[0]);
@@ -107,97 +132,12 @@
else
led4 = 1;
-
i2c.read(0); // Enviamos un NACK (Espero)
i2c.stop();
EN = 0; // SHUTDOWN Mode
wait(0.1);
+
}
-
- /*i2c.write(0xAA, data_write, 1);
-
- char data_read[7];
- i2c.read(0xAB, data_read, 7, true);
-
- if(data_read[0] == 0)
- led1 = 0;
-
- if(data_read[1] == 0)
- led2 = 0;
-
- if(data_read[2] == 0)
- led3 = 0;
-
- if(data_read[3] == 0)
- led4 = 0;
-
- i2c.stop();*/
-
- //data_write[0] = 0x01;
- //data_write[1] = 0x00;
- //i2c.start();
- //int status = i2c.write(addr,data_write, 2);
-
- /*if (status != 0) { // Error
- while (1) {
-
- }
- } */
-
- //bool haPasado = false;
-
- //i2c.frequency(200000);
-
- /*while(1){
-
- i2c.start();
-
- wait(0.5);
-
- int status = i2c.read(addr, data_read, 3);
-
- if (status == 0)
- haPasado = true;
-
- if (!haPasado)
- led4 = !led4;
-
- i2c.stop();
-
- }*/
- //i2c.start();
- /*data_write[0] = 0x00;
- i2c.write(addr, data_write, 1,1);
-
- i2c.read(addr, data_read, 2,0);
-
- int ch0 =(data_read[0]<<8) | data_read[1];
-
- if (ch0 < 0x001FF){
-
- led1 = 0;
- led2 = 1;
- led3 = 1;
- }
- else if (ch0 > 0x001FF && ch0 < 0x0FFF) {
-
- led1 = 1;
- led2 = 0;
- led3 = 1;
-
- }
- else {
- led1 = 1;
- led2 = 1;
- led3 = 0;
- }
-
- //pc.printf("channel 0: %x,%x \n", data_read[0], data_read[1]);
-
- wait(0.5);
-
- //i2c.stop();
- } */
}
\ No newline at end of file
