Project

General

Profile

MSP430USCIB I2C

The USCI_B module on MSP430 devices can support I2C up to 400kHz.

Initialization for master operation

First, assign the SDA and SCL pins to the USCI_B module:
P1SEL |= SDA + SCL;
P1SEL2 |= SDA + SCL;
Either replace or redefine macros SDA and SCL with the appropriate pin numbers according to the device datasheet.

Next, set the clock prescaler to the desired SCL frequency, and set the clock line (SMCLK is used here):
//clk_div stores prescaler value from datasheet
UCB0BR1 = (unsigned char)(clk_div >> 8);
UCB0BR0 = (unsigned char)clk_div;
UCB0CTL1 = UCSSEL_2;//SMCLK for clock line

Now, set the USCI_B to be an I2C master, and optionally assign an address (in case master loses arbitration):
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC;
UCB0I2COA = addr;

Finally, enable the USCI_B module by clearing the reset bit, and assign pins to the USCI module
UCB0CTL1 &= ~UCSWRST;
P1SEL |= SDA + SCL;
P1SEL2 |= SDA + SCL;
SDA and SCL are macros for the device-specific pins that correspond to SDA and SCL. You must define these at the top of the file.

Master transmit mode

Load the slave address:
UCB0I2CSA = addr;

Master receive mode

Slave transmit mode

Slave receive mode