codice

Dependencies:   X_NUCLEO_IKS01A1 mbed

Committer:
prolucus
Date:
Sun Dec 04 09:29:08 2016 +0000
Revision:
2:20c97410676b
Parent:
1:c3bb15cf5b08
Aggiunte alcune annotazioni

Who changed what in which revision?

UserRevisionLine numberNew contents of line
russof 0:44863862864b 1 #include "mbed.h"
prolucus 2:20c97410676b 2
prolucus 2:20c97410676b 3 //ANNOTATIONS
prolucus 2:20c97410676b 4 //
prolucus 2:20c97410676b 5 //accelerometer: LSM6DS0
prolucus 2:20c97410676b 6 //
prolucus 2:20c97410676b 7 // SDO/SA0 pin is connected to voltage supply -> device address is 1101011
prolucus 2:20c97410676b 8 //
prolucus 2:20c97410676b 9 // CTRL_REG8:
prolucus 2:20c97410676b 10 // IF_ADD_INC default value: 1 -> Register address automatically increased
prolucus 2:20c97410676b 11 // during a multiple byte access with a serial
prolucus 2:20c97410676b 12 // interface (I2C)
prolucus 2:20c97410676b 13 // BLE default value: 0 -> Big Endian (LSB @ lower address)
prolucus 2:20c97410676b 14 //
prolucus 2:20c97410676b 15
Samuel56 1:c3bb15cf5b08 16 #define ACC_ADD_W 0b11010110
Samuel56 1:c3bb15cf5b08 17 #define ACC_ADD_R 0b11010111
Samuel56 1:c3bb15cf5b08 18 #define X_OUT_1 0x28
Samuel56 1:c3bb15cf5b08 19 #define X_OUT_2 0x29
Samuel56 1:c3bb15cf5b08 20 #define Y_OUT_1 0x2A
Samuel56 1:c3bb15cf5b08 21 #define Y_OUT_2 0x2B
Samuel56 1:c3bb15cf5b08 22 #define Z_OUT_1 0X2C
Samuel56 1:c3bb15cf5b08 23 #define Z_OUT_2 0X2D
Samuel56 1:c3bb15cf5b08 24 #define CTRL_REG6_XL 0x20
Samuel56 1:c3bb15cf5b08 25 #define WHO_AM_I 0x0F
Samuel56 1:c3bb15cf5b08 26 #define REFERENCE_G 0x0B
russof 0:44863862864b 27
prolucus 2:20c97410676b 28
Samuel56 1:c3bb15cf5b08 29 I2C myi2c(D14, D15);
Samuel56 1:c3bb15cf5b08 30 Serial pc (SERIAL_TX, SERIAL_RX);
prolucus 2:20c97410676b 31
Samuel56 1:c3bb15cf5b08 32 //void testRead(); void testWrite();
prolucus 2:20c97410676b 33
Samuel56 1:c3bb15cf5b08 34 int read_reg(int reg_add);
prolucus 2:20c97410676b 35
Samuel56 1:c3bb15cf5b08 36 void setup(void);
prolucus 2:20c97410676b 37
Samuel56 1:c3bb15cf5b08 38 int merge_int(int val_msb, int val_lsb);
prolucus 2:20c97410676b 39
Samuel56 1:c3bb15cf5b08 40 void write_reg(int reg_add, int data_in);
russof 0:44863862864b 41
prolucus 2:20c97410676b 42
prolucus 2:20c97410676b 43
Samuel56 1:c3bb15cf5b08 44 int main ()
Samuel56 1:c3bb15cf5b08 45 {
Samuel56 1:c3bb15cf5b08 46 setup();
Samuel56 1:c3bb15cf5b08 47 //testRead(); testWrite();
Samuel56 1:c3bb15cf5b08 48 int out[2];
prolucus 2:20c97410676b 49
prolucus 2:20c97410676b 50 while(true)
prolucus 2:20c97410676b 51 {
prolucus 2:20c97410676b 52 //testWrite();
Samuel56 1:c3bb15cf5b08 53 out[1]=read_reg(X_OUT_1);
Samuel56 1:c3bb15cf5b08 54 out[0]=read_reg(X_OUT_2);
Samuel56 1:c3bb15cf5b08 55 pc.printf("X: %d \n\r", merge_int(out[0], out[1]));
Samuel56 1:c3bb15cf5b08 56 out[1]=read_reg(Y_OUT_1);
Samuel56 1:c3bb15cf5b08 57 out[0]=read_reg(Y_OUT_2);
Samuel56 1:c3bb15cf5b08 58 pc.printf("Y: %d%d \n\r ", out[1], out[0]);
Samuel56 1:c3bb15cf5b08 59 out[1]=read_reg(Z_OUT_1);
Samuel56 1:c3bb15cf5b08 60 out[0]=read_reg(Z_OUT_2);
Samuel56 1:c3bb15cf5b08 61 pc.printf("Z: %d%d \n\r", out[1], out[0]);
russof 0:44863862864b 62 wait(1.0);
Samuel56 1:c3bb15cf5b08 63 }
russof 0:44863862864b 64 }
russof 0:44863862864b 65
Samuel56 1:c3bb15cf5b08 66
prolucus 2:20c97410676b 67
Samuel56 1:c3bb15cf5b08 68 int read_reg(int reg_add)
Samuel56 1:c3bb15cf5b08 69 {
Samuel56 1:c3bb15cf5b08 70 int status=0;
Samuel56 1:c3bb15cf5b08 71 char dato;
Samuel56 1:c3bb15cf5b08 72 myi2c.start(); //viene mandato il segnale di start
Samuel56 1:c3bb15cf5b08 73 status=myi2c.write(ACC_ADD_W); //scrivo l'indirizzo dell'accelerometro in scrittura
Samuel56 1:c3bb15cf5b08 74 if (status==0)
Samuel56 1:c3bb15cf5b08 75 pc.printf("ERRORE1");
Samuel56 1:c3bb15cf5b08 76 status=myi2c.write(reg_add); //mando indirizzo del registro
Samuel56 1:c3bb15cf5b08 77 if (status==0)
Samuel56 1:c3bb15cf5b08 78 pc.printf("ERRORE2");
Samuel56 1:c3bb15cf5b08 79 myi2c.start(); //mando il segnale di restart
Samuel56 1:c3bb15cf5b08 80 status=myi2c.write(ACC_ADD_R); //mando indirizzo dell'accelerometro in lettura
Samuel56 1:c3bb15cf5b08 81 if (status==0)
Samuel56 1:c3bb15cf5b08 82 pc.printf("ERRORE3");
Samuel56 1:c3bb15cf5b08 83 dato=myi2c.read(0); //leggo il dato sul bus e lo salvo in dato
Samuel56 1:c3bb15cf5b08 84 myi2c.stop(); //mando il segnale di stop
Samuel56 1:c3bb15cf5b08 85 return dato; //ritorno il dato
Samuel56 1:c3bb15cf5b08 86 }
Samuel56 1:c3bb15cf5b08 87
prolucus 2:20c97410676b 88
prolucus 2:20c97410676b 89
Samuel56 1:c3bb15cf5b08 90 void setup(void)
russof 0:44863862864b 91 {
Samuel56 1:c3bb15cf5b08 92 pc.printf("Starting up the machine...\n\r");
Samuel56 1:c3bb15cf5b08 93 write_reg(CTRL_REG6_XL, 0x20);
Samuel56 1:c3bb15cf5b08 94 return;
Samuel56 1:c3bb15cf5b08 95 }
Samuel56 1:c3bb15cf5b08 96
prolucus 2:20c97410676b 97
prolucus 2:20c97410676b 98
Samuel56 1:c3bb15cf5b08 99 int merge_int(int val_msb, int val_lsb)
Samuel56 1:c3bb15cf5b08 100 {
Samuel56 1:c3bb15cf5b08 101 int to_return = val_lsb + val_msb<<8;
Samuel56 1:c3bb15cf5b08 102 return to_return;
russof 0:44863862864b 103 }
russof 0:44863862864b 104
prolucus 2:20c97410676b 105
prolucus 2:20c97410676b 106
Samuel56 1:c3bb15cf5b08 107 /*void testRead()
Samuel56 1:c3bb15cf5b08 108 {
Samuel56 1:c3bb15cf5b08 109 int out[2];
Samuel56 1:c3bb15cf5b08 110 out[1]=read_reg(WHO_AM_I);
Samuel56 1:c3bb15cf5b08 111 pc.printf("WHO_AM_I: %d \n\r", out[1]);
Samuel56 1:c3bb15cf5b08 112 }*/
Samuel56 1:c3bb15cf5b08 113
prolucus 2:20c97410676b 114
prolucus 2:20c97410676b 115
Samuel56 1:c3bb15cf5b08 116 void write_reg(int reg_add, int data_in)
russof 0:44863862864b 117 {
Samuel56 1:c3bb15cf5b08 118 int status=0;
Samuel56 1:c3bb15cf5b08 119 myi2c.start(); //viene mandato il segnale di start
Samuel56 1:c3bb15cf5b08 120 status=myi2c.write(ACC_ADD_W); //scrivo l'indirizzo dell'accelerometro in scrittura
Samuel56 1:c3bb15cf5b08 121 if (status==0)
Samuel56 1:c3bb15cf5b08 122 pc.printf("ERRORE1");
Samuel56 1:c3bb15cf5b08 123 status=myi2c.write(reg_add); //mando indirizzo del registro
Samuel56 1:c3bb15cf5b08 124 if (status==0)
Samuel56 1:c3bb15cf5b08 125 pc.printf("ERRORE2");
Samuel56 1:c3bb15cf5b08 126 status=myi2c.write(data_in);
Samuel56 1:c3bb15cf5b08 127 if (status==0)
Samuel56 1:c3bb15cf5b08 128 pc.printf("ERRORE3");
Samuel56 1:c3bb15cf5b08 129 myi2c.stop(); //mando il segnale di stop
Samuel56 1:c3bb15cf5b08 130 return;
russof 0:44863862864b 131 }
russof 0:44863862864b 132
Samuel56 1:c3bb15cf5b08 133
prolucus 2:20c97410676b 134
Samuel56 1:c3bb15cf5b08 135 /*void testWrite()
russof 0:44863862864b 136 {
Samuel56 1:c3bb15cf5b08 137
Samuel56 1:c3bb15cf5b08 138 int dato;
Samuel56 1:c3bb15cf5b08 139 write_reg(REFERENCE_G,89);
Samuel56 1:c3bb15cf5b08 140 dato=read_reg(REFERENCE_G);
Samuel56 1:c3bb15cf5b08 141 pc.printf("vediamo se funziona: %d", dato);
Samuel56 1:c3bb15cf5b08 142 return ; //ritorno il dato
Samuel56 1:c3bb15cf5b08 143
Samuel56 1:c3bb15cf5b08 144 }*/