- /*
- 【Arduino】168種傳感器模塊系列實驗(資料代碼+圖形編程+仿真編程)
- 實驗一百五十二:GY-25 串口直接輸出角度數(shù)據(jù) 傾斜度角度傳感器模塊 MPU-6050
- 使用步驟:
- 1.先下載GY25_uart程序至arduino
- 2.再接上GY25模塊
- 3.按復位按鍵
- 4.打開串口,波特率115200
- 5、接線
- GY25 arduino uno
- VCC----------------------VCC
- RX-----------------------TX
- TX-----------------------RX
- GND----------------------GND
- ---------------------------------------
- IICLCD2004 arduino uno
- VCC----------------------VCC
- SCL----------------------A5
- SDA----------------------A4
- GND----------------------GND
- 實驗之二:IICLCD2004顯示動態(tài)角度數(shù)值
- */
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- int YPR[3];
- unsigned char Re_buf[8], counter = 0;
- unsigned char sign = 0;
- int led = 13;
- LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x20 for a 20 chars and 4 line display
- //-----------------------------------------------------------
- void setup()
- {
- lcd.init(); // initialize the lcd
- // Print a message to the LCD.
- Serial.begin(115200);
- delay(2000);
- Serial.write(0XA5);
- Serial.write(0X52); //初始化GY25,連續(xù)輸出模式
- lcd.backlight();
- lcd.setCursor(0, 0); //I2C接口LCD2004顯示初始值
- lcd.print("Yaw:");
- lcd.setCursor(0, 1);
- lcd.print("Pitch:");
- lcd.setCursor(0, 2);
- lcd.print("Roll:");
- }
- //-------------------------------------------------------------
- void loop() {
- if (sign)
- {
- sign = 0;
- if (Re_buf[0] == 0xAA && Re_buf[7] == 0x55) //檢查幀頭,幀尾
- {
- YPR[0] = (Re_buf[1] << 8 | Re_buf[2]) / 100; //合成數(shù)據(jù),去掉小數(shù)點后2位
- YPR[1] = (Re_buf[3] << 8 | Re_buf[4]) / 100;
- YPR[2] = (Re_buf[5] << 8 | Re_buf[6]) / 100;
- lcd.setCursor(4, 0);
- lcd.print(" ");
- lcd.setCursor(4, 0);
- lcd.print(YPR[0]); //顯示航向
- lcd.setCursor(6, 1);
- lcd.print(" ");
- lcd.setCursor(6, 1); //顯示俯仰角
- lcd.print(YPR[1]);
- lcd.setCursor(5, 2);
- lcd.print(" ");
- lcd.setCursor(5, 2); //顯示橫滾角
- lcd.print(YPR[2]);
- delay(100);
- }
- }
- }
- //----------------------------------------------------------
- void serialEvent() {
- while (Serial.available()) {
- Re_buf[counter] = (unsigned char)Serial.read();
- if (counter == 0 && Re_buf[0] != 0xAA) return; // 檢查幀頭
- counter++;
- if (counter == 8) //接收到數(shù)據(jù)
- {
- counter = 0; //重新賦值,準備下一幀數(shù)據(jù)的接收
- sign = 1;
- }
- }
- }
復制代碼
|