This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

msp430g2553 reading lidar lite v2 via i2c problem




hello, i am trying to make the lidar lite v2 to measure distance. the problem is that the register 0x10 and 0x0f keep reading zeros back. I wrote to pulsed light 3d and they said that its usually because the read function is in repeated read mode and the sensor needs it to be in read stop read.

any help thanks



#include "msp430g2553.h"


#define i2cAddress 0x62 // i2c address of slave -- LiDAR lite V2


// I2C PIN Definition
#define SDA_PIN BIT7 //P1.7 (SDA)
#define SCL_PIN BIT6 //P1.6 (SCL)


void I2C_Write(unsigned char slave_address, unsigned char register_address, char * data, unsigned char DataLength );
void I2C_Read(unsigned char slave_address, unsigned char register_address, char * data, unsigned char DataLength );
void I2C_Init(void);

void main(void)
{
char buf[2]={0}; //i2c received data buffer - can receive up to 4 bytes
char txdata[1]={0x04};// data to be tramnsmitted to i2c slave - potentially up to 4 bytes
char Distance;
int j;
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
BCSCTL1 = CALBC1_16MHZ; //set DCO to 16Mhz
DCOCTL = CALDCO_16MHZ; // make sure to update the Delay_ms function if the DCO is changed
//Port initialisation
P1DIR= 0x0f;
P1OUT=0x01; // P1.0 output (Launchpad LED)

P2DIR=0xff;
P2IE=0x00;
P2SEL=0x00;
P2OUT=0x00;
//__delay_cycles(16*2000000);// 2s startup delay (@16MHz)


I2C_Init(); // configure i2c
__delay_cycles(16*2000000);// 2s startup delay (@16MHz)

txdata[0]=0x04;
I2C_Write(i2cAddress,0x00,txdata,1);
__delay_cycles(20000); // 20 mS delay
while(1)
{
j=0;


for(j=0;j<=255;j++)
{

I2C_Read(i2cAddress,0x0f,buf,1);
__delay_cycles(1); // 1 mS delay to prevent overpolling
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
I2C_Read(i2cAddress,0x10,buf,1);
__delay_cycles(1); // 1 mS delay to prevent overpolling
while (UCB0CTL1 & UCTXSTP); // Ensure stop condition got sent
Distance = buf[0]+buf[1];
if(Distance == 0)
{
P1OUT = 0x02;
}
else P1OUT= 0x04;
}

} // end of while
}// end of main

void I2C_Init(void)
{
//USCI_I2C_Init
P1SEL |= SDA_PIN + SCL_PIN; // Assign I2C pins to USCI_B0
P1SEL2|= SDA_PIN + SCL_PIN;
UCB0CTL1 = UCSWRST; // Enable SW reset, HOLD USCB in a reset state
UCB0CTL0 = UCMST + UCMODE_3 + UCSYNC; // I2C Master, MODE 3 = I2C, synchronous mode
UCB0CTL1 = UCSSEL_2 + UCSWRST; // Use SMCLK, keep SW reset
UCB0BR0 = 0xff; // Set I2C master speed 1024 gives approx 16kHz clock at 16Mhz
UCB0BR1 = 0x06; // Set I2C master speed
UCB0CTL1 &= ~UCSWRST; // Clear SW reset, resume operation
}

void I2C_Read(unsigned char slave_address, unsigned char register_address, char * data, unsigned char DataLength )
{
int i = 0; //loop counter

while (UCB0STAT & UCBBUSY); //wait for USCI B0 bus to be inactive
UCB0I2CSA = slave_address; //set SLAVE address

UCB0CTL1 |= UCTR + UCTXSTT; //set USCI to be I2C TX, send start condition
UCB0TXBUF = register_address;
while (UCB0CTL1 & UCTXSTT); // waiting for slave address to transfer
while ((IFG2 & UCB0TXIFG) != UCB0TXIFG); //wait for TX IFG to clear


UCB0CTL1 &= ~UCTR; //set USCI to be RECEIVER
UCB0CTL1 |= UCTXSTT; //send restart
while (UCB0CTL1 & UCTXSTT); // wait until I2C STT is sent

for (i=0; i<DataLength; i++) //receive loop
{
if(DataLength==1)
{
UCB0CTL1 |= UCTXNACK; //generate a NACK
UCB0CTL1 |= UCTXSTP; //generate a stop condition
while ((IFG2 & UCB0RXIFG) != UCB0RXIFG); //wait for RX buffer to have data
data[i] = UCB0RXBUF; //Move rvcd data of or USCI buffer. This also clears the UCB0RXIFG flag
}
else
{
while ((IFG2 & UCB0RXIFG) != UCB0RXIFG); //wait for RX buffer to have data
data[i] = UCB0RXBUF; //Move rvcd data of or USCI buffer. This also clears the UCB0RXIFG flag

if (i == DataLength-2) //NACK and STOP must be send before the last byte is read from the buffer.
//if not the CPU will read an extra byte.
{
UCB0CTL1 |= UCTXNACK; //generate a NACK
UCB0CTL1 |= UCTXSTP; //generate a stop condition
}
}
}


}

void I2C_Write(unsigned char slave_address, unsigned char register_address, char * data, unsigned char DataLength )
{

int i;
__disable_interrupt(); //prevent interrupts from messing with the I2C functions
while (UCB0STAT & UCBBUSY); //wait for USCI B0 bus to be inactive
UCB0I2CSA = slave_address; //set SLAVE address


UCB0CTL1 |= UCTR + UCTXSTT; //set USCI to be I2C TX, send start condition
UCB0TXBUF = register_address; // register address

while (UCB0CTL1 & UCTXSTT); // wait for slave address to be transferred
while ((IFG2 & UCB0TXIFG) != UCB0TXIFG); //wait for TX IFG to clear


for (i=0; i <DataLength; i++)
{
UCB0TXBUF = data[i]; //send data.

while ((IFG2 & UCB0TXIFG) != UCB0TXIFG); //wait for TX IFG to clear

}

UCB0CTL1 |= UCTXSTP; // set I2C stop condition
while ((UCB0STAT & UCSTPIFG) == UCSTPIFG); //wait for Stop condition to be set

__delay_cycles(96000); // 6ms delay for internal write

__enable_interrupt();

}

**Attention** This is a public forum