BasicMicro - Forums

www.basicmicro.com
It is currently Sun May 20, 2012 11:33 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Reading the DS1820 thermometer with a subroutine
PostPosted: Sat Feb 03, 2007 11:00 pm 
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


Top
  
 
 Post subject: Re: Reading the DS1820 thermometer with a subroutine
PostPosted: Mon Feb 05, 2007 11:00 pm 
Well, Nathan told me I had run into a bug with the old version of basic and the problem was that the routine was on a page that could not be accessed. I moved to 5.3.

David Voit


Top
  
 
 Post subject: Re: Reading the DS1820 thermometer with a subroutine
PostPosted: Sat Feb 10, 2007 11:00 pm 
There is a mistake in the temperature calculation I did not pick up at first. The calculation for the cpr and cpc numbers is wrong. Cpc is hard wired to 16 and can be ignored but the mistake is the fraction is the difference between 16 and the count divided by 16. The code I was testing did not subtract from 16 and gave an error. If you use the code, fix that mistake.


David Voit


Top
  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO