Hello, There,
I am trying to get readings out of my position sensors, however, the spi returns zeros when i dont
specify the pinmode and write HIGH on all of the SPI port pins.
I have seen topics on this particular sensor, I could not use already available codes as most of them I
couldnt understand because they all are related to an arduino mega pilot shield or something.
some wiring diagrams ive seen connect the chip select pin to the power pin for some reason. I ignored it
and followed the original pinout of the SPI library in the Arduino.
and I'm using an Arduino2560 with the adafruit 8 channel bidirectional logic level converter to connect from the sensor to the arduino mega pins.
this related to a university project im doing so any help would be appreciated, ive only been using arudino for a six months now so be gentle please.
#include <SPI.h>
#define Product_ID 0x00
#define Revision_ID 0x01
#define Motion 0x02
#define Delta_X 0x03
#define Delta_Y 0x04
#define SQUAL 0x05
#define Pixel_Sum 0x06
#define Maximum_Pixel 0x07
#define Config_bits 0x0a
#define Extend_Config 0x0b
#define Data_Out_Lower 0x0c
#define Data_Out_Upper 0x0d
#define Shutter_Lower 0x0e
#define Shutter_Upper 0x0f
#define Frame_Period_Lower 0x1a
#define Frame_Period_Upper 0x11
#define Motion_Clear 0x12
#define Frame_Capture 0x13
#define SROM_Enable 0x14
#define Frame_Period_Max_BoundLower 0x19
#define Frame_Period_Max_BoundUpper 0x1a
#define Frame_Period_Min_BoundLower 0x1b
#define Frame_Period_Min_BoundUpper 0x1c
#define Shtter_Max_Bound_Lower 0x1d
#define Shutter_Max_Bound_Upper 0x1e
#define SROM_ID 0x1f
#define Observation 0x3d
#define Inverse_Product_ID 0x3f
#define Pixel_Burst 0x40
#define Motion_Burst 0x50
#define SROM_Load 0x60
#define top 250
#define tpureset 35
#define tinrst 500
#define tpd 2.1
#define tpupd 75
#define tcompute 3.1
#define tpwreset 10
#define tsww 50
#define tswr 50
#define tsrad 50
#define tsradmot 75
#define tload 10
#define tbexit 4
//int SS=53;
int reset=35;
int npd=34;
//int MOSI=51;
//int MISO=50;
//int SCLK=52;
void setup(){
SPI.begin();
SPI.setDataMode(SPI_MODE3);
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV8);//Divides the Arduino's clock frequency to match the sensor's 16/8=2 Mhz
Serial.begin(9600);
pinMode(SS, OUTPUT);
pinMode(reset, OUTPUT);
pinMode(npd, OUTPUT);
pinMode(SCK, OUTPUT);
pinMode(MOSI, OUTPUT);
pinMode(MISO,INPUT);
// digitalWrite(SCK, LOW);
// digitalWrite(MOSI, LOW);
// digitalWrite(MISO, HIGH);
digitalWrite(reset, LOW);
delayMicroseconds(35);
digitalWrite(npd, HIGH);
delayMicroseconds(1);
//attachInterrupt(0,UpdatePointer, Falling);
digitalWrite(reset, HIGH);
digitalWrite(reset, LOW);
delayMicroseconds(tpureset);
configuration();
}
//begin spi communication
void commb(){
digitalWrite(SS, LOW);
}
//end spi communication
void comme(){
digitalWrite(SS, HIGH);
}
//read operation
byte readop(byte address){
commb();//enable spi
//send address w/ MSB0 for read
byte data=SPI.transfer(address & 0x7f);
delayMicroseconds(tsrad);
comme();//close spi
return data;
}
void writeop(byte address, byte data){
commb();
SPI.transfer(address| 0x80);//send the address with MSB as 1 to write
SPI.transfer(data);
delayMicroseconds(1);
comme();
}
void configuration(){
byte id=readop(Revision_ID);
delayMicroseconds(tswr);
//if (readop(Product_ID)==0x17)
// {
Serial.println("product id: ");
Serial.println(id, HEX);//check if the serial port is functional
//}
//else{
// Serial.println("Your inialization is bad and you should feel bad");
//} //configuration bits
// int res=1;//resolution 1600
// int syst=0;//system test: no system test
// int ledmode=1;// shutter mode on
// int rw=1;//read/write, bit 7
byte configbyte= 01010000;
writeop(Config_bits, configbyte);
Serial.println("config reg: ");
Serial.println(readop(Config_bits),HEX);
delayMicroseconds(tsww);
// Serial.println("motion reg: ");
// byte mot=readop(Motion);
// delayMicroseconds(75);
// Serial.println(mot, HEX);
}
void loop(){
// delayMicroseconds(tswr);
// byte mot=readop(Motion);
// delayMicroseconds(75);
// Serial.println(mot,BIN);
// delay(1000);
//// byte dx=readop(Delta_X);
//// delayMicroseconds(tswr);
// Serial.println("delta_x: ");
// Serial.println(dx);
// delay(1000);
//// byte dy=readop(Delta_Y);
//// Serial.println(dy);
//// byte squal=readop(SQUAL);
//// Serial.println(squal, HEX);
}