sketchbook
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ADNS3050.h
Go to the documentation of this file.
1 #include <SPI.h>
2 
3  // SPI and misc pins for the ADNS
4  #define PIN_SCLK SCK
5  #define PIN_MISO MISO
6  #define PIN_MOSI MOSI
7  #define PIN_NCS 3
8  #define PIN_MOTION 5
9 
10 
11 // Define all the registers
12  #define PROD_ID 0x00
13  #define REV_ID 0x01
14  #define MOTION_ST 0x02
15  #define DELTA_X 0x03
16  #define DELTA_Y 0x04
17  #define SQUAL 0x05
18  #define SHUT_HI 0x06
19  #define SHUT_LO 0x07
20  #define PIX_MAX 0x08
21  #define PIX_ACCUM 0x09
22  #define PIX_MIN 0x0a
23  #define PIX_GRAB 0x0b
24  #define MOUSE_CTRL 0x0d
25  #define RUN_DOWNSHIFT 0x0e
26  #define REST1_PERIOD 0x0f
27  #define REST1_DOWNSHIFT 0x10
28  #define REST2_PERIOD 0x11
29  #define REST2_DOWNSHIFT 0x12
30  #define REST3_PERIOD 0x13
31  #define PREFLASH_RUN 0x14
32  #define PREFLASH_RUN_DARK 0x18
33  #define MOTION_EXT 0x1b
34  #define SHUT_THR 0x1c
35  #define SQUAL_THRESHOLD 0x1d
36  #define NAV_CTRL2 0x22
37  #define MISC_SETTINGS 0x25
38  #define RESOLUTION 0x33
39  #define LED_PRECHARGE 0x34
40  #define FRAME_IDLE 0x35
41  #define RESET 0x3a
42  #define INV_REV_ID 0x3f
43  #define LED_CTRL 0x40
44  #define MOTION_CTRL 0x41
45  #define AUTO_LED_CONTROL 0x43
46  #define REST_MODE_CONFIG 0x45
47 
48 
49 void com_start(){
51  delay(20);
53 }
54 
55 
56 
57 byte Read(byte reg_addr){
58  digitalWrite(PIN_NCS, LOW);//begin communication
59  // send address of the register, with MSBit = 0 to say it's reading
60  SPI.transfer(reg_addr & 0x7f );
61  delayMicroseconds(100);
62  // read data
63  byte data = SPI.transfer(0);
65  digitalWrite(PIN_NCS, HIGH);//end communication
67 
68  return data;
69 }
70 
71 
72 void Write(byte reg_addr, byte data){
74  //send address of the register, with MSBit = 1 to say it's writing
75  SPI.transfer(reg_addr | 0x80 );
76  //send data
77  SPI.transfer(data);
79  digitalWrite(PIN_NCS, HIGH);//end communication
81 }
82 
83 void startup(){
84  //--------Setup SPI Communication---------
85  Serial.begin(115200);
86  byte out = 0;
87  byte read = 0;
88  byte bit = 0;
91  SPI.begin();
92  // set the details of the communication
93  SPI.setBitOrder(MSBFIRST); // transimission order of bits
94  SPI.setDataMode(SPI_MODE3); // sampling on rising edge
95  SPI.setClockDivider(SPI_CLOCK_DIV16); // 16MHz/16 = 1MHz
96  delay(10);
97 
98  //----------------- Power Up and config ---------------
99  com_start();
100  Write(RESET, 0x5a); // force reset
101  delay(100); // wait for it to reboot
102  Write(MOUSE_CTRL, 0x20);//Setup Mouse Control
103  Write(MOTION_CTRL, 0x00);//Clear Motion Control register
104  delay(100);
105  }
106 
107 
108 
109 int convTwosComp(int b){ //Convert from 2's complement
110  if(b & 0x80){
111  b = -1 * ((b ^ 0xff) + 1);
112  }
113  return b;
114  }
115 
116 void getXY(){//Prints out X and Y values to the serial terminal, use getX and getY sequentially for most operations
117  byte x = 0;
118  byte y = 0;
119  x= Read(0x03);
120  y = Read(0x04);
121  Serial.println("x");
123  Serial.println("y");
125 }
126 
127 
128 int getX(){//returns the X acceleration value
129  byte x = 0;
130  x= Read(0x03);
131  return(convTwosComp(x));
132 }
133 
134 int getY(){//returns the Y acceleration value
135  byte y = 0;
136  y= Read(0x04);
137  return(convTwosComp(y));
138 }
139 
140 
141 
void pinMode(uint8_t, uint8_t)
Definition: main.cpp:278
#define MOUSE_CTRL
Definition: ADNS3050.h:24
void com_start()
Definition: ADNS3050.h:49
#define PIN_MISO
Definition: ADNS3050.h:5
string out
Definition: rf-id-control.py:34
void startup()
Definition: ADNS3050.h:83
uint8_t byte
Definition: Arduino.h:123
#define MSBFIRST
Definition: wiringShift.h:26
HardwareSerial Serial
int getX()
Definition: ADNS3050.h:128
void delayMicroseconds(unsigned int howLong)
Definition: wiringPi.c:2118
#define OUTPUT
Definition: Arduino.h:34
#define HIGH
Definition: Arduino.h:30
#define INPUT
Definition: Arduino.h:33
int32_t int16_t b
Definition: IMU.cpp:172
byte Read(byte reg_addr)
Definition: ADNS3050.h:57
tuple data
Definition: ser-mon-AS7265X.py:40
def read
Definition: ow-slave-test.py:39
void digitalWrite(uint8_t, uint8_t)
Definition: main.cpp:298
int convTwosComp(int b)
Definition: ADNS3050.h:109
#define LOW
Definition: Arduino.h:31
#define RESET
Definition: ADNS3050.h:41
static __inline__ uint32_t uint32_t y
Definition: arm_acle.h:113
#define MOTION_CTRL
Definition: ADNS3050.h:44
#define PIN_NCS
Definition: ADNS3050.h:7
void begin(unsigned long baud)
Definition: HardwareSerial.h:123
int getY()
Definition: ADNS3050.h:134
void Write(byte reg_addr, byte data)
Definition: ADNS3050.h:72
size_t println(const char[])
Definition: Print.cpp:140
void getXY()
Definition: ADNS3050.h:116