This forum has been a great help to me on a previous project
http://www.picaxeforum.co.uk/showthread.php?24819-Pinewood-Derby-Stopwatch-Timer&highlight=pinewood+derby
and hopefully you guys can help me sort this one out.
My Grandson has an idea for a science fair project (heat of fusion of water) and he will need a precision thermometer that reads in degrees Celsius. I have built a few PICAXE projects before and thought that this would be an interesting project to tackle. After researching the web I decided on an HD44780 display driven by an AXE132 serial interface. The temperature sensor I chose was the DS18B20 because there seemed to be a lot of information available and the degree of precision achievable. I decided to use an 18M2 controller because that is what I had on hand.
After more scrounging around the internet, I hacked together the attached code. Now we get to my problem – it doesn’t work. When I run it through the simulator it returns the following result:
Input(Word) Output
0 0.00oC
16000 -24.00oC
32000 -48.00oC
48000 -72.00oC
64000 -96.00oC
65536 -0.06oC
It should be returning an output of -55oC to 125oC.
Aside from the fact that this code doesn’t work, I want to be able to understand it rather than blindly copy someone else’s work. So here is the first question:
On Line 45 there is a test for positive or negative . If I read the code correctly this should return a number between 0.00 and 1.99. Somehow it is supposed to interpret the integer as a zero or one and make a determination if the number is negative. It seems to me that there is a line of code missing. Am I interpreting this correctly? Is there a better way to isolate the sign bit?
Going to Line 47 generates another question. What is the purpose of generating a two’s complement? At first I was puzzled how the code generated a two’s complement until I realized that the symbol was for a bitwise XOR function. The binary value of $FFFF is all ones. By XORing the Temperature12 value with $FFFF and adding 1 we get the two’s complement. But why go through all that?
I would appreciate any help in understanding this code and getting it to do what it is supposed to do.
http://www.picaxeforum.co.uk/showthread.php?24819-Pinewood-Derby-Stopwatch-Timer&highlight=pinewood+derby
and hopefully you guys can help me sort this one out.
My Grandson has an idea for a science fair project (heat of fusion of water) and he will need a precision thermometer that reads in degrees Celsius. I have built a few PICAXE projects before and thought that this would be an interesting project to tackle. After researching the web I decided on an HD44780 display driven by an AXE132 serial interface. The temperature sensor I chose was the DS18B20 because there seemed to be a lot of information available and the degree of precision achievable. I decided to use an 18M2 controller because that is what I had on hand.
After more scrounging around the internet, I hacked together the attached code. Now we get to my problem – it doesn’t work. When I run it through the simulator it returns the following result:
Input(Word) Output
0 0.00oC
16000 -24.00oC
32000 -48.00oC
48000 -72.00oC
64000 -96.00oC
65536 -0.06oC
It should be returning an output of -55oC to 125oC.
Aside from the fact that this code doesn’t work, I want to be able to understand it rather than blindly copy someone else’s work. So here is the first question:
On Line 45 there is a test for positive or negative . If I read the code correctly this should return a number between 0.00 and 1.99. Somehow it is supposed to interpret the integer as a zero or one and make a determination if the number is negative. It seems to me that there is a line of code missing. Am I interpreting this correctly? Is there a better way to isolate the sign bit?
Going to Line 47 generates another question. What is the purpose of generating a two’s complement? At first I was puzzled how the code generated a two’s complement until I realized that the symbol was for a bitwise XOR function. The binary value of $FFFF is all ones. By XORing the Temperature12 value with $FFFF and adding 1 we get the two’s complement. But why go through all that?
I would appreciate any help in understanding this code and getting it to do what it is supposed to do.
Code:
' ************** TemperatureSensorSerial.bas **************
'This program runs on a PICAXE 18-M2 at 4MHz which reads the temperature from a
' DS18B20 temperature sensor & sends it to an HD44780 compatible 16x2 serial LCD display.
'
' Developed by Albert Zalner February 6, 2015.
' *** Constants ***
symbol LCD = C.6 ' Assign LCD to port C.6
' *** Variables ***
symbol Temp = b20 ' calculation variable
symbol Temperature12 = w11 ' Temperature word using b22 & b23
symbol TempC_100 = w12 ' Temperature value in C * 100 (uses b24 & b25)
symbol Tempsign = bit0 ' The sign of the temperature reading
symbol Whole = b2 'The whole temperature reading
symbol Fract = b3 'The fraction temperature reading
' *** Directives ***
#com 3 ' specify download port
#picaxe 18M2 ' specify processor
#no_data ' save time downloading
#terminal off ' disable terminal window
' *** Initialize the LCD ***
' *** Control commands are all prefixed by the number 254 ***
iniLCD:
serout LCD,N2400, (254,1) 'Clear Display
pause 800 ' pause 800 mS for LCD initialization
serout LCD,N2400, (254,128) 'Move to Line 1, Position 1
serout LCD,N2400, ("The Temperature") 'Output Text
main:
serout LCD,N2400, (254,196) 'Move to line 2, position 4
call ReadTemp12Sensor 'Read the temperature
goto main 'loop to beginning
ReadTemp12Sensor:
'**************************************************************
'** Read DS18B20 & display temperature in 12 bit resolution **
'**************************************************************
ReadTemp12 B.3, Temperature12 'Read the DS18B20 temp sensor
Pause 800 'pause 800 ms per DS18B20 spec
TempSign = Temperature12 / 256 /128 'Isolate the MSB bit
If TempSign = 0 then Positive 'Test for negative
Temperature12 = Temperature12 ^ $ffff + 1 'Take 2's complement
Positive:
TempC_100 = Temperature12 * 6 'TC = value = 0.0625, TC*100 = 6.25
Temperature12 = Temperature12 * 25 / 100 'the rest of the value
TempC_100 = TempC_100 + Temperature12 'the temperature value * 100 in C
Whole = TempC_100 / 100 'separate the whole degrees
Fract = TempC_100 % 100 'separate the fractional degrees
If TempSign > 0 then 'temp is negative
Serout LCD,N2400,("-") 'send negative symbol
end if
Serout LCD,N2400,(#Whole) 'Send temp to LCD as a whole number
Serout LCD,N2400,(".") 'send decimal point
'*** To ensure the fraction is double digits ***
Temp = Fract / 10 'Calc the first digit of fract
Serout LCD,N2400,(#Temp) 'send first decimal digit to LCD
Temp = Fract % 10 'Calc the second digit of fract
Serout LCD,N2400,(#Temp) 'send second decimal digit to LCD
Serout LCD,N2400,(%11010010) 'send degree character
Serout LCD,N2400,("C") 'send letter “C”
Return