|
#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;
}
|
|
|