先上代码- #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[16];//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上显示浮点数?
百思不得其解啊 请大家指教
谢谢 |