Req. I2C Scanner


+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Req. I2C Scanner1657 days old

  1. #1
    Member
    Join Date
    Jul 2006
    Posts
    147
    Thumbs Up
    Received: 1
    Given: 13
    Total Downloaded
    5.03 GB

    0 Not allowed!

    Default Req. I2C Scanner

    Is there any code for an I2C scanner.

    I need to find out the address of a I2C device connected to the PIC micro.

    Thank you.

  2. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  3. #2
    Prolific Poster
    Join Date
    Mar 2012
    Posts
    2,318
    Thumbs Up
    Received: 80
    Given: 182
    Total Downloaded
    5.30 GB

    0 Not allowed!

    Default Re: Req. I2C Scanner

    This isn't Basic so I don't know if this will help but it might get you started. http://www.ccsinfo.com/forum/viewtopic.php?t=49713&start=0&postdays=0&postorder =asc&highlight=

    George
    George.

  4. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  5. #3
    Member
    Join Date
    Jul 2006
    Posts
    147
    Thumbs Up
    Received: 1
    Given: 13
    Total Downloaded
    5.03 GB

    0 Not allowed!

    Default Re: Req. I2C Scanner

    thanks george........
    but that's C, and I don't know C.

  6. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  7. #4
    Prolific Poster hadv215's Avatar
    Join Date
    Sep 2009
    Posts
    1,136
    Thumbs Up
    Received: 66
    Given: 26
    Total Downloaded
    3.61 GB

    1 Not allowed!

    Default Re: Req. I2C Scanner

    What the program does is loop through I2C addresses and attempting to write something to the device.
    If an ACK is received, this address is the address of the device else it is not.
    Should not be too difficult to write it in PicBasic.

  8. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  9. #5
    Member
    Join Date
    Oct 2013
    Posts
    179
    Thumbs Up
    Received: 8
    Given: 13
    Total Downloaded
    3.14 GB

    1 Not allowed!

    Default Re: Req. I2C Scanner

    I wrote the code below yesterday and it seems to do the trick. I based it originally on the C code linked to in post 2. That only caters for seven bit addresses and expects reply to be zero. My code checks for all addresses between 1 and 255 and accepts any replay except 255 as valid. As it is it seems to do the job.




    ' -----------------------------------------------------------
    ' i2c scanner
    ' The i2c scanner uses the return value of HBusOut
    ' to see if a device did acknowledge to the address.
    ' -----------------------------------------------------------

    Dim nDevices As Byte
    Dim address As Byte
    Dim Reply As Byte

    Dim I2CScanner[13] As Code = " I2C Scanner ",0
    Dim DeviceFound[25] As Code = " Reply at address ",0
    Dim NumberOfDevices[11] As Code = " Replies = ",0


    HRSOut CStr I2CScanner,13,13

    nDevices = 0
    For address = 1 To 255
    Reply = HBusIn address
    If Reply < 255 Then
    HRSOut CStr deviceFound,Hex address,13
    Inc nDevices
    EndIf
    Next
    HRSOut 13,CStr NumberOfDevices , Dec nDevices,13,13

    Stop
    Last edited by flosigud; 21st November 2015 at 16:03.

  10. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  11. #6
    Prolific Poster hadv215's Avatar
    Join Date
    Sep 2009
    Posts
    1,136
    Thumbs Up
    Received: 66
    Given: 26
    Total Downloaded
    3.61 GB

    0 Not allowed!

    Default Re: Req. I2C Scanner

    Doing 255 tests is overkill because every I2C device uses only 7 bits for the address, bit 0 indicates a write or a read.
    From the manual "bit-0 is set to signify that we wish to read from the eeprom.
    Note that this bit is automatically cleared by the Hbusin command, regardless of its initial value."
    And to be complete you should have started at 0 instead of 1.

    So what you are doing is basically test each device twice and probably you're getting an even number for nDevices.

    A better approach would be
    For address = 0 to 255
    if address.0 = 1 then
    reply = hbusin address
    etcetera
    endif
    next

    Regards
    Harm

  12. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  13. #7
    Member
    Join Date
    Oct 2013
    Posts
    179
    Thumbs Up
    Received: 8
    Given: 13
    Total Downloaded
    3.14 GB

    0 Not allowed!

    Default Re: Req. I2C Scanner

    Yes, each device is tested twice. I don't have a problem with that.

  14. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  15. #8
    Prolific Poster hadv215's Avatar
    Join Date
    Sep 2009
    Posts
    1,136
    Thumbs Up
    Received: 66
    Given: 26
    Total Downloaded
    3.61 GB

    0 Not allowed!

    Default Re: Req. I2C Scanner

    But your result nDevices will actually be too high

  16. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  17. #9
    Prolific Poster
    Join Date
    Mar 2012
    Posts
    2,318
    Thumbs Up
    Received: 80
    Given: 182
    Total Downloaded
    5.30 GB

    0 Not allowed!

    Default Re: Req. I2C Scanner

    Leaving aside the issue of Bit 0, the r/w bit, the above code works but I don't understand why the following detects Ack?
    Reply = HBusIn address
    If Reply < 255 Then
    In the manual in both HBusOut and BusOut the Ack/Nak status is meant to be in both STATUS.0 and PP4.0, 0 being Ack. If I plug that into the posted code I get 256 devices. If I change "Reply = HBusIn address" to "HBusOut address" (in line with manual) I still get 256 devices.

    Can anyone explain why the snippet detects Ack and/or why using carry/borrow or PP4.0 does not?
    George.

  18. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  19. #10
    Prolific Poster
    Join Date
    Mar 2012
    Posts
    2,318
    Thumbs Up
    Received: 80
    Given: 182
    Total Downloaded
    5.30 GB

    0 Not allowed!

    Default Re: Req. I2C Scanner

    Setting aside my lack of understanding, I guess this would fail if the device actually sent $FF?

    It certainly was worth while running the code posted by flosigud, having spent far to long trying to get an alleged I2C Oled working, I find it's either dead or not I2C.

    A million thank you are winging there way to Reykjavk from the emerald isle (although if current trend continue, no rain anywhere for TWO days, it won't be so emerald). You have saved my dev board, PicKit2, a pane of glass and my grip on reality.

    From now on when doing battle with a new I2C device, the first thing I'm going to do it run the scanner.

    Still need a bulletproof way to detect an Ack.
    Last edited by towlerg; 18th September 2019 at 15:57.
    George.

  20. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  21. #11
    Senior Member
    Join Date
    May 2011
    Posts
    506
    Thumbs Up
    Received: 59
    Given: 0
    Total Downloaded
    408.14 MB

    0 Not allowed!

    Default Re: Req. I2C Scanner

    This is the way I would do it...
    Code:
    nDevices = 0
    For address = 0 To 254 Step 2
      HbStart			' send START
      HBusOut address     ' send ADDR byte w/RW=0 (write)
      If PP4.0 = 0 Then	' check ACK result (also in STATUS.0)
        HRSOut CStr deviceFound,Hex address,13
        Inc nDevices
      EndIf
      HbStop			' send STOP
    Next
    You should get an ACK when you try to write an address byte to a slave device (first byte after a START). Trying to read isn't how to do it.
    You can skip the "odd addresses" since those are really just the read address.

  22. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  23. #12
    Prolific Poster
    Join Date
    Mar 2012
    Posts
    2,318
    Thumbs Up
    Received: 80
    Given: 182
    Total Downloaded
    5.30 GB

    0 Not allowed!

    Default Re: Req. I2C Scanner

    @Tumbleweed. Thanks for posting code. I had tried both PP4=0 and STATUS.0=0 (see post #9) but on a 18F25K50 at least both are always 0 and thus detect detect every possible address.
    George.

  24. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  25. #13
    Senior Member
    Join Date
    May 2011
    Posts
    506
    Thumbs Up
    Received: 59
    Given: 0
    Total Downloaded
    408.14 MB

    0 Not allowed!

    Default Re: Req. I2C Scanner

    Looks like you're right, George.
    I took a look at the asm and maybe I'm missing something but it looks like it's anding the result and losing the flag.

    Try this and see if it works any better:
    Code:
    symbol ACKSTAT 6
    nDevices = 0
    For address = 0 To 254 Step 2
      HbStart			' send START
      HBusOut address     ' send ADDR byte w/RW=0 (write)
      If SSP1CON2.ACKSTAT = 0 Then	' check ACK result
        HRSOut CStr deviceFound,Hex address,13
        Inc nDevices
      EndIf
      HbStop			' send STOP
    Next

  26. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  27. #14
    Prolific Poster
    Join Date
    Mar 2012
    Posts
    2,318
    Thumbs Up
    Received: 80
    Given: 182
    Total Downloaded
    5.30 GB

    0 Not allowed!

    Default Re: Req. I2C Scanner

    Yep that does the trick. While I have your interest, any idea how to detect Ack on bitbang I2C?
    George.

  28. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

  29. #15
    Senior Member
    Join Date
    May 2011
    Posts
    506
    Thumbs Up
    Received: 59
    Given: 0
    Total Downloaded
    408.14 MB

    0 Not allowed!

    Default Re: Req. I2C Scanner

    That one looks like it just might work using PP4.0 or STATUS.0 to check the ACK bit, so try:
    Code:
    ' bit-banged
    Dim PP4 As Byte System
    ndevices = 0
    For addr = 0 To 254 Step 2
      BStart			' send START
      BusOut addr     ' send ADDR byte w/RW=0 (write)
      If PP4.0 = 0 Then	' check ACK result (also in STATUS.0)
        Inc ndevices           ' device found
      EndIf
      BStop			' send STOP
    Next
    Don't have anything to test it on at the moment...

  30. Attention

    This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
    where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

    If you can, please report issues in the forum area WebSite / Forum Issues


    Thank you for your attention.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Epson Scanner for free
    By johngb in forum Wanted and For Sale
    Replies: 0
    Last Post: 31st January 2017, 11:44
  2. Fingerprint scanner
    By charliecoultas in forum The Lounge
    Replies: 5
    Last Post: 23rd March 2016, 10:36
  3. Req: LED ACTIVEX FOR VB2008
    By CharlieBrown in forum The Lounge
    Replies: 6
    Last Post: 28th November 2010, 11:30
  4. Replies: 23
    Last Post: 19th August 2009, 15:22
  5. 3.3V Regulator SD Power Req'ts???
    By ohararp in forum SWright's Feedback Forum
    Replies: 3
    Last Post: 4th May 2006, 15:53

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts