Lire capteur souris optique

De Wiki LOGre
Révision de 27 novembre 2015 à 13:52 par A427 (discuter | contributions)

(diff) ← Version précédente | Voir la version courante (diff) | Version suivante → (diff)
Aller à : navigation, rechercher


Voici un sketch Arduino pour lire le capteur optique d'une souris, compatible avec la datasheet ADNS2610 :

(version locale : Fichier:AV02-1184EN.pdf)

(note : ce code est expérimental et à nettoyer)

Pinout : (voir la datasheet ADNS2610)

La pin SCK du capteur (pin 4) est a relier a la pin 3 de l'arduino

La pin SDIO du capteur (pin 3) est a relier a la pin 2 de l'arduino

La pin GND du capteur (pin 6) est a relier a la pin GND de l'arduino

La pin VDD du capteur (pin 7) est a alimenter en 5V

/*
 * Read X/Y delta displacements from Optical Mouse sensor (compatible with ADNS2610)
 */
int led = 13;
int sck = 3;
int sdio = 2;
 
void mwrite(int reg, int data)
{
  int io;
  int i;
  pinMode(sdio, OUTPUT);
  pinMode(sck, OUTPUT);
  for (i =0; i < 8; i++)
  {
    if (reg & 0x80)
      io = HIGH;
    else
      io = LOW;
    reg <<= 1;
    digitalWrite(sck, LOW);
    digitalWrite(sdio, io);
    //delayMicroseconds(10);
    del();
    digitalWrite(sck, HIGH);
    //delayMicroseconds(10);
    del();
  }
  digitalWrite(sdio, HIGH);
  //delayMicroseconds(20);
  pinMode(sdio, INPUT);
  delayMicroseconds(150);
  for (i =0; i < 8; i++)
  {
    digitalWrite(sck, LOW);
    //delayMicroseconds(1);
    if (data & 0x80)
      io = HIGH;
    else
      io = LOW;
    data <<= 1;
    digitalWrite(sdio, io);
    del();
    digitalWrite(sck, HIGH);
    //delayMicroseconds(1);
    del();
    data <<= 1;
    data += io;
  }
  pinMode(sck, OUTPUT);
  digitalWrite(sck, HIGH);
}
 
void setup() {
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(led, OUTPUT);
  pinMode(sck, OUTPUT);
  digitalWrite(sck, HIGH);
  pinMode(sdio, OUTPUT);
  digitalWrite(sdio, HIGH);
  Serial.begin(9600);
 
  pinMode(sck, OUTPUT);
  digitalWrite(sck, HIGH);
  pinMode(sdio, OUTPUT);
  digitalWrite(sdio, HIGH);
 
  delay(1000);
  int reg = 0x80;    // 00010001
  int data = 0x01;
 
  mwrite(reg,data);
  delay(1000);
}
 
void del()
{
  delayMicroseconds(10);
}
 
int x = 0;
void loop() {
  pinMode(sck, OUTPUT);
  digitalWrite(sck, HIGH);
  pinMode(sdio, OUTPUT);
  digitalWrite(sdio, HIGH);
 
  digitalWrite(led, HIGH);   // set the LED on
  //delay(500);              // wait for a second
  digitalWrite(led, LOW);    // set the LED off
  //delay(500);              // wait for a second
  //int reg = 0x11;    // 00010001
  int reg = 0x03;    // ok=>X Delta
  //int reg = 0x08;    // pixels
  int data = 0;
  int io;
  int i;
  pinMode(sdio, OUTPUT);
  pinMode(sck, OUTPUT);
  for (i =0; i < 8; i++)
  {
    if (reg & 0x80)
      io = HIGH;
    else
      io = LOW;
    reg <<= 1;
    digitalWrite(sck, LOW);
    digitalWrite(sdio, io);
    //delayMicroseconds(10);
    del();
    digitalWrite(sck, HIGH);
    //delayMicroseconds(10);
    del();
  }
  digitalWrite(sdio, HIGH);
  //delayMicroseconds(20);
  pinMode(sdio, INPUT);
  delayMicroseconds(150);
  for (i =0; i < 8; i++)
  {
    digitalWrite(sck, LOW);
    //delayMicroseconds(1);
    del();
    digitalWrite(sck, HIGH);
    io = digitalRead(sdio);
    //delayMicroseconds(1);
    del();
    data <<= 1;
    data += io;
  }
  pinMode(sck, OUTPUT);
  digitalWrite(sck, HIGH);
  delay(100);
  Serial.print("x=");
  Serial.println( data);
#if 0
  if (data & 0x80)
  {
    Serial.print( "S");
    x = 0;
  }
  else
    Serial.print( " ");
  if (data & 0x40)
  {
    //Serial.print( " ");
    //else
    //Serial.print( "*");
    data &= 0x3F;
    Serial.print(data,16);
    //Serial.print(data>>4);
    Serial.print( ",");
    //Serial.print(data&0xf);
    x++;
    if (x >= 18)
    {
      Serial.println();
      x = 0;
    }
  }
#endif
 
  reg = 0x02;    // ok?=>Y Delta
  data = 0;
  pinMode(sdio, OUTPUT);
  pinMode(sck, OUTPUT);
  for (i =0; i < 8; i++)
  {
    if (reg & 0x80)
      io = HIGH;
    else
      io = LOW;
    reg <<= 1;
    digitalWrite(sck, LOW);
    digitalWrite(sdio, io);
    //delayMicroseconds(10);
    del();
    digitalWrite(sck, HIGH);
    //delayMicroseconds(10);
    del();
  }
  digitalWrite(sdio, HIGH);
  //delayMicroseconds(20);
  pinMode(sdio, INPUT);
  delayMicroseconds(150);
  for (i =0; i < 8; i++)
  {
    digitalWrite(sck, LOW);
    //delayMicroseconds(1);
    del();
    digitalWrite(sck, HIGH);
    io = digitalRead(sdio);
    //delayMicroseconds(1);
    del();
    data <<= 1;
    data += io;
  }
  pinMode(sck, OUTPUT);
  digitalWrite(sck, HIGH);
  delay(100);
  Serial.print("y=");
  Serial.println( data);
}