I thought that I would try out the i2c scanner by bitfogav. The program is a great idea in so far as, if the i2c device isn't found by the scanner, one could rest well assured that sending or receiving data to the device would be a fool's errand...
What I did: copied the code, had to change the pic to an18f24k22 as I did not have the device used in the published code, and I took the liberty of adding a couple of spaces at the beginning of each line of output....
When the compiled code is run, every scanned address appears as being present.
The exact code I used follows:
I sure hope that I didn't do anything too stupid...
Code: Select all
Device = 18F24K22
Clock=16
Config
FOSC = HSMP, // HS oscillator (medium power 4-16 MHz)
PLLCFG = OFF, // Oscillator used directly
PRICLKEN = ON, // Primary clock enabled
FCMEN = OFF, // Fail-Safe Clock Monitor disabled
IESO = OFF, // Oscillator Switchover mode disabled
PWRTEN = OFF, // Power up timer disabled
BOREN = SBORDIS, // Brown-out Reset enabled in hardware only (SBOREN is disabled)
BORV = 250, // VBOR set to 2.50 V nominal
WDTEN = ON, // WDT is always enabled. SWDTEN bit has no effect
WDTPS = 32768, // 1:32768
CCP2MX = PORTC1, // CCP2 input/output is multiplexed with RC1
PBADEN = OFF, // PORTB<5:0> pins are configured as digital I/O on Reset
CCP3MX = PORTC6, // P3A/CCP3 input/output is mulitplexed with RC6
HFOFST = ON, // HFINTOSC output and ready status are not delayed by the oscillator stable status
T3CMX = PORTC0, // T3CKI is on RC0
P2BMX = PORTB5, // P2B is on RB5
MCLRE = EXTMCLR, // MCLR pin enabled, RE3 input pin disabled
STVREN = ON, // Stack full/underflow will cause Reset
LVP = OFF, // Single-Supply ICSP disabled
XINST = OFF, // Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
DEBUG = OFF, // Disabled
CP0 = OFF, // Block 0 (000800-001FFFh) not code-protected
CP1 = OFF, // Block 1 (002000-003FFFh) not code-protected
CPB = OFF, // Boot block (000000-0007FFh) not code-protected
CPD = OFF, // Data EEPROM not code-protected
WRT0 = OFF, // Block 0 (000800-001FFFh) not write-protected
WRT1 = OFF, // Block 1 (002000-003FFFh) not write-protected
WRTC = OFF, // Configuration registers (300000-3000FFh) not write-protected
WRTB = OFF, // Boot Block (000000-0007FFh) not write-protected
WRTD = OFF, // Data EEPROM not write-protected
EBTR0 = OFF, // Block 0 (000800-001FFFh) not protected from table reads executed in other blocks
EBTR1 = OFF, // Block 1 (002000-003FFFh) not protected from table reads executed in other blocks
EBTRB = OFF // Boot Block (000000-0007FFh) not protected from table reads executed in other blocks
// import libraries...
Include "usart.bas"
Include "convert.bas"
Include "I2C.bas"
Dim nDevices As Byte
Sub ScanForDevices()
Dim addrs, error As Byte
nDevices = 0 ' reset
For addrs = 0 To 126 Step 2
I2C.Start()
I2C.WriteByte(addrs)
I2C.Stop()
If (I2C.NotAcknowledged = true) Then
error = 1 ' no device
Else
error = 0 ' device detected
EndIf
If error = 0 Then ' no error we have found a device.
USART.Write(" $", HexToStr(addrs), 13, 10)
Inc(nDevices) ' found a device so add number of device.
End If
' add a little delay betweens transactions for some slow slave devices
DelayUS(100)
Next
USART.Write(" ",DecToStr(nDevices), " I2C device(s) found", 13, 10)
End Sub
// init I2C...
I2C.Initialize(I2C_100_KHZ)
// start serial...
USART.SetBaudrate(br19200)
USART.Write(" I2C Scanner", 13, 10)
USART.Write(" By Gavin Wiggett (Bitfogav)", 13, 10)
// main prog loop...
While true
USART.Write(" Scanning for Devices..", 13, 10)
ScanForDevices()
DelayMS(5000) ' 5sec delay between bus reads.
Wend