1

I have a project which uses a DS18B20 temperature sensor and an LCD with an I2C interface on it. I have connected the sensor to my micro-controller (the DS18B20 has a 12-bit digital output) to the ATmega16 PD17, but as I try to print my values on the terminal the sensor output a really high value in decimal. Here is part of the code (and yes everything is working fine, this is a home-made Arduino):

DDRD &= 0x00;
PORTD |=  0x00;    
ds18b20_init(&PORTD, 20, 30, DS18B20_12BIT_RES); 
int temperature_sensor_output =  (float)ds18b20_temperature(&PORTD);
printf("Temperature: %d  ", temperature_sensor_output);
delay_ms(500);

ds18b20_init is the Dallas function for initializing the module. ds18b20_temperature returns a float value of the temperature

From this code it just prints:

Temperature: 51603

If I change %d to %f it prints:

Temperature: without any value

I do not understand where I may have missed some things.

dda
  • 1,595
  • 1
  • 12
  • 17
Diana Lc
  • 125
  • 1
  • 2
  • 6

2 Answers2

1

Without more detail about the system, I won't understand what printf() does; in non-embedded systems, printf() writes to stdout by default, but Arduino systems typically don't use such a device.

That aside, %d is the proper format code for printing an integer value, and %f is proper for a float or double. If you attempt to print an integer value like temperature_sensor_output under a %f format code, either the compiler will complain or the integer pattern of bits (plus perhaps two additional random bytes) will be interpreted as if it is a float or double pattern of bits. No type cast occurs within printf(). If you want to use a %f format code, pair it up with a float-type variable.

If, as you say, ds18b20_temperature() returns a float, then the type cast (float)ds18b20_temperature(&PORTD) will have no effect. Generally, casts convert data from one form to another, or in some cases declare that data has a given form. Going from float to float doesn't change anything.

In a C statement like a=b, if b has a type that's different from that of a, the b value will be cast to the type of a. Thus, the float value on the right hand side of

int temperature_sensor_output =  (float)ds18b20_temperature(&PORTD);

will be converted to an integer value and stored into the integer variable temperature_sensor_output.

James Waldby - jwpat7
  • 8,920
  • 3
  • 21
  • 33
0

Have you looked at the raw return value from ds18b20_temperature(), without casting it to float and then to int, perhaps in hex? It may not be your printing code that's causing trouble but the value returned from the function. If the function is returning something equivalent to 51603, it is already bogus. DS18b20s temperature values are in 16-ths of a degree (Celsius, assuming the routine didn't convert it). 51603/16 is still an unreasonable value, not to mention that viewed as as a 16-bit int, the sign bit is set. Is it truly negative? Did something overlow?

I think you need to look at what that function is doing.

JRobert
  • 15,407
  • 3
  • 24
  • 51