Reading temperatures in your Windows C# program via a U421 USB interface is very easy if you’re using the DS1822 (or related) temperature sensor (which are digital and 1-Wire based).
The biggest challenge I had was orienting the pins correctly. The pin-out in the datasheet was from the bottom perspective but I was reading it from the top perspective and thus got it wrong. The bad news is that it gets VERY hot that way; the good news is that it doesn’t burn out (at least as long as you catch that issue within a few seconds).
My code (as follows, and poorly formatted, I know), gives the core details in C#. My previous posts on the U421 apply as well, of course. I haven’t confirmed the calibration (-1.0) beyond very limited testing, so be sure to do your own calibration.
// get the ROM details (at least to confirm the sensor is responding)
buffer[0] = 0x06; // whichever port it’s on – see the U421 documentation for details
USBm.USBm_Reset1Wire(Device, buffer); // open it, essentially
present = buffer[1]==0; // 1=not present, 0=present
USBm.USBm_Write1Wire(Device, 0x33); // read ROM
USBm.USBm_Read1Wire(Device, buffer); // the first byte is the sensor type
sensor_type = buffer[1];
for (int i = 1; i < 7; i++) // then the ROM address
{
USBm.USBm_Read1Wire(Device, buffer);
ROM = ROM + buffer[1].ToString(“X2”);
}
USBm.USBm_Read1Wire(Device, buffer); // and the CRC, if you want to confirm everything was sent properly
CRC = buffer[1];
// get the temperature
buffer[0] = 0x06; // the buffer is written to by the reset, so we have to set it back to the right port again, even if we were to use a separate buffer
USBm.USBm_Reset1Wire(Device, buffer);
USBm.USBm_Write1Wire(Device, 0xCC); // i.e. use any 1-wire device (even though we do have the ROM number)
USBm.USBm_Write1Wire(Device, 0x44); // tell it to get the temperature
// read the temperature
buffer[0] = 0x06;
USBm.USBm_Reset1Wire(Device, buffer);
USBm.USBm_Write1Wire(Device, 0xCC);
USBm.USBm_Write1Wire(Device, 0xBE); // read the temperature into the scratchpad – the first two bytes in particular
USBm.USBm_Read1Wire(Device, buffer);
byte1 = buffer[1];
USBm.USBm_Read1Wire(Device, buffer);
byte2 = (byte)((int)buffer[1] & 0x07);
}
celcius = Math.Round( ( (byte1 + (byte2 * 256)) / 16.0 ) – 1.0, 1); // the “16.0” is dependent on the granularity – the 1.0 is based on calibration with physical thermometers (and feeling)
farenheit = Math.Round( (celcius * 1.8) + 32, 1); // the standard conversion algorithm