codice
Dependencies: X_NUCLEO_IKS01A1 mbed
main.cpp@1:c3bb15cf5b08, 2016-12-01 (annotated)
- Committer:
- Samuel56
- Date:
- Thu Dec 01 17:54:29 2016 +0000
- Revision:
- 1:c3bb15cf5b08
- Parent:
- 0:44863862864b
- Child:
- 2:20c97410676b
y
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
russof | 0:44863862864b | 1 | #include "mbed.h" |
Samuel56 | 1:c3bb15cf5b08 | 2 | #define ACC_ADD_W 0b11010110 |
Samuel56 | 1:c3bb15cf5b08 | 3 | #define ACC_ADD_R 0b11010111 |
Samuel56 | 1:c3bb15cf5b08 | 4 | #define X_OUT_1 0x28 |
Samuel56 | 1:c3bb15cf5b08 | 5 | #define X_OUT_2 0x29 |
Samuel56 | 1:c3bb15cf5b08 | 6 | #define Y_OUT_1 0x2A |
Samuel56 | 1:c3bb15cf5b08 | 7 | #define Y_OUT_2 0x2B |
Samuel56 | 1:c3bb15cf5b08 | 8 | #define Z_OUT_1 0X2C |
Samuel56 | 1:c3bb15cf5b08 | 9 | #define Z_OUT_2 0X2D |
Samuel56 | 1:c3bb15cf5b08 | 10 | #define CTRL_REG6_XL 0x20 |
Samuel56 | 1:c3bb15cf5b08 | 11 | #define WHO_AM_I 0x0F |
Samuel56 | 1:c3bb15cf5b08 | 12 | #define REFERENCE_G 0x0B |
russof | 0:44863862864b | 13 | |
Samuel56 | 1:c3bb15cf5b08 | 14 | I2C myi2c(D14, D15); |
Samuel56 | 1:c3bb15cf5b08 | 15 | Serial pc (SERIAL_TX, SERIAL_RX); |
Samuel56 | 1:c3bb15cf5b08 | 16 | //void testRead(); void testWrite(); |
Samuel56 | 1:c3bb15cf5b08 | 17 | int read_reg(int reg_add); |
Samuel56 | 1:c3bb15cf5b08 | 18 | void setup(void); |
Samuel56 | 1:c3bb15cf5b08 | 19 | int merge_int(int val_msb, int val_lsb); |
Samuel56 | 1:c3bb15cf5b08 | 20 | void write_reg(int reg_add, int data_in); |
russof | 0:44863862864b | 21 | |
Samuel56 | 1:c3bb15cf5b08 | 22 | int main () |
Samuel56 | 1:c3bb15cf5b08 | 23 | { |
russof | 0:44863862864b | 24 | |
Samuel56 | 1:c3bb15cf5b08 | 25 | setup(); |
Samuel56 | 1:c3bb15cf5b08 | 26 | //testRead(); testWrite(); |
Samuel56 | 1:c3bb15cf5b08 | 27 | int out[2]; |
Samuel56 | 1:c3bb15cf5b08 | 28 | while (1) |
Samuel56 | 1:c3bb15cf5b08 | 29 | { //testWrite(); |
Samuel56 | 1:c3bb15cf5b08 | 30 | out[1]=read_reg(X_OUT_1); |
Samuel56 | 1:c3bb15cf5b08 | 31 | out[0]=read_reg(X_OUT_2); |
Samuel56 | 1:c3bb15cf5b08 | 32 | pc.printf("X: %d \n\r", merge_int(out[0], out[1])); |
Samuel56 | 1:c3bb15cf5b08 | 33 | out[1]=read_reg(Y_OUT_1); |
Samuel56 | 1:c3bb15cf5b08 | 34 | out[0]=read_reg(Y_OUT_2); |
Samuel56 | 1:c3bb15cf5b08 | 35 | pc.printf("Y: %d%d \n\r ", out[1], out[0]); |
Samuel56 | 1:c3bb15cf5b08 | 36 | out[1]=read_reg(Z_OUT_1); |
Samuel56 | 1:c3bb15cf5b08 | 37 | out[0]=read_reg(Z_OUT_2); |
Samuel56 | 1:c3bb15cf5b08 | 38 | pc.printf("Z: %d%d \n\r", out[1], out[0]); |
russof | 0:44863862864b | 39 | wait(1.0); |
Samuel56 | 1:c3bb15cf5b08 | 40 | } |
Samuel56 | 1:c3bb15cf5b08 | 41 | |
Samuel56 | 1:c3bb15cf5b08 | 42 | |
russof | 0:44863862864b | 43 | } |
russof | 0:44863862864b | 44 | |
Samuel56 | 1:c3bb15cf5b08 | 45 | |
Samuel56 | 1:c3bb15cf5b08 | 46 | int read_reg(int reg_add) |
Samuel56 | 1:c3bb15cf5b08 | 47 | |
Samuel56 | 1:c3bb15cf5b08 | 48 | { |
Samuel56 | 1:c3bb15cf5b08 | 49 | int status=0; |
Samuel56 | 1:c3bb15cf5b08 | 50 | char dato; |
Samuel56 | 1:c3bb15cf5b08 | 51 | myi2c.start(); //viene mandato il segnale di start |
Samuel56 | 1:c3bb15cf5b08 | 52 | status=myi2c.write(ACC_ADD_W); //scrivo l'indirizzo dell'accelerometro in scrittura |
Samuel56 | 1:c3bb15cf5b08 | 53 | if (status==0) |
Samuel56 | 1:c3bb15cf5b08 | 54 | pc.printf("ERRORE1"); |
Samuel56 | 1:c3bb15cf5b08 | 55 | status=myi2c.write(reg_add); //mando indirizzo del registro |
Samuel56 | 1:c3bb15cf5b08 | 56 | if (status==0) |
Samuel56 | 1:c3bb15cf5b08 | 57 | pc.printf("ERRORE2"); |
Samuel56 | 1:c3bb15cf5b08 | 58 | myi2c.start(); //mando il segnale di restart |
Samuel56 | 1:c3bb15cf5b08 | 59 | status=myi2c.write(ACC_ADD_R); //mando indirizzo dell'accelerometro in lettura |
Samuel56 | 1:c3bb15cf5b08 | 60 | if (status==0) |
Samuel56 | 1:c3bb15cf5b08 | 61 | pc.printf("ERRORE3"); |
Samuel56 | 1:c3bb15cf5b08 | 62 | dato=myi2c.read(0); //leggo il dato sul bus e lo salvo in dato |
Samuel56 | 1:c3bb15cf5b08 | 63 | myi2c.stop(); //mando il segnale di stop |
Samuel56 | 1:c3bb15cf5b08 | 64 | return dato; //ritorno il dato |
Samuel56 | 1:c3bb15cf5b08 | 65 | } |
Samuel56 | 1:c3bb15cf5b08 | 66 | |
Samuel56 | 1:c3bb15cf5b08 | 67 | void setup(void) |
russof | 0:44863862864b | 68 | { |
Samuel56 | 1:c3bb15cf5b08 | 69 | pc.printf("Starting up the machine...\n\r"); |
Samuel56 | 1:c3bb15cf5b08 | 70 | write_reg(CTRL_REG6_XL, 0x20); |
Samuel56 | 1:c3bb15cf5b08 | 71 | return; |
Samuel56 | 1:c3bb15cf5b08 | 72 | } |
Samuel56 | 1:c3bb15cf5b08 | 73 | |
Samuel56 | 1:c3bb15cf5b08 | 74 | int merge_int(int val_msb, int val_lsb) |
Samuel56 | 1:c3bb15cf5b08 | 75 | { |
Samuel56 | 1:c3bb15cf5b08 | 76 | int to_return = val_lsb + val_msb<<8; |
Samuel56 | 1:c3bb15cf5b08 | 77 | return to_return; |
russof | 0:44863862864b | 78 | } |
russof | 0:44863862864b | 79 | |
Samuel56 | 1:c3bb15cf5b08 | 80 | /*void testRead() |
Samuel56 | 1:c3bb15cf5b08 | 81 | { |
Samuel56 | 1:c3bb15cf5b08 | 82 | int out[2]; |
Samuel56 | 1:c3bb15cf5b08 | 83 | out[1]=read_reg(WHO_AM_I); |
Samuel56 | 1:c3bb15cf5b08 | 84 | pc.printf("WHO_AM_I: %d \n\r", out[1]); |
Samuel56 | 1:c3bb15cf5b08 | 85 | }*/ |
Samuel56 | 1:c3bb15cf5b08 | 86 | |
Samuel56 | 1:c3bb15cf5b08 | 87 | void write_reg(int reg_add, int data_in) |
russof | 0:44863862864b | 88 | { |
Samuel56 | 1:c3bb15cf5b08 | 89 | int status=0; |
Samuel56 | 1:c3bb15cf5b08 | 90 | myi2c.start(); //viene mandato il segnale di start |
Samuel56 | 1:c3bb15cf5b08 | 91 | status=myi2c.write(ACC_ADD_W); //scrivo l'indirizzo dell'accelerometro in scrittura |
Samuel56 | 1:c3bb15cf5b08 | 92 | if (status==0) |
Samuel56 | 1:c3bb15cf5b08 | 93 | pc.printf("ERRORE1"); |
Samuel56 | 1:c3bb15cf5b08 | 94 | status=myi2c.write(reg_add); //mando indirizzo del registro |
Samuel56 | 1:c3bb15cf5b08 | 95 | if (status==0) |
Samuel56 | 1:c3bb15cf5b08 | 96 | pc.printf("ERRORE2"); |
Samuel56 | 1:c3bb15cf5b08 | 97 | status=myi2c.write(data_in); |
Samuel56 | 1:c3bb15cf5b08 | 98 | if (status==0) |
Samuel56 | 1:c3bb15cf5b08 | 99 | pc.printf("ERRORE3"); |
Samuel56 | 1:c3bb15cf5b08 | 100 | myi2c.stop(); //mando il segnale di stop |
Samuel56 | 1:c3bb15cf5b08 | 101 | return; |
russof | 0:44863862864b | 102 | } |
russof | 0:44863862864b | 103 | |
Samuel56 | 1:c3bb15cf5b08 | 104 | |
Samuel56 | 1:c3bb15cf5b08 | 105 | /*void testWrite() |
russof | 0:44863862864b | 106 | { |
Samuel56 | 1:c3bb15cf5b08 | 107 | |
Samuel56 | 1:c3bb15cf5b08 | 108 | int dato; |
Samuel56 | 1:c3bb15cf5b08 | 109 | write_reg(REFERENCE_G,89); |
Samuel56 | 1:c3bb15cf5b08 | 110 | dato=read_reg(REFERENCE_G); |
Samuel56 | 1:c3bb15cf5b08 | 111 | pc.printf("vediamo se funziona: %d", dato); |
Samuel56 | 1:c3bb15cf5b08 | 112 | return ; //ritorno il dato |
Samuel56 | 1:c3bb15cf5b08 | 113 | |
Samuel56 | 1:c3bb15cf5b08 | 114 | }*/ |