Guest User

Arduino ADNS-2160 communication

a guest
Sep 21st, 2011
19,968
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //-------------------------------------------------------
  2. #define SDIO 8
  3. #define SCLK 9
  4. //-------------------------------------------------------
  5. /*class ADNS2610
  6. {
  7. public:
  8. ADNS2610(int sdio,int sclk);
  9. void sync();
  10. char read(char address);
  11. void write(char address,char value);
  12. private:
  13. int _sdio;
  14. int _sclk;
  15. }*/
  16. //-------------------------------------------------------
  17. byte DriverRead(byte address)
  18. {
  19. pinMode (SDIO, OUTPUT);
  20. for (byte i=128; i >0 ; i >>= 1)
  21. {
  22. digitalWrite (SCLK, LOW);
  23. delayMicroseconds(10);
  24. digitalWrite (SDIO, (address & i) != 0 ? HIGH : LOW);
  25. delayMicroseconds(10);
  26. digitalWrite (SCLK, HIGH);
  27. delayMicroseconds(10);
  28. }
  29. delayMicroseconds(120);
  30. pinMode (SDIO, INPUT);
  31. byte res = 0;
  32. for (byte i=128; i >0 ; i >>= 1)
  33. {
  34. digitalWrite (SCLK, LOW);
  35. delayMicroseconds(10);
  36. digitalWrite (SCLK, HIGH);
  37. delayMicroseconds(10);
  38. if( digitalRead (SDIO) == HIGH )
  39. {
  40. res |= i;
  41. }
  42. }
  43. delayMicroseconds(100);
  44. return res;
  45. }
  46. //-------------------------------------------------------
  47. void DriverWrite(byte address, byte data)
  48. {
  49. address |= 0x80;
  50. pinMode (SDIO, OUTPUT);
  51. for (byte i = 128; i > 0 ; i >>= 1)
  52. {
  53. digitalWrite (SCLK, LOW);
  54. delayMicroseconds(10);
  55. digitalWrite (SDIO, (address & i) != 0 ? HIGH : LOW);
  56. delayMicroseconds(10);
  57. digitalWrite (SCLK, HIGH);
  58. delayMicroseconds(10);
  59. }
  60. delayMicroseconds(120);
  61. for (byte i = 128; i > 0 ; i >>= 1)
  62. {
  63. digitalWrite (SCLK, LOW);
  64. delayMicroseconds(10);
  65. digitalWrite (SDIO, (data & i) != 0 ? HIGH : LOW);
  66. delayMicroseconds(10);
  67. digitalWrite (SCLK, HIGH);
  68. delayMicroseconds(10);
  69. }
  70. delayMicroseconds(100);
  71. }
  72. //-------------------------------------------------------
  73. void DriverInit()
  74. {
  75. pinMode(SCLK, OUTPUT);
  76. pinMode(SDIO, OUTPUT);
  77. digitalWrite(SCLK, HIGH);
  78. delayMicroseconds(5);
  79. digitalWrite(SCLK, LOW);
  80. delayMicroseconds(1);
  81. digitalWrite(SCLK, HIGH);
  82. delay(1025);
  83. DriverWrite(0x00, 0x01);
  84. delay(3000);
  85. }
  86. //-------------------------------------------------------
  87. int DriverReadFrame(byte *arr, int len)
  88. {
  89. byte *pos=arr;
  90. byte *uBound=arr+len;
  91. unsigned long timeout = millis() + 1000;
  92. byte val;
  93. DriverWrite(0x08, 0x2A);
  94. while( millis() < timeout && pos < uBound)
  95. {
  96. val = DriverRead(0x08);
  97.  
  98. //Only bother with the next bit if the pixel data is valid.
  99. if( !(val & 64) )
  100. {
  101. continue;
  102. }
  103. //If we encounter a start-of-field indicator, and the cursor isn't at the first pixel,
  104. //then stop. ('Cause the last pixel was the end of the frame.)
  105. if( ( val & 128 ) && ( pos != arr) )
  106. {
  107. break;
  108. }
  109. *pos = val & 63;
  110. pos++;
  111. }
  112. return (pos-arr);
  113. }
  114. //-------------------------------------------------------
  115. void setup()
  116. {
  117. Serial.begin(9600);
  118. DriverInit();
  119. }
  120. //-------------------------------------------------------
  121. void loop()
  122. {
  123. /*byte x = DriverRead(0x02);
  124. byte y = DriverRead(0x03);
  125. if(x || y)
  126. {
  127. Serial.print("X:");
  128. Serial.print(x,DEC);
  129. Serial.print(" Y:");
  130. Serial.println(y,DEC);
  131. }*/
  132.  
  133. if (Serial.available() > 0)
  134. {
  135. byte c = Serial.read();
  136. if (c == 'F')
  137. {
  138. byte tmp[324];
  139. int len = DriverReadFrame(tmp,324);
  140. for(int i=0;i<len;i++)
  141. {
  142. Serial.write(tmp[i]);
  143. }
  144. Serial.println();
  145. Serial.println(len);
  146. }
  147. }
  148. }
  149. //-------------------------------------------------------
RAW Paste Data