I'm using Atmega8 with CVAVR 2.05.0 and trying to slightly modify the way ds18b20 library works. I want to store temperatures from sensors in global array and start conversion for all sensors at one time – so i can avoid useless delays and do something usefull.
For now in "Project Configure – C Compiler -Code Generation – (s)printf Features" are set to "float,width,precision".
I found a problem (tested it in Proteus 7.7 SP2 and on real atmega8) – if i call tempature reading in function inside printf like this:
printf("t%i read=%.4f \n\r", i, ds18b20_read_temperature(&rom_code_ds18b20[i][0]));
it works fine, except it can't read global variable inside function. But if before that print i put any assigment to global float array like this:
temperatures[i] = i + 0.275;
printf("t%i read=%.4f \n\r", i, ds18b20_read_temperature(&rom_code_ds18b20[i][0]));
i start getting errors (-9999) for every read for 2nd and 3rd sensor, no matter what is in asigment – if it is assigment to global varible. But it starts reading from global array right values.
Firs I tried to read global float array in main – it works fine. Then i tried to do same temperature reading in main – again, inside printf it works fine, but if i try assign result of reading to local variable t1 and then printf those t1 – i get zero.
Here is example for "good" output (no assign before prints, but no global variable read in function):
Here is example for "bad" output (put assign before prints, global variable starts reading normally, but 2&3 sensors are lost (see those -9999?) ):
Where is the problem?
Or there is something i should know about defining or using arrays and/or in functions?
About global variable i found only about problems inside interrupts, there was recomended to use volatile. Tried volatile – no effect. Also tried pointers (i'm not familiar with them, but with help of friend…) – the same thing – no reading global in function and "loosing sensor readings".
Also after compiling project i get warnings:
Warning: C:…\my_reading_ds18b20\my_reading_ds18b20.c(261): array index is out of range
Warning: C:…\my_reading_ds18b20\my_reading_ds18b20.c(263): array index is out of range
Warning: C:…\my_reading_ds18b20\my_reading_ds18b20.c(265): array index is out of range
I checked that variable number_of_sensors=3 (with printf), so i don't understand why i get this warning. Maybe because this varible is set with preprocessor?
Here is my .c file: watch for lines 80, 251, 252
https://app.box.com/s/8e3no26kfetoutiaqt6vn1rvsxq7wo2n
(with comments and "debug" printf. if anything isn't clear – ask me)
And here (if needed) .prj file:
https://app.box.com/s/52oo40bvo0fx02dshmjaw0nsv1mlywco
I draw your attention that it is NOT about reading dsb18b20 – those function is from library and works well, but about problems got from global float variable. I really need to store those values for later use!
UPDATE1: Seems the problem in everything except t1..t4 was in setting variable number of sensors, because if change initializng array float temperatures[number_of_sensors] to float temperatures[4] all previous problems are gone. So it seems i can't get number of sensors defined by way i used. Is there any other way except automatically change number of sensors depending on used options?
Best Answer
From those warning messages, it appears there is a problem with the first index of the rom_code_ds18b20[i][0] array - looks like "number_of_sensors" is not getting set correctly.