Problem communicating btwn DSPIC & LIS3LV02DL accelerometer

Thread Starter

ardent01

Joined Jun 19, 2012
1
I am using a MicroChip's DSPIC33FJ128MC802 with a LIS3LV02DL accelerometer (ST Micro). I am having trouble communicating with the accelerometer. The SPI in 16bit mode and with Microcontroller as Master. Below, is the code I use to request the OUTX_L from the accelerometer.

Rich (BB code):
PORTBbits.RB13 = 0;         //Lower SS line for accelerometer 
 // Delay_us_ms('u', 0x6);    
 temp = SPI1BUF;       //Dummy read    
 SPI1BUF = 0xA800;     //get low axis    Bit15 set for Read    
 // while (!SPI1STATbits.SPITBF){};    //Wait for data to be sent 
 // while (SPI1STATbits.SPITBF){};     //Wait for data to be sent i.e flag to be cleared 
 while(!IFS0bits.SPI1IF){}; 
 IFS0bits.SPI1IF = 0; 
 while (!SPI1STATbits.SPIRBF){};    //Wait for data to be received 
 tempm = SPI1BUF; 
 PORTBbits.RB13 = 1;         //Raise SS line for accelerometer 
 Delay_us_ms('u', 0x6);
Rich (BB code):
void Init_Accel_Over_SPI(void) 
{ 
    int temp; 
  //hard defines are the only way the SPI will send this info 
    Init_SPI1_For_Word(); 

   Delay_us_ms('u', 0x0A); 
//**** accel set up ****** 
   PORTBbits.RB13 = 0;         //Lower SS line for accelerometer 
   // Delay_us_ms('u', 0x0A); 

    temp = SPI1BUF;             //Dummy read 
    SPI1BUF = Write_CTRL_REG1;  //(0x20, 0b011010111) CTRL_REG1: Device On, 160Hz, No self test, Enable all Axis 
   // while (!SPI1STATbits.SPITBF){};     //Wait for data to be sent 
    // while (SPI1STATbits.SPITBF){};     //Wait for flag to be cleared 
   while(!IFS0bits.SPI1IF){}; 
   IFS0bits.SPI1IF = 0; 

   PORTBbits.RB13 = 1;         //Raise SS line for accelerometer 
   Delay_us_ms('u', 0x0A); 
   //******************** 
   PORTBbits.RB13 = 0;         //Lower SS line for accelerometer 
   // Delay_us_ms('u', 0x0A); 
    
   temp = SPI1BUF;             //Dummy read 
    SPI1BUF = Write_CTRL_REG2; //(0x21, 0b01110101) CTRL_REG2: 2G, BDU=1; big endian, boot NOT, DTR, 4Wire, 16 bit 
     while(!IFS0bits.SPI1IF){}; 
   IFS0bits.SPI1IF = 0; 
  
   PORTBbits.RB13 = 1;         //Raise SS line for accelerometer 
   Delay_us_ms('u', 0x0A); 
    //******************** 
   PORTBbits.RB13 = 0;         //Lower SS line for accelerometer 
   // Delay_us_ms('u', 0x0A); 
    temp = SPI1BUF;             //Dummy read    
   SPI1BUF = Write_CTRL_REG3; //(0x22, 0b00010011)  CTRL_REG3: Int clock, Filter Enabled, FDSEnabled, HPC=4096 
   while(!IFS0bits.SPI1IF){}; 
   IFS0bits.SPI1IF = 0; 
  
   PORTBbits.RB13 = 1;         //Raise SS line for accelerometer 
   Delay_us_ms('u', 0x0A);    
}   //end Init_Accel_Over_SPI 

void Init_SPI1_For_Word(void) 
{ 
    SPI1STATbits.SPIEN = 0;         //Disable SPI1 
     IFS0bits.SPI1IF = 0;           //Clear interrupt flag for SPI1 
    IEC0bits.SPI1IE = 0;           //Disable SPI interrupt 
//******* 
    SPI1CON1bits.DISSCK = 0;    //Use Internal serial clock 
    SPI1CON1bits.DISSDO = 0;    //SPI1 SDO controlled by SPI1 controller 
    SPI1CON1bits.MODE16 = 1;    //Communication is word wide (16-bit) 
  //SPI1CON1bits.SPRE = 0;          //Set secondary prescaler to 8:1 
    SPI1CON1bits.SPRE = 6;    //change to 2:1 
    SPI1CON1bits.PPRE = 0;          //Set primary prescaler to 64:1 
    SPI1CON1bits.MSTEN = 1;     //SP11 mode is master mode 
    SPI1CON1bits.SMP = 1;           //Sample input data at middle of data output time 
//**********************    
    SPI1CON1bits.CKE = 1;         //Serial out data changes on transition from active clock state to idle clock state 
      // SPI1CON1bits.CKE = 0;      // Serial out data changes from on transition from active clock state to idle clock state 
   SPI1CON1bits.SSEN = 1;      // SSx pin not used by module. Pin controlled by port function 
//**********************    
    SPI1CON1bits.CKP = 1;          //Idle state for clock is high level; active is low 
//**********************    
    SPI1STATbits.SPIROV = 0;    //Clear SPI1 Receive overflow flag 
   SPI1STATbits.SPISIDL = 0; 
   SPI1STATbits.SPITBF = 0; 
   // SPI1STATbits.SPIRBF = 0; 
   SPI1CON2bits.FRMEN = 0;      // Framed SPIx support disabled    
   SPI1STATbits.SPIEN = 1;         //Enable SPI1 

   IFS0bits.SPI1IF = 0;           //Clear interrupt flag for SPI1.  
     IEC0bits.SPI1IE = 0;           //Disable SPI interrupt 

}   // end Init_SPI1_For_Word()
I see the MOSI and SCK on PicoScope fine, and the Chip Select goes low right before the MOSI/SCK and goes back high after it. However, I do not see any signals coming back on the MISO, on the scope. All I see is noise. The values in 'tempm' variable is always 0xFFFF. Same thing with WhoAmI. I checked all the connections too and they seem fine. What am I doing wrong? Any suggestions? Thank you.
I am using MPLAB IDE v8.76 with an MPLAB ICD3.
 
Top