I'm working on a project of Ultrasonic Flow Meter with TDC-GP22 using Arduino UNO. I used the library of leokoppel
In my code,the configurations of the registers are copied from the Application Note of GP22, from page 22 to page 25. I didn't use the temperature sensor so I jumped those steps related to that. No calibration neither. I just want to get some results of time of flight to be able to calculate the distance between two sensors for now.
My code for test:
#include "GP22.h"
#include <SPI.h>
#include <avr/io.h>
/*
*
* Test setup (Arduino Uno, through 5->3.3V level shift)
* 10 -> SS
* 11 -> MOSI
* 12 -> MISO
* 13 -> SCK
* 2 -> INT
* 5V -> VCC
* GND -> GND
* 4 -> DS18B20 temperature sensor (optional)
*/
const bool DEBUG = 1;
const int FAST_MODE = 1;
const int PIN_INT = 2;
GP22 tdc(PIN_INT, DEBUG);
float cycleFactor_ns = 0;
void setup() {
Serial.begin(115200);
Serial.println("Go !");
tdc.init();
tdc.writeRegister(0, 0x430BE800);
tdc.writeRegister(1, 0x21444000);
tdc.writeRegister(2, 0xA0138800);
tdc.writeRegister(3, 0xD0A24800);
tdc.writeRegister(4, 0x10004000);
tdc.writeRegister(5, 0x40000000);
tdc.writeRegister(6, 0xC0C06100);
tdc.printConfigRegisters();
byte a = tdc.readRegister(READ_REG_1);
Serial.println(a,HEX);
Serial.println("Test communication: ");
Serial.print(tdc.testCommunication());
}
void loop() {
tdc.sendOpcode(OPCODE_INIT);
tdc.sendOpcode(OPCODE_START_TOF_RESTART);
tdc.waitForInterrupt(10000000);
delay(20);
Serial.println(tdc.readRegister(READ_RES_3),HEX);
}
From oscilloscope I got the right number of fire pulses(20) with right frequency(1MHz): Signal (the blue one is the signal of transmitter before amplification, green one is after amplification, yellow one is the signal of receiver)
But what I got from the code:
Go !
attached pinChangeISR_debug
Register 0: 0x430be800
Register 1: 0x21444000
Register 2: 0xa0138800
Register 3: 0xd0a24800
Register 4: 0x10004000
Register 5: 0x40000000
Register 6: 0xc0c06100
21
Test communication:
\/Test took 180 us
1
69978000
69978000
69978000
69978000
69978000
69978000
69978000
...
21 is the first byte of register 1, and 1 is the result of testcommunication(). These two means the test of communication is successful. But the results of reading RES_3 are always the same, and the waitForInterrupt() doesn't work(maybe the code shouldn't be written like this) -- Timeout all the time.
What I expect, of course, is the result can change each time it measures.(impossible to get exactly the same measurement of ToF all the time)
Any help appreciated!