极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 15078|回复: 2

求助:12864液晶显示MPU6050数据

[复制链接]
发表于 2012-6-21 21:26:05 | 显示全部楼层 |阅读模式
先上代码
  1. #include "Wire.h"

  2. // I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
  3. // for both classes must be in the include path of your project
  4. #include "I2Cdev.h"
  5. #include "MPU6050.h"
  6. #include "ST7920.h"


  7. // class default I2C address is 0x68
  8. // specific I2C addresses may be passed as a parameter here
  9. // AD0 low = 0x68 (default for InvenSense evaluation board)
  10. // AD0 high = 0x69
  11. MPU6050 accelgyro;
  12. ST7920 lcd;
  13. int16_t ax, ay, az;
  14. int16_t gx, gy, gz;

  15. #define LED_PIN 13
  16. bool blinkState = false;
  17. char str[16];//define variable
  18. float gx1;
  19. void setup() {
  20.     // join I2C bus (I2Cdev library doesn't do this automatically)
  21.     Wire.begin();

  22.     // initialize serial communication
  23.     // (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
  24.     // it's really up to you depending on your project)
  25.     Serial.begin(38400);

  26.     // initialize device
  27.     Serial.println("Initializing I2C devices...");
  28.     accelgyro.initialize();

  29.     // verify connection
  30.     Serial.println("Testing device connections...");
  31.     Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");

  32.     // configure Arduino LED for
  33.     pinMode(LED_PIN, OUTPUT);
  34.     //LCD initialize
  35.     lcd.LCD_Init();
  36.     lcd.LCD_ClearRam();
  37. }

  38. void loop() {
  39.     // read raw accel/gyro measurements from device
  40.     accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

  41.     // these methods (and a few others) are also available
  42.     //accelgyro.getAcceleration(&ax, &ay, &az);
  43.     //accelgyro.getRotation(&gx, &gy, &gz);

  44.     // display tab-separated accel/gyro x/y/z values
  45.     Serial.print("a/g:\t");
  46.     Serial.print(ax); Serial.print("\t");
  47.     Serial.print(ay); Serial.print("\t");
  48.     Serial.print(az); Serial.print("\t");
  49.     Serial.print(gx); Serial.print("\t");
  50.     Serial.print(gy); Serial.print("\t");
  51.     Serial.println(gz);

  52.     // blink LED to indicate activity
  53.     blinkState = !blinkState;
  54.     digitalWrite(LED_PIN, blinkState);
  55.       
  56.       gx1=gx/131.0;//x axis gyro data after converting to international unit
  57.         sprintf(str,"%2d",gx1);//convert float to string
  58.         lcd.LCD_DisplayStrings(0,0,str);//LCD display converted data
  59.         delay(1000);

  60.        
复制代码
Arduino1.0.1没法写汉语,只好用英语注释了,请见谅。
其中 gx1=gx/131.0;//x axis gyro data after converting to international unit 这一行,读取的gx1数据正常,是x轴角速度
我想把它显示到12864液晶上,但是显示上去后却是正负变化无常的6位整数(陀螺仪静止时正常的gx1数据在0.01左右)

若把“%2d”改为“%2f” 则屏幕上会显示一个问号
请问怎样才能在12864上显示浮点数?
百思不得其解啊 请大家指教
谢谢
回复

使用道具 举报

发表于 2013-1-6 16:48:25 | 显示全部楼层
应该是有个类似库的东西,?的原因是因为不直持浮点。我用其它的IDE 也是这样的问题,后来加了浮点库。
但arduino的浮点库不会加。
同样请高人指点。
回复 支持 反对

使用道具 举报

发表于 2013-1-7 14:36:45 | 显示全部楼层
帮忙顶一下这个贴子。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-5-21 18:51 , Processed in 0.049711 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表