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: X_NUCLEO_IKS01A1 mbed
Revision 1:c3bb15cf5b08, committed 2016-12-01
- Comitter:
- Samuel56
- Date:
- Thu Dec 01 17:54:29 2016 +0000
- Parent:
- 0:44863862864b
- Child:
- 2:20c97410676b
- Commit message:
- y
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Thu Nov 24 18:07:10 2016 +0000
+++ b/main.cpp Thu Dec 01 17:54:29 2016 +0000
@@ -1,66 +1,114 @@
#include "mbed.h"
-#include "I2C.h"
-#include "Serial.h"
-#include "x_nucleo_iks01a1.h"
-#define OUT_X_XL (0x28)
-#define CTRL_REG6_XL (0x20)
-#define XL_ADDR (0b1101010)
+#define ACC_ADD_W 0b11010110
+#define ACC_ADD_R 0b11010111
+#define X_OUT_1 0x28
+#define X_OUT_2 0x29
+#define Y_OUT_1 0x2A
+#define Y_OUT_2 0x2B
+#define Z_OUT_1 0X2C
+#define Z_OUT_2 0X2D
+#define CTRL_REG6_XL 0x20
+#define WHO_AM_I 0x0F
+#define REFERENCE_G 0x0B
-//port D15 SCL clock
-//port D14 SDA data
-
-Serial pc_comm(SERIAL_TX, SERIAL_RX);
-I2C xel_bus(I2C_SDA, I2C_SCL);
-char xel_outx[2], xel_outy[2], xel_outz[2];
-
-void setupRoutine();
+I2C myi2c(D14, D15);
+Serial pc (SERIAL_TX, SERIAL_RX);
+//void testRead(); void testWrite();
+int read_reg(int reg_add);
+void setup(void);
+int merge_int(int val_msb, int val_lsb);
+void write_reg(int reg_add, int data_in);
-void writeRoutine();
-
-int readRoutine();
+int main ()
+{
-int main()
-{
- setupRoutine();
- while(1)
- {
- pc_comm.printf("I'm working!!!\n");
- if( !readRoutine() )
- pc_comm.printf("Data read failed\n");
- else
- pc_comm.printf("%d %d\n",xel_outx[0],xel_outx[1]);
+ setup();
+ //testRead(); testWrite();
+ int out[2];
+ while (1)
+ { //testWrite();
+ out[1]=read_reg(X_OUT_1);
+ out[0]=read_reg(X_OUT_2);
+ pc.printf("X: %d \n\r", merge_int(out[0], out[1]));
+ out[1]=read_reg(Y_OUT_1);
+ out[0]=read_reg(Y_OUT_2);
+ pc.printf("Y: %d%d \n\r ", out[1], out[0]);
+ out[1]=read_reg(Z_OUT_1);
+ out[0]=read_reg(Z_OUT_2);
+ pc.printf("Z: %d%d \n\r", out[1], out[0]);
wait(1.0);
- //xel_bus.read(0x2A,xel_outx,2);
- //xel_bus.read(0x28,xel_outx,2);
- }
+ }
+
+
}
-void setupRoutine()
+
+int read_reg(int reg_add)
+
+{
+ int status=0;
+ char dato;
+ myi2c.start(); //viene mandato il segnale di start
+ status=myi2c.write(ACC_ADD_W); //scrivo l'indirizzo dell'accelerometro in scrittura
+ if (status==0)
+ pc.printf("ERRORE1");
+ status=myi2c.write(reg_add); //mando indirizzo del registro
+ if (status==0)
+ pc.printf("ERRORE2");
+ myi2c.start(); //mando il segnale di restart
+ status=myi2c.write(ACC_ADD_R); //mando indirizzo dell'accelerometro in lettura
+ if (status==0)
+ pc.printf("ERRORE3");
+ dato=myi2c.read(0); //leggo il dato sul bus e lo salvo in dato
+ myi2c.stop(); //mando il segnale di stop
+ return dato; //ritorno il dato
+}
+
+void setup(void)
{
- //pc_comm = new Serial(D1,D0,9600);
- //xel_bus = new I2C(D15, D14);
- xel_bus.frequency(10);
- xel_Routine();
- //CTRL_REG6_XL = 0x20; //write to register to enable accelerometer //fondo scala ±2g, ODR= 10Hz ----> BW=408Hz, Filtro AA= 408Hz
- //CTRL_REG1_G = 0x10; //write to register to enable the accelerometer and gyroscope
+ pc.printf("Starting up the machine...\n\r");
+ write_reg(CTRL_REG6_XL, 0x20);
+ return;
+}
+
+int merge_int(int val_msb, int val_lsb)
+{
+ int to_return = val_lsb + val_msb<<8;
+ return to_return;
}
-void xel_Routine()
+/*void testRead()
+{
+ int out[2];
+ out[1]=read_reg(WHO_AM_I);
+ pc.printf("WHO_AM_I: %d \n\r", out[1]);
+}*/
+
+void write_reg(int reg_add, int data_in)
{
- char config[2];
- config[0] = CTRL_REG6_XL;
- config[1] = 0x00;
- xel_bus.write(XL_ADDR, config, 1); // write to register to enable accelerometer
- config[1] = 0x20;
- xel_bus.write(XL_ADDR, config, 2); // fondo scala ±2g, ODR= 10Hz ----> BW=408Hz, Filtro AA= 408Hz
+ int status=0;
+ myi2c.start(); //viene mandato il segnale di start
+ status=myi2c.write(ACC_ADD_W); //scrivo l'indirizzo dell'accelerometro in scrittura
+ if (status==0)
+ pc.printf("ERRORE1");
+ status=myi2c.write(reg_add); //mando indirizzo del registro
+ if (status==0)
+ pc.printf("ERRORE2");
+ status=myi2c.write(data_in);
+ if (status==0)
+ pc.printf("ERRORE3");
+ myi2c.stop(); //mando il segnale di stop
+ return;
}
-int readRoutine()
+
+/*void testWrite()
{
- int result;
- //xel_bus.start();
- xel_bus.write(XL_ADDR, OUT_X_XL, 1, 1);
- result = xel_bus.read(XL_ADDR, xel_outx, 2, 0);
- xel_bus.stop();
-
-}
\ No newline at end of file
+
+ int dato;
+ write_reg(REFERENCE_G,89);
+ dato=read_reg(REFERENCE_G);
+ pc.printf("vediamo se funziona: %d", dato);
+ return ; //ritorno il dato
+
+}*/
\ No newline at end of file