MSP430I2CinDetail

Download Report

Transcript MSP430I2CinDetail

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;
while (!(IFG2 & UCB0TXIFG));
// I2C TX, start condition
// Wait while IFG is low
UCB0TXBUF = Byte1;
while (!(IFG2 & UCB0TXIFG));
UCB0TXBUF = Byte2;
while (!(IFG2 & UCB0TXIFG));
:
UCB0TXBUF = ByteN;
while (!(IFG2 & UCB0TXIFG));
//
//
//
//
// Load TX buffer
// Wait for IFG
UCB0CTL1 |= UCTXSTP;
IFG2 &= ~UCB0TXIFG;
// I2C stop condition
// Clear USCI_B0 TX int flag
while (UCB0CTL1 &
// Wait UCTXSTP to be clear (stop done)
UCTXSTP);
Load
Wait
Load
Wait
TX buffer (clears ifg)
for IFG
TX buffer
for IFG
As soon as transmit
starts, TXBUF is empty
Read multiple bytes
UCB0CTL1 &= ~UCTR;
UCB0CTL1 |= UCTXSTT;
// I2C RX, start condition
while (UCB0CTL1 &
// wait while UCTXSTT is clear
UCTXSTT);
for (i=0;i<N;i++) {
while (!(IFG2 & UCB0RXIFG));
inData[i] = UCB0RXBUF;
// Wait for byte to be available;
// 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)
The details of the device are
not that important to us.
The general method of using
I2C devices is important.
• 24 bits of GPIO
• Can generate interrupts on pin change
• I2C Address (+ LSB for R/W):
– 34 if ADDR=0
– 35 if ADDR=1
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
Write 0x55
Stop Condition
(register # on nunchuck)
(command to nunchuck)
• Read nunchuck data
– Start condition
– Read six bytes
– Stop condition
• Initiate new read
– Start condition
– Write 0x00
– Stop condition
(initiate new read)
• Loop to “Read nunchuck data”
Data format
• 6 Bytes
From: http://wiibrew.org/wiki/Wiimote/Extension_Controllers/Nunchuck