Tool/software:
Hi,
I'm currently trying to write a code that reads and writes to the APDS-9900, it's a device that measures the distance between sensor and objects. With my current code i try to preform a simple read operation that reads the ID of the chip just to if the I2C works, that is address 0x12 and i expect value 0x29 back.
But when i try to run it keeps stuck on the while (UCB0CTL1 & UCTXSTT); and locking through my osciloscoop it seems the last acknowedge clock is not send.
Any idea on how to fix that or what am i doing wrong?
Under the code functions that i use to read byte and word.
#include <Header/i2c.h>
#include <msp430.h>
#include <stdint.h>
void I2C_Init(void) {
// Set P1.6 and P1.7 as I2C pins
P1SEL |= BIT6 + BIT7;
P1SEL2 |= BIT6 + BIT7;
// Set up USCI_B0 for I2C master mode
UCB0CTL1 |= UCSWRST; // Enable software reset
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C master mode, synchronous mode
UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep reset
UCB0BR0 = 12; // Set bit rate (assuming 1MHz SMCLK, 100kHz I2C)
UCB0BR1 = 0;
UCB0CTL1 &= ~UCSWRST; // Clear reset to enable I2C
IFG2 &= ~(UCB0RXIFG + UCB0TXIFG); // Clear flags
IE2 |= UCB0RXIE + UCB0TXIE; // Enable RX and TX interrupts
}
unsigned char I2C_Read_Combined(unsigned char regAddr) {
UCB0I2CSA = 0x39; // Set APDS-9900 I2C address
UCB0CTL1 |= UCTR + UCTXSTT; // Set as transmitter, generate START condition
while (UCB0CTL1 & UCTXSTT); // Wait for START condition to complete
UCB0TXBUF = regAddr; // Send register address
while (!(IFG2 & UCB0TXIFG)); // Wait for transmission to complete
UCB0CTL1 &= ~UCTR; // Set as receiver
UCB0CTL1 |= UCTXSTT; // Generate repeated START condition
while (UCB0CTL1 & UCTXSTT); // Wait for START condition to complete
UCB0CTL1 |= UCTXSTP; // Generate STOP after next byte
while (!(IFG2 & UCB0RXIFG)); // Wait for data to be received
return UCB0RXBUF; // Return received data
}
unsigned int I2C_Read_Word(unsigned char regAddr) {
unsigned int data = 0;
UCB0I2CSA = 0x39; // Set APDS-9900 I2C address
UCB0CTL1 |= UCTR + UCTXSTT; // Set as transmitter, generate START
while (UCB0CTL1 & UCTXSTT); // Wait for START to complete
UCB0TXBUF = regAddr; // Send register address
while (!(IFG2 & UCB0TXIFG)); // Wait for transmission to complete
UCB0CTL1 &= ~UCTR; // Set as receiver
UCB0CTL1 |= UCTXSTT; // Generate repeated START
while (UCB0CTL1 & UCTXSTT); // Wait for START to complete
data = UCB0RXBUF; // Read first byte
data <<= 8; // Shift to high byte
while (!(IFG2 & UCB0RXIFG)); // Wait for second byte
data |= UCB0RXBUF; // Read second byte
UCB0CTL1 |= UCTXSTP; // Generate STOP
return data;
}
How the datasheet says the I2C protocol must work in order to comunicate with the APDS-9900.

My results using my osciloscoop. blue is SDA and yellow is SCL.
The last clock signal is not send as you can see.
