3
\$\begingroup\$

I have a a CPLD (Lattice MachXO2) that echos a signal from an Arduino to turn on an LED.

Arduino:

//send out .1s pulse on output pin 2 when a 'q' is recieved
void loop () {
  USBinByte = Serial.read();
   if (USBinByte == 'q') {
     digitalWrite(ledPin, HIGH);
     delay (100);
     digitalWrite(ledPin, LOW);
   } 
}

CPLD:

//When LEDin goes high toggle LED
module toggleLED (
 LED,
 LEDin);
    output LED;
    input LEDin;
    reg LED;

    assign led_next = ~LED;
    always @(posedge LEDin) begin
        LED = led_next;
    end
endmodule

How do I setup I2C on both chips to be able to load an 8 bit register on the CPLD and also read the register, so I can set up multiple LEDs? I've done I2C between three Arduinos before using the WIre library. I2C is required for a project I'm working on.

\$\endgroup\$
1
  • \$\begingroup\$ Is there a specific reason you want I2C? SPI is easier to implement. \$\endgroup\$
    – Saad
    Jul 9, 2012 at 17:50

2 Answers 2

7
\$\begingroup\$

You'll have to begin with detecting the start condition: SDA going from high to low while SCL is high. This should reset your I2C logic.

enter image description here

Then shift in 8 bits (count them) and after the eighth bit is received compare the address with the CPLD's address (the first 7 bits). If they match pull SDA low until the next clock pulse. Don't forget that this should be an open-drain output!

The eighth received bit was the \$\mathrm{R/ \overline{W}}\$ bit, but you probably only want to write, then either ignore it, or don't acknowledge if the bit is set (indicating a read operation).

If the address matches wait for the next byte. Again shift in 8 bits, and acknowledge after the eighth.

enter image description here

This should complete a transmission, and when you detect a stop condition (SDA going high while SCL is high) latch the eight received data bits.

Further reading
I2C specification

\$\endgroup\$
4
\$\begingroup\$

Steven provides a nice overview of the I2C protocol. I have worked with the protocol before; designing a spec compliant implementation would be more difficult than designing a SPI shift register, as Saad commented above.

If you do need to use I2C, and do not want to develop your own implementation, you could to use or examine an existing (open source) implementation.

For example, you could check out this I2C Slave project at opencores.org

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.