2013-03-03 24 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()