Other Parts Discussed in Thread: OPT4001EVM
I have been trying to interface the MSP430FR4133 launchpad with an OPT4001EVM board. I am attempting to have them communicate over I2C with each other. I am having issues understanding how to write data over I2C, however. I currently have the following code for setting up I2C which I found on Resource Explorer.
WDTCTL = WDTPW | WDTHOLD;
// Configure GPIO
P1OUT &= ~BIT0; // Clear P1.0 output latch
P1DIR |= BIT0; // For LED
P5SEL0 |= BIT2 | BIT3; // I2C pins
// Disable the GPIO power-on default high-impedance mode to activate
// previously configured port settings
PM5CTL0 &= ~LOCKLPM5;
// Configure USCI_B0 for I2C mode
UCB0CTLW0 |= UCSWRST; // Software reset enabled
UCB0CTLW0 |= UCMODE_3 | UCMST | UCSYNC; // I2C mode, Master mode, sync
UCB0CTLW1 |= UCASTP_2; // Automatic stop generated
// after UCB0TBCNT is reached
UCB0BRW = 0x0008; // baudrate = SMCLK / 8
UCB0TBCNT = 3; // number of bytes to be received
UCB0I2CSA = 0b1000101; // Slave address
UCB0CTL1 &= ~UCSWRST;
UCB0IE |= UCRXIE | UCNACKIE | UCBCNTIE;
From what I've gathered, after setting it up, you perform a write using the code below to access the device at the slave address stored above.
UCB0CTL1 |= UCTR; //put into tx mode
UCB0CTL1 |= UCTXSTT; //start
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
UCB0CTL1 |= UCTXSTT; // I2C start condition
When connecting a logic analyzer, I can see that I send this address over I2C and it is acknowledged by the device.
I am unsure on the next part, where I am trying to write to the device at the slave addressed stored above. I have seen that you need to load the Tx Buffer (UCB0TXBUF) and then send a write using the instructions above. I have tried loading the Tx Buffer both inside the main loop, and in the interrupt vector USCI_I2C_UCTXIFG0 but I can only ever get 0xFF to be sent over the data line no matter what I load into the buffer.
Am I loading the buffer incorrectly, or is there a different way to write data over I2C that I don't know of?