The TMP03 is a temperature sensor with an open-collector output. I need to make a free running count when the output is high, continue with a new count when the input is low and then calculate temperature with this function:
Tf = 455.0 - (720.0 * t_hi / t_lo);
I have used many of these with PIC micros and the Arduino and I get very stable temperatures. I am trying to measure the temps in the IMP using the following but I get ± 10 degrees on my reading. t_hi and t_lo are fairly large numbers since they are read using the micros() counter. I do see variations in the t_hi and t_lo variables if I monitor those.
`
function readTemp(ch) {
// read the TMP03 by waiting for high then low then reading high and counting up then read low counting up and calculate F.
local t_hi = 0;
local t_lo = 0;
local t_beg = 0;
local t_beg2 = 0;
local tout = 0;
local tflt = 0.0;
if (ch == 1){
while(hardware.pin2.read()){//in case no sensor is connected
tout++;
//delay_ms(1);
if (tout > 200) {
server.log(tout);
return 1;
}
Since Squirrel is an interpreted language, accessing hardware takes a bit more time than in a compiled language (like c).
Is there a reason you’re polling the pin, rather than the digital_in callback?
Take a look at this snippet of code, and see if it can be of use:
`/***********************************************
Reads a pulse (either HIGH or LOW) on a pin.
If the pulseValue is HIGH, the object will start
timing when the pin goes HIGH, then waits for
the pin to go LOW, and executes the callback
(callback expects a function with one parameter,
the length of the pulse in microseconds).
The accuracy of this function has not been tested,
and will have some variance because of how callbacks
work.
***********************************************/
class Pulse {
pulsePin = null; // Pin to look for pulses on
pulseValue = null; // 1 for HIGH -> LOW, 0 for LOW -> HIGH
timer = null; // Time since start of pulse
callback = null; // callback function when pulse finished
You might be better off using SPI to read this peripheral: at the imp’s minimum SPI data rate of 117.1875KHz, one TMP03 output waveform is about 8,000 bits or 1,000 bytes. Ignore the SPI MOSI and clock lines, read a 1,000-byte blob from the SPI MISO line, and see how long the runs of 1 bits and 0 bits in it are. That will give the T_hi and T_lo timings to the nearest 8.53us, which is a lot closer than you’ll get with a pin callback.
Depending on how much imp memory your application has available, you can increase the SPI frequency, and thus input blob size, and thus input resolution (in powers of 2). If you can spare an 8,000-byte blob, you could run SPI at 937.5KHz and be accurate to the nearest 1.07us.