Hello!
I'm trying to read 3 different registers from Accelerometer using I2C.
Here's my initialisation code:
WDT_A_hold(WDT_A_BASE); //Set DCO frequency to 1MHz CS_setDCOFreq(CS_DCORSEL_0,CS_DCOFSEL_0); //Set ACLK = VLO with frequency divider of 1 CS_initClockSignal(CS_ACLK,CS_VLOCLK_SELECT,CS_CLOCK_DIVIDER_1); //Set SMCLK = DCO with frequency divider of 1 CS_initClockSignal(CS_SMCLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_1); //Set MCLK = DCO with frequency divider of 1 CS_initClockSignal(CS_MCLK,CS_DCOCLK_SELECT,CS_CLOCK_DIVIDER_1); // Configure Pins for I2C //Set P1.6 and P1.7 as Secondary Module Function Input. /* * Select Port 1 * Set Pin 6, 7 to input Secondary Module Function, (UCB0SIMO/UCB0SDA, UCB0SOMI/UCB0SCL). */ GPIO_setAsPeripheralModuleFunctionInputPin( GPIO_PORT_P1, GPIO_PIN6 + GPIO_PIN7, GPIO_SECONDARY_MODULE_FUNCTION ); /* * Disable the GPIO power-on default high-impedance mode to activate * previously configured port settings */ PMM_unlockLPM5(); EUSCI_B_I2C_initMasterParam param = {0}; param.selectClockSource = EUSCI_B_I2C_CLOCKSOURCE_SMCLK; param.i2cClk = CS_getSMCLK(); param.dataRate = EUSCI_B_I2C_SET_DATA_RATE_100KBPS; param.byteCounterThreshold = 0; param.autoSTOPGeneration = EUSCI_B_I2C_NO_AUTO_STOP; EUSCI_B_I2C_initMaster(EUSCI_B0_BASE, ¶m); //Specify slave address EUSCI_B_I2C_setSlaveAddress(EUSCI_B0_BASE, SLAVE_ADDRESS); //Set Master in receive mode EUSCI_B_I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_TRANSMIT_MODE); //Enable I2C Module to start operations EUSCI_B_I2C_enable(EUSCI_B0_BASE); EUSCI_B_I2C_clearInterrupt(EUSCI_B0_BASE, EUSCI_B_I2C_TRANSMIT_INTERRUPT0 + EUSCI_B_I2C_NAK_INTERRUPT); //Enable master Receive interrupt EUSCI_B_I2C_enableInterrupt(EUSCI_B0_BASE, EUSCI_B_I2C_TRANSMIT_INTERRUPT0 + EUSCI_B_I2C_NAK_INTERRUPT);
I'm successful in writing values to the registers. I can see the correct signals in Oscilloscope.
However, when I try reading register that has Acceleration along X, Y, Z axis, I'm getting only Acceleration along Z-Axis. Here's the code for receiving part:
EUSCI_B_I2C_masterSendMultiByteStart(EUSCI_B0_BASE, 0x01); while(EUSCI_B_I2C_masterIsStartSent(EUSCI_B0_BASE) == EUSCI_B_I2C_SENDING_START); EUSCI_B_I2C_setMode(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_MODE); EUSCI_B_I2C_clearInterrupt(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_INTERRUPT0+ EUSCI_B_I2C_TRANSMIT_INTERRUPT0); EUSCI_B_I2C_enableInterrupt(EUSCI_B0_BASE, EUSCI_B_I2C_RECEIVE_INTERRUPT0); EUSCI_B_I2C_masterReceiveStart(EUSCI_B0_BASE); while(EUSCI_B_I2C_masterIsStopSent(EUSCI_B0_BASE) == EUSCI_B_I2C_SENDING_STOP); ZAcceleration = RXData/21.0; __bis_SR_register(GIE); // Enable interrupts #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__) #pragma vector=USCI_B0_VECTOR __interrupt #elif defined(__GNUC__) __attribute__((interrupt(USCI_B0_VECTOR))) #endif void USCIB0_ISR(void) { static uint8_t count = 0; switch(__even_in_range(UCB0IV,0x1E)) { case 0x00: break; // Vector 0: No interrupts break; case 0x02: break; case 0x04: //resend start if NACK // EUSCI_B_I2C_masterSendStart(EUSCI_B0_BASE); break; // Vector 4: NACKIFG break; case 0x16: RXData = EUSCI_B_I2C_masterReceiveSingle(EUSCI_B0_BASE); // Get RX data if(++count > 1) { count = 0; EUSCI_B_I2C_masterReceiveMultiByteStop(EUSCI_B0_BASE); } __no_operation(); break; // Vector 24: RXIFG0 break; case 0x18: if(TXByteCtr) // Check TX byte counter { EUSCI_B_I2C_masterSendMultiByteNext(EUSCI_B0_BASE, *(++dataPointer)); TXByteCtr--; // Decrement TX byte counter } else { EUSCI_B_I2C_masterSendMultiByteStop(EUSCI_B0_BASE); __no_operation(); } break; // Vector 26: TXIFG0 break; default: break; } }
No matter what register I point with EUSCI_B_I2C_masterSendMultiByteStart(EUSCI_B0_BASE, 0x01); function, I'm getting data from 0x02 register. I see the values changing in Oscilloscope but can't get correct data back from Accelerometer.( 0x01 Register holds acceleration along Y-Axis)
Please help me solve this problem.
Best
Teja