Advertisement
Guest User

ArduMouse2

a guest
Mar 25th, 2016
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. #define sclk 5 //Define pin 5 as clock
  2. #define sdio 6 //Define pin 6 as data
  3.  
  4. //Mouse sensor register addresses
  5. #define CONF 0x1B
  6. #define LORES 0x80
  7. #define CHNG  0x80
  8. #define OFLOW 0x18
  9. #define NEG 0x80
  10.  
  11. byte stat;
  12. byte quality;
  13. byte ovfl;
  14. byte readx;
  15. byte ready;
  16.  
  17. int dx;
  18. int dy;
  19. int x;
  20. int y;
  21.  
  22. void WriteAddr(byte addr, byte data)
  23. {
  24.   byte i;
  25.  
  26.   addr |= 0x80; //Setting MSB high indicates a write operation.
  27.  
  28.   //Write the address
  29.   pinMode (sdio, OUTPUT);
  30.   for (i = 8; i != 0; i--)
  31.   {
  32.     digitalWrite (sclk, LOW);
  33.     digitalWrite (sdio, addr & (1 << (i-1) ));
  34.     digitalWrite (sclk, HIGH);
  35.   }
  36.  
  37.   //Write the data    
  38.   for (i = 8; i != 0; i--)
  39.   {
  40.     digitalWrite (sclk, LOW);
  41.     digitalWrite (sdio, data & (1 << (i-1) ));
  42.     digitalWrite (sclk, HIGH);
  43.   }
  44. }
  45.  
  46. byte ReadAddr(byte addr)
  47. {
  48.   byte i;
  49.   byte r = 0;
  50.  
  51.   //Write the address
  52.   pinMode (sdio, OUTPUT);
  53.   for (i = 8; i != 0; i--)
  54.   {
  55.     digitalWrite (sclk, LOW);
  56.     digitalWrite (sdio, addr & (1 << (i-1) ));
  57.     digitalWrite (sclk, HIGH);
  58.   }
  59.  
  60.   pinMode (sdio, INPUT);  //Switch the dataline from output to input
  61.   delayMicroseconds(110);  //Wait (per the datasheet, the chip needs a minimum of 100 µsec to prepare the data)
  62.  
  63.   //Clock the data back in
  64.   for (i = 8; i != 0; i--)
  65.   {                            
  66.     digitalWrite (sclk, LOW);
  67.     digitalWrite (sclk, HIGH);
  68.     r |= (digitalRead (sdio) << (i-1) );
  69.   }
  70.  
  71.   delayMicroseconds(110);  //Tailing delay guarantees >100 µsec before next transaction
  72.  
  73.   return r;
  74. }
  75.  
  76. void setup()
  77. {
  78.   Serial.begin(9600);
  79.   pinMode(sclk, OUTPUT);
  80.   digitalWrite(sclk, LOW);
  81.   delay(100);
  82.   WriteAddr(CONF, LORES);
  83.  
  84. }
  85. byte i;
  86. bool stop=false;
  87. void loop()
  88. {
  89.   stat = ReadAddr(0x02);
  90.   if (stat & CHNG)
  91.   {
  92.     if (stat & OFLOW)
  93.     {
  94.       ovfl =10 ;
  95.     }
  96.     readx = ReadAddr(0x03);
  97.     if (readx & NEG)
  98.     {
  99.       dx = -1 * (256 - readx);
  100.     } else {
  101.       dx = readx;
  102.     }
  103.     x = x + dx;
  104.     ready = ReadAddr(0x04);
  105.     if (ready & NEG)
  106.     {
  107.       dy = -1 * (256 - ready);
  108.     } else {
  109.       dy = ready;
  110.     }
  111.     y = y + dy;
  112.     quality = ReadAddr(0x07);
  113.     Serial.print(dx, DEC);
  114.     Serial.print("\t");
  115.     Serial.print(x, DEC);
  116.     Serial.print("\t");
  117.     Serial.print(dy, DEC);
  118.     Serial.print("\t");
  119.     Serial.print(y, DEC);
  120.     Serial.print("\t");
  121.     Serial.print(quality, DEC);
  122.     if (ovfl > 0)
  123.     {
  124.       ovfl = ovfl - 1;
  125.       Serial.print("\t");
  126.       Serial.print("Overflow");
  127.     }  
  128.     Serial.print("\n");
  129.   }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement