Library to use SPI-3Wire with Arduino with position sensor

Hi!

I need your help to know if the library support 3-wire SPI and if yes. How?

I attacht the picture.

warriorfenix26's picture:

I don't know anything about this, but it looks to me like there is nothing special needed from the Arduino SPI library. You just need to wire it up according to the schematic. Have you tried it?

assuming you wired it up as in the picture posted, did you try the example code?

I took the liberty of making it Arduino friendly....

(compiled but not tested!)

#include <SPI.h>

#define NUM_REGISTERS 86
#define PRODUCT_ID1 0x00
#define PRODUCT_ID2 0x01
#define MOTION_STATUS 0x02
#define DELTA_XL 0x03
#define DELTA_YL 0x04
#define CONFIGURATION 0x06
#define WRITE_PROTECT 0x09
#define RES_X 0x0D
#define RES_Y 0x0E
#define DELTA_XH 0x11
#define DELTA_YH 0x12
#define IQ 0x13
#define SHUTTER 0x15
#define FRAME_AVG 0x17
#define MFIO_CONFIG 0x26
#define LD_SRC 0x51
#define PXI_WMI 0x31

static const uint8_t Pixart_OTS_9130_InitSetting[NUM_REGISTERS][2] =
{
  { 0x7F, 0x00 }, // switch to bank0, not allowed to perform SPIWriteRead
  { 0x05, 0xA8 }, // sleep mode disabled
  { 0x09, 0x5A }, // disable write protect
  { 0x51, 0x06 }, // set LD current source to 6
  { 0x0D, 0x1F }, // CPI resolution setting for X-direction
  { 0x0E, 0x1F }, // CPI resolution setting for Y-direction
  { 0x07, 0x00 },
  { 0x1B, 0x42 },
  { 0x2E, 0x40 },
  { 0x32, 0x40 },
  { 0x33, 0x02 },
  { 0x34, 0x00 },
  { 0x36, 0xE0 },
  { 0x38, 0xA0 },
  { 0x39, 0x01 },
  { 0x3E, 0x14 },
  { 0x44, 0x02 },
  { 0x4A, 0xE0 },
  { 0x4F, 0x02 },
  { 0x52, 0x0D }, // Turn off internal VDDA
  { 0x57, 0x03 },
  { 0x59, 0x03 },
  { 0x5B, 0x03 },
  { 0x5C, 0xFF },

  { 0x7F, 0x01 }, // switch to bank1, not allowed to perform SPIWriteRead
  { 0x00, 0x25 },
  { 0x07, 0x78 },
  { 0x20, 0x00 },
  { 0x21, 0x40 },
  { 0x23, 0x00 },
  { 0x2F, 0x64 },
  { 0x37, 0x30 },
  { 0x3B, 0x64 },
  { 0x43, 0x0A },
  { 0x59, 0x01 },
  { 0x5A, 0x01 },
  { 0x5C, 0x04 },
  { 0x5E, 0x04 },

  { 0x7F, 0x02 }, // switch to bank2, not allowed to perform SPIWriteRead
  { 0x51, 0x02 },

  { 0x7F, 0x03 }, // switch to bank3, not allowed to perform SPIWriteRead
  { 0x05, 0x0C },
  { 0x06, 0x0C },
  { 0x07, 0x0C },
  { 0x08, 0x0C },

  { 0x7F, 0x04 }, // switch to bank4, not allowed to perform SPIWriteRead
  { 0x05, 0x01 },
  { 0x53, 0x08 },

  { 0x7F, 0x05 }, // switch to bank5, not allowed to perform SPIWriteRead
  { 0x00, 0x02 },
  { 0x09, 0x01 },
  { 0x0A, 0x1C },
  { 0x0B, 0x24 },
  { 0x0C, 0x1C },
  { 0x0D, 0x24 },
  { 0x12, 0x08 },
  { 0x14, 0x02 },
  { 0x16, 0x02 },
  { 0x18, 0x1C },
  { 0x19, 0x24 },
  { 0x1A, 0x1C },
  { 0x1B, 0x24 },
  { 0x20, 0x08 },
  { 0x22, 0x02 },
  { 0x24, 0x02 },
  { 0x26, 0x88 },
  { 0x2F, 0x7C },
  { 0x30, 0x07 },
  { 0x3D, 0x00 },
  { 0x3E, 0x98 },

  { 0x7F, 0x06 }, // switch to bank6, not allowed to perform SPIWriteRead
  { 0x34, 0x03 },

  { 0x7F, 0x07 }, // switch to bank7, not allowed to perform SPIWriteRead
  { 0x00, 0x01 },
  { 0x02, 0xC4 },
  { 0x03, 0x13 },
  { 0x06, 0x0C },
  { 0x0F, 0x0A },
  { 0x14, 0x02 },
  { 0x35, 0x39 },
  { 0x36, 0x3F },
  { 0x46, 0x03 },
  { 0x47, 0x0F },
  { 0x4B, 0x97 },

  { 0x7F, 0x00 }, // switch to bank0, not allowed to perform SPIWriteRead
  { 0x09, 0x00 }, // enable write protect
};

const int chipSelectPin = 8;
uint8_t SensorPID;
int16_t m_totalX = 0, m_totalY = 0, deltaX_high = 0, deltaY_high = 0;
uint8_t deltaX_low = 0, deltaY_low = 0;

void writeRegister(uint8_t addr, uint8_t data)
{
  digitalWrite(chipSelectPin, LOW);
  uint8_t _addr = addr | 0x80;
  SPI.transfer(_addr);
  SPI.transfer(data);
  digitalWrite(chipSelectPin, HIGH);
}

uint8_t readRegister(uint8_t addr)
{
  digitalWrite(chipSelectPin, LOW);
  uint8_t _addr = addr & 0x7F;
  SPI.transfer(_addr);
  delayMicroseconds(1);
  uint8_t data_read = SPI.transfer(0x00);
  digitalWrite(chipSelectPin, HIGH);

  return data_read;
}

uint8_t sensor_init()
{
  writeRegister(0x7F, 0x00);
  writeRegister(0x06, 0x80);

  delay(10);

  if (readRegister(0x00) != PXI_WMI) return 0;

  SensorPID = readRegister(0x00);
  for (uint8_t i = 0; i < NUM_REGISTERS; ++i)
  {
    writeRegister(Pixart_OTS_9130_InitSetting[0], Pixart_OTS_9130_InitSetting[1]);
  }

  return 1;
}

void setup()
{
  SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE3));
  SerialUSB.begin(115200);
  pinMode (chipSelectPin, OUTPUT);
  digitalWrite(chipSelectPin, HIGH);

  if (sensor_init()) {
    SerialUSB.println("Sensor Initialisation Successful");
  }
  else {
    SerialUSB.println("Failed to Initialise Sensor!");
    while (1);
  }
}

void loop()
{
  int16_t x, y;

  if (readRegister(0x02) & 0x80) {
    deltaX_high = readRegister(0x11) << 8;
    deltaY_high = readRegister(0x12) << 8;
    deltaX_low = readRegister(0x03);
    deltaY_low = readRegister(0x04);

    x = deltaX_high | deltaX_low;
    y = deltaY_high | deltaY_low;

    if ((x != 0) || (y != 0))
    {
      m_totalX += x;
      m_totalY += y;

      SerialUSB.print("x = ");
      SerialUSB.print(x);
      SerialUSB.print(", y = ");
      SerialUSB.println(y);

      SerialUSB.print("m_totalX = ");
      SerialUSB.print(m_totalX);
      SerialUSB.print(", m_totalY = ");
      SerialUSB.println(m_totalY);
    }
  }

  delay(250); //performs read every 250ms approximately
}

hope that helps...

warriorfenix26:

something strange is going on.... I would at least expect to see the SCLK pulses but they do not seem to be generated correctly

something of this form:

please share information about the board you are using the a schematic (hand drawn is OK) of how you ACTUALLY wired up the sensor to your board