我很难通过I2C从Arduino接收负数。
我可以毫无问题地得到正整数,但是到目前为止,负整数和浮点数已经不在了。
C ++代码
//Declare and Include the necessary header files
#include <iostream>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <fcntl.h>
//Define Address of Slave Address
#define ADDRESS 0x08
//Eliminate the Used of std in the future
using namespace std;
static const char *devName="/dev/i2c-1";
int main()
{
//Check to see if C++ works
cout<<"Hello, World!\n";
cout<<"I2C: Connecting"<<endl;
int file;
if ((file = open(devName, O_RDWR))<0)
{
fprintf(stderr, "I2C: Failed to access");
exit(1);
}
if (ioctl(file, I2C_SLAVE, ADDRESS)<0)
{
cout<<"Failed to Access"<<endl;
}
char buf[7];
float dist1;
int AcZ;
short AcX;
int AcY;
for (int i=0;i<=100;i++){
read(file,buf, 7);
dist1=buf[0]<<8|buf[1];
AcZ=buf[2]<<8 | buf[3];
AcY=buf[4]<<8 | buf[5];
AcX=buf[6]<<8 | buf[7];
cout<<"To See"<<endl;
cout<<buf[4]<<" "<<buf[5]<<endl;
cout<<"UltrasonicDistance"<<endl;
cout<<"Distance 1: "<<dist1<<" cm"<<endl;
cout<<" "<<endl;
cout<<"Acceleration"<<endl;
cout<<"X Direction: "<<AcX<<endl;
cout<<"Y Direction: "<<AcY<<endl;
cout<<"Z Direction: "<<AcZ<<endl;
//cout<<f11<<endl;
usleep(2000000);
cout<<" "<<endl;
}
return 0;
}
Arduino代码
//Declare Needed header file
#include <Wire.h>
//Declare the pins
#define echoPin 7
#define trigPin 8
//Define Accelerometer Address
const int MPU=0x68;
uint32_t timer=millis();
//Declare Accelerometer Variable
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
float f1= 1.123214324532514;
byte *fp = (byte *) &f1;
//Declare the ultrasonic variables
long duration;
long distance;
void setup()
{
//Join I2C bus as Slave with Address 8;
Wire.begin (8);
//Call SendData Command
Wire.onRequest(SendData);
//Setup Pins as output and input to operate ultrasonic sensor
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
//Setup for Accelerometer Sensor
Wire.beginTransmission(MPU);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
//Begin Serial Monitor
Serial.begin (9600);
}
void loop ()
{
dist();
GY521();
delay(2000);
}
//Obtain Ultrasonic Data
void dist()
{
digitalWrite (trigPin,LOW);
delayMicroseconds(2);
digitalWrite(trigPin,HIGH);
delayMicroseconds(2);
digitalWrite(trigPin,LOW);
duration=pulseIn(echoPin,HIGH);
distance=duration/58.2;
Serial.print(distance);
Serial.println(" cm");
}
//Obtain Accelerometer Data
void GY521()
{
Wire.beginTransmission(MPU);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU,12,true);
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
GyX=Wire.read()<<8|Wire.read();
GyY=Wire.read()<<8|Wire.read();
GyZ=Wire.read()<<8|Wire.read();
Serial.println("Accelerometer: ");
Serial.print("X= "); Serial.println(AcX);
Serial.print("Y= "); Serial.println(AcY);
Serial.print("Z= "); Serial.println(AcZ);
Serial.print("f1= ");
Serial.print(fp[0], HEX);
Serial.print(fp[1], HEX);
Serial.print(fp[2], HEX);
Serial.print(fp[3], HEX);
Serial.println();
}
void SendData()
{
byte test[]={distance>>0x8,distance,AcZ>>0x8, AcZ,AcY>>8,AcY,AcX>>8,AcX};
Wire.write(test,8);
}
对于“加速度值”,它可以是负值,如果负值浮出水面,我需要接收,但是我只能得到大约65000的数字