Shop OBEX P1 Docs P2 Docs Learn Events
1st I2C slave program — Parallax Forums

1st I2C slave program

AlessandroAlessandro Posts: 26
edited 2008-06-04 17:33 in General Discussion
Hi guys!
I-m trying to simulate a sensor with my SX28 and I2c.

The original sensor, stores the measure·of a distance on the location 42h, its I2c address is 01.

Now i'm tring to reading with another i2c master the momory of my SX28 slave with a number.

I-m using a code provide from Gunther (sorry for spelling), with out any result.

Can be done? i assert:

org $42
distance equ 40·· ;simulating distance

any other suggestion?

Alessandro

Comments

  • Guenther DaubachGuenther Daubach Posts: 1,321
    edited 2008-05-23 15:04
    Hi Alessandro,

    you have already asked me similar questions via eMail...

    Without knowing any details of the sensor you like to simulate with an SX acting as an I²C slave, I assume that the original sensor has a set of internal memory locations that can be read out, where one location with address 42h is holding the distance.

    Usually, such kind of I²C devices first expect the initialization for a write operation, even when you want to read the device, i.e. you send the device address with bit 0 clear (for write), followed by the internal device address (42h in this case). This simply sets the sensor's internal address pointer but without actually writing something else. Next, the master would generate another start condition (a "running start"), send the device address, this time with bit 0 set (for read). The sensor should acknowledge this, and then send out the data stored at 42h on the following SCL pulses generated by the master until the eight bits are done. The master may send an acknowledge, and continue reading subsequent locations, or send a no acknowledge, followed by a stop condition to terminate the read.

    All this is only guesswork as I don't have any information about what sensor you want to simulate.

    Anyway, simply saving some value in the SXes RAM at address $42 will not work at all.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Greetings from Germany,

    Günther
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2008-05-23 15:45
    http://www.nxp.com/acrobat_download/literature/9398/39340011.pdf

    http://www.nxp.com/acrobat_download/applicationnotes/AN10216_1.pdf

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Time·─ Space·─ Gravity·─ Darkness·─ Death
  • AlessandroAlessandro Posts: 26
    edited 2008-05-24 08:03
    I know exactly the working of my distance sensor, it works as you say! (GREAT!)

    This is a small beginig of a bigger project with I2c.

    the sensor is the LEGO Ultrasonic·sensor of LEGO MINDSTORM NXT http://shop.lego.com/ByCategory/Product.aspx?p=9846&cn=389&d=292

    I·hope·to Know·how·works the I2C, i have read the specifics.

    of this sensor i have the sheme, and the memory distribution.

    But what i have to modify in the APP25 of your book?

    I have done some test.... nothing·mad.gif

    i modify the i2c slave id as request by the lego spec, but i don't see the ACK coming from the SX.

    Inside there is a controller, as you say, i have the specifics also of this controller.

    SOME HELP?



    ·
  • AlessandroAlessandro Posts: 26
    edited 2008-05-28 14:56
    Hi again, so i tried to modify the program in the gunther's book,
    i don't see the·check·of the addres·of the slave·in the program code (ISR).

    So·my SX (slave)·doesn't reply "ACK" to the master neither for the first byte.

    Then what i have to do? additind another state to the state machine?

    Help me...

    Alessandro

    ·
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-05-28 16:00
    Attached you will find my I2C slave code. This is adapted from an original Ubicom source.
    It adds a few states, in that if the master wants more data (by sending ACK), this slave
    will stretch the SCL line until it has data ready. Furthermore it supports a broadcast ID
    (so this slave recognizes two ID's). I have used this code in a setup where a basic stamp
    as I2C master, communicates with 10 SX as I2C slaves.
    The I2C VP code is located in codepage_1.

    regards peter
  • AlessandroAlessandro Posts: 26
    edited 2008-06-03 10:43
    Hi G
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-06-04 14:22
    Alessandro,
    In reply to your private email:
    I know my code is working and I am sure so is Gunther's.

    You should approach this in the following order:
    1. Create a program for your master processor to read and write a serial eeprom.
    Only when you can reliable read/write the first 64 bytes (because an SX28 cannot hold much more)
    continue with step 2. Do not change the master program at this point.
    2. Create a program for the SX28 to emulate a serial eeprom of 64 bytes.
    Only when the master can reliable read/write the 64 bytes from/to the sx28
    continue with step 3.
    3. Adjust the master program to read·your sensor.
    It should be sufficient to alter the device ID and possible address byte.
    Only when the master can reliable read your sensor
    continue with step 4. Do not change the master program at this point.
    4. Alter the SX28 program to emulate your·sensor.
    It should be sufficient to alter the slave ID value and possible address byte reading.

    regards peter
  • John CoutureJohn Couture Posts: 370
    edited 2008-06-04 17:33
    Peter,

    Good advice. That's one of the most difficult tasks in the classroom is to convince students to first test a working program without changes. Then make very small incremental changes and save that as a version. Then when it breaks, just go back one version. I can't say I always do that but when I get stuck I remember my masters peering down at me with a wary eye.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    John J. Couture

    San Diego Miramar College
Sign In or Register to comment.