dkinzer Site Admin
Joined: 03 Sep 2005
Posts: 2593
Location: Portland, OR
|
|
Posted: 17 June 2010, 22:58 PM Post subject: |
|
|
The code below can be used to set the I2C slave address for an MLX90614. This is necessary if you want to use multiple MLX90614 devices connected on the same bus since they must each have a unique address.
Note, also, that this code contains a function to assist in the calculation of the CRC-8 value of an SMBus datastream. Because all of the bytes sent, including the slave address byte, contribute to the CRC-8 value, the low-level I2C routines are used. The I2CCmd() function will still work but it hides some of the data bytes sent over the bus that must be factored into the CRC value. | Code: | '
' This program can be used to set the SMBus slave address of an MLX90614.
' Note, particularly, that the device being modified must be the only
' device on the bus since "all call" addressing is used.
Private Const chan as Byte = 0
Private Const slaveAddr as Byte = 0 ' slave address 0 is used
Private Const SlaveIdAddr as Byte = &H2e ' the EEPROM address of the I2C slave address
' set this constant to the desired address
Private Const newAddr as Byte = &H6a
Sub Main()
Call OpenI2C(chan, 0, 0)
Call Sleep(0.25)
Dim curAddr as Byte
curAddr = GetAddr_MLX90614()
If (curAddr = newAddr) Then
Debug.Print "The slave address is already set to &H"; CStrHex(newAddr)
Else
Debug.Print "The slave address is currently &H"; CStrHex(curAddr)
' set the address to zero first
Call SetAddr_MLX90614(&H00)
Call Sleep(0.5)
' then set to the desired address
Call SetAddr_MLX90614(newAddr)
Call Sleep(0.5)
Debug.Print "The slave address is now set to &H"; CStrHex(newAddr)
Debug.Print "Cycle the power to have the new address take effect"
End If
Call CloseI2C(chan)
End Sub
'
'' GetAddr_MLX90614
'
' Determine the current slave address setting.
'
Private Function GetAddr_MLX90614() as Byte
Dim data(1 to 10) as Byte
Dim stat as Boolean
Call I2CStart(chan)
stat = I2CPutByte(chan, slaveAddr And &Hfe)
stat = I2CPutByte(chan, SlaveIdAddr)
Call I2CStart(chan)
stat = I2CPutByte(chan, slaveAddr Or &H01)
data(1) = I2CGetByte(chan, True) ' low byte
data(2) = I2CGetByte(chan, True) ' high byte (meaningless)
data(3) = I2CGetByte(chan, False) ' CRC-8
Call I2CStop(chan)
GetAddr_MLX90614 = data(1)
End Function
'
'' SetAddr_MLX90614
'
' Set the slave address value.
'
Private Sub SetAddr_MLX90614(ByVal addr as Byte)
Dim crc as Byte = 0
Call I2CStart(chan)
Call sendByte(chan, slaveAddr And &Hfe, crc)
Call sendByte(chan, SlaveIdAddr, crc)
Call sendByte(chan, addr, crc) ' low address byte
Call sendByte(chan, 0, crc) ' high address byte (meaningless)
Call sendByte(chan, crc, crc) ' the computed CRC-8 value
Call I2CStop(chan)
End Sub
Private Sub sendByte(ByVal i2cChan as Byte, ByVal data as Byte, ByRef crc as Byte)
Dim stat as Boolean
stat = I2CPutByte(i2cChan, data)
crc = CRC8(crc, data)
End Sub
'
'' CRC8
'
' Permute the CRC-8 accumulator with a given data byte. The initial
' CRC-8 accumulator value should be zero.
'
Private Function CRC8(ByVal CRC as byte, ByVal data as byte) as byte
CRC8 = crcTable((CRC xor data) + 1)
End Function
' Polynominal lookup table for CRC calculation
Private crcTable as ByteVectorData({
&H00, &H07, &H0E, &H09, &H1C, &H1B, &H12, &H15, &H38, &H3F, &H36, &H31, &H24, &H23, &H2A, &H2D,
&H70, &H77, &H7E, &H79, &H6C, &H6B, &H62, &H65, &H48, &H4F, &H46, &H41, &H54, &H53, &H5A, &H5D,
&HE0, &HE7, &HEE, &HE9, &HFC, &HFB, &HF2, &HF5, &HD8, &HDF, &HD6, &HD1, &HC4, &HC3, &HCA, &HCD,
&H90, &H97, &H9E, &H99, &H8C, &H8B, &H82, &H85, &HA8, &HAF, &HA6, &HA1, &HB4, &HB3, &HBA, &HBD,
&HC7, &HC0, &HC9, &HCE, &HDB, &HDC, &HD5, &HD2, &HFF, &HF8, &HF1, &HF6, &HE3, &HE4, &HED, &HEA,
&HB7, &HB0, &HB9, &HBE, &HAB, &HAC, &HA5, &HA2, &H8F, &H88, &H81, &H86, &H93, &H94, &H9D, &H9A,
&H27, &H20, &H29, &H2E, &H3B, &H3C, &H35, &H32, &H1F, &H18, &H11, &H16, &H03, &H04, &H0D, &H0A,
&H57, &H50, &H59, &H5E, &H4B, &H4C, &H45, &H42, &H6F, &H68, &H61, &H66, &H73, &H74, &H7D, &H7A,
&H89, &H8E, &H87, &H80, &H95, &H92, &H9B, &H9C, &HB1, &HB6, &HBF, &HB8, &HAD, &HAA, &HA3, &HA4,
&HF9, &HFE, &HF7, &HF0, &HE5, &HE2, &HEB, &HEC, &HC1, &HC6, &HCF, &HC8, &HDD, &HDA, &HD3, &HD4,
&H69, &H6E, &H67, &H60, &H75, &H72, &H7B, &H7C, &H51, &H56, &H5F, &H58, &H4D, &H4A, &H43, &H44,
&H19, &H1E, &H17, &H10, &H05, &H02, &H0B, &H0C, &H21, &H26, &H2F, &H28, &H3D, &H3A, &H33, &H34,
&H4E, &H49, &H40, &H47, &H52, &H55, &H5C, &H5B, &H76, &H71, &H78, &H7F, &H6A, &H6D, &H64, &H63,
&H3E, &H39, &H30, &H37, &H22, &H25, &H2C, &H2B, &H06, &H01, &H08, &H0F, &H1A, &H1D, &H14, &H13,
&HAE, &HA9, &HA0, &HA7, &HB2, &HB5, &HBC, &HBB, &H96, &H91, &H98, &H9F, &H8A, &H8D, &H84, &H83,
&HDE, &HD9, &HD0, &HD7, &HC2, &HC5, &HCC, &HCB, &HE6, &HE1, &HE8, &HEF, &HFA, &HFD, &HF4, &HF3
}) |
|
|