Hi everyone,
I could not read the real datas from registers. I paste my codes below. After running my code, i saw this message in my serial monitor:
Communication SuccesSful
255
x=255
y=255
But if i move my mouse, nothing changed. all the time x and y file always were 255.
I need your help…
#include <SPI.h>
// SPI and misc pins for the ADNS
#define PIN_SCLK SCK
#define PIN_MISO MISO
#define PIN_MOSI MOSI
#define PIN_NCS 9
#define PIN_MOTION 5
// Define all the registers
#define PROD_ID 0x00
#define REV_ID 0x01
#define MOTION_ST 0x02
#define DELTA_X 0x03
#define DELTA_Y 0x04
#define SQUAL 0x05
#define SHUT_HI 0x06
#define SHUT_LO 0x07
#define PIX_MAX 0x08
#define PIX_ACCUM 0x09
#define PIX_MIN 0x0a
#define PIX_GRAB 0x0b
#define MOUSE_CTRL 0x0d
#define RUN_DOWNSHIFT 0x0e
#define REST1_PERIOD 0x0f
#define REST1_DOWNSHIFT 0x10
#define REST2_PERIOD 0x11
#define REST2_DOWNSHIFT 0x12
#define REST3_PERIOD 0x13
#define MOUSE_CTRL_EN 0x21
#define FRAME_IDLE 0x35
#define RESET 0x3a
#define NOT_REV_ID 0x3f
#define LED_CTRL 0x40
#define MOTION_CTRL 0x41
#define BURST_READ_FIRST 0x42
#define AUTO_LED_CTRL 0x43
#define REST_MODE_CONFIG 0x45
#define MOTION_BURST 0x63
void com_start() {
digitalWrite(PIN_NCS, HIGH);
delay(20);
digitalWrite(PIN_NCS, LOW);
}
void Write(byte reg_addr, byte data) {
digitalWrite(PIN_NCS, LOW);
//send address of the register, with MSBit = 1 to say it's writing
SPI.transfer(reg_addr | 0x80 );
//send data
SPI.transfer(data);
delayMicroseconds(30);
digitalWrite(PIN_NCS, HIGH);//end communication
delayMicroseconds(30);
}
byte Read(byte reg_addr) {
digitalWrite(PIN_NCS, LOW);//begin communication
// send address of the register, with MSBit = 0 to say it's reading
SPI.transfer(reg_addr & 0x7f );
delayMicroseconds(100);
// read data
byte data = SPI.transfer(0);
delayMicroseconds(30);
digitalWrite(PIN_NCS, HIGH);//end communication
delayMicroseconds(30);
return data;
}
int convTwosComp(int b) { //Convert from 2's complement
if (b & 0x80) {
b = -1 * ((b ^ 0xff) + 1);
}
return b;
}
void getXY() { //Prints out X and Y values to the serial terminal, use getX and getY sequentially for most operations
byte x = 0;
byte y = 0;
x = Read(0x03);
y = Read(0x04);
Serial.print("x=");
//Serial.println(convTwosComp(x));
Serial.println(x);
Serial.print("y=");
//Serial.println(convTwosComp(y));
Serial.println(y);
}
void setup() {
Serial.begin(115200);
// start the SPI library:
byte out = 0;
byte read = 0;
byte bit = 0;
pinMode(PIN_MISO, INPUT);
pinMode(PIN_MOSI, OUTPUT);
SPI.begin();
SPI.setBitOrder(MSBFIRST); // transimission order of bits
SPI.setDataMode(SPI_MODE3); // sampling on rising edge
SPI.setClockDivider(SPI_CLOCK_DIV16); // 16MHz/16 = 1MHz
delay(10);
com_start();
Write(RESET, 0x5a); // force reset
delay(100); // wait for it to reboot
Write(MOUSE_CTRL, 0x20);//Setup Mouse Control
Write(MOTION_CTRL, 0x00);//Clear Motion Control register
delay(100);
// ADNS5090_WRITE_VAL (0x80)
// ADNS5090_RESET_VAL (0x5a
// ADNS5090_POWERDOWN_VAL (0x02)
}
void loop() {
byte id = 0;
id = Read(PROD_ID);
if (id == 255) {
Serial.println("Communication SuccesSful");
}
Serial.println(id);
delay(1000);
getXY();//Get X and Y accerlation and print
delay(1000);
}