Hello guys,
i have written small code where i can communicate with the MPU9250 of sparkfun, my pin connection are followiing
MSP4302553 mpu9250 sparkfun board
VCC---------------------------------------------VDD
P1.1 (UCA0SOMI/)--------------------------SDO
P1.2 (UCA0SIMO/)--------------------------SDA
P1.4 (UCA0CLK/)---------------------------SCL
P1.5---------------------------------------------nCS
and below is my code for just having a first communication with the MPU9250 by asking his recognization:
#include <msp430g2553.h> #include <mpu9250.h> #define READ 0x80 #define I2C_IF_DIS 0x10 #define USER_CTRL 0x6A // Bit 7 enable DMP, bit 3 reset DMP #define WHO_AM_I 0x75 // Should return 0x71 /* * main.c */ char connect; int main(void) { WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer P2DIR |=BIT2; SPI_init(); while(1){ SpiMpuTxWrByte(USER_CTRL,I2C_IF_DIS); //Disable I2C Slave module and put the serial interface in SPI mode only // Read the WHO_AM_I register,test of communication SpiMpuRxReByte(WHO_AM_I,&connect,1); if(connect==0x71){ P2OUT&=~BIT2; } else P2OUT|=BIT2; } return 0; } /* * mpu9250.c * * Created on: Jun 30, 2017 * Author: tarik H */ #include <msp430g2553.h> #include <mpu9250.h> void SPI_init(void){ //UCA0CTL1 |= UCSWRST; // enable software reset ,USCI held in reset state P1SEL |= BIT1|BIT2|BIT4; //P3.0 UCA0CLK select,P3.4 UCA0SIMO select P1SEL2 |= BIT1|BIT2|BIT4; //~(BIT0|BIT4) UCA0CTL0 |= UCCKPH|UCMSB|UCMST|UCSYNC; //UCCKPH=(ph=1;po=00, 3-pin, MSB first, synchronous mode, MAster mode, 8-bit UCA0CTL1 |= UCSSEL_2; //BRCLK Source Clock,USCI 0 Clock Source: 2,f=f(BRCLK)/UCBRx, sub main clock SMCLK UCA0BR0 |= 0x02; //set clock prescaler to 2 UCA0BR1 = 0x00; //(UCAxBR0 + UCAxBR1 × 256)=UCBRx forms the prescaler value UCA0MCTL = 0x00; // Modulation disabled UCA0CTL1 &=~UCSWRST; // USCI reset released for operation __delay_cycles(99000); //52500 of f=16Mhz or 2.1875ms } void SpiMpuTxWrByte(char WriteAddress,char WriteData){ P1OUT |=BIT5; P1OUT &=~BIT5; // write the address of the register while(UCB0STAT & UCBUSY); while(!(IFG2 & UCA0TXIFG)); //TX buffer is ready and has transmitted the data,check if the data written in UC0TXIFG is set and IFG2(its initial state is 0x1001110) UCA0TXBUF=WriteAddress; //write address // write the data to the register while(UCB0STAT & UCBUSY); while(!(IFG2 & UCA0TXIFG)); //TX buffer is ready and has transmitted the data,check if the data written in UC0TXIFG is set and IFG2(its initial state is 0x1001110) UCA0TXBUF=WriteData; //P1OUT |=BIT5; } void SpiMpuRxReByte(char ReadAddress,char* ReadBuff, char ReadSize){ P1OUT |=BIT5; P1OUT &=~BIT5; // register address to read while(UCB0STAT & UCBUSY); while(!(IFG2 & UCA0TXIFG)); //TX buffer is ready and has transmitted the data, UCA0TXBUF=ReadAddress|READ; //read address // data to read while(ReadSize){ while(UCB0STAT & UCBUSY); while(!(IFG2 & UCA0TXIFG)); //TX buffer is ready and has transmitted the data UCA0TXBUF=0x00; // to rea data we need to send 0x00 to data register while (!(IFG2 & UCA0RXIFG)); // wait for transmission complete *ReadBuff = UCA0RXBUF; ReadBuff++; ReadSize--; } //P1OUT |=BIT5; } My question is that, whenever i disconnect the the SDO from pin P1.1 when running the code step by step, i find that the UCA0RXBUF is filled whith the value of 0XFF which it suppose to be empty, so how can the UCA0 receives 0xFF while it is completely disconnected fro the MPU9250,
i have noticedd also the bit UCA0RXIFG is set to 1 whenver i send the data which is supposed to be set when i receive the data.
please any clarificaion is welcome