Sending Temperature from one BasicX to Another


' T_Meas1.Bas 
'
' Performs a temperature measurment using a DS1624 and displays locally
' and also writes the result to a remote BasicX.  The remote BasicX
' continually displays the result and if the value rises above a threshold
' turns on a stepping motor. (see T_Meas2.Bas below).
'
' Include SerialPort.Bas and I2C.Bas in Project.
'
' copyright, David Seebold, Baltimore, MD, Oct, '99

Sub Main()
    
   Dim NodeAddress as Integer, MemoryAddress as Integer 
   Dim Result as Byte, Whole_temp as Byte, Fract_temp as Byte
   Dim Whole_temp_F_single as Single
   Dim Whole_temp_F_byte as Byte
   Dim Space as String

   NodeAddress = &H1234
   MemoryAddress = 200
   Space = " "

   Call OpenSerialPort(2, 9600)		' open local com port
   Call ds1624_config(&H00,&H01)	' configure DS1624
   Call OpenNetwork(NodeAddress,200)

   Do 
      Call ds1624_init(&H00)		' perform a temperature meas
      Call ds1624_meas_temp(&H00, Whole_temp, Fract_temp)
      Whole_temp_F_single = ((1.8 * CSng(Whole_temp)) + 32.0)
      Whole_temp_F_byte = CByte(Whole_temp_F_single)

      ' send result to the remote
      Call PutNetwork(NodeAddress, MemoryAddress, Whole_temp_F_byte, Result)

      ' display locally
      Call PutB(Whole_temp_F_byte)
      Call PutStr(Space)
      Call PutStr(Space)
      Call PutB(Result)
      Call NewLine()
      Call Sleep(1.0)
 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.  Values t_whole and t_fract 
' returned by writing to their addresses

   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)	' value pointed to by p_whole
   Call i2c_ack()
   Call i2c_in_byte(fract)
   Call i2c_stop()

End Sub

' *************************

' T_Meas2.Bas
'
' Enables the BasicX to be written to remotely by another BasicX and 
' continually displays the result on the local com port.
'
' If the temperature, which was remotely written to RAM is above 71F
' a "Fan" is turned on.  The "Fan" in this case is a small stepping motor.
'
' copyright, David Seebold, Baltimore, MD, Oct, '99

Sub Main()

   Dim Address as Integer
   Dim Remote_Temp as Byte
   Call Rampoke(26, 200)	' put something foolish in location 200

   Call OpenSerialPort(2, 9600)
   Call OpenNetwork(&H1234, 200)

   Do
      Remote_Temp = RAMpeek(200)	' continually monitor location 200
      Call PutB(Remote_Temp)		' and display it
      Call NewLine()

      If (Remote_Temp >= 71) Then	' if above a threshold turn a motor
         Call Fan_time()	
      End If
      Call Sleep(1.0)
   Loop		

End Sub

'-------------

Sub Fan_time()

   Dim Patts(1 To 8) as byte, Fan_ON as string, Fan_OFF as string
   Dim n as integer, On_or_Off as byte, Fast_or_Slow as byte, _
	    Direction as byte, sleep_time as single

   Patts(1) = bx00000001	' initialize the array 'Patts'
   Patts(2) = bx00000011	' in such a way that it will
   Patts(3) = bx00000010	' turn the motor...notice
   Patts(4) = bx00000110	' that the patterns of 1's
   Patts(5) = bx00000100	' are always to each other...
   Patts(6) = bx00001100
   Patts(7) = bx00001000
   Patts(8) = bx00001001

   Dim n as integer, On_or_Off as byte, Fast_or_Slow as byte, _
	    Direction as byte, sleep_time as single

   Register.DDRC = bx11111111	'sets all 8 pins on port C as outputs
   Fan_ON = "Fan is ON!"
   Fan_OFF = "Fan is OFF!"
   Call PutStr(Fan_ON)
   Call NewLine()

   Do
      sleep_time = 0.001
      Direction = 1
      Call turn_that_motor(Patts, Direction, sleep_time)  

   Loop Until (Rampeek(200) < 70) 
		'fan stays on until temp is less than 70)
 
   Call PutStr(Fan_OFF)
   Call Newline()

End Sub

'--------

Sub turn_that_motor(byRef Patts() as byte, _
		      byVal Direction as byte, _
		      byVal sleep_time as single)

   Dim N as Integer

   If(Direction = 0) Then		'turn one way
      For N = 1 To 8 Step 1
	 Register.PortC = Patts(N)
	 ' Call PutB(patts(N))   'used for debugging
	 ' Call NewLine()
         Call Sleep(sleep_time)
      Next

   Else					'turn the other way
      For N = 8 To 1 step -1
         Register.PortC = Patts(N)
         ' Call PutB(patts(N))	   'used for debugging
         ' Call NewLine()
	 Call Sleep(sleep_time)
      Next
   End If
  
End Sub