Bit bang I2C code


+ Reply to Thread
Results 1 to 10 of 10

Thread: Bit bang I2C code37 days old

  1. #1
    Prolific Poster RGV250's Avatar
    Join Date
    Aug 2005
    Posts
    2,271
    Thumbs Up
    Received: 33
    Given: 7
    Total Downloaded
    704.64 MB

    0 Not allowed!

    Default Bit bang I2C code

    Hi,
    The speed display on my lathe went pop so I am looking at using one of these to replace it.
    https://www.ebay.com/itm/5-Colors-0-...n-HHQovLJ4bC2g
    Searching google it seems to suggest it is similar to I2C but then it goes on that it does not use an address and the data is reversed so I think I need to find a simple bit bang I2C example to modify.

    I might use this as a project to see if it will work with a 10F device as I will only need 3 pins.

    Regards,
    Bob
    Last edited by RGV250; 19th May 2019 at 13:02.

  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 RGV250's Avatar
    Join Date
    Aug 2005
    Posts
    2,271
    Thumbs Up
    Received: 33
    Given: 7
    Total Downloaded
    704.64 MB

    0 Not allowed!

    Default Re: Bit bang I2C code

    Hi,
    I may not need it now as I have found a datasheet in English that has a simple example in C which I should be able to convert it.
    It would be nice to see a bit bashed example in PDS if someone has it though.

    Ragards,
    Bob

  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
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,229
    Thumbs Up
    Received: 372
    Given: 131
    Total Downloaded
    1.58 GB

    0 Not allowed!

    Default Software (Bit-Banged) I2C library for Proton

    Here is a standard I2C library that uses bit-bashing. i.e. Software. It can easily be changed if LSB is required instead of MSB for the I2C interface by changing the bit examined and the bit written in the I2C_ReadByte and I2C_WriteByte procedures:

    The library code is here. Make a file named "I2C_Software.inc" from it:

    Code:
    $ifndef _I2C_INC_
    $define _I2C_INC_
    '
    '   /\\\\\\\\\
    '  /\\\///////\\\
    '  \/\\\     \/\\\                                                 /\\\          /\\\
    '   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
    '    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
    '     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
    '      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
    '       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
    '        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
    '                                  Let's find out together what makes a PIC Tick!
    '
    ' Software I2C writing and reading routines
    ' Written for the Proton BASIC compiler by Les Johnson
    '
    ' Setup the default pins for SDA and SCL
    '
    $ifndef I2C_SDA_Pin
        $define I2C_SDA_Pin PORTC.4
    $endif
    
    $ifndef I2C_SCL_Pin
        $define I2C_SCL_Pin PORTC.3
    $endif
    
    '----------------------------------------------------------------------------------------------
    ' Delay a fixed number of microseconds
    '
    $define I2C_hDelay() DelayUS 2
    
    '----------------------------------------------------------------------------------------------
    ' Send a single byte to the I2C bus
    ' Input     : pData holds the byte to write
    ' Output    : None
    ' Notes     : MSB first
    '
    Proc I2C_WriteByte(pData As Byte)
    
        Dim bIndex As Byte = 8
        Repeat                              ' Create a loop for the 8-bits
            If pData.7 = 1 Then
                Input I2C_SDA_Pin           ' Make the SDA pin an Input (High)
            Else
                Output I2C_SDA_Pin          ' Make the SDA pin an Output (Low)
            EndIf
            I2C_hDelay()
            Input I2C_SCL_Pin               ' Make the SCL pin an Input (High)
            I2C_hDelay()
            Output I2C_SCL_Pin              ' Make the SCL pin an Output (Low)
            I2C_hDelay()
            pData = pData << 1
            Dec bIndex
        Until bIndex = 0
    '
    ' Close up
    '
        Input I2C_SDA_Pin                   ' Make the SDA pin an Input (High)
        Input I2C_SCL_Pin                   ' Make the SCL pin an Input (High)
        I2C_hDelay()
        Output I2C_SCL_Pin                  ' Make the SCL pin an Output (Low)
        I2C_hDelay()
    EndProc
    
    '----------------------------------------------------------------------------------------------
    ' Read a single byte from the I2C bus
    ' Input     : None
    ' Output    : Returns the byte received
    ' Notes     : MSB first, sample before clock
    '
    Proc I2C_ReadByte(), Byte
        
        Dim bIndex As Byte = 8
        Result = 0
        Input I2C_SDA_Pin                   ' Make the SDA pin an Input (High)
        Repeat                              ' Create a loop for the 8-bits
            Result = Result << 1
            '
            ' Wait for any clock stretching (This may benefit from a timeout being added)
            '
            Repeat                          ' Create a loop
                Input I2C_SCL_Pin           ' Make the SCL pin an Input
            Until I2C_SCL_Pin = 1           ' Until the SCL pin is high
            '
            ' Read the data
            '
            I2C_hDelay()
            Result.0 = I2C_SDA_Pin
            Output I2C_SCL_Pin              ' Make the SCL pin an Output (Low)
            I2C_hDelay()
            Dec bIndex
        Until bIndex = 0
    EndProc
    
    '----------------------------------------------------------------------------------------------
    ' Send an I2C bus start condition
    ' Input     : None
    ' Output    : None
    ' Notes     : A Start condition is high to low of SDA when SCL is high
    '
    Proc I2C_Start()
        Input I2C_SDA_Pin                   ' Make the SDA pin an Input (High)
        I2C_hDelay()
        Input I2C_SCL_Pin                   ' Make the SCL pin an Input (High)
        I2C_hDelay()
        Output I2C_SDA_Pin                  ' Make the SDA pin an Output (Low)
        I2C_hDelay()
        Output I2C_SCL_Pin                  ' Make the SCL pin an Output (Low)
        I2C_hDelay()
    EndProc
    
    '----------------------------------------------------------------------------------------------
    ' Send an I2C bus restart condition
    ' Input     : None
    ' Output    : None
    ' Notes     : None
    '
    $define I2C_Restart() I2C_Start()
    
    '----------------------------------------------------------------------------------------------
    ' Send an I2C bus stop condition
    ' Input     : None
    ' Output    : None
    ' Notes     : A Stop condition is low to high of SDA when SCL is high
    '
    Proc I2C_Stop()
        Output I2C_SDA_Pin                  ' Make the SDA pin an Output (Low)
        I2C_hDelay()
        Input I2C_SCL_Pin                   ' Make the SCL pin an Input (High)
        I2C_hDelay()
        Input I2C_SDA_Pin                   ' Make the SDA pin an Input (High)
        I2C_hDelay()
    EndProc
    
    '----------------------------------------------------------------------------------------------
    ' Initiate an I2C acknowledge
    ' Input     : None
    ' Output    : None
    ' Notes     : None
    '
    Proc I2C_Ack()
        I2C_hDelay()
        Output I2C_SDA_Pin                  ' Make the SDA pin an Output (Low)
        I2C_hDelay()
        Input I2C_SCL_Pin                   ' Make the SCL pin an Input (High)
        I2C_hDelay()
        Output I2C_SCL_Pin                  ' Make the SCL pin an Output (Low)
        I2C_hDelay()
        Input I2C_SDA_Pin                   ' Make the SDA pin an Input (High)
    EndProc
    
    '----------------------------------------------------------------------------------------------
    ' Initiate an I2C Not acknowledge
    ' Input     : None
    ' Output    : None
    ' Notes     : None
    '
    Proc I2C_NAck()
        Input I2C_SDA_Pin                   ' Make the SDA pin an Input (High)
        I2C_hDelay()
        Input I2C_SCL_Pin                   ' Make the SCL pin an Input (High)
        I2C_hDelay()
        Output I2C_SCL_Pin                  ' Make the SCL pin an Output (Low)
        I2C_hDelay()
        Input I2C_SDA_Pin                   ' Make the SDA pin an Input (High)
    EndProc
    
    '----------------------------------------------------------------------------------------------
    ' Startup section
    '
    _I2C_Main_:
        Low I2C_SDA_Pin                   ' Make the I2C SDA pin an output low
        Low I2C_SCL_Pin                   ' Make the I2C SCL pin an output low
    
    $endif
    Here's a demo runing the above library to write and read to and from an I2C eeprom:
    Code:
    '
    '   /\\\\\\\\\
    '  /\\\///////\\\
    '  \/\\\     \/\\\                                                 /\\\          /\\\
    '   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
    '    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
    '     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
    '      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
    '       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
    '        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
    '                                  Let's find out together what makes a PIC Tick!
    '
    ' Write and read an I2C eeprom using the software I2C library routines
    ' Written for the Proton BASIC compiler by Les Johnson
    '    
        Include "Amicus18.inc"                  ' Setup the program to run on an Amicus18 board(PIC18F25K20 at 64MHz)
    
    $define I2C_SDA_Pin PORTC.4                 ' Set the SDA pin for the I2C library
    $define I2C_SCL_Pin PORTC.3                 ' Set the SCL pin for the I2C library
        Include "I2C_Software.inc"              ' Load the software I2C library routines
    '
    ' Create variables for the demo
    '
        Dim MyLoop As Byte
        Dim MyByte As Byte
        
    '---------------------------------------------------------------------------------------------
    Main:  
    '
    ' Transmit bytes to the I2C eeprom
    '
        I2C_Start()                             ' Send a Start condition
        I2C_WriteByte($A0)                      ' Target an eeprom, and send a Write command
        I2C_WriteByte(0)                        ' Send the High Byte of the address
        I2C_WriteByte(0)                        ' Send the Low Byte of the address
        For MyLoop = 48 To 58                   ' Create a loop containing ASCII 0 to 9
          I2C_WriteByte(MyLoop)                 ' Send the value of Loop to the eeprom
        Next                                    ' Close the loop
        I2C_Stop()                              ' Send a Stop condition
        DelayMS 10                              ' Wait for the data to be entered into eeprom matrix
    '
    ' Receive bytes from the I2C eeprom and display them on a serial terminal
    '
        I2C_Start()                             ' Send a Start condition
        I2C_WriteByte($A0)                      ' Target an eeprom, and send a Write command
        I2C_WriteByte(0)                        ' Send the High Byte of the address
        I2C_WriteByte(0)                        ' Send the Low Byte of the address
        I2C_Restart()                           ' Send a Restart condition
        I2C_WriteByte($A1)                      ' Target an eeprom, and send a Read command
        For MyLoop = 0 To 9                     ' Create a loop
            MyByte = I2C_ReadByte()             ' Load MyByte with byte received
            If MyLoop = 9 Then                  ' Are we at the end of the reads?
                I2C_Stop()                      ' Yes. So send a stop command
            Else                                ' Otherwise...
                I2C_Ack()                       ' Send an Ack command
            EndIf
            HRSOutLn Dec MyByte                 ' Display MyByte on the serial terminal
        Next                                    ' Close the loop
    Hopefully, the above routines show how flexible and useful Procedures are in the Proton compiler. Because they are not mandatory as in other procedural languages, where everything has to be a procedure, but can be used with the standard BASIC syntax and commands, so they offer a whole load more flexability. This makes code far more easy to read and write, unlike the vast majority of procedure only languages.
    Last edited by top204; 21st May 2019 at 11:26.

  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 RGV250's Avatar
    Join Date
    Aug 2005
    Posts
    2,271
    Thumbs Up
    Received: 33
    Given: 7
    Total Downloaded
    704.64 MB

    0 Not allowed!

    Default Re: Bit bang I2C code

    Thanks Les,
    I think I have done what I need to do as I found a good English datasheet, trying to reverse engineer the Arduino library was a nightmare.
    It will be good to study your code anyway to see if I can improve on what I have done.
    I am just waiting for the module to arrive which is should be Thursday.

    Regards,
    Bob

  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
    Prolific Poster RGV250's Avatar
    Join Date
    Aug 2005
    Posts
    2,271
    Thumbs Up
    Received: 33
    Given: 7
    Total Downloaded
    704.64 MB

    0 Not allowed!

    Default Re: Bit bang I2C code

    Hi Les,
    Thanks for posting that, it has been very helpful. I did not know about the input / output command which is neater and easier than TRIS. Also I did not realise you could use the pData in the procedure directly, I had been creating a word in the procedure and then aliasing pData to the new word. It is only a small program but saves space so I expect it to save lots on larger programs.

    One off topic question, did you use a program to create the Rosetta text or was it lots of trial and error.

    Regards,
    Bob

  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
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,229
    Thumbs Up
    Received: 372
    Given: 131
    Total Downloaded
    1.58 GB

    0 Not allowed!

    Default Re: Bit bang I2C code

    Once you get used to procedures Bob you will find code even easier to write with Proton because they are not mandatory, as in other languages, but a benefit to the standard Proton language and can be used or not used. :-)

    There was an online site that allowed different forms of text in ASCII. Unfortunately, I can't remember the site, and I NEVER keep history from browsers etc... Once the browser is closed, everything is automatically erased. i.e. All cookies, history, cache, passwords etc...

  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
    Prolific Poster RGV250's Avatar
    Join Date
    Aug 2005
    Posts
    2,271
    Thumbs Up
    Received: 33
    Given: 7
    Total Downloaded
    704.64 MB

    0 Not allowed!

    Default Re: Bit bang I2C code

    Thanks Les,
    I found this https://www.kammerl.de/ascii/AsciiSignature.php and used s-relief

    Code:
    '____/\\\\\\\\\_________/\\\\\\\\\\\\__/\\\________/\\\____/\\\\\\\\\_______/\\\\\\\\\\\\\\\______/\\\\\\\____        
    ' __/\\\///////\\\_____/\\\//////////__\/\\\_______\/\\\__/\\\///////\\\____\/\\\///////////_____/\\\/////\\\__       
    '  _\/\\\_____\/\\\____/\\\_____________\//\\\______/\\\__\///______\//\\\___\/\\\_______________/\\\____\//\\\_      
    '   _\/\\\\\\\\\\\/____\/\\\____/\\\\\\\__\//\\\____/\\\_____________/\\\/____\/\\\\\\\\\\\\_____\/\\\_____\/\\\_     
    '    _\/\\\//////\\\____\/\\\___\/////\\\___\//\\\__/\\\___________/\\\//______\////////////\\\___\/\\\_____\/\\\_    
    '     _\/\\\____\//\\\___\/\\\_______\/\\\____\//\\\/\\\_________/\\\//____________________\//\\\__\/\\\_____\/\\\_   
    '      _\/\\\_____\//\\\__\/\\\_______\/\\\_____\//\\\\\________/\\\/____________/\\\________\/\\\__\//\\\____/\\\__  
    '       _\/\\\______\//\\\_\//\\\\\\\\\\\\/_______\//\\\________/\\\\\\\\\\\\\\\_\//\\\\\\\\\\\\\/____\///\\\\\\\/___ 
    '        _\///________\///___\////////////__________\///________\///////////////___\/////////////________\///////_____
    Bob
    Last edited by RGV250; 23rd May 2019 at 22:02.

  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
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,229
    Thumbs Up
    Received: 372
    Given: 131
    Total Downloaded
    1.58 GB

    0 Not allowed!

    Default Re: Bit bang I2C code

    You are welcome....

    How come your text in teh code window lines up? I set my font to Courier New when I write the code and it looks exellent when writing and editing it, but as soon as it is published, it turns to rubbish and the code does not line up and teh font is not Courier New anymore! I'm using standard Firefox and no add ons etc...

  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 RGV250's Avatar
    Join Date
    Aug 2005
    Posts
    2,271
    Thumbs Up
    Received: 33
    Given: 7
    Total Downloaded
    704.64 MB

    0 Not allowed!

    Default Re: Bit bang I2C code

    Hi,
    I had the same problem, in the end I pasted it into PDS and added the ' to the start of the lines and then used advanced and posted as code here.

    Bob

  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 towlerg's Avatar
    Join Date
    Mar 2012
    Posts
    2,040
    Thumbs Up
    Received: 188
    Given: 169
    Total Downloaded
    4.36 GB

    1 Not allowed!

    Default Re: Bit bang I2C code



    It's as if the font used in the post compose screen is monospace but the font used in the body of the board is not. This text should be monospaced ......

    Edit. and it is so whats going wrong?


    Last edited by towlerg; 24th May 2019 at 13:37.
    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.

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Conversion of I2C code from Firewing to Proton
    By Tim in forum Proton Plus Compiler v3
    Replies: 0
    Last Post: 12th July 2018, 16:05
  2. MPL115A2 I2C code problem
    By RossMcLennan in forum Absolute Beginners Section
    Replies: 4
    Last Post: 21st April 2014, 21:45
  3. I2C code query for LCD
    By Paul Thompson in forum Absolute Beginners Section
    Replies: 2
    Last Post: 28th August 2013, 22:33
  4. uALFAT I2C code examples/macros
    By richardy in forum ALFAT
    Replies: 1
    Last Post: 24th September 2008, 19:04
  5. Same code, different I2C addresses
    By Foober in forum Proton Plus Compiler v3
    Replies: 2
    Last Post: 6th February 2008, 17:02

Members who have read this thread : 85

Actions :  (Set Date)

You do not have permission to view the list of names.

Posting Permissions

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