Using the Avago ADNS-2610 Optical Sensor - Hi-Tech C code for 16F690



Back to main ADNS-2610 page

Following is the code for handling the ADNS-2610 sensor using the PIC16F690. It is written in Hi-Tech C. Pass the address of the register you want to write to to the "Address" variable, and the data you want to write to this register to the "Data" variable in the WriteSensor routine. You can buy the ADNS-2610 from
Digikey. If you need the lens (HDNS-2100) or the LED clip (HDNS-2200) to go with it in small quantities, you can find them here.

#include < htc.h>
#include < conio.h>

#define SCK RC0 //create alias for C0 pin
#define SDIO RC1 //create alias for C1 pin

void Delay1(long x) //delay loop to ensure timing is correct
{
long y=0;
while (y < x)
{
y++;
}
}

void PulseClock() //used to clock the data in/out of the ADNS-2610
{
SCK=0;
SCK=0;
SCK=1;
SCK=1;
}

void WriteSensor(int Address, int Data) //used to write data to the ADNS2610
{
TRISC=0; //set all pins in PORTC to output
SCK=1;
SDIO=0;

SDIO=1; //bit 7 is set for write instructions
PulseClock();

if (Address>=128) //Address 7
{Address=Address-128;}

int x=0;
int y=64;

while (x<7)
{

if (Address>=y) //Address 6
{SDIO=1;
Address=Address-y;}
else
{SDIO=0;}
PulseClock();

y=y/2;
x++;
}

x=0;
y=128;

while (x<8)
{
if (Data>=y)
{SDIO=1;
Data=Data-y;}
else
{SDIO=0;}
PulseClock();

y=y/2;
x++;
}

SCK=1;
SDIO=0;
}

int ReadSensor(int Address)// Used to read registers of the ADNS2610
{
TRISC=0; //set all pins in PORTC to output
SCK=1;
SDIO=0;

int x=0;
int y=128;

while (x<8)
{

if (Address>=y)
{SDIO=1;
Address=Address-y;}
else
{SDIO=0;}
PulseClock();

y=y/2;
x++;
}

x=0;
y=128;
int Data=0;

SDIO=0;
TRISC=2; //set SDIO to input

Delay1(50); //This delay is madatory to give the ADNS-2610 time to load the data

while (x<8)
{
PulseClock();
if (SDIO==1)
{Data=Data+y;}

y=y/2;
x++;

}

TRISC=0;
SCK=1;
SDIO=0;

return Data;
}


Back to main ADNS-2610 page

Any questions or comments - contact me at pwclark1977@hotmail.com