This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

how to send and receive data using i2c and spi protocol

hello there, i want to send and receive data using spi and i2c protocol and i am using nrf51422 .how to interface which pins should i use to make spi and i2c communication.

good help appriciated!!

cheers, Abhijeet

Parents Reply Children
  • Thanks for your reply!!!...i have already done the mpu 9250 programming with arduino. And i am famliiar with the mpu 9250 registor reading and writing. i am new to keil and i dont know how to code mpu 9250 using i2c protocol with nrf51422. i have sample arduino code which i want to share with you #include <Wire.h>

    long accelX, accelY, accelZ; float gForceX, gForceY, gForceZ;

    long gyroX, gyroY, gyroZ; float rotX, rotY, rotZ;

    void setup() { Serial.begin(9600); Wire.begin(); setupMPU(); }

    void loop() { recordAccelRegisters(); /* if(accelX<=0) { accelX=accelX+accelX; } if(accelY<=0){ accelY=accelY+accelY; } if(accelZ<=0){ accelZ=accelZ+accelZ; }*/ // recordGyroRegisters(); processAccelData(); printData(); delay(100); }

    void setupMPU(){ Wire.beginTransmission(0b1101000); //This is the I2C address of the MPU (b1101000/b1101001 for AC0 low/high datasheet sec. 9.2) Wire.write(0x6B); //Accessing the register 6B - Power Management 1 (Sec. 4.28) Wire.write(0b00101000); //Setting SLEEP register to 0,cycle bit to 1 and disable temp (Required; see Note on p. 9) Wire.endTransmission();
    /* Wire.write(0x6C); //Accessing the register 6c - Power Management 2 (Sec. 4.28) Wire.beginTransmission(0b1101000); Wire.write(0b00000111); //Setting STBY_XG,STBY_YG,STBY_ZG bits to 1 disable gyroscope (low power mode) (Required; see Note on p. 9) Wire.endTransmission();*/ Wire.beginTransmission(0b1101000); //I2C address of the MPU Wire.write(0x1B); //Accessing the register 1B - Gyroscope Configuration (Sec. 4.4) Wire.write(0x00000000); //Setting the gyro to full scale +/- 250deg./s Wire.endTransmission(); Wire.beginTransmission(0b1101000); //I2C address of the MPU Wire.write(0x1C); //Accessing the register 1C - Acccelerometer Configuration (Sec. 4.5) Wire.write(0b00000000); //Setting the accel to +/- 2g Wire.endTransmission(); }

    void recordAccelRegisters() { Wire.beginTransmission(0b1101000); //I2C address of the MPU Wire.write(0x3B); //Starting register for Accel Readings Wire.endTransmission(); Wire.requestFrom(0b1101000,6); //Request Accel Registers (3B - 40) while(Wire.available() < 6); accelX = Wire.read()<<8|Wire.read(); //Store first two bytes into accelX accelY = Wire.read()<<8|Wire.read(); //Store middle two bytes into accelY accelZ = Wire.read()<<8|Wire.read(); //Store last two bytes into accelZ

    }

    void processAccelData(){

    gForceX = accelX+32767 ; gForceY = accelY+32767; gForceZ = accelZ+32767; }

    void recordGyroRegisters() { Wire.beginTransmission(0b1101000); //I2C address of the MPU Wire.write(0x43); //Starting register for Gyro Readings Wire.endTransmission(); Wire.requestFrom(0b1101000,6); //Request Gyro Registers (43 - 48) while(Wire.available() < 6); gyroX = Wire.read()<<8|Wire.read(); //Store first two bytes into accelX gyroY = Wire.read()<<8|Wire.read(); //Store middle two bytes into accelY gyroZ = Wire.read()<<8|Wire.read(); //Store last two bytes into accelZ processGyroData(); }

    void processGyroData() { rotX = gyroX / 131.0; rotY = gyroY / 131.0; rotZ = gyroZ / 131.0; }

    void printData() { Serial.print("Gyro (deg)"); Serial.print("X="); Serial.print(rotX); Serial.print("\t"); Serial.print("|Y="); Serial.print(rotY); Serial.print("\t"); Serial.print("|Z="); Serial.print(rotZ); //Serial.print("\t"); Serial.print("\n"); //Serial.print(" Accel (g)"); //Serial.print("\n");*/ Serial.print("X="); Serial.print(gForceX); //Serial.print(" "); Serial.print("\t"); Serial.print("|Y="); Serial.print(gForceY); Serial.print("\t"); Serial.print("|Z="); Serial.print(gForceZ ); Serial.print("\n"); delay(5000); }

    i want to write code like this...could you send me the simple code like this using keil software ..As i have already gone through various github example but didnt understand that much... great help appreciated!!! cheers, Abhijeet Kapse

  • Hi,

    Please take a look at the MPU example and drivers on this Github page.

Related