PICAXE-18X - Interfacing with a Maxim MAX518 Dual 8-bit D/A

copyright, Peter H Anderson, Baltimore, MD, Mar, '04


This note discusses how up to four Maxim MAX518 Dual 8-bit D/A devices may be interfaced with a PICAXE-18X or similar using two of the output terminals.

The MAX518 is a dual 8-bit D/A. Each of the outputs may be set to 256 values over the range of the +5 VDC supply.

The MAX518 uses the Philips I2C protocol and although the PICAXE does include I2C write and read commands, the format used is amenable only to I2C devices which have command structures similar to an EEPROM or RAM. That is, specify an address on the slave device followed by the data to be written, or specify an address on the slave device and then read the data. The PICAXE approach is powerful and will work for many I2C devices. However, with the MAX518, there is no start address where data is to be written. Rather, the command is simply one command byte, possibly followed by the setting of the D/A.

With the MAX518, there is no need to read data on the SDA lead.

Thus, in the following routine, recognize that this does not use the PICAXE I2C commands and also recognize that this is not strictly in accordance with the I2C standard. As noted, up to four MAX518 devices may be accommodated on the same two signal leads, but as there is no way to read the slave, devices such as the 24LC256 or similar EEPROMs and the DS1302 RTC should not be located on the two outputs used for the MAX518.

The factory assigned address is %01011. The other two bits are set using straps on the A1 and A0 terminals on the MAX518.

The sequence begins with the I2CStart sequence which is a matter of bringing SDA low while SCL is high. For normal operation, SCL is then brought low.

The specific device is then addressed by sending its unique address on the bus, in this case, %0101 1000, using subroutine I2COutByte. The least significant bit at zero indicates to the addressed slave that this is a write command. This byte is sent to the slave, most significant bit first, by setting the SDA lead to the appropriate state and bringing SCL high and then low. This is repeated for each of the eight bits. SDA is then brought high for one clock pulse. Note that in general, the full address byte is 2 * DevAdr + $58.

The command is then sent using subroutine I2COutByte.

  MAX518Shutdown       %00001000
  MAX518Reset          %00010000
  MAX518SetChannel0    %00000000
  MAX518SetChannel1    %00000001
For the last two commands, the data value is then sent using I2COutByte.

The communications sequence is terminated using the I2CStop which brings SDA high while SCL is high.


' MAX518.Bas (PICAXE-18X)
'
' Illustrates an interface with the Maxim MAX518 Dual 8-bit D/A
'
' Resets the MAX518
'
' Ramps DAC value on Out0 from 0 to 255 (0.0 VDC to 255/256 * 5.0 VDC) or
' about 20 mV per step.
' Ramps DAC value on Out1 from 255 down to 0
' Power down for 10 secs
'
' Continually loops.
'
'
' PICAXE-18X                  MAX518
'
' Pin0 (term 6) -------------- SCL (term 3) ---------- To other devices
' Pin 1 (term 7) -- 4.7K ----- SDA (term 4) ---------- each with different A1, A0 strapping
'
' Note that the strapping of A1 and A0 on the MAX518 determine the DevAdr.  This may
' be in the range of 0 to 3.  Note that up to four MAX518 devices, each having a different
' strapping, may be accommodated on the same two signal leads.
'
' Note that although the MAX518 uses the Philips I2C protocol.  However, the nature of the
' PICAXE I2C commands is not ammenable to control of the MAX518.
'
' In addition, the lack of a bidirectional I/O on the PICAXE-18X precludes bringing SDA to
' a high impedance state.  A series 4.7K resistor is used such that a logic one is +5 VDC
' through a 4.7K resistor.  This is not in keeping with the I2C standard, but it does work
' in applications where it is not necessary to read from the I2C slave.  Of course, I2C devices
' such as a 24LC256 EEPROM or DS1302 RTC where reads are required may not be used on these
' signal leads.
'
' Uses nominally 200 bytes of program memory.
'
' copyright, Peter H Anderson, Baltimore, MD, Mar, '04


Symbol  SCL = Pin0
Symbol  SDA = Pin1

Symbol  MAX518Shutdown = %00001000
Symbol  MAX518Reset = %00010000
Symbol  MAX518SetChannel0 = %00000000
Symbol  MAX518SetChannel1 = %00000001

Symbol  DevAdr = B0
Symbol  DACVal = B1

Symbol  OByte = B2
Symbol  N = B3

TOP:

    DevAdr = $00	'  A1, A0 strapping

    GoSub I2CStart	' reset the MAX518
    OByte = DevAdr * 2 + $58		' address the device for the purpose of writing
    GoSub I2COutByte
    OByte = MAX518Reset			' send the reset command
    GoSub I2COutByte
    GoSub I2CStop

    For DACVal = 0 to 255

       GoSub I2CStart			' start a sequence
       OByte = DevAdr * 2 + $58
       GoSub I2COutByte
       OByte = MAX518SetChannel0	' set DAC on Channel 0
       GoSub I2COutByte
       OByte = DACVal			' send the DAC value
       GoSub I2COutByte			' and the PWM duty in the low five bits
       GoSub I2CStop

       GoSub I2CStart			' start a sequence
       OByte = DevAdr * 2 + $58		' address the device for the purpose of writing
       GoSub I2COutByte
       OByte = MAX518SetChannel1	' set DAC on Channel 1
       GoSub I2COutByte
       OByte = 255 - DACVal		' send the DAC value
       GoSub I2COutByte			' and the PWM duty in the low five bits
       GoSub I2CStop

       Pause 250

   Next

   GoSub I2CStart			' shut down for ten secs
   OByte = DevAdr * 2 + $58
   GoSub I2COutByte
   OByte = MAX518Shutdown
   GoSub I2COutByte
   GoSub I2CStop

   Sleep 10

   GoTo TOP

I2CStart:
    SDA = 1
    SCL = 1
    SDA = 0				' bring SDA low when SCL is high
    SCL = 0
    Return

I2CStop:
    SCL = 1
    SDA = 1				' bring SDA high when SCL is high
    Return

I2COutByte:

    For N = 1 to 8
       SDA = OByte / 128	' most sign bit
       SCL = 1
       SCL = 0
       OByte = OByte * 2	' shift byte such that next bit is in most sig bit position
    Next
    SDA = 1				' null clock pulse to allow for slave to acknowledge
    SCL = 1
    SCL = 0
    SDA = 0
    Return