Announcements

Help us improve the Power & Sensing Selection Guide. Share feedback

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
dwiw_
Level 1
Level 1
50 sign-ins 25 sign-ins 10 sign-ins

Hi,

I'm using CY8CKIT-044, I want to get data from MPU9250. I have already make code (based on github) in arduino Mega and the program running nicely, I got data from accelerometer, gyroscope and magnetometer very well. 

I want to convert code from arduino to PSoC creator, but I can't get even raw data, so i think my code isn't right, first i want to check if my 'i2c read/write functions' for the MPU9250 is right.

Hope can find the solution from this community.

note : first row is arduino code and second row is converted code.

#1

uint8_t readByte(uint8_t addressuint8_t subAddress)
{
uint8_t data; // `data` will store the register data
     Wire.beginTransmission(address);         // Initialize the Tx buffer
     Wire.write(subAddress);                  // Put slave register address in Tx buffer
     Wire.endTransmission(false);             // Send the Tx buffer, but send a restart to keep connection alive
     Wire.requestFrom(address, (size_t1);   // Read one byte from slave register address
     data = Wire.read();                      // Fill Rx buffer with result
     return data;                             // Return data read from slave register
}
 

uint8_t i2c_readByte(uint8_t address, uint8_t reg)
{
     I2C_MPU9250_I2CMasterSendStart(address, 0, I2C_TimeOut);
     I2C_MPU9250_I2CMasterWriteByte(reg, I2C_TimeOut);
     I2C_MPU9250_I2CMasterSendRestart(address, 1, I2C_TimeOut);
     uint8_t data = I2C_MPU9250_I2CMasterReadByte(address, 0,I2C_TimeOut); //store the register data
     I2C_MPU9250_I2CMasterSendStop(I2C_TimeOut);
     return data;
}


 
#2
void readBytes(uint8_t addressuint8_t subAddressuint8_t countuint8_t * dest)
{
     Wire.beginTransmission(address);   // Initialize the Tx buffer
     Wire.write(subAddress);            // Put slave register address in Tx buffer
     Wire.endTransmission(false);       // Send the Tx buffer, but send a restart to keep connection alive
     uint8_t i = 0;
     Wire.requestFrom(address, (size_t) count);  // Read bytes from slave register address
     while (Wire.available()) {
          dest[i++] = Wire.read();
     }         // Put read results in the Rx buffer
}
void i2c_readBytes(uint8_t address, uint8_t reg, uint8_t byteCount, uint8_t * destinationBuffer)
{
     I2C_MPU9250_I2CMasterSendStart(address, 0, I2C_TimeOut);
     I2C_MPU9250_I2CMasterWriteByte(reg, I2C_TimeOut);
     I2C_MPU9250_I2CMasterSendRestart(address, 1, I2C_TimeOut);
     while (byteCount--) {
          if (!byteCount) {
               /* Last byte */
               I2C_MPU9250_I2CMasterReadByte(0,destinationBuffer++,I2C_TimeOut);
          } else {
               I2C_MPU9250_I2CMasterReadByte(1,destinationBuffer++,I2C_TimeOut);
          }
     }
     I2C_MPU9250_I2CMasterSendStop(I2C_TimeOut);
}
 
#3
void writeByte(uint8_t addressuint8_t subAddressuint8_t data)
{
    Wire.beginTransmission(address);  // Initialize the Tx buffer
    Wire.write(subAddress);           // Put slave register address in Tx buffer
    Wire.write(data);                 // Put data in Tx buffer
    Wire.endTransmission();           // Send the Tx buffer
}
void i2c_writeByte(uint8_t address, uint8_t reg, uint8_t data) {
     I2C_MPU9250_I2CMasterSendStart(address, 0, I2C_TimeOut);
     I2C_MPU9250_I2CMasterWriteByte(reg, I2C_TimeOut);
     I2C_MPU9250_I2CMasterWriteByte(data, I2C_TimeOut);
     I2C_MPU9250_I2CMasterSendStop(I2C_TimeOut);
}
 
Thanks,
 
Dwi
0 Likes
1 Solution
1 Reply
Vasanth
Moderator
Moderator
Moderator
250 sign-ins 500 solutions authored First question asked
0 Likes