2013-03-03 50 views
0

我想转换一些用于Arduino微控制器的C代码与Raspberry Pi微控制器一起使用。他们都使用不同的库,它可能是我遇到的问题,但我不确定希望我有几个小错误,我没有看到我一直在试图解决这一段时间。我收到以下错误:隐式减速和未知类型的错误

temp.c: In function âloopâ: 
temp.c:29:5: error: implicit declaration of function âgetGyroValuesâ [-Werror=implicit-function-declaration] 
temp.c: At top level: 
temp.c:37:6: error: conflicting types for âgetGyroValuesâ [-Werror] 
temp.c:29:5: note: previous implicit declaration of âgetGyroValuesâ was here 
temp.c: In function âgetGyroValuesâ: 
temp.c:38:5: error: unknown type name âbyteâ 
cc1: all warnings being treated as errors 

这里是我的代码,我已经写了:

#include <stdio.h> 
#include <string.h> 
#include <errno.h> 
#include <unistd.h> 
#include <stdlib.h> 
#include <stdint.h> 
#include <time.h> 
#include <wiringPi.h> 
#include <wiringPiI2C.h> 

#define CTRL_REG1 0x20 
#define CTRL_REG2 0x21 
#define CTRL_REG3 0x22 
#define CTRL_REG4 0x23 


int fd,x,y,z; 

void setup(){ 
    fd = wiringPiI2CSetup(0x69); // I2C address of gyro 
    wiringPiI2CWriteReg8(fd, CTRL_REG1, 0x1F); //Turn on all axes, disable power down 
    wiringPiI2CWriteReg8(fd, CTRL_REG3, 0x08); //Enable control ready signal 
    wiringPiI2CWriteReg8(fd, CTRL_REG4, 0x80); // Set scale (500 deg/sec) 
    delay(100);         // Wait to synchronize 
} 

void loop(){ 
    getGyroValues(); 
    // In following Divinding by 114 reduces noise 
    printf("Value of X is: %d\n", x/114); 
    printf("Value of Y is: %d\n", y/114); 
    printf("Value of Z is: %d\n", z/114); 
    delay(500); 
} 

void getGyroValues(){ 
    byte MSB, LSB; 

    LSB = wiringPiI2CReadReg16(fd, 0x28); 
    MSB = wiringPiI2CReadReg16(fd, 0x29); 
    x = ((MSB << 8) | LSB); 

    MSB = wiringPiI2CReadReg16(fd, 0x2B); 
    LSB = wiringPiI2CReadReg16(fd, 0x2A); 
    y = ((MSB << 8) | LSB); 

    MSB = wiringPiI2CReadReg16(fd, 0x2D); 
    LSB = wiringPiI2CReadReg16(fd, 0x2C); 
    z = ((MSB << 8) | LSB); 
} 

这是我转换的代码:

#include <Wire.h> 

#define CTRL_REG1 0x20 
#define CTRL_REG2 0x21 
#define CTRL_REG3 0x22 
#define CTRL_REG4 0x23 

int Addr = 105;     // I2C address of gyro 
int x, y, z; 

void setup(){ 
    Wire.begin(); 
    Serial.begin(9600); 
    writeI2C(CTRL_REG1, 0x1F); // Turn on all axes, disable power down 
    writeI2C(CTRL_REG3, 0x08); // Enable control ready signal 
    writeI2C(CTRL_REG4, 0x80); // Set scale (500 deg/sec) 
    delay(100);     // Wait to synchronize 
} 

void loop(){ 
    getGyroValues();    // Get new values 
    // In following Dividing by 114 reduces noise 
    Serial.print("Raw X:"); Serial.print(x/114); 
    Serial.print(" Raw Y:"); Serial.print(y/114); 
    Serial.print(" Raw Z:"); Serial.println(z/114); 
    delay(500);     // Short delay between reads 
} 

void getGyroValues() { 
    byte MSB, LSB; 

    MSB = readI2C(0x29); 
    LSB = readI2C(0x28); 
    x = ((MSB << 8) | LSB); 

    MSB = readI2C(0x2B); 
    LSB = readI2C(0x2A); 
    y = ((MSB << 8) | LSB); 

    MSB = readI2C(0x2D); 
    LSB = readI2C(0x2C); 
    z = ((MSB << 8) | LSB); 
} 

int readI2C (byte regAddr) { 
    Wire.beginTransmission(Addr); 
    Wire.write(regAddr);    // Register address to read 
    Wire.endTransmission();    // Terminate request 
    Wire.requestFrom(Addr, 1);   // Read a byte 
    while(!Wire.available()) { };  // Wait for receipt 
    return(Wire.read());    // Get result 
} 

void writeI2C (byte regAddr, byte val) { 
    Wire.beginTransmission(Addr); 
    Wire.write(regAddr); 
    Wire.write(val); 
    Wire.endTransmission(); 
} 
+0

减速=减速。 1个单词中的2个错误完全改变了意思。 – 2013-03-03 10:45:25

回答

1

1.返回类型为int。因此,使用int作为变量MSB和LSB的数据类型而不是字节。

void getGyroValues(){ 
    int MSB, LSB; 

    LSB = wiringPiI2CReadReg16(fd, 0x28); 
    MSB = wiringPiI2CReadReg16(fd, 0x29); 
    x = ((MSB << 8) | LSB); 

    MSB = wiringPiI2CReadReg16(fd, 0x2B); 
    LSB = wiringPiI2CReadReg16(fd, 0x2A); 
    y = ((MSB << 8) | LSB); 

    MSB = wiringPiI2CReadReg16(fd, 0x2D); 
    LSB = wiringPiI2CReadReg16(fd, 0x2C); 
    z = ((MSB << 8) | LSB); 
} 

2.And任一移动的getGyroValues()定义中的loop()功能之前或loop()之前定义该函数的原型。

+0

我做了更改,现在我得到一个新的错误'/usr/lib/gcc/arm-linux-gnueabihf/4.6/../../../arm-linux-gnueabihf/crt1.o:In function '_start': (.text + 0x34):对'main'的未定义引用 collect2:ld返回1退出状态# – Yamaha32088 2013-03-03 04:55:41

+1

@ Yamaha32088 - 您的'temp.c'不包含任何'main()'函数。所以我假设你只是编译这个c文件作为库的一部分,或者你在其他文件中有一个main()函数 - 是否正确? – Tuxdude 2013-03-03 05:02:46

+0

我忘了在这个文件中加入main()函数 – Yamaha32088 2013-03-03 05:03:35

2
  1. 声明原型对于函数getGyroValues(以及其他函数)或在调用之前定义它们。

  2. 有在C.任何数据类型byte所以这是错误的(除非你已经Typedef的东西作为字节)从wiringPiI2CReadReg16()