Skip to content

Instantly share code, notes, and snippets.

@surinoel
Last active July 30, 2019 00:51
Show Gist options
  • Save surinoel/3dfd2e55096bc0a3a67ba26f2ca295ce to your computer and use it in GitHub Desktop.
Save surinoel/3dfd2e55096bc0a3a67ba26f2ca295ce to your computer and use it in GitHub Desktop.
#include <Wire.h>
const int mpu6050_addr = 0x68;
int16_t GyX, GyY, GyZ;
void setup() {
Serial.begin(115200);
Wire.begin();
Wire.beginTransmission(mpu6050_addr);
Wire.write(0x6b); // Power Management Register
Wire.write(0);
Wire.endTransmission(true);
}
void loop() {
// put your main code here, to run repeatedly:
Wire.beginTransmission(mpu6050_addr);
Wire.write(0x43);
/*
재시작 시 Wire.endTransmission(false)를 보낸다
If false, endTransmission() sends a restart message after transmission.
The bus will not be released, which prevents another master device from transmitting between messages.
This allows one master device to send multiple transmissions while in control.
*/
Wire.endTransmission(false);
/*
0x43부터 6바이트에 대한 레지스터 내용을 읽게 된다
*/
Wire.requestFrom(mpu6050_addr, 6, true);
GyX = Wire.read()<<8 | Wire.read();
GyY = Wire.read()<<8 | Wire.read();
GyZ = Wire.read()<<8 | Wire.read();
static int cnt_loop;
cnt_loop++;
/*
void loop 구조랑 return을 해도 처음부터 재시작한다
*/
if(cnt_loop%50 != 0) return;
Serial.print(" GyX = "); Serial.print(GyX);
Serial.print(" | GyY = "); Serial.print(GyY);
Serial.print(" | GyZ = "); Serial.print(GyZ);
Serial.println();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment