This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Software I2C in slave mode

I want to simulate an I2C slave device using a generic 8051 MPU which doesn't have hardware I2C(not like 80C552). I know it is easy to implement software simulated I2C in master mode, but I want to turn my 8051 MPU to be a slave device. How can?

Parents
  • I think you have forgotten an important thing
    in the IIC protocol, slaves can be very slow and in fact they can keep the incoming clock signal low for a time to slow the master. But all software master implementations i've seen are not checking the clock state .
    for the slave, it is quite simple in the very first instant detecting the clock ( using interrupt ) to fix the clock state for a while until bit treatment is done.
    I hope this can help.

Reply
  • I think you have forgotten an important thing
    in the IIC protocol, slaves can be very slow and in fact they can keep the incoming clock signal low for a time to slow the master. But all software master implementations i've seen are not checking the clock state .
    for the slave, it is quite simple in the very first instant detecting the clock ( using interrupt ) to fix the clock state for a while until bit treatment is done.
    I hope this can help.

Children
  • in the IIC protocol, slaves can be very slow and in fact they can keep the incoming clock signal low for a time to slow the master

    How wonderful, now we slow the slave and slow the master. Of course if we really do not need to do anything else such as gathering information to transmit?

    Of course, if the master has gasp interrupt driven hardware IIC only the slave will come to a crawl.

    So many .....s see "slowing down" as a solution to a problem, this is ridiculous.

    When we were using the 10MHz PCs many things ran faster than they do today when the so called "programmers" have no concept of time.


    Erik

  • Hi Cal,

    Just clarify what you ment by "however the data received is DEFINITELY not being sent.
    I think the problem may be timing between the transmitting and receiving or the my method of receiving the data."

    If your slave is triggering on the START condition then from the I2C protocol "Data on the SDA pin may change only during SCL low time periods". So if you look at the following it should hopefully help:-

    jb SCL,$ ;get past "START" condition
    
    REPT 8   ;repeat block get all 8 bits of data
    jnb SCL,$ ;wait for clock to go high
    mov c,SDA ;read data transmitted MSB first
    rrc a     ;and save it
    ENDM
    

    This code should be fast enough to work at 400KHz, I have written a transmitter and had to insert nop's to slow it down.

    Note that above i havent bothered with the 9th bit - the ACK/NACK which you may want to consider and also from the I2C protocol "Data changes during SCL high periods will indicate a start or stop condition" so you will have to add extra code to decide wether it is then next byte or the end of the message if that is important to you.

    Mark.

  • This was only a suggestion to keep in sync with quite slow software peripherals ( with a few dedicated IIC hardware " bit level " or even no dedicated hardware at all ) and with work to do before replying.
    Hardware for master take this extended clock as mandatory, but keep in mind that software masters have to test it. This protocol has not been developped for nothing.