codice
Dependencies: X_NUCLEO_IKS01A1 mbed
main.cpp@0:44863862864b, 2016-11-24 (annotated)
- Committer:
- russof
- Date:
- Thu Nov 24 18:07:10 2016 +0000
- Revision:
- 0:44863862864b
- Child:
- 1:c3bb15cf5b08
Read_acceler_girosc REPO
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
russof | 0:44863862864b | 1 | #include "mbed.h" |
russof | 0:44863862864b | 2 | #include "I2C.h" |
russof | 0:44863862864b | 3 | #include "Serial.h" |
russof | 0:44863862864b | 4 | #include "x_nucleo_iks01a1.h" |
russof | 0:44863862864b | 5 | #define OUT_X_XL (0x28) |
russof | 0:44863862864b | 6 | #define CTRL_REG6_XL (0x20) |
russof | 0:44863862864b | 7 | #define XL_ADDR (0b1101010) |
russof | 0:44863862864b | 8 | |
russof | 0:44863862864b | 9 | //port D15 SCL clock |
russof | 0:44863862864b | 10 | //port D14 SDA data |
russof | 0:44863862864b | 11 | |
russof | 0:44863862864b | 12 | Serial pc_comm(SERIAL_TX, SERIAL_RX); |
russof | 0:44863862864b | 13 | I2C xel_bus(I2C_SDA, I2C_SCL); |
russof | 0:44863862864b | 14 | char xel_outx[2], xel_outy[2], xel_outz[2]; |
russof | 0:44863862864b | 15 | |
russof | 0:44863862864b | 16 | void setupRoutine(); |
russof | 0:44863862864b | 17 | |
russof | 0:44863862864b | 18 | void writeRoutine(); |
russof | 0:44863862864b | 19 | |
russof | 0:44863862864b | 20 | int readRoutine(); |
russof | 0:44863862864b | 21 | |
russof | 0:44863862864b | 22 | int main() |
russof | 0:44863862864b | 23 | { |
russof | 0:44863862864b | 24 | setupRoutine(); |
russof | 0:44863862864b | 25 | while(1) |
russof | 0:44863862864b | 26 | { |
russof | 0:44863862864b | 27 | pc_comm.printf("I'm working!!!\n"); |
russof | 0:44863862864b | 28 | if( !readRoutine() ) |
russof | 0:44863862864b | 29 | pc_comm.printf("Data read failed\n"); |
russof | 0:44863862864b | 30 | else |
russof | 0:44863862864b | 31 | pc_comm.printf("%d %d\n",xel_outx[0],xel_outx[1]); |
russof | 0:44863862864b | 32 | wait(1.0); |
russof | 0:44863862864b | 33 | //xel_bus.read(0x2A,xel_outx,2); |
russof | 0:44863862864b | 34 | //xel_bus.read(0x28,xel_outx,2); |
russof | 0:44863862864b | 35 | } |
russof | 0:44863862864b | 36 | } |
russof | 0:44863862864b | 37 | |
russof | 0:44863862864b | 38 | void setupRoutine() |
russof | 0:44863862864b | 39 | { |
russof | 0:44863862864b | 40 | //pc_comm = new Serial(D1,D0,9600); |
russof | 0:44863862864b | 41 | //xel_bus = new I2C(D15, D14); |
russof | 0:44863862864b | 42 | xel_bus.frequency(10); |
russof | 0:44863862864b | 43 | xel_Routine(); |
russof | 0:44863862864b | 44 | //CTRL_REG6_XL = 0x20; //write to register to enable accelerometer //fondo scala ±2g, ODR= 10Hz ----> BW=408Hz, Filtro AA= 408Hz |
russof | 0:44863862864b | 45 | //CTRL_REG1_G = 0x10; //write to register to enable the accelerometer and gyroscope |
russof | 0:44863862864b | 46 | } |
russof | 0:44863862864b | 47 | |
russof | 0:44863862864b | 48 | void xel_Routine() |
russof | 0:44863862864b | 49 | { |
russof | 0:44863862864b | 50 | char config[2]; |
russof | 0:44863862864b | 51 | config[0] = CTRL_REG6_XL; |
russof | 0:44863862864b | 52 | config[1] = 0x00; |
russof | 0:44863862864b | 53 | xel_bus.write(XL_ADDR, config, 1); // write to register to enable accelerometer |
russof | 0:44863862864b | 54 | config[1] = 0x20; |
russof | 0:44863862864b | 55 | xel_bus.write(XL_ADDR, config, 2); // fondo scala ±2g, ODR= 10Hz ----> BW=408Hz, Filtro AA= 408Hz |
russof | 0:44863862864b | 56 | } |
russof | 0:44863862864b | 57 | |
russof | 0:44863862864b | 58 | int readRoutine() |
russof | 0:44863862864b | 59 | { |
russof | 0:44863862864b | 60 | int result; |
russof | 0:44863862864b | 61 | //xel_bus.start(); |
russof | 0:44863862864b | 62 | xel_bus.write(XL_ADDR, OUT_X_XL, 1, 1); |
russof | 0:44863862864b | 63 | result = xel_bus.read(XL_ADDR, xel_outx, 2, 0); |
russof | 0:44863862864b | 64 | xel_bus.stop(); |
russof | 0:44863862864b | 65 | |
russof | 0:44863862864b | 66 | } |