10 years, 12 months ago.

Using float value from DS1820 sensor to PID controller and serial?

Hello all,

I'm coming from the arguably lax type checking Arduino environment, and I was wondering how I would use the example found at http://mbed.org/users/feabhas/code/DS18B20/ to pass a float value to both a PID controller for further processing, as well as to the USB UART line for monitoring?

I have some code, which isn't doing anything constructive at the moment:

// temperature is store as 7.4 fixed point format (assuming 12 bit conversion)
void displayTemperature(float s) {
    DoConversion();
    uint32_t temp = GetTemperature();
    float f = (temp & 0x0F) * 0.0625;    // calculate .4 part
    f += (temp >> 4);    // add 7.0 part to it
    s.printf("%f", f);    // display in 2.1 format
}

That in DS18B20.cpp.

while (1) {
    if(pc.readable())
      {
      displayTemperature(pv);
     
      pc.printf("Temperature: %f", pv, "\n\r");
      pc.printf("Enter a laser power (in floating point format):");
      pc.scanf("%f", &laser);
      displayTemperature(pv);
      pc.printf("\n\r Temperature: %f", pv, "\n\r");
      }

That in main.cpp.

=====================================================

DS18B20 Configuration

Family code: 0x28

Serial Number: 00:00:04:A8:1B:78
CRC: 0x0

Running temperature conversion...


Temperature: 0.000000Enter a laser power (in floating point format):100.00

 Temperature: 0.000000Temperature: 0.000000Enter a laser power (in floating point format):

That is my programs output over the USB console. Some newlines would be good too.

Many thanks in advance for your help!

1 Answer

10 years, 12 months ago.

For the printfs, you first have to type the entire string you want to use, and next the data in it, so:

pc.printf("Temperature: %f\n\r", pv);

If you have want to print two variables you can use for example:

pc.printf("Temperature: %f, Laser power: %f\n\r", pv, laser);

Regarding the temperature sensor, that library isn't really handy. I would change displaytemperature to:

float displayTemperature( void ) {
    DoConversion();
    uint32_t temp = GetTemperature();
    float f = (temp & 0x0F) * 0.0625;    // calculate .4 part
    f += (temp >> 4);    // add 7.0 part to it
    return f;
}

And in the corresponding .h file, change it to:

float displayTemperature(void) ;

If the code then works you should be able to do in your main.cpp:

float temperature = displayTemperature();

And use that in your printfs.

The problem now is that there are two DS1820 libraries, one is this, which isn't really handy to use. The other one is: http://mbed.org/users/Michael_/code/DS1820/, who accidently in his latest commit deleted all code. You can still get it back by going to an older revision, but it seems at first glance also to be a fairly complicated library.

Hello Erik,

Many thanks for your answer. It does indeed compile (half the battle), but now I'm getting nothing on the serial console and the red error LED flashes continuously. Reflashing the original program works, but unlike I had suspected, by plugging in the probe backwards, that I had broken it.

Not sure about this one... a curly one indeed...!

Thanks again!

posted by Shane Morris 07 Jun 2013