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: mbed
Revision 5:653dac107d67, committed 2017-01-31
- Comitter:
- ymerdushku
- Date:
- Tue Jan 31 10:31:32 2017 +0000
- Parent:
- 4:420dc2851775
- Commit message:
- versione con gli interi
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Tue Jan 10 15:31:01 2017 +0000 +++ b/main.cpp Tue Jan 31 10:31:32 2017 +0000 @@ -1,6 +1,7 @@ #include "mbed.h" #include <I2C.h> #include <string.h> +#include <stdlib.h> #define LSM6DS0_ADDRESS_WRITE (0xD6) // Register_write #define LSM6DS0_ADDRESS_READ (0xD7) // Register_read @@ -15,11 +16,11 @@ I2C i2c(I2C_SDA,I2C_SCL); Serial pc(SERIAL_TX, SERIAL_RX); +//Serial fpga(D15, D14); char readREG_I2C(char REG_ADDR[1]); void setREG_I2C(char REG_ADDR_DATA[2]); int main() { - int cnt=0; char X_value[2]; char ident[1]; char data_write[2]; @@ -30,6 +31,7 @@ char X[2]; char Y[2]; char Z[2]; + int cnt=0; //read WHO_AM_I //ADDR[0]=0x0F; @@ -65,22 +67,35 @@ ADDR_Z[0]=READ_Z_L; Z[0]=readREG_I2C(ADDR_Z); pc.printf("%c",Z[0]); - -//parte aggiuntiva -> i valori di accelerazione sono X[0],Y[0],Z[0] parti basse + + //conversione da char a intero + int X_int[2]; + int Y_int[2]; + int Z_int[2]; + + X_int[0]=X[0]-'0'; + X_int[1]=X[1]-'0'; + Y_int[0]=Y[0]-'0'; + Y_int[1]=Y[1]-'0'; + Z_int[0]=Z[0]-'0'; + Z_int[1]=Z[1]-'0'; + + //parte aggiuntiva -> i valori di accelerazione sono X[0],Y[0],Z[0] parti basse //X[1],Y[1],Z[1] parti alte - char x_acceleration[2]; - char y_acceleration[2]; - char z_acceleration[2]; - char x_velocity[2]; - char y_velocity[2]; - char z_velocity[2]; - char x_position[2]; - char y_position[2]; - char z_position[2]; - char x_pos[2]; - char y_pos[2]; - char z_pos[2]; + + int x_acceleration[2]; + int y_acceleration[2]; + int z_acceleration[2]; + int x_velocity[2]; + int y_velocity[2]; + int z_velocity[2]; + int x_position[2]; + int y_position[2]; + int z_position[2]; + int x_pos[2]; + int y_pos[2]; + int z_pos[2]; if(cnt==0) //variabile int cnt da definire e settare uguale a zero ad inizio codice @@ -114,9 +129,9 @@ cnt++; //unione delle parti da 8 bit in variabili da 16 bit -x_acceleration[1]=(X[1]<<8)|X[0]; -y_acceleration[1]=(Y[1]<<8)|Y[0]; -z_acceleration[1]=(Z[1]<<8)|Z[0]; +x_acceleration[1]=(X_int[1]<<8)|X[0]; +y_acceleration[1]=(Y_int[1]<<8)|Y[0]; +z_acceleration[1]=(Z_int[1]<<8)|Z[0]; //first integration @@ -133,38 +148,60 @@ //divisione in parti da 8 bit rispettivamente alte e basse x_pos[1]=x_position[1] >> 8; -x_pos[0]=x_position[0] & 0xFF; +x_pos[0]=x_position[1] & 0xFF; y_pos[1]=y_position[1] >> 8; -y_pos[0]=y_position[0] & 0xFF; +y_pos[0]=y_position[1] & 0xFF; z_pos[1]=z_position[1] >> 8; -z_pos[0]=z_position[0] & 0xFF; +z_pos[0]=z_position[1] & 0xFF; //inviare sulla linea sequenza di byte di start int start=0xFF; -pc.printf("%d",start); -pc.printf("%d",start); +fpga.printf("%d",start); +fpga.printf("%d",start); start=0x00; -pc.printf("%d",start); -pc.printf("%d",start); +fpga.printf("%d",start); +fpga.printf("%d",start); -pc.printf("%c",x_pos[1]); -pc.printf("%c",x_pos[0]); -pc.printf("%c",y_pos[1]); -pc.printf("%c",y_pos[0]); -pc.printf("%c",z_pos[1]); -pc.printf("%c",z_pos[0]); +fpga.printf("%c",x_pos[1]); +fpga.printf("%c",x_pos[0]); +fpga.printf("%c",y_pos[1]); +fpga.printf("%c",y_pos[0]); +fpga.printf("%c",z_pos[1]); +fpga.printf("%c",z_pos[0]); int stop=0x00; -pc.printf("%d",stop); +fpg.printf("%d",stop); pc.printf("%d",stop); start=0xFF; pc.printf("%d",stop); pc.printf("%d",stop); //inviare sulla linea sequenza di byte di stop + } + + +void setREG_I2C(char REG_ADDR_DATA[2]) //argument to pass : address register and data to write in +{ + if ((i2c.write(LSM6DS0_ADDRESS_WRITE,REG_ADDR_DATA,2,0))!=0)//ADDR+SUB+DATA+STOP, return 0 if ack + { + pc.printf("Error, not ack\n"); + } + +} + +// read register +char readREG_I2C(char REG_ADDR[1]) //argument to pass : address register to read +{ + char data_read[1]; + + i2c.write(LSM6DS0_ADDRESS_WRITE,REG_ADDR,1,1); //ADDR+SUB, return 0 if ack + i2c.read(LSM6DS0_ADDRESS_READ,data_read,1,0); //ADDR+data+STOP, return 0 if ack + + return data_read[0]; +} \ No newline at end of file