Expand the DS1820 library

General discussion relating to the library modules supplied with the compiler

Moderators: David Barker, Jerry Messina

Post Reply
CS
Posts: 127
Joined: Thu Nov 02, 2006 9:14 am

Expand the DS1820 library

Post by CS » Mon Dec 04, 2006 7:21 pm

Hi David,

after a long test phase I found out, that it would be nice to have the possibility to get negativ or positiv sign before the temperature.
This information is stored in the DS18S20 Scratchpad Byte 1. If all bits are 0 then we have a positive temperature and when all bits are 1 then we have a negative temperature. So I overloaded the GetTemp function with the following:

Code: Select all


{
****************************************************************************
* Name    : GetTemp (OVERLOAD)                                             *
* Purpose : Read the device temperature  (pSign = 0 means negativ)         *
****************************************************************************
}   
Public Sub GetTemp(ByRef pTemp As ShortInt, ByRef pFraction As Byte,ByRef pSign As Byte)
   OW.WaitForHigh
   ReadSP
   pTemp = FSP(0).AsWord >> 1
   pFraction = (FSP(0) And $01) * 5
   If (FSP(1)) = $FF Then
      psign = 0
   Else
       psign = 1
   End If
   
End Sub

Of course it would be possible to build a function 'GetTempAsStringWithSign' like the following:

Code: Select all


{
*****************************************************************************
* Name    : GetTempAsStrWithSign                                                    *
* Purpose : Read device temperature and return as formatted string with sign*
*****************************************************************************
}  
Public Function GetTempAsStrWithSign() As String
   Dim TempA As ShortInt
   Dim TempB, Sign As Byte
   Dim s As String
   
   GetTemp(TempA, TempB, Sign)
   If sign = 0 Then
      s = "-"
   Else
      s = "+"
   End If
   
   Result =  s + DecToStr(TempA) + "." + DecToStr(TempB)
End Function

I can't test the last function because the testprogramm run into the demo limit.

Maybe you would integrate it in your next update!?

Best wishes

CS

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Mon Dec 04, 2006 9:57 pm

Nice to see you are getting to play will the compiler.

According to the DS18S20 datasheet...

"The temperature data is stored as a 16-bit sign-extended two’s complement number in the temperature register"

So you shouldn't need to do what you are doing as pTemp is already a signed number.

You also shouldn't need an explicit "-" symbol, as the DecToStr() routines will add this for you, if needed. For example...

Code: Select all

include "usart.bas"
include "convert.bas"

dim SignedNum as shortint

SetBaudrate(br19200)
SignedNum = -10
Write(DecToStr(SignedNum), 13, 10)

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Post by David Barker » Tue Dec 05, 2006 1:29 pm

I've posted a short article on the wiki showing how you can use Swordfish to connect to multiple OW devices of the same family...

http://www.sfcompiler.co.uk/wiki/pmwiki ... B20Article

CS
Posts: 127
Joined: Thu Nov 02, 2006 9:14 am

Post by CS » Tue Dec 05, 2006 2:48 pm

Thanks David,

it seems that I have overread the part with the 16-bit sign-extended. :oops:

CS

P.S.: Because of my starting problems with the OW-Devices I think it is a good idea to post an article on the wiki. Great !

Post Reply