Interfacing with a Dallas DS1803 Dual Potentiometer (I2C)


' DS1803.Bas (BASICX)
'
' Illustrates how to control DS1803 Addressable Dual Potentiometer
'
' SCL_PIN (term 2 on BASICX) ------- SCL (term 9 on DS1803)
' SDA_PIN (term 3 on BASICX)-------- SDA (term 10 on DS1803)
'
' The manufacturer's device code foe the DS1803 is 0101.
'
' Note that the secondary address is determined by A2 (term 4), A1 (term 5)
' and A0 (term 6) on the 1803.  The above SCL and SDA leads may be multipled
' to eight devices, each strapped for a unique A2 A1 A0 setting.
'
' Pot 0 is set to a value of &H55 and Pot 1 to &H80.  The settings of the
' two pots are then read from the 1803 and displayed on the screen.
'
' Compile with I2C.Bas and SerialPort.Bas.
'
' copyright Peter H. Anderson and David Seebold, Baltimore, MD, Oct '99

Sub Main()

   Dim pot_setting_0 as byte, pot_setting_1 as byte
   Dim space as String
   
   space = " "
   Call OpenSerialPort(2,9600)
   
   Do 
      Call ds1803_write_pot(0, 0, &H55)  
			' dev 0, pot 0, setting &H55 (85 decimal)
      Call ds1803_write_pot(0, 1, &H80)  
                        ' dev 0, pot 1, setting &H80, or 128
      Call ds1803_read_pots(0, pot_setting_0, pot_setting_1)
			'note that pot_settings are passed by reference

      Call PutB(pot_setting_0)      
      Call PutStr(space)
      Call PutB(pot_setting_1)
      Call NewLine()
      Call Sleep(0.5)
   Loop

End Sub

'---------

Sub ds1803_write_pot(byVal device as byte, _
                     byVal pot as byte, _
                     byVal setting as byte)

'writes specified setting to specified potentiometer on specified device

   Call i2c_start()
   Call i2c_out_byte(&H50 + (device * 2))
   Call i2c_nack()
   Call i2c_out_byte(&Ha9 + pot)  ' 0xa9 for pot 0, 0xaa for pot 1
   Call i2c_nack()
   Call i2c_out_byte(setting)
   Call i2c_nack()
   Call i2c_stop()
   Call Sleep(0.025)  ' wait for it to burn into EEPROM

End Sub

'----------

Sub ds1803_read_pots(byVal device as byte, _
                     byRef p_setting_0 as byte, _
                     byRef p_setting_1 as byte)
'reads data from both potentiometers

   Call i2c_start()
   Call i2c_out_byte(&H51 + (device * 2))
   Call i2c_nack()
   Call i2c_in_byte(p_setting_0)
   Call i2c_ack()
   Call i2c_in_byte(p_setting_1)
   Call i2c_ack()
   Call i2c_stop()

End Sub