DS18s20 temp sensor
uosamor007
Posts: 21
i am trying to get a temperature read out from this temperature sensor to the PST. i am new to spin programming and not sure how to proceed with this. any suggestions would be helpful
thank you.
sample code
CON
_clkmode = xtal1 + pll16x ' Change to xtal1 + pll8x for Propeller Backpack.
_xinfreq = 5_000_000 ' Change to 10_000_000 for Propeller Backpack.
OW_DATA = 0 ' 1-wire data pin
SKIP_ROM = $CC ' 1-wire commands
MATCH_ROM = $55
READ_SCRATCHPAD = $BE
CONVERT_T = $44
MAX_DEVICES = 10 ' maximum number of 1-wire devices
DEG = 176 ' degree symbol
CR = 13
VAR
byte myTemp[9] ' 9 Byte array to hold the DS1820 registers
long addressList[MAX_DEVICES*2] ' 64-bit address buffer
OBJ
pst : "parallax serial terminal"
ow : "OneWire" ' Define Dallas 1-Wire routines
PUB Start
ow.start(OW_DATA) ' start 1-wire object on pin define as OW_DATA
DS1wire
getTemperature
tempDS1820
PUB getTemperature : temp | idx2
'term.Str(String("getTemperature", CR))
ow.reset ' reset 1-Wire buss
ow.writeByte(MATCH_ROM) ' send match ROM command
ow.writeAddress(@addressList) ' Works now|***** can't get this to work
ow.writeByte(CONVERT_T) ' send command to initiate a single temperature conversion
repeat ' wait for conversion
waitcnt(cnt + clkfreq / 1000 * 25)
if ow.readBits(1)
quit
ow.reset ' reset 1-Wire buss
ow.writeByte(MATCH_ROM) ' send match ROM command
ow.writeAddress(@addressList) ' Works now|***** can't get this to work
'sendSerialNumber ' send serial number of the DS1820
ow.writeByte(READ_SCRATCHPAD) ' send command to read the contents of the scratchpad
idx2 := 0 ' zero loop counter variable
repeat 9 ' read 9 bytes from 1-Wire buss
mytemp[idx2++] := ow.readByte ' store byte in array
PUB tempDS1820 |tempC , fract
tempC := getTemperature ' get temperature in celsius
repeat
pst.str(alt.formatn(tempC, alt#Centigrades | alt#CECR, 8))
if (pst.rxcount) Respond to any key by clearing screen.
pst.rxflush
pst.char(pst#CS
to be honest i'm not even sure if any of this is right but am i in the right direction or not?
thank you
thank you.
sample code
CON
_clkmode = xtal1 + pll16x ' Change to xtal1 + pll8x for Propeller Backpack.
_xinfreq = 5_000_000 ' Change to 10_000_000 for Propeller Backpack.
OW_DATA = 0 ' 1-wire data pin
SKIP_ROM = $CC ' 1-wire commands
MATCH_ROM = $55
READ_SCRATCHPAD = $BE
CONVERT_T = $44
MAX_DEVICES = 10 ' maximum number of 1-wire devices
DEG = 176 ' degree symbol
CR = 13
VAR
byte myTemp[9] ' 9 Byte array to hold the DS1820 registers
long addressList[MAX_DEVICES*2] ' 64-bit address buffer
OBJ
pst : "parallax serial terminal"
ow : "OneWire" ' Define Dallas 1-Wire routines
PUB Start
ow.start(OW_DATA) ' start 1-wire object on pin define as OW_DATA
DS1wire
getTemperature
tempDS1820
PUB getTemperature : temp | idx2
'term.Str(String("getTemperature", CR))
ow.reset ' reset 1-Wire buss
ow.writeByte(MATCH_ROM) ' send match ROM command
ow.writeAddress(@addressList) ' Works now|***** can't get this to work
ow.writeByte(CONVERT_T) ' send command to initiate a single temperature conversion
repeat ' wait for conversion
waitcnt(cnt + clkfreq / 1000 * 25)
if ow.readBits(1)
quit
ow.reset ' reset 1-Wire buss
ow.writeByte(MATCH_ROM) ' send match ROM command
ow.writeAddress(@addressList) ' Works now|***** can't get this to work
'sendSerialNumber ' send serial number of the DS1820
ow.writeByte(READ_SCRATCHPAD) ' send command to read the contents of the scratchpad
idx2 := 0 ' zero loop counter variable
repeat 9 ' read 9 bytes from 1-Wire buss
mytemp[idx2++] := ow.readByte ' store byte in array
PUB tempDS1820 |tempC , fract
tempC := getTemperature ' get temperature in celsius
repeat
pst.str(alt.formatn(tempC, alt#Centigrades | alt#CECR, 8))
if (pst.rxcount) Respond to any key by clearing screen.
pst.rxflush
pst.char(pst#CS
to be honest i'm not even sure if any of this is right but am i in the right direction or not?
thank you
Comments
thank you
Please check other threads on the forums as there have been problems when using this object with multiple sensors at high temperatures.
Hope this helps....
Larry
thanks again for the help
tempF = Fahrenheit Temperature
tempC := getTemperature
in the main pub i get a output...what could be the issue???
thank you once again for your help
Yes, you might want to have a look at the following:
http://forums.parallax.com/showthread.php?136782-How-to-use-the-One-Wire-(1-Wire)-object-in-the-Obex&
And when you do post the code, use the code tags as described below:
That will help keep the alignment in your code.
and i am using the same code that he posted
that should be enough to have my sensor working correct?
work correctly on a DS18S20 or a DS18B20. I am probably wrong, hopefully someone else can comment on that aspect.
The only other problem may be a missing pullup resistor (usually 4.7K), have you included the resistor
In your circuit?
what could i change if anything to get a more accurate reading. i know the conversion factors are different but i am not sure what to change and i am scared to mess up the code
To read multiple sensors, you will need to address each sensor by its ROM code, which is unique for each sensor chip. You might want to look at JonnyMac's code posted in the following link:
http://forums.parallax.com/showthread.php?136782-How-to-use-the-One-Wire-(1-Wire)-object-in-the-Obex&
From that thread:
"...To get started, you will want to wire up only one DS18B20 at a time and run DS18B20jmReadROM1a.spin to get each serial number, then you will have to manually type the serial numbers into the software that runs more than one of the sensors at the same time, using DS18B20jm1d.spin...."
JonnyMac's code is different and, in my opinion, much more stable than Micah's code. I think there are methods in JonnyMac's code that allow you to read the ROM off an individual chip placed on the 1-wire line. After you have read the ROM code off of each sensor chip by placing each sensor individually on the 1-wire line one by one, you can then manually write all of those ROM addresses into the DAT section of your code. (You place them into the DAT section because they are too big to fit into a LONG, etc.)
Later, you can then make reference to each sensor when you want to ask that particular sensor to read a temperature even when multiple sensors are on the 1-wire line. So you have multiple sections of code that call up the individual ROM addresses stored in the DAT section, and each of these gives a separate reading.
I hope that makes some sense. I have an example of how this works on my other computer, but probably can't get to it until late Sunday.
SKIP_ROM = $CC ' 1-wire commands
MATCH_ROM = $55
READ_SCRATCHPAD = $BE
CONVERT_T = $44
In the data sheet it says theses commands are CCh, 55h, BEh,44h respectively. So if there is an h at the end to you just put a $ at the front? Where did that come from. I tried to look in the propeller manual for the $ but didn't see it.
Look in the Prop manual v1.2 on page 207. It mentions the $ sign as a hexadecimal indicator, which is what the h indicates in some documents you find.
if tempf < 0.0
The < operator (and its friends >, <>, etc.) can only work with integers.