Hello,
I am trying to interface an Arduino Uno with a TDC GP22 chip (ultrasonic fluid flow time converter device) and I am trying to read from the registers. Here is the function/method that I have created to read from a register (used barometric sensor code as template):
unsigned int readRegister(uint8_t regAd, int numOfBytesToRead){
byte inByte = 0; //incoming byte from the SPI
byte read_opcode = B0; //**B0 is not actual opcode**
unsigned int result = 0; //initialize variable that is to be returned
Serial.print(regAd, BIN); //display register that is to be read in binary
Serial.print("\t"); //creates seperation from register address that was previously printed
digitalWrite(slaveSelectPin, LOW); //enable chip to communicate via SPI
//NOT SURE IF READ OPCODE OR ADDRESS SHOULD BE SENT FIRST
SPI.transfer(read_opcode); //send read opcode to GP22
SPI.transfer(regAd); //send address of register to be read and should return a byte from the register
numOfBytesToRead--; //Decrements the bytes to read
/*if there are still bytes to be read, then bit shift the read value to the left
to make room for the rest of the bytes that need to be read*/
if (numOfBytesToRead > 0 ){
result = result << 8;
inByte = SPI.transfer(0x00); //read value from the register
//combine the byte(s)
result = result | inByte;
numOfBytesToRead--; //decrement bytes to read
}
digitalWrite(slaveSelectPin, HIGH); //deselect the GP22
return (result); //return the value that was stored in the register
}
I am not quite sure if my logic is correct. I have two questions:
-
When specifying what register address to read from, do I first transfer the read opcode and then transfer the register address? (Like in the code above) or do I transfer in the reverse order?
-
Also, how do I actually acquire the value that is read? What function or method do I use? How is the value returned after I transfer the read opcode and register address?
Thank you,
Anthony