' MLX90614_3.Bas
'
' Prompts user for SlaveAddress and writes this to EEPROM address $0e. Note that
' $00 is used as the SlaveAddress up to this point. But the new slave address is
' used to read and display the content of location $0e.
'
' The intent is a framework for a program to modify the slave address to something
' other than $5a to allow for more than one device on the same bus.
'
' Note that after a lot of frustration, I discovered that after programming a new
' slave address, I had to recycle power to the MLX90614 for the new slave address to
' be recognized.
'
'
' PICAXE-20X2 MLX90614ESF-AAA
'
' Term 11 SCL --------------- SCL
' Term 13 SDA --------------- SDA
'
' 4.7K resistors to +5 VDC on SDA and SCL
'
' Note that this is a direct interface with the PICAXE. The Parallax and
' Sparkfun boards are not required.
'
'
' copyright, Peter H Anderson, Baltimore, MD, Mar 15, 11
#picaxe 20x2
#Terminal 9600
#No_Table
#No_Data
#freq m4
Symbol Lo = B0
Symbol Hi = B1
Symbol PEC = B2
Symbol Status = B3
Symbol Ch = B5
Symbol Val = W3
Symbol I = B8
Symbol J = B9
Symbol K = B10
Symbol SlaveAdr_2 = B11
Symbol EEPROMLocation = B12
Symbol CRC8 = B13
Symbol X = B14
Symbol NewSlaveAdr = B15
Top:
Pause 1000
Hi2cSetup I2CMaster, %01010000, I2CSlow, I2CByte
TryAgn:
SerTxD ("Enter New Slave Address: ")
SerRxD [15000, TryAgn], #NewSlaveAdr
SerTxD (#NewSlaveAdr, CR, LF);
Pause 5000 ' pause to admire
SlaveAdr_2 = $00 * 2 ' Use the general address for now
Again:
EEPROMLocation = $20 + $0e
GoSub ReadEEPROM
If Status = 1 Then
Val = Val & $00ff ' take the low byte
SerTxD ("Current Slave Adr: ", #Val, CR, LF)
Else
SerTxD ("Current Slave Adr", "Invalid", CR, LF)
EndIf
Pause 5000
SlaveAdr_2 = $00 * 2
Val = $00
GoSub WriteEEPROM ' erase location $0e
Val = NewSlaveAdr ' now program in the new slave address
GoSub WriteEEPROM
' recycle power to the MLX90614 before trying to use the new slave address
SerTxD ("Recycle power to the MLX90614. Key in a number when done", CR, LF)
SerRxD [45000], Ch
SlaveAdr_2 = NewSlaveAdr * 2
GoSub ReadEEPROM
If Status = 1 Then
Val = Val & $00ff ' take the low byte
SerTxD ("Current Slave Adr: ", #Val, CR, LF)
Else
SerTxD ("Current Slave Adr", "Invalid", #Val, CR, LF)
EndIf
Pause 10000
GoTo Again
ReadEEPROM:
Hi2cin [SlaveAdr_2], EEPROMLocation, (Lo, Hi, PEC)
Val = Hi
Val = Val * 256 + Lo
CRC8 = $00
X = SlaveAdr_2
GoSub CalcCRC8
X = EEPROMLocation
GoSub CalcCRC8
X = SlaveAdr_2 + 1
GoSub CalcCRC8
X = Lo
GoSub CalcCRC8
X = Hi
GoSub CalcCRC8
X = PEC
GoSub CalcCRC8
' SerTxD ("CRC = ", #CRC8, CR, LF)
If CRC8 = 0 Then
Status = 1 ' success
Else
Status = 0
Endif
SerTxD (#SlaveAdr_2, " ", #EEPROMLocation, CR, LF)
Return
WriteEEPROM:
Lo = Val
Hi = $00
CRC8 = $00
X = SlaveAdr_2
GoSub CalcCRC8
X = EEPROMLocation
GoSub CalcCRC8
X = Lo
GoSub CalcCRC8
X = Hi
GoSub CalcCRC8
PEC = CRC8
Hi2cOut [SlaveAdr_2], EEPROMLocation, (Lo, Hi, PEC)
Pause 20
Return
CalcCRC8:
X = X ^ CRC8
For K = 0 to 7
If X > 127 Then
X = X * 2
X = X ^ $07
Else
X = X * 2
Endif
Next
CRC8 = X
Return