SparkFun Forums 

Where electronics enthusiasts find answers.

Have questions about a SparkFun product or board? This is the place to be.
By stefa24
#200715
Hi
I would like to read the register for an AS7263 with Arduino UNO board
I made a sketch to read those, but what it return looks like wrong
Reading the datasheet I don't what mean "virtual register"
Below the sketch
Code: Select all
/*
  I2C comunication
  to read from registers
  i2c_read_regAS7263

  CS pin 3v3
  SDA pin SDA +10k to 3v3
  SDO pin GND
  SCL pin SCL +10k to 3v3
  3v3 VCC
  GND GND
*/
/*
  #define AS72XX_Device_Type       (0x00)    // HW_Version Device Type
  #define AS72XX_HW_Version        (0x01)    // HW_Version HW_Version
  #define AS72XX_FW_Version_m      (0x02)    // FW_Version minor
  #define AS72XX_FW_Version_M      (0x03)    // FW_Version major
  #define AS72XX_Control_Setup     (0x04)    // Control_Setup
  #define AS72XX_INT_T             (0x05)    // INT_T
  #define AS72XX_Device_Temp       (0x06)    // Device_Temp
  #define AS72XX_LED_Control       (0x07)    // LED Control

  #define AS72XX_R_High            (0x08)    // R_High
  #define AS72XX_R_Low             (0x09)    // R_Low

  #define AS72XX_S_High            (0x0A)    // S_High
  #define AS72XX_S_Low             (0x0B)    // S_Low

  #define AS72XX_T_High            (0x0C)    // T_High
  #define AS72XX_T_Low             (0x0D)    // T_Low

  #define AS72XX_U_High            (0x0E)    // U_High
  #define AS72XX_U_Low             (0x0F)    // U_Low

  #define AS72XX_V_High            (0x10)    // V_High
  #define AS72XX_V_Low             (0x11)    // V_Low

  #define AS72XX_W_High            (0x12)    // W_High
  #define AS72XX_W_Low             (0x13)    // W_Low

  #define AS72XX_R_Cal_1           (0x14)    // R_Cal
  #define AS72XX_R_Cal_2           (0x15)    // R_Cal
  #define AS72XX_R_Cal_3           (0x16)    // R_Cal
  #define AS72XX_R_Cal_4           (0x17)    // R_Cal

  #define AS72XX_S_Cal_1           (0x18)    // S_Cal
  #define AS72XX_S_Cal_2           (0x19)    // S_Cal
  #define AS72XX_S_Cal_3           (0x1A)    // S_Cal
  #define AS72XX_S_Cal_4           (0x1B)   // S_Cal

  #define AS72XX_T_Cal_1           (0x1C)    // T_Cal
  #define AS72XX_T_Cal_2           (0x1D)    // T_Cal
  #define AS72XX_T_Cal_3           (0x1E)    // T_Cal
  #define AS72XX_T_Cal_4           (0x1F)    // T_Cal

  #define AS72XX_U_Cal_1           (0x20)    // U_Cal
  #define AS72XX_U_Cal_2           (0x21)    // U_Cal
  #define AS72XX_U_Cal_3           (0x22)    // U_Cal
  #define AS72XX_U_Cal_4           (0x23)    // U_Cal

  #define AS72XX_V_Cal_1           (0x24)    // V_Cal
  #define AS72XX_V_Cal_2           (0x25)    // V_Cal
  #define AS72XX_V_Cal_3           (0x26)    // V_Cal
  #define AS72XX_V_Cal_4           (0x27)    // V_Cal

  #define AS72XX_W_Cal_1           (0x28)    // W_Cal
  #define AS72XX_W_Cal_2           (0x29)    // W_Cal
  #define AS72XX_W_Cal_3           (0x2A)    // W_Cal
  #define AS72XX_W_Cal_4           (0x2B)    // W_Cal
*/

#define AS72XX_SLAVE_TX_VALID     0x02
#define AS72XX_SLAVE_RX_VALID     0x01
#define AS72XX_SLAVE_STATUS_REG   0x00
#define AS72XX_SLAVE_WRITE_REG    0x01
#define AS72XX_SLAVE_READ_REG     0x02



//44 registrers
/*=========================================================================*/
#include <Wire.h>

//#define DEVICE (0x49)//ADXL345 device address
#define DEVICE (0x3F)//ADXL345 device address

//#define TO_READ (44)//num of bytes-1 we are going to read each time
#define TO_READ (5)//num of bytes-1 we are going to read each time
byte buff[TO_READ];    //n bytes buffer for saving data read from the device

char* reg[] =
{
  /*  "AS72XX_Device_Type",
    "AS72XX_HW_Version",
    "AS72XX_FW_Version_m",
    "AS72XX_FW_Version_M",
    "AS72XX_Control_Setup",
    "AS72XX_INT_T",
    "AS72XX_Device_Temp",
    "AS72XX_LED Control",

    "AS72XX_R_High",
    "AS72XX_R_Low",

    "AS72XX_S_High",
    "AS72XX_S_Low",

    "AS72XX_T_High",
    "AS72XX_T_Low",

    "AS72XX_U_High",
    "AS72XX_U_Low",

    "AS72XX_V_High",
    "AS72XX_V_Low",

    "AS72XX_W_High",
    "AS72XX_W_Low",

    "AS72XX_R_Cal_1",
    "AS72XX_R_Cal_2",
    "AS72XX_R_Cal_3",
    "AS72XX_R_Cal_4",

    "AS72XX_S_Cal_1",
    "AS72XX_S_Cal_2",
    "AS72XX_S_Cal_3",
    "AS72XX_S_Cal_4",

    "AS72XX_T_Cal_1",
    "AS72XX_T_Cal_2",
    "AS72XX_T_Cal_3",
    "AS72XX_T_Cal_4",

    "AS72XX_U_Cal_1",
    "AS72XX_U_Cal_2",
    "AS72XX_U_Cal_3",
    "AS72XX_U_Cal_4",

    "AS72XX_V_Cal_1",
    "AS72XX_V_Cal_2",
    "AS72XX_V_Cal_3",
    "AS72XX_V_Cal_4",

    "AS72XX_W_Cal_1",
    "AS72XX_W_Cal_2",
    "AS72XX_W_Cal_3",
    "AS72XX_W_Cal_4"*/

  "AS72XX_SLAVE_TX_VALID",
  "AS72XX_SLAVE_RX_VALID",
  "AS72XX_SLAVE_STATUS_REG",
  "AS72XX_SLAVE_WRITE_REG",
  "AS72XX_SLAVE_READ_REG"
};

byte reg_[] =
{
  /*  AS72XX_Device_Type,
    AS72XX_HW_Version,
    AS72XX_FW_Version_m,
    AS72XX_FW_Version_M,
    AS72XX_Control_Setup,
    AS72XX_INT_T,
    AS72XX_Device_Temp,
    AS72XX_LED_Control,

    AS72XX_R_High,
    AS72XX_R_Low,

    AS72XX_S_High,
    AS72XX_S_Low,

    AS72XX_T_High,
    AS72XX_T_Low,

    AS72XX_U_High,
    AS72XX_U_Low,

    AS72XX_V_High,
    AS72XX_V_Low,

    AS72XX_W_High,
    AS72XX_W_Low,

    AS72XX_R_Cal_1,
    AS72XX_R_Cal_2,
    AS72XX_R_Cal_3,
    AS72XX_R_Cal_4,

    AS72XX_S_Cal_1,
    AS72XX_S_Cal_2,
    AS72XX_S_Cal_3,
    AS72XX_S_Cal_4,

    AS72XX_T_Cal_1,
    AS72XX_T_Cal_2,
    AS72XX_T_Cal_3,
    AS72XX_T_Cal_4,

    AS72XX_U_Cal_1,
    AS72XX_U_Cal_2,
    AS72XX_U_Cal_3,
    AS72XX_U_Cal_4,

    AS72XX_V_Cal_1,
    AS72XX_V_Cal_2,
    AS72XX_V_Cal_3,
    AS72XX_V_Cal_4,

    AS72XX_W_Cal_1,
    AS72XX_W_Cal_2,
    AS72XX_W_Cal_3,
    AS72XX_W_Cal_4*/

  AS72XX_SLAVE_TX_VALID,
  AS72XX_SLAVE_RX_VALID,
  AS72XX_SLAVE_STATUS_REG,
  AS72XX_SLAVE_WRITE_REG,
  AS72XX_SLAVE_READ_REG
};

void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
  //Turning on the sensor

  //  writeTo(DEVICE, reg_[7], 0x08); //enter in standby mode to configure
}

void loop()
{
  Serial.println("START/////////////////////////////////////////");
  for (int k = 0; k < TO_READ; k++)
  {
    readFrom(DEVICE, reg_, TO_READ, buff); //read the acceleration data from the ADXL345
    Serial.println(reg[k]);
    Serial.print(k);

    Serial.print('\t');

    Serial.print("0x");
    Serial.print(reg_[k], HEX);

    Serial.print('\t');

    Serial.print(buff[k], BIN);

    Serial.println();
    delay(100);//It appears that delay is needed in order not to clog the port
  }
  delay(100);//It appears that delay is needed in order not to clog the port
}//fine loop

///////////////////////////////////////////////////////////////////////////////////
void readFrom(int device, byte address, int num, byte buff[])
{
  Wire.beginTransmission(device); //start transmission to device
  Wire.write(address);        //sends address to read from
  Wire.endTransmission(); //end transmission

  Wire.beginTransmission(device); //start transmission to device (initiate again)
  Wire.requestFrom(device, num);    // request TO_READ bytes from device

  int i = 0;
  while (Wire.available())   //device may send less than requested (abnormal)
  {
    buff[i] = Wire.read(); // receive a byte
    i++;
  }
  Wire.endTransmission(); //end transmission
}

/////////////////////////////////////////////////////////////////////////////
void writeTo(int device, byte address, byte val)
{
  Wire.beginTransmission(device); //start transmission to device
  Wire.write(address);        // send register address
  Wire.write(val);        // send value to write
  Wire.endTransmission(); //end transmission
}


By Valen
#200721
"I made a sketch to read those, but what it return looks like wrong"

I cannot confirm nor deny that. Think for a minute, why is that?

What is that code for an ADXL345 doing with an AS7263. (Whatever that last one is.)

It would be nice if you linked the datasheet.