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.
main.cpp
- Committer:
- yamaguchiatsushi
- Date:
- 2021-12-06
- Revision:
- 1:04b7521f737d
- Parent:
- 0:950c605f154f
- Child:
- 2:0eff70bb95df
File content as of revision 1:04b7521f737d:
# include<mbed.h>
I2C i2c(D4, D5);//sda,scl
Serial pc(USBTX,USBRX);
void main()
{
/*
// Create I2C bus
int file;
//ここから下6行はいらないかも
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); //1101010のことやと思うからSA0をGNDにつなげばいいと思う
int i = slave.receive();
*/
const int addr = 0x6A;
// Enable X, Y, Z-Axis and disable Power down mode(0x0F)
char config[2] = {0};
config[0] = 0x20;
config[1] = 0x0F;
i2c.write(addr, config, 2);
// Full scale range, 2000 dps(0x30)
config[0] = 0x23;
config[1] = 0x30;
i2c.write(addr, config, 2);
wait(1);
// Read 6 bytes of data
// lsb first
// Read xGyro lsb data from register(0x28)
char reg[1] = {0x28};
i2c.write(addr, reg, 1);
char data[1] = {0};
if(i2c.read(addr, 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;
i2c.write(addr, reg, 1);
i2c.read(addr, data, 1);
char data_1 = data[0];
// Read yGyro lsb data from register(0x2A)
reg[0] = 0x2A;
i2c.write(addr, reg, 1);
i2c.read(addr, data, 1);
char data_2 = data[0];
// Read yGyro msb data from register(0x2B)
reg[0] = 0x2B;
i2c.write(addr, reg, 1);
i2c.read(addr, data, 1);
char data_3 = data[0];
// Read zGyro lsb data from register(0x2C)
reg[0] = 0x2C;
i2c.write(addr, reg, 1);
i2c.read(addr, data, 1);
char data_4 = data[0];
// Read zGyro msb data from register(0x2D)
reg[0] = 0x2D;
i2c.write(addr, reg, 1);
i2c.read(addr, 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
while(1){
printf("Rotation in X-Axis : %d \n", xGyro);
printf("Rotation in Y-Axis : %d \n", yGyro);
printf("Rotation in Z-Axis : %d \n", zGyro);
wait(0.5);
}
}