Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
crackerjack Senior Member Joined: 11/07/2011 Location: AustraliaPosts: 164
Posted: 10:38am 19 Mar 2012
Copy link to clipboard
Print this post
Ever get baffled by I2C addressing? Sometime unsure if your I2C device is present on the bus?
Here is a little utility I wrote to help with those I2C address issues which sometimes arise. If there is an earlier, better solution, please let us all know. If there are issues with the code (or it doesn't work for you), post the fix! Even better still, if you use 10-bit addressing and can enhance the scanner - well, you know what to do...
I'd be happy to hear if this helps anybody out.
[code]
' I2CScan Utility - crackerjack, March 2012
' Function: Scans the full 7-bit I2C Address range
' and reports addresses where device
' responds to an "i2csend".
' Addresses are shown as "raw" and
' 7-bit (W/R). Use 7-Bit addressing
' with MMBasic's I2C implementation.
' ------------------------------------------------------------ ------------
Cls
I2CEN 100, 100 ' Enable I2C at 100kHz, Timeout 100 mS
' Raw Write Raw Read
' ---- ------ ---- ------
' 0xA0(0x50 W) 0xA1(0x50 R)
For i=&h8 To &h77 ' Valid 7-bit I2C address range
I2CSEND i, 0, 1, 0 ' Send a single-byte to the address
If MM.I2C = 0 Then ' An ACK was received, device @ this address
' Left-shift address for "raw" display value
? "0x" Hex$(i*2) "(0x" Hex$(i) "W) 0x" Hex$(i*2+1) "(0x" Hex$(i) "R)"
EndIf
Next i