|
各位高手们,有谁用过ADNS5050芯片的吗?通过arduino atmega328来计算位移进行定位?本人是新手,请各位大侠帮帮忙,本人不胜感激。网上的代码好多有点头大了。
里面好多的寄存器啊。Motion, SQUAL,Piexl_Sum , Pixel_Grab 几个寄存器怎么用。能不能把代码贴出来学习一下啊。这是我的代码,各位帮忙看看
#include "arduino.h"
#include "Adns.h"
/******************************************************************************
* Definitions
******************************************************************************/
#define SCLK 6
#define SDIO 5
#define NCS 4
#define PRODUCT_ID 0x00
#define PRODUCTID2 0x3e
#define REVISION_ID 0x01
#define MOTION_REG 0x02
#define DELTA_Y_REG 0x03
#define DELTA_X_REG 0x04
#define SQUAL_REG 0x05
#define MAXIMUM_PIXEL_REG 0x08
#define MINIMUM_PIXEL_REG 0x0a
#define PIXEL_SUM_REG 0x09
#define PIXEL_DATA_REG 0x0b
#define SHUTTER_UPPER_REG 0x06
#define SHUTTER_LOWER_REG 0x07
#define RESET 0x3a
#define MOUSE_CONTROL2 0x19
#define RES_CPI 0x1B
#define NUM_PIXELS 361
byte pix[361];
int dx;
int dy;
void setup()
{
Serial.begin(9600);
pinMode(SDIO, OUTPUT);
pinMode(SCLK, OUTPUT);
pinMode(NCS, OUTPUT);
sync();
byte productId = ADNS_read_old(PRODUCT_ID);
byte revisionId = ADNS_read_old(REVISION_ID);
Serial.print("Found productId ");
Serial.print(productId);
Serial.print(", rev. ");
Serial.println(revisionId);
}
void loop()
{
// Freezes DX and DY until they are read or MOTION is read again.
dx += ADNS_read_old(DELTA_X_REG);
dy += ADNS_read_old(DELTA_Y_REG);
Serial.print("DELTA:");
Serial.print(dx, DEC);
Serial.print(" ");
Serial.print(dy, DEC);
Serial.print('\r');
Serial.print('\n');
}
void sync()
{
pinMode(NCS, OUTPUT);
digitalWrite(NCS, LOW);
delayMicroseconds(2);
digitalWrite(NCS, HIGH);
ADNS_write_old(RESET, 0x5a);
delay(100); // From NRESET pull high to valid motion, assuming VDD and motion is present.
if(ADNS_read_old(PRODUCT_ID) == 0x12)
{
ADNS_write_old(MOUSE_CONTROL2, 0x1B);
}
}
void ADNS_write_old(unsigned char addr, unsigned char data) {
char temp;
int n;
digitalWrite(NCS, LOW);//nADNSCS = 0; // select the chip
temp = addr ;
digitalWrite(SCLK, LOW);//SCK = 0; // start clock low
pinMode(SDIO, OUTPUT);//DATA_OUT; // set data line for output
for (n=0; n<8; n++) {
digitalWrite(SCLK, LOW);//SCK = 0;
pinMode(SDIO, OUTPUT);
delayMicroseconds(1);
if (temp & 0x80)
digitalWrite(SDIO, HIGH);//SDOUT = 1;
else
digitalWrite(SDIO, LOW);//SDOUT = 0;
temp = (temp << 1);
digitalWrite(SCLK, HIGH);//SCK = 1;
delayMicroseconds(1);//delayMicroseconds(1); // short clock pulse
}
temp = data;
for (n=0; n<8; n++) {
digitalWrite(SCLK, LOW);//SCK = 0;
delayMicroseconds(1);
if (temp & 0x80)
digitalWrite(SDIO, HIGH);//SDOUT = 1;
else
digitalWrite(SDIO, LOW);//SDOUT = 0;
temp = (temp << 1);
digitalWrite(SCLK, HIGH);//SCK = 1;
delayMicroseconds(1); // short clock pulse
}
delayMicroseconds(20);
digitalWrite(NCS, HIGH);//nADNSCS = 1; // de-select the chip
}
byte ADNS_read_old(unsigned char addr) {
byte temp;
int n;
digitalWrite(NCS, LOW);//nADNSCS = 0; // select the chip
temp = addr;
digitalWrite(SCLK, OUTPUT); //SCK = 0; // start clock low
pinMode(SDIO, OUTPUT); //DATA_OUT; // set data line for output
for (n=0; n<8; n++) {
digitalWrite(SCLK, LOW);//SCK = 0;
pinMode(SDIO, OUTPUT); //DATA_OUT;
if (temp & 0x80) {
digitalWrite(SDIO, HIGH);//SDOUT = 1;
}
else {
digitalWrite(SDIO, LOW);//SDOUT = 0;
}
temp = (temp << 1);
delayMicroseconds(1);
digitalWrite(SCLK, HIGH); //SCK = 1;
delayMicroseconds(1); // short clock pulse
}
temp = 0; // This is a read, switch to input
pinMode(SDIO, INPUT); //DATA_IN;
for (n=0; n<8; n++) { // read back the data
digitalWrite(SCLK, LOW);
if(digitalRead(SDIO)) {// got a '1'
temp |= 0x1;
}
if( n != 7) temp = (temp << 1); // shift left
digitalWrite(SCLK, HIGH);
}
digitalWrite(NCS, HIGH);// de-select the chip
return temp;
}
|
欢迎继续阅读楼主其他信息
|