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

Forum Post: I2c examples problem

$
0
0
Hello, I have been trying for a while to make my MSP430FR5739 with I2c device (MPU9250). I tried to follow the Texas example, but still i have no data coming back from the device. This is my code so far: #include unsigned char TXData = 0x71; // Pointer to TX data unsigned char TXByteCtr; unsigned char flag = 0; unsigned char rxData; int main(void) { WDTCTL = WDTPW + WDTHOLD; // Init SMCLK = MCLk = ACLK = 1MHz CSCTL0_H = 0xA5; CSCTL1 |= DCOFSEL0 + DCOFSEL1; // Set max. DCO setting = 8MHz CSCTL2 = SELA_3 + SELS_3 + SELM_3; // set ACLK = MCLK = DCO CSCTL3 = DIVA_3 + DIVS_3 + DIVM_3; // set all dividers to 1MHz // Configure Pins for I2C P1SEL1 |= BIT6 + BIT7; // Pin init UCB0CTLW0 |= UCSWRST; // put eUSCI_B in reset state UCB0CTLW0 |= UCMODE_3 + UCMST + UCSSEL_2;// I2C master mode, SMCLk //UCB0CTLW1 |= UCASTP_2; //automatic stop generated UCB0BRW = 0x8; // baudrate = SMCLK / 8 UCB0I2CSA = 0x68; // address slave is 48hex //UCB0TBCNT |= 0x001; //number of bytes to be received UCB0CTLW0 &=~ UCSWRST; //clear reset register // Configure UART pins P2SEL1 |= BIT0 + BIT1; P2SEL0 &= ~(BIT0 + BIT1); // Configure UART 0 UCA0CTL1 |= UCSWRST; UCA0CTL1 = UCSSEL_1; // Set ACLK = 32768 as UCBRCLK UCA0BR0 = 3; // 9600 baud UCA0BR1 = 0; UCA0MCTLW |= 0x5300; // 32768/9600 - INT(32768/9600)=0.41 // UCBRSx value = 0x53 (See UG) UCA0CTL1 &= ~UCSWRST; // release from reset while(1) { __delay_cycles(10000); // Delay between transmissions UCB0IE |= UCTXIE0 + UCNACKIE; //transmit and NACK interrupt enable TXByteCtr = 1; // Load TX byte counter while (UCB0CTLW0 & UCTXSTP); // Ensure stop condition got sent UCB0CTLW0 |= UCTR + UCTXSTT; // I2C TX, start condition while (UCB0CTLW0 & UCTXSTP); // Ensure stop condition got sent UCB0IE |= UCRXIE0 + UCNACKIE; //transmit and NACK interrupt enable UCB0CTLW0 &= ~UCTR; // I2C start condition UCB0CTLW0 |= UCTXSTT; // I2C start condition while (!(UCA0IFG&UCTXIFG)); // USCI_A0 TX buffer ready? UCA0TXBUF = rxData; // TX -> RXed character __bis_SR_register(CPUOFF + GIE); // Enter LPM0 w/ interrupts // Remain in LPM0 until all data // is TX'd //TXData++; // Increment data byte } } #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__) #pragma vector = USCI_B0_VECTOR __interrupt void USCIB0_ISR(void) #elif defined(__GNUC__) void __attribute__ ((interrupt(USCI_B0_VECTOR))) USCIB0_ISR (void) #else #error Compiler not supported! #endif { switch(__even_in_range(UCB0IV,0x1E)) { case 0x00: break; // Vector 0: No interrupts break; case 0x02: break; case 0x04: UCB0CTLW0 |= UCTXSTT; //resend start if NACK break; // Vector 4: NACKIFG break; case 0x16: rxData = UCB0RXBUF; // Get RX data UCB0CTLW0 |= UCTXSTP; __bic_SR_register_on_exit(CPUOFF); // Exit LPM0 break; // Vector 24: RXIFG0 break;*/ case 0x18: if (TXByteCtr) // Check TX byte counter { UCB0TXBUF = TXData; // Load TX buffer TXByteCtr--; // Decrement TX byte counter } else { UCB0CTLW0 |= UCTXSTP; // I2C stop condition UCB0IFG &= ~UCTXIFG; // Clear USCI_B0 TX int flag __bic_SR_register_on_exit(CPUOFF);// Exit LPM0 } break; // Vector 26: TXIFG0 break; default: break; } } I should be able to access the resgister ("Who am I") 0x71 and send the response of the register to the serial port. I simply don't receive anything. And I have also another question: In the datasheet of the device I am trying to access there is a schema for the Burst Read Sequence like this: M || START | Address+W | | Register | | Start Condition | Address + R | | ACK | | NACK | STOP S || | ACK | | ACK | | ACK | DATA | | DATA | Why the Stop condition is only at the end of the communication? In the TI examples a transmission of data always ends with STOP, so I was expecting to send (as you can see from the code) a stop also at the end of the transmission of the Register. Thank you for your help

Viewing all articles
Browse latest Browse all 225245

Trending Articles



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