I2C in Detail
I2C in Detail. I2C Block Diagram. Key for diagrams. Write multiple bytes. UCBxTXIFG is set when START condition is generated and first data can be written to UCBxTXBUF . UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition
I2C in Detail
E N D
Presentation Transcript
Write multiple bytes UCBxTXIFG is set when STARTcondition is generated and first data can be written to UCBxTXBUF. UCB0CTL1 |= UCTR + UCTXSTT; // I2C TX, start condition while (!(IFG2 & UCB0TXIFG)); // Wait while IFG is low UCB0TXBUF = Byte1; // Load TX buffer (clears ifg) while (!(IFG2 & UCB0TXIFG)); // Wait for IFG UCB0TXBUF = Byte2; // Load TX buffer while (!(IFG2 & UCB0TXIFG)); // Wait for IFG : UCB0TXBUF = ByteN; // Load TX buffer while (!(IFG2 & UCB0TXIFG)); // Wait for IFG UCB0CTL1 |= UCTXSTP; // I2C stop condition IFG2 &= ~UCB0TXIFG; // Clear USCI_B0 TX intflag while (UCB0CTL1 & UCTXSTP); // Wait UCTXSTP to be clear (stop done) As soon as transmit starts, TXBUF is empty
Read multiple bytes UCB0CTL1 &= ~UCTR; UCB0CTL1 |= UCTXSTT; // I2C RX, start condition while (UCB0CTL1 & UCTXSTT); // wait while UCTXSTT is clear for (i=0;i<N;i++) { while (!(IFG2 & UCB0RXIFG)); // Wait for byte to be available; inData[i] = UCB0RXBUF; // Read data (also clears IFG) if (i==(N-2)) UCB0CTL1 |= UCTXSTP; // I2C stop condition } while (UCB0CTL1 & UCTXSTP); // Wait for UCTXSTP to be clear (stop done)
Typical I2C peripheral (TCA6424 port expander) • 24 bits of GPIO • Can generate interrupts on pin change • I2C Address (+ LSB for R/W): • 34 if ADDR=0 • 35 if ADDR=1 The details of the device are not that important to us.The general method of using I2C devices is important.
Write Data AI = Auto Increment
Read data Must write the command byte, and then switch to read mode to get the data. Note that R/W bit changes
Wii Nunchuck • x-y potentiometer • 3 axis accelerometer • 2 buttons • I2C (address = 0x52 = 82)
Nunchuck with I2C • Initialize: • Start condition • Write 0xF0 (register # on nunchuck) • Write 0x55 (command to nunchuck) • Stop Condition • Read nunchuck data • Start condition • Read six bytes • Stop condition • Initiate new read • Start condition • Write 0x00 (initiate new read) • Stop condition • Loop to “Read nunchuck data”
Data format • 6 Bytes From: http://wiibrew.org/wiki/Wiimote/Extension_Controllers/Nunchuck