Quantcast
Channel: Microcontrollers
Viewing all articles
Browse latest Browse all 232836

Forum Post: MSP432P401R: I2C with LSM303DLHC

$
0
0
Part Number: MSP432P401R Hello, I'm very new to using I2C protocol, and I was hoping for some help using it to get a digital compass/magnetometer (LSM303DLHC) working with my MSP432 board. I'm using the driverlib library currently. Based on the spec sheet that I found for this compass, I am supposed to follow the following protocol: From what I've gathered so far, here's what I've done in my code: #include "msp.h" #include "driverlib.h" #include "stdio.h" #include "stdbool.h" const eUSCI_I2C_MasterConfig i2cConfig = { EUSCI_B_I2C_CLOCKSOURCE_SMCLK, // SMCLK Clock Source 3000000, // SMCLK = 3MHz EUSCI_B_I2C_SET_DATA_RATE_100KBPS, // Desired I2C Clock of 100khz 0, // No byte counter threshold EUSCI_B_I2C_NO_AUTO_STOP // No Autostop }; /** * main.c */ void main(void) { WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD; // stop watchdog timer // Set DCO frequency and using the sub-systems master clock source: unsigned int dcoFrequency = 3E+6; CS_setDCOFrequency(dcoFrequency); CS_initClockSignal(CS_SMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1); //initialization I2C_initMaster(EUSCI_B1_BASE, &i2cConfig); I2C_enableModule(EUSCI_B1_BASE); I2C_setSlaveAddress(EUSCI_B1_BASE, 0x1E); I2C_setMode(EUSCI_B1_BASE, EUSCI_B_I2C_TRANSMIT_MODE); // After the START condition (ST) a slave address is sent I2C_masterSendStart(EUSCI_B1_BASE); bool result = I2C_masterSendSingleByteWithTimeout(EUSCI_B1_BASE, 0x3C,1000); if(result){ printf("send Byte worked!"); } In the above code, I verified 0x1E as the address of the slave device through Arduino. I'm most confused by "masterSendStart" and "masterSendSingleByteWithTimeout". In order to recreate the protocol from the spec sheet, I tried sending the "Start" command first. Then, I tried sending the SAD+W command. I found 0x3C from a related compass' spec sheet. I was expecting masterSendSingleByteWithTimeout to return "true", indicating the acknowledgement from the compass, but I have not seen that yet and am not convinced that I'm using this function properly. Once I can get this working, I was planning on moving on to implementing the rest of the protocol to read from the magnetometer. Regarding my hardware setup, I've got an external 3.3V power supply powering the compass (common ground with the MSP). The SDA pin on the compass is going to Port6,Pin4. The SCL pin goes to Port6,Pin5.

Viewing all articles
Browse latest Browse all 232836

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>