Lire capteur souris optique

De Wiki LOGre
Révision de 26 avril 2012 à 20:49 par A427 (discuter | contributions) (Page créée avec « Voici un sketch Arduino pour lire le capteur optique d'une souris, compatible avec la datasheet ADNS2610 : <pre> /* Blink Turns on an LED on for one second, then o... »)

(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 :


/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
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;    // 
  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);
   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;
}
   }
}