Shop OBEX P1 Docs P2 Docs Learn Events
i2c usage on the Propeller — Parallax Forums

i2c usage on the Propeller

CumQuaTCumQuaT Posts: 156
edited 2011-08-13 16:39 in Propeller 1
Hi all,

Does anyone know of a good place to learn the basics of interacting with an i2c device? I'd like to use one of the LiPo Fuel Gauges and RTC Modules from Sparkfun with my Prop, but I don't know the first thing about reading and writing i2c, or how it all works. Can anyone give me a push in the right direction as I'd very much like to learn.

Thanks in advance.

Comments

  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2011-08-13 05:04
    Basics are very different for different people. Maybe it's just because I am very familiar with I2C and it's history that it seems very basic to me but really there is nothing really complicated about it. If you are familiar with shift registers then you understand clocked serial transfers, that is data that is clocked one bit (usually) at a time and latched by the receiver on a clock edge (in this case high). SPI does this to but it needs a chip select for every device whereas I2C is a little clever and defines two conditions that don't occur in a clocked transfer to define the START condition and a STOP condition (data line going high or low while the clock is high). Once an I2C device sees a START it will reset it's serial reception and the first byte that is clocked in will always be the device address that the master is "selecting". Mind you, all devices on the I2C bus will see the START and the address but only the device whose hardwired address matches will acknowledge (more later) and proceed from there.

    Maybe if you tell us what you are familiar with and we can explain I2C in relation to what you know rather than what we know or assume you know.
  • CumQuaTCumQuaT Posts: 156
    edited 2011-08-13 06:50
    I'm familiar with sending and receiving data over serial, and that's about the closest I've come. I've never used shift registers before...

    I don't even know where to begin with i2c... I have a LiPo fuel gauge from Sparkfun, but I don't know where to begin when it comes to getting the information out of it. Doing some reading I've found things about needing addresses and reading pages and so on and so forth, and none of it really makes sense... In serial a device simply broadcasts and receives information upon request, but i2c seems to be more complicated.

    So when it comes to what I mean by "basics" i really mean "from the start".
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-08-13 06:56
    I suggest you start with the Basic_I2C_Driver in the OBEX. It is well documented, and it will allow you to test with the EEPROM that's already connected to the Prop.
  • CumQuaTCumQuaT Posts: 156
    edited 2011-08-13 07:02
    Hi Dave,

    Thanks for that, but I've given that a go and it makes no sense to me. It goes into addresses and such and I don't know what address to use for the things I am connecting to...
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2011-08-13 07:34
    CumQuaT wrote: »
    I'm familiar with sending and receiving data over serial, and that's about the closest I've come. I've never used shift registers before...

    I don't even know where to begin with i2c... I have a LiPo fuel gauge from Sparkfun, but I don't know where to begin when it comes to getting the information out of it. Doing some reading I've found things about needing addresses and reading pages and so on and so forth, and none of it really makes sense... In serial a device simply broadcasts and receives information upon request, but i2c seems to be more complicated.

    So when it comes to what I mean by "basics" i really mean "from the start".

    Well you're familiar with sending data over serial by which I take it you mean "asynchronous" serial, that is in this case meaning that no clock is used between the two ends although clocks are still required.

    Now this may be long winded but let's see if you grasp any of it first.

    Shift registers are just a chain of flip-flops registers that remember the data bit that was at it's input when the clock went from low to high. So it's a one bit memory. The trick is that these flip-flops output that bit only when the clock goes from high to low. So you see if you chain the output of the first one into another one and so on and then join the clock lines together the bit on the very first input will seem to shift across from register to register. If you have eight of these cascaded together then if you input a new bit of data every time the clock went from low to high then you could store 8 bits of data in this "shift register".

    If you can preload each individual register with 8 bits then apply 8 clock pulse you will see each bit appear on the output of the last stage bit by bit with every pulse. This is basically the way we can transmit serial data and as explained previously also the way we receive serial data.

    The serial stuff that you are familiar with is much more complicated as it needs to reconstruct the exact same clock at the receive end just by knowing the speed and synchronizing with the data bits.

    I2C just sends out packets of 8 bits (bytes) of data strung together but preceded by a simple START condition where normally both the clock and data lines are idling high and when the data goes from high to low while the clock is high then this is interpreted as a start condition. Normally the data line remains stable while the clock is high. So the very first 8 bits after the START happens to be a device address which selects a particular chip. There may be more bytes of data which this device will accept which in the case of EEPROMs would include a memory address and data because.....it's a memory chip.


    Confusing? soak it up and then ask some questions and we can fine tune it.

    BTW. you could simulate the I2C transmission just by using 2 push-buttons and two LEDs on the I2C line. It would be slow but you would see what was happening every time you push a button.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-08-13 07:38
    You might have a look at Nuts and Volts Column #85. It's written for the BS2 Stamp, but it's mostly an introduction to the I2C protocol
  • CumQuaTCumQuaT Posts: 156
    edited 2011-08-13 07:50
    Fantastic references and info guys. I'll have a good long play around tomorrow (it's quite late here) and see if I can figure it out from this. Great stuff! Thanks!
  • JonnyMacJonnyMac Posts: 9,208
    edited 2011-08-13 08:05
    As I point out in the article, it's easier than you think. As already suggested, an EEPROM (you have on with any Propeller board) is a great place to start. Most I2C data sheets have clear protocol diagrams so you can see what's going on.
  • CumQuaTCumQuaT Posts: 156
    edited 2011-08-13 16:39
    Brilliant :D cheers!
Sign In or Register to comment.