Interfacing with a DS1624 Thermometer / EEPROM


' DS1624.BAS (BASICX)
'
' The DS1624 is a digital thermometer plus 256 byte EEPROM.  The 
' EEPROM might be used to store temperature trip points or calibration
' data specific to the DS1624.
'
' Performs 10 temperature measurements.  High bytes are written to
' the first 10 locations in the EEPROM of the Dallas 1624 Digital 
' Thermometer.  The data is then read from EEPROM and displayed.  This
' probably is not terribly practical, but is a convenient way to 
' illustrate how to write to and read from the EEPROM
'
' Note that the DS1624 is an I2C device with a manufacturers code of 1001. 
' The secondary address is determined by the strapping of A2, A1, A0.  
' In this example the device was strapped to 010.  Thus, the device 
' code is &H02.
' 
' Note that the display of negative temperature readings was not tested.
'
' Complile with I2C.BAS and SerialPort.BAS
'
' copyright, David Seebold, Baltimore, MD, Oct '99

Const NUM_SAMPS As Byte = 10	' take 10 samples

Sub main()   
   Dim ee_adr as Byte   
   Dim t_whole as Byte, t_fract as Byte   
   Dim minus as String     
   Dim space as String 	   
   minus = "-"   
   space = " "   

   Call OpenSerialPort(2, 9600)   
   Call ds1624_config(&H02, &H01) 	
     ' configures the ds1624...dev adr is Hex 02, mode is Hex 01 - 1 shot	    
     ' which takes just one temperature measurement.

   Do  ' continual loop

   ' take some temperature measurments and write whole part to EEPROM       
      For ee_adr = 0 To (NUM_SAMPS-1)      
         Call ds1624_init(&H02)	' device address is 2     
         Call ds1624_meas_temp(&H02, t_whole, t_fract)   
  		' note that t_whole and t_fract are passed by reference.	         Call ds1624_ee_write(&H02, ee_adr, t_whole) ' save the reading

         Call PutPin(14, bxOutputHigh)	' flash an LED     
         Call Sleep(0.5)            
         Call PutPin(14, bxOutputLow)     
         Call Sleep(0.5)		' one second total delay   
      Next   

      For ee_adr = 0 To (NUM_SAMPS-1) 
      ' now fetch each reading from EEPROM and display         
         Call ds1624_ee_read(&H02, ee_adr, t_whole)      
         If ((t_whole * &H80)\256 = 1) Then  ' its negative    
            t_whole = (t_whole XOR &Hff) + 1   ' take the 2s complement      
            Call PutStr(minus) ' print a minus sign
         End if

         Call PutB(t_whole) 
         If (((ee_adr + 1)Mod 5) = 0) Then ' new line every five values      
            Call NewLine()      
         Else         
            Call PutStr(space) 	' otherwise space between values     
         End If

         Call PutPin(14, bxOutputHigh)	' flash an LED a few times  
         Call Sleep(0.1)      
         Call PutPin(14, bxOutputLow)      
         Call Sleep(0.1)      
         Call PutPin(14, bxOutputHigh)      
         Call Sleep(0.1)      
         Call PutPin(14, bxOutputLow)      
         Call Sleep(0.1)   
      Next
   Loop
End Sub

'-----------
Public Sub ds1624_config(ByVal dev_adr as Byte, _
				 ByVal mode as Byte)
   ' configures DS1624 in 1SHOT temperature conversion mode   
   Call i2c_start()   
   Call i2c_out_byte(&H90 + (dev_adr * 2))   
   Call i2c_nack()   
   Call i2c_out_byte(&Hac)	' access configuration   
   Call i2c_nack()   
   Call i2c_out_byte(mode)   
   Call i2c_nack()   
   Call i2c_stop()   
   Call Sleep(0.025)  	' wait for EEPROM to program
End Sub

'-------------
Public Sub ds1624_init(ByVal dev_adr as Byte)   

   Call i2c_start()   
   Call i2c_out_byte(&H90 + (dev_adr *2))   
   Call i2c_nack()   
   Call i2c_out_byte(&Hee)	' start conversion   
   Call i2c_nack()   
   Call i2c_stop()   
   Call Sleep(1)      ' wait for conversion to complete
End Sub

'---------------
Public Sub ds1624_meas_temp(ByVal dev_adr as Byte, _
			    ByRef whole as Byte, _		
                         ByRef fract as Byte)
   ' fetches temperature result.
   
   Call i2c_start()   
   Call i2c_out_byte(&H90 + (dev_adr * 2))   
   Call i2c_nack()   
   Call i2c_out_byte(&Haa)	' fetch temperature   
   Call i2c_nack()   

   Call i2c_start()		' no intermediate stop   
   Call i2c_out_byte(&H90 + (dev_adr * 2) + &H01)   
   Call i2c_nack()   
   Call i2c_in_byte(whole)	   
   Call i2c_ack()   
   Call i2c_in_byte(fract)   
   Call i2c_stop()
End Sub

'-----------------
Public Sub ds1624_ee_read(byVal dev_adr as Byte, _
			     byVal ee_adr as Byte, _
 	                     byRef t_whole as Byte)
 ' returns content location of location ee_adr in DS1624 EEPROM     
   Call i2c_start()   
   Call i2c_out_byte(&H90 + (dev_adr * 2))   
   Call i2c_nack()   
   Call i2c_out_byte(&H17)		' access memory   
   Call i2c_nack()   
   Call i2c_out_byte(ee_adr)   ' the eeprom address...   
   Call i2c_nack()   
   Call i2c_start()			' no intermediate stop   
   Call i2c_out_byte(&H90 + (dev_adr * 2) + &H01)   
   Call i2c_nack()   
   Call i2c_in_byte(t_whole)   ' no ack as it is the last byte prior to stop 
   Call i2c_stop()
End Sub
'----------------
Public Sub ds1624_ee_write(ByVal dev_adr as Byte, _
               ByVal ee_adr as Byte, _				
               ByVal dat as Byte)

' writes content of dat to specified address ee_adr   
   Call i2c_start()   
   Call i2c_out_byte(&H90 + (dev_adr * 2))   
   Call i2c_nack()   
   Call i2c_out_byte(&H17)		' access memory   
   Call i2c_nack()   
   Call i2c_out_byte(ee_adr)   
   Call i2c_nack()   
   Call i2c_out_byte(dat)		' the eeprom data   
   Call i2c_nack()   
   Call i2c_stop()   
   Call Sleep(0.050)  		' wait for eeprom to program
End Sub