News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

I2C scanner

Started by Wimax, Aug 25, 2025, 05:19 AM

Previous topic - Next topic

Wimax

Hello everybody ;)

I found several I2C scanners for 8-bit microcontrollers on the website, but it seems that they use the I2C hardware engine. I am looking for a simple, completely software-based I2C scanner for 16-bit microcontrollers, and perhaps I have overlooked it.


Abdullah

Quote from: Wimax on Aug 25, 2025, 05:19 AMHello everybody ;)

I found several I2C scanners for 8-bit microcontrollers on the website, but it seems that they use the I2C hardware engine. I am looking for a simple, completely software-based I2C scanner for 16-bit microcontrollers, and perhaps I have overlooked it.


Hello Sir, I have been searching for a program for many days but I haven't been able to find it. Kindly help me a little. I want to control an IC using a remote control. I will adjust the remote according to my needs, whichever remote I want to use. But I don't know how to use the IR receiver in the program. Please make me a demo program so that I can use a remote to turn two ports ON and OFF in the 16C72. Thank you very much.

flosigud

Below is my take on this:

    Declare SCL_Pin = PORTB.8                ' SCL connected to this pin
    Declare SDA_Pin = PORTB.9                ' SDA connected to this pin
    Include "Bus.inc"

    Dim bAddress As Byte
    Dim bAddresses As Byte
    Dim bReply

    Dim sI2CScanner[13]      As Code = " I2C Scanner ",0   
    Dim sReply[25]          As Code = " Reply at address ",0
    Dim sNumberOfReplies[11] As Code = " Replies = ",0
    DelayMS 1000
    Output PORTB
    HRSOut CStr sI2CScanner,13,13

    bAddresses = 0
    For bAddress = 1 To 255
        bReply = BusIn bAddress
        If bReply < 255 Then
            HRSOut CStr sReply, Hex bAddress,13
            Inc bAddresses
        EndIf
    Next
    HRSOut CStr sNumberOfreplies , Dec bAddresses,13

    Do                                        ' Toggle port pin
        Toggle PORTB.15                        ' see that program is running.
    Loop 

Wimax

Hello,

Thanks for the code. I couldn't get it to work, but it was very useful for generating a compact version using standard "BusOut" command that gave excellent results.

Assuming that the SCL and SDA pins and the UART have already been declared, just call the procedure, and an address range (which can be modified) is scanned, selecting only those with bit 0 set to zero (typically write) for which an ACK is received after a blank write.

Proc I2C_Scan()

Dim Vespa as Byte
 
DelayMS 100
   
    For Vespa = 123 To 255
         If Vespa=(Vespa) & 0xFE Then
             BStart
             BusOut Vespa
                If SRbits_C = 0  Then
                    HRSOutLn "Detected ","  ", Hex Vespa,"  ",Bin8 Vespa
                    BStop
                EndIf
         EndIf       
         DelayMS 10
    Next
   
EndProc

keytapper

I wrote mine pretty long ago, I was thinking also for the extended version of 1024 addresses, but I kept it simpler.
Basically the method is to see when one device responds to the signal. Then just start sending from address 0 to 255.
Ignorance comes with a cost

Wimax

I ported an application developed for the Pic24HJ series to DsPIC33CK256MP508, adding a DSP section that the old microcontroller could never have supported in real time.
The work took a long time because the architectures are very different and the way ADCs (among other things) are managed is really different. I appreciated and continue to appreciate the power of this MCU, which allows you to manage a multitude of different types of interrupts smoothly and is really fast.
But...even though I reused well-known I2C peripherals, I encountered several problems and the I2Cin/out commands were sometimes not interpreted correctly even setting I2C_Slow_Bus 0. I solved some of them by using the I2C_Software.inc library, but with one chip in particular I had to struggle so much that I began to doubt the correct implementation of the PCB routing.
At this point, I had to resort to an I2C scanner to be absolutely sure that the device was at least declaring its existence on the bus.
I started with the code suggested by Flosigud with a few modifications and, finally, I was able to verify the detection of all the I2C chips present and then to investigate and isolate the causes of the problems encountered.  I must say that, for the first time, ChatGPT also helped me by suggesting a simple software solution to the problem I had encountered. Incredibly, it put me on the right road for once especially thanks to its ability to scour the web thoroughly.