DS1620

Started by bryanwasherein04 January 15, 2006
I am having problems trying to interface a temp sensor. Can someone
help me out?I know it something in the shiftin/shiftout but I can get
a clear understanding of the functions and the only code I have found
doesnt help with the understanding aspect of it. Public Const High As Byte = 1
Public Const Low As Byte = 0

Public Const PIN_DS1620_DQ As Byte = 7
Public Const PIN_DS1620_Clock As Byte = 8
Public Const PIN_DS1620_Reset As Byte = 9

Public Const DS1620_Read_Temp As Byte = &HAA
Public Const DS1620_Write_TH As Byte = &H1
Public Const DS1620_Write_TL As Byte = &H2
Public Const DS1620_Read_TH As Byte = &HA1
Public Const DS1620_Read_TL As Byte = &HA2
Public Const DS1620_Read_Counter As Byte = &HA0
Public Const DS1620_Read_Slope As Byte = &HA9
Public Const DS1620_Start_Convert_T As Byte = &HEE
Public Const DS1620_Stop_Convert_T As Byte = &H22
Public Const DS1620_Write_Config As Byte = &HC
Public Const DS1620_Read_Config As Byte = &HAC

Sub Main()
Dim byte_NumberOfBits As Byte
byte_NumberOfBits = &H30
' Initialize
Call PutPin(PIN_DS1620_Reset, High)
Call ShiftOut(PIN_DS1620_Reset, PIN_DS1620_Clock,
byte_NumberOfBits, DS1620_Write_Config)
Call PutPin(PIN_DS1620_Reset, Low)

Call Sleep(2)

Call PutPin(PIN_DS1620_Reset, High)
Call ShiftOut(PIN_DS1620_Reset, PIN_DS1620_Clock,
byte_NumberOfBits, DS1620_Start_Convert_T)
Call PutPin(PIN_DS1620_Reset, Low)

' Read Temp
Dim byte_TempIn As Byte
Call PutPin(PIN_DS1620_Reset, High)
Call ShiftOut(PIN_DS1620_Reset, PIN_DS1620_Clock,
byte_NumberOfBits, DS1620_Read_Temp)
byte_TempIn = ShiftIn(PIN_DS1620_Reset, PIN_DS1620_Clock,
byte_NumberOfBits)
'byte_TempIn = FlipBits(byte_TempIn)
Call PutPin(PIN_DS1620_Reset, Low)
Debug.Print CStr(byte_TempIn)
End Sub



--- In basicx@basi..., "bryanwasherein04"
<registration@m...> wrote:
> I am having problems trying to interface a temp sensor.

A quick look at your code reveals several problems. Firstly, the
first parameter to both the ShiftOut and the ShiftIn calls is
incorrect. You should be using PIN_DS1620_DQ instead of
PIN_DS1620_Reset.

Secondly, the command codes either need to be run through FlipBits()
or just define them in the reverse bit order. The latter is the
more efficient solution although it might be more confusing.

Thirdly, the ShiftIn and ShiftOut routines can shift, at most, 8
bits at a time. You have defined byte_NumberOfBits to be 48
(decimal). I don't know, offhand, what the BX does when the shift
count is invalid. It might do nothing (return immediately) or it
might just limit the shift count to 8.

Don


--- In basicx@basi..., "bryanwasherein04" <registration@m...>
wrote:
> I am having problems trying to interface a temp sensor.

One other thing, you probably need to set the clock pin high
initially. The data pin need not be configured because ShiftOut and
ShiftIn configure it automatically.

Call PutPin(PIN_DS1620_Clock, High)

The timing diagram in the data sheet shows the required waveforms.


Thanks for the reply. I have corrected the shiftin/shiftout part (i
overlooked the pin being called).

When you say the command code needs to be run through flipbits. Are
you refereing to the shiftout/in such as:

Call ShiftOut(PIN_DS1620_DQ, PIN_DS1620_Clock, byte_NumberOfBits,
FlipBits(DS1620_Write_Config))

I changed the byte_NumberOfBits to &H8 but I still get invalid responses.

I will continue to try to work through this and post back when/if I
get it figured out.

Thanks again

[]


--- In basicx@basi..., "bryanwasherein04" <registration@m...>
wrote:
> When you say the command code needs to be run through flipbits. Are
> you referring to the shiftout/in such as:
>
> Call ShiftOut(PIN_DS1620_DQ, PIN_DS1620_Clock, byte_NumberOfBits,
> FlipBits(DS1620_Write_Config))

Exactly. However, as I pointed out it would be more efficient to
simply define DS1620_Write_config so that FlipBits() is not required.
The BasicX compiler isn't "smart" enough to make the obvious
substitution for you.



Ok I think I got it or got close anyway. Sadly after this I was
attempting to turn a fan on when the temp got up using the 1620 and
somehow (maybe by putting 12v to the 5v rail being fed by the BX)
fried the BX. This turned out to be a costly mistake. Guess its not
idiot proof afterall :) Anywho, below you will find the code I did
which was able to get some form of temp reading back which I think
closely matched what I was looking for.

I don't know if their is anyone who repairs BXes but I now have 2 non
workers :( I described how one met its demise above. The other was
actually working fine until it was placed in storage. When I got it
back out for tinkering I could not get it to respond. The BX only
shows a solid red light but will not respond to stop requests nor
reprogramming. I think at this point I will atempt to move off the
basicx/stamp environment and move to a cheaper atmel or pic so I can
cheaply mess up. Down side is the cost and learning curve will stink.
I am not looking forward to it but the good side is I already have a
epic.

I have a NIB BX24 dev board I will sell or trade if anyone is
interested. This was never used as I just decided to use parallax's
NX-1000 instead. It is still in the white BX box bubble wrapped and
will include everthing minus the bx24 chip, wall wart and serial
cable. Software, board, manual and jumper wires are included. <!-- BEGIN CODE --!>

Option Explicit

Public Const High As Byte = 1
Public Const Low As Byte = 0

Public Const PIN_Photo As Byte = 5
Public Const PIN_LED As Byte = 6
Public Const PIN_DS1620_DQ As Byte = 7
Public Const PIN_DS1620_Clock As Byte = 8
Public Const PIN_DS1620_Reset As Byte = 9
Public Const PIN_FAN As Byte = 10

Public Const DS1620_Read_Temp As Byte = &HAA
Public Const DS1620_Write_TH As Byte = &H1
Public Const DS1620_Write_TL As Byte = &H2
Public Const DS1620_Read_TH As Byte = &HA1
Public Const DS1620_Read_TL As Byte = &HA2
Public Const DS1620_Read_Counter As Byte = &HA0
Public Const DS1620_Read_Slope As Byte = &HA9
Public Const DS1620_Start_Convert_T As Byte = &HEE
Public Const DS1620_Stop_Convert_T As Byte = &H22
Public Const DS1620_Write_Config As Byte = &HC
Public Const DS1620_Read_Config As Byte = &HAC

Public Sub Main()
' call PutPin(PIN_FAN, High)
'exit sub

Dim byte_fanState As Byte
Do
Dim sng_Temp As Single
sng_Temp = GetTemp
If sng_Temp > 76# Then
If PinState(PIN_FAN) = 0 Then ' if fan not already running
Call PutPin(PIN_FAN, 1)
Debug.Print "FAN ON @ " & CStr(sng_Temp)
End If
Else
If PinState(PIN_FAN) = 1 Then ' only do if fan is running
Call PutPin(PIN_FAN, 0)
Debug.Print "FAN OFF @ " & CStr(sng_Temp)
End If
End If
Call Delay(3#)
Loop
End Sub Public Function GetTemp() As Single
Dim byte_NumberOfBits As Byte
byte_NumberOfBits = &H8
' Initialize
Call PutPin(PIN_DS1620_Reset, Low)
Call PutPin(PIN_DS1620_Clock, High)
Call PutPin(PIN_DS1620_Reset, High)
Call ShiftOut(PIN_DS1620_DQ, PIN_DS1620_Clock, byte_NumberOfBits,
flipbits(DS1620_Write_Config))
Call PutPin(PIN_DS1620_Reset, Low)

Call Sleep(2)

Call PutPin(PIN_DS1620_Reset, High)
Call ShiftOut(PIN_DS1620_DQ, PIN_DS1620_Clock, byte_NumberOfBits,
flipbits(DS1620_Start_Convert_T))
Call PutPin(PIN_DS1620_Reset, Low)

Call Sleep(2)
' Read Temp
Dim byte_TempIn As Byte
Call PutPin(PIN_DS1620_Reset, High)
Call ShiftOut(PIN_DS1620_DQ, PIN_DS1620_Clock, byte_NumberOfBits,
flipbits(DS1620_Read_Temp))
byte_TempIn = ShiftIn(PIN_DS1620_DQ, PIN_DS1620_Clock,
byte_NumberOfBits)
byte_TempIn = flipbits(byte_TempIn)
Call PutPin(PIN_DS1620_Reset, Low)

Dim byte_Sign As Byte
byte_Sign = GetBit(byte_TempIn, 8) ' Determine if we are pos or neg
byte_TempIn = byte_TempIn \ 2

'Debug.Print "byte_Sign=" & CStr(byte_Sign)
Dim f As Single
f = (1.8 * CSng(byte_TempIn)) + 32#
Debug.Print "byte_TempIn = " & CStr(byte_TempIn) & " OR " &
CStr(f) & "F"

GetTemp = f
If byte_Sign = 0 Then

ElseIf byte_Sign = 1 Then
Else
Debug.Print "Error"
End If

End Function

' Used to turn on a LED when the lights reach to low
' TODO-Add pent to change the light amount variable
Public Sub WatchLight()
Dim int_AmountOfLight As Integer

Do
Call PutPin(PIN_Photo, High)
Call Delay(0.001)

int_AmountOfLight = RCTime(PIN_Photo, High)
'Debug.Print CStr(int_AmountOfLight)

If int_AmountOfLight < 4000 Then
Call LED(High)
Else
Call LED(Low)
End If

Loop

End Sub

' Used to turn LED on/off
Public Sub LED(ByVal State As Byte)
If State = 0 Then
Call PutPin(PIN_LED, Low)
Else
Call PutPin(PIN_LED, High)
End If
End Sub Function PinState(ByVal Pin As Byte) As Byte
Select Case Pin
Case 5 To 12
PinState = GetBit(Register.PortC, 12 - Pin)
Case 13 To 20
PinState = GetBit(Register.PortA, 20 - Pin)
Case Else
PinState = 0
End Select
End Function <!-- SNIP ALL OTHER --!


--- In basicx@basi..., "bryanwasherein04"
<registration@m...> wrote:
>
> This turned out to be a costly mistake.

Well, lots of people like PIC's but they seem to me to be fairly low
on the usefulness spectrum.

Probably a sweet-spot for BX users in the AVR's is the ATMega32

But, you will still have to shell out for some sort of development
board, and a programmer. The total cost will be substantially more
than a BX, or the new ZX. And if you fry something, it will
probably still cost more than $40 to replace stuff.

IMHO, you will not do better cost-wise than the ZX-24. You can
improve heavily on performance by writing in C, but stuff like
interrupt-driven I2C is not trivial on the raw ATMega hardware. It
is FAST though.

For instance, I was just writing a FRAM interface for the I2C system
on an ATMega128. I have a fully functioning FRAM routine for the
ZX, and it was written in an evening. I spent much of an evening
yesterday on this, and I barely have the module to write a block
finished. Certainly NOT debugged yet, and it is the exact SAME
hardware module for the FRAM!! There are many more things I need to
juggle when working so close to the hardware.

Is this what you want? Maybe yes, for me on this project it is
pretty essential (although I might have been able to get it to work
on the ZX40 if it existed 2 years ago).

Tony