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.
Diff: main.cpp
- Revision:
- 0:950c605f154f
- Child:
- 1:04b7521f737d
diff -r 000000000000 -r 950c605f154f main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Dec 02 01:55:51 2021 +0000
@@ -0,0 +1,97 @@
+# include<mbed.h>
+
+I2CSlave slave(D9, D10);
+
+void main() 
+{
+    // Create I2C bus
+    int file;
+    char *bus = "/dev/i2c-1";
+    if((file = open(bus, O_RDWR)) < 0) 
+    {
+        printf("Failed to open the bus. \n");
+        exit(1);
+    }
+    // Get I2C device, L3GD20 I2C address is 0x6A(106)
+   // ioctl(file, I2C_SLAVE, 0x6A);
+    slave.address(0x6A);
+    int i = slave.receive();
+
+    // Enable X, Y, Z-Axis and disable Power down mode(0x0F)
+    char config[2] = {0};
+    config[0] = 0x20;
+    config[1] = 0x0F;
+    write(file, config, 2);
+    // Full scale range, 2000 dps(0x30)
+    config[0] = 0x23;
+    config[1] = 0x30;
+    write(file, config, 2);
+    wait(1);
+
+    // Read 6 bytes of data
+    // lsb first
+    // Read xGyro lsb data from register(0x28)
+    char reg[1] = {0x28};
+    write(file, reg, 1);
+    char data[1] = {0};
+    if(read(file, data, 1) != 1)
+    {
+        printf("Error : Input/Output Error \n");
+        exit(1);
+    }
+    char data_0 = data[0];
+
+    // Read xGyro msb data from register(0x29)
+    reg[0] = 0x29;
+    write(file, reg, 1);
+    read(file, data, 1);
+    char data_1 = data[0];
+
+    // Read yGyro lsb data from register(0x2A)
+    reg[0] = 0x2A;
+    write(file, reg, 1);
+    read(file, data, 1);
+    char data_2 = data[0];
+
+    // Read yGyro msb data from register(0x2B)
+    reg[0] = 0x2B;
+    write(file, reg, 1);
+    read(file, data, 1);
+    char data_3 = data[0];
+
+    // Read zGyro lsb data from register(0x2C)
+    reg[0] = 0x2C;
+    write(file, reg, 1);
+    read(file, data, 1);
+    char data_4 = data[0];
+
+    // Read zGyro msb data from register(0x2D)
+    reg[0] = 0x2D;
+    write(file, reg, 1);
+    read(file, data, 1);
+    char data_5 = data[0];
+
+    // Convert the data
+    int xGyro = (data_1 * 256 + data_0);
+    if(xGyro > 32767)
+    {
+        xGyro -= 65536;
+    }
+
+    int yGyro = (data_3 * 256 + data_2);
+    if(yGyro > 32767)
+    {
+        yGyro -= 65536;
+    }
+
+    int zGyro = (data_5 * 256 + data_4);
+    if(zGyro > 32767)
+    {
+    zGyro -= 65536;
+    }
+
+    // Output data to screen
+    printf("Rotation in X-Axis : %d \n", xGyro);
+    printf("Rotation in Y-Axis : %d \n", yGyro);
+    printf("Rotation in Z-Axis : %d \n", zGyro);
+}
\ No newline at end of file