Dear Forum Members,
I am trying to interface LSM303DLH carrier board with MSP430. My problem is when I read the accelerometer, it reads all zero values. My code is able to detect the presence of LSM303 over I2C with address 0x18.
I have attached a part of my code below:
void write_ACC_REG(unsigned char byteCount, unsigned char *field)
{
TI_USCI_I2C_transmitinit(0x18,0x12); // prescaler generates I2C of 100KHz
while( TI_USCI_I2C_notready() );
if(TI_USCI_I2C_slave_present(0x18)) // check slave address
{TI_USCI_I2C_transmit(byteCount,field);}
}
char read_ACC(unsigned char count1)
{unsigned char val1;
unsigned char store1[1] = {'j'};
TI_USCI_I2C_receiveinit(0x18,0x12);
while( TI_USCI_I2C_notready() );
TI_USCI_I2C_receive(count1,store1);
while( TI_USCI_I2C_notready() ); // wait till bus is free before leaving this function
val1 = store1[0];
return val1;
}
unsigned char initial1_ACC[2] = {LSM303DLH_CTRL_REG1_A, 0x27};
unsigned char initial2_ACC[2] = {LSM303DLH_CTRL_REG4_A, 0x40};
unsigned char reg_DATA_ACC[1] = {(LSM303DLH_OUT_X_L_A | 0x80)};
void main(void){
unsigned char X_H_A, X_L_A, Y_H_A, Y_L_A, Z_H_A, Z_L_A;
write_ACC_REG(2,initial1_ACC); // enable ACC
write_ACC_REG(2,initial2_ACC); // REG4_A
for (;;){
write_ACC_REG(1,reg_DATA_ACC);
_delay_cycles(200000);
X_L_A = read_ACC(1);
X_H_A = read_ACC(1);
Y_L_A = read_ACC(1);
Y_H_A = read_ACC(1);
Z_L_A = read_ACC(1);
Z_H_A = read_ACC(1);
send2pc(X_L_A); // this function transmits data over RS232 link to PC
_delay_cycles(1000);
send2pc(X_H_A);
_delay_cycles(1000);
send2pc(Y_L_A);
_delay_cycles(1000);
send2pc(Y_H_A);
_delay_cycles(1000);
send2pc(Z_L_A);
_delay_cycles(1000);
send2pc(Z_H_A);
_delay_cycles(1000);
}
Can anyone please tell me where am I going wrong? Am I missing something?
I will highly appreciate any help to resolve this problem.
Regards,
Nick