Generic i2c example?

Posted on
  • Module info:
    All I2C mode responses are in ASCII format however, they do not terminate with a
    rather, they terminate with a NULL. The Null termination makes data
    manipulation easier once it has been received.

    default address: 0x63


    I am looking for a generic i2c example. I didn't see a generic i2c example in the Tutorials and Examples. All I saw were examples under the Using I2C section. I then had a look in the I2C Class in the Reference. The i2c reference page sort of makes sense, but I am so used to serial that all this i2c bits and bytes confuses me.

    What confuses me is this: address 0x63 represents the ASCII decimal of 99. Besides 0x63 as an address what is that format also called? The reason why I ask is, I can change the address from 1 to 127. So, for an example, I send the ASCII decimal 25 to the module to change the address, what does 25 represent? Is there a ASCII decimal to whatever that format is converter? Another thing that I am unsure about is when I receive data in ASCII format from the module, how do I convert that to readable output?

    I basically need an i2c example of setup, read and write.

    so far I have:

    function onInit() {
         I2C1.setup({scl:b6, sda:b7});
    }
    onInit();
    
  • I2C addresses (like 0x63) are hexadecimal (base 16). You can use the number represented as base 10 or hexadecimal (or binary, ie, 0b01100011) interchangably.

    ie, 0x63 evaluates to 99 - you can do I2C1.sendTo(0x63,...) or I2C1.sendTo(99,...) - they're exactly the same. Which format you use matters only for the purposes of code readability. (in fact, if you use the code minification option, it converts all the hexadecimal values in your code to decimal, since that saves 1-2 bytes of code size per instance)

    I2C1.readFrom() returns an array of bytes. When you do I2C1.readFrom(99,10), that will request 10 bytes from the device, returning them as an array of 10 bytes. It's up to you to turn that array into something meaningful. A "null" is probably a byte with a value of 0.

    You can find a lot of examples of using I2C in the source code for the modules ( http://espruino.com/modules ). Also, have you read the reference, or just the tutorial? The reference ( http://espruino.com/Reference ) is a great resource, and I think it would have answered at least the question about the format data is returned

    As to why one would use it, it's because it's easy to convert between binary and hexadecimal in your head. It's a real help when you're writing code to control a device where you need to send one byte where each bit sets some feature.

  • @DrAzzy Thank you! i2c isn't difficult after all. One more question, I will eventually have multiple modules hooked up to the same i2c bus. Will each module require its own pullup resister?

  • @DrAzzy thanks!

    @d0773d you just need one pullup resistor per wire, and you can have several I2C devices on the same wires with just the 2 resistors.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Generic i2c example?

Posted by Avatar for d0773d @d0773d

Actions