Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define sclk 5 //Define pin 5 as clock
- #define sdio 6 //Define pin 6 as data
- //Mouse sensor register addresses
- #define CONF 0x1B
- #define LORES 0x80
- #define CHNG 0x80
- #define OFLOW 0x18
- #define NEG 0x80
- byte stat;
- byte quality;
- byte ovfl;
- byte readx;
- byte ready;
- int dx;
- int dy;
- int x;
- int y;
- void WriteAddr(byte addr, byte data)
- {
- byte i;
- addr |= 0x80; //Setting MSB high indicates a write operation.
- //Write the address
- pinMode (sdio, OUTPUT);
- for (i = 8; i != 0; i--)
- {
- digitalWrite (sclk, LOW);
- digitalWrite (sdio, addr & (1 << (i-1) ));
- digitalWrite (sclk, HIGH);
- }
- //Write the data
- for (i = 8; i != 0; i--)
- {
- digitalWrite (sclk, LOW);
- digitalWrite (sdio, data & (1 << (i-1) ));
- digitalWrite (sclk, HIGH);
- }
- }
- byte ReadAddr(byte addr)
- {
- byte i;
- byte r = 0;
- //Write the address
- pinMode (sdio, OUTPUT);
- for (i = 8; i != 0; i--)
- {
- digitalWrite (sclk, LOW);
- digitalWrite (sdio, addr & (1 << (i-1) ));
- digitalWrite (sclk, HIGH);
- }
- pinMode (sdio, INPUT); //Switch the dataline from output to input
- delayMicroseconds(110); //Wait (per the datasheet, the chip needs a minimum of 100 µsec to prepare the data)
- //Clock the data back in
- for (i = 8; i != 0; i--)
- {
- digitalWrite (sclk, LOW);
- digitalWrite (sclk, HIGH);
- r |= (digitalRead (sdio) << (i-1) );
- }
- delayMicroseconds(110); //Tailing delay guarantees >100 µsec before next transaction
- return r;
- }
- void setup()
- {
- Serial.begin(9600);
- pinMode(sclk, OUTPUT);
- digitalWrite(sclk, LOW);
- delay(100);
- WriteAddr(CONF, LORES);
- }
- byte i;
- bool stop=false;
- void loop()
- {
- stat = ReadAddr(0x02);
- if (stat & CHNG)
- {
- if (stat & OFLOW)
- {
- ovfl =10 ;
- }
- readx = ReadAddr(0x03);
- if (readx & NEG)
- {
- dx = -1 * (256 - readx);
- } else {
- dx = readx;
- }
- x = x + dx;
- ready = ReadAddr(0x04);
- if (ready & NEG)
- {
- dy = -1 * (256 - ready);
- } else {
- dy = ready;
- }
- y = y + dy;
- quality = ReadAddr(0x07);
- Serial.print(dx, DEC);
- Serial.print("\t");
- Serial.print(x, DEC);
- Serial.print("\t");
- Serial.print(dy, DEC);
- Serial.print("\t");
- Serial.print(y, DEC);
- Serial.print("\t");
- Serial.print(quality, DEC);
- if (ovfl > 0)
- {
- ovfl = ovfl - 1;
- Serial.print("\t");
- Serial.print("Overflow");
- }
- Serial.print("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement