Search code examples
craspberry-pii2craspbian

implicit deceleration and unknown type errors


I am trying to convert some C code that is used for an Arduino microcontroller to be used with a Raspberry Pi microcontroller. They both use different libraries and it may be the problem I am having but am unsure hopefully I just have a couple small errors I'm not seeing I've been trying to solve this for a while now. I am getting the following errors:

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

Here is my code that I have written:

#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);
}

This is the code I am converting:

#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();
}

Solution

  • 1.The return type from wiringPiI2CReadReg16() is int. So use int as the datatype for variables MSB and LSB instead of byte.

    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 either move the definition of getGyroValues() before the loop() function or define a prototype for this function before loop().