|
I am running into something I don't understand. I have written this code to read the DS1820 thermometer and it works just fine like this. I am planning on using the temperature for PID control loop temperature control and the temperature is 100 times the celsius reading for an accuracy of .01C. The problem is that the subroutine works just fine like this
temp var long temp1 var long temp2 var long temp3 var long temp4 var long T_C var long T_val var long CPR var word cpc var word fract var long
main debug["temperature is: ",real T_C," C",13] debug["temp is: ",dec temp," cpr is: ",dec cpr,13] debug["temp4 = ",dec T_val,13,13] gosub read_1820 goto main '*********************************subroutine to read DS1820 temperature sensor read_1820 owout p5,1,main,[$cc,$44] 'Send the command to read the temperature wait
OWIN p5,0,[temp] 'Check the line till data is available if temp=0 then wait owout p5,1,main,[$cc,$be] 'send the command to read the data owin p5,0,[temp.byte0,temp.byte1,temp1,temp1,temp1,temp1,cpr,cpc] 'read the data temp1= temp/2 'divide by 2 and truncate the half degree fract= float cpr fdiv float cpc 'convert the extra counts into 16ths of a degree T_C = float temp1 fsub 0.25 fadd fract 'add to the whole number per the formula T_val = int (T_C fmul 100.0) 'convert to an integer temperature times 100 return
But when I copy this exact subroutine into my code and run it again, temp does not correctly detect the data ready and the temperature received is junk.
Why would this code work as written here as a subroutine but then when pasted into a larger program not function properly?
David Voit
|