我爱胆机妙音 发表于 2012-6-21 21:26:05

求助:12864液晶显示MPU6050数据

先上代码#include "Wire.h"

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


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

#define LED_PIN 13
bool blinkState = false;
char str;//define variable
float gx1;
void setup() {
    // join I2C bus (I2Cdev library doesn't do this automatically)
    Wire.begin();

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

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

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

    // configure Arduino LED for
    pinMode(LED_PIN, OUTPUT);
    //LCD initialize
    lcd.LCD_Init();
    lcd.LCD_ClearRam();
}

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

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

    // display tab-separated accel/gyro x/y/z values
    Serial.print("a/g:\t");
    Serial.print(ax); Serial.print("\t");
    Serial.print(ay); Serial.print("\t");
    Serial.print(az); Serial.print("\t");
    Serial.print(gx); Serial.print("\t");
    Serial.print(gy); Serial.print("\t");
    Serial.println(gz);

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

        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上显示浮点数?
百思不得其解啊 请大家指教
谢谢

jinglishi 发表于 2013-1-6 16:48:25

应该是有个类似库的东西,?的原因是因为不直持浮点。我用其它的IDE 也是这样的问题,后来加了浮点库。
但arduino的浮点库不会加。
同样请高人指点。

jinglishi 发表于 2013-1-7 14:36:45

帮忙顶一下这个贴子。
页: [1]
查看完整版本: 求助:12864液晶显示MPU6050数据