Problem communicating btwn DSPIC & LIS3LV02DL accelerometer

Discussion in 'Programmer's Corner' started by ardent01, Jun 19, 2012.

  1. ardent01

    Thread Starter New Member

    Jun 19, 2012
    1
    0
    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.

    Code ( (Unknown Language)):
    1. PORTBbits.RB13 = 0;         //Lower SS line for accelerometer
    2.  // Delay_us_ms('u', 0x6);    
    3.  temp = SPI1BUF;       //Dummy read    
    4.  SPI1BUF = 0xA800;     //get low axis    Bit15 set for Read    
    5.  // while (!SPI1STATbits.SPITBF){};    //Wait for data to be sent
    6.  // while (SPI1STATbits.SPITBF){};     //Wait for data to be sent i.e flag to be cleared
    7.  while(!IFS0bits.SPI1IF){};
    8.  IFS0bits.SPI1IF = 0;
    9.  while (!SPI1STATbits.SPIRBF){};    //Wait for data to be received
    10.  tempm = SPI1BUF;
    11.  PORTBbits.RB13 = 1;         //Raise SS line for accelerometer
    12.  Delay_us_ms('u', 0x6);
    Code ( (Unknown Language)):
    1. void Init_Accel_Over_SPI(void)
    2. {
    3.     int temp;
    4.   //hard defines are the only way the SPI will send this info
    5.     Init_SPI1_For_Word();
    6.  
    7.    Delay_us_ms('u', 0x0A);
    8. //**** accel set up ******
    9.    PORTBbits.RB13 = 0;         //Lower SS line for accelerometer
    10.    // Delay_us_ms('u', 0x0A);
    11.  
    12.     temp = SPI1BUF;             //Dummy read
    13.     SPI1BUF = Write_CTRL_REG1;  //(0x20, 0b011010111) CTRL_REG1: Device On, 160Hz, No self test, Enable all Axis
    14.    // while (!SPI1STATbits.SPITBF){};     //Wait for data to be sent
    15.     // while (SPI1STATbits.SPITBF){};     //Wait for flag to be cleared
    16.    while(!IFS0bits.SPI1IF){};
    17.    IFS0bits.SPI1IF = 0;
    18.  
    19.    PORTBbits.RB13 = 1;         //Raise SS line for accelerometer
    20.    Delay_us_ms('u', 0x0A);
    21.    //********************
    22.    PORTBbits.RB13 = 0;         //Lower SS line for accelerometer
    23.    // Delay_us_ms('u', 0x0A);
    24.    
    25.    temp = SPI1BUF;             //Dummy read
    26.     SPI1BUF = Write_CTRL_REG2; //(0x21, 0b01110101) CTRL_REG2: 2G, BDU=1; big endian, boot NOT, DTR, 4Wire, 16 bit
    27.      while(!IFS0bits.SPI1IF){};
    28.    IFS0bits.SPI1IF = 0;
    29.  
    30.    PORTBbits.RB13 = 1;         //Raise SS line for accelerometer
    31.    Delay_us_ms('u', 0x0A);
    32.     //********************
    33.    PORTBbits.RB13 = 0;         //Lower SS line for accelerometer
    34.    // Delay_us_ms('u', 0x0A);
    35.     temp = SPI1BUF;             //Dummy read    
    36.    SPI1BUF = Write_CTRL_REG3; //(0x22, 0b00010011)  CTRL_REG3: Int clock, Filter Enabled, FDSEnabled, HPC=4096
    37.    while(!IFS0bits.SPI1IF){};
    38.    IFS0bits.SPI1IF = 0;
    39.  
    40.    PORTBbits.RB13 = 1;         //Raise SS line for accelerometer
    41.    Delay_us_ms('u', 0x0A);    
    42. }   //end Init_Accel_Over_SPI
    43.  
    44. void Init_SPI1_For_Word(void)
    45. {
    46.     SPI1STATbits.SPIEN = 0;         //Disable SPI1
    47.      IFS0bits.SPI1IF = 0;           //Clear interrupt flag for SPI1
    48.     IEC0bits.SPI1IE = 0;           //Disable SPI interrupt
    49. //*******
    50.     SPI1CON1bits.DISSCK = 0;    //Use Internal serial clock
    51.     SPI1CON1bits.DISSDO = 0;    //SPI1 SDO controlled by SPI1 controller
    52.     SPI1CON1bits.MODE16 = 1;    //Communication is word wide (16-bit)
    53.   //SPI1CON1bits.SPRE = 0;          //Set secondary prescaler to 8:1
    54.     SPI1CON1bits.SPRE = 6;    //change to 2:1
    55.     SPI1CON1bits.PPRE = 0;          //Set primary prescaler to 64:1
    56.     SPI1CON1bits.MSTEN = 1;     //SP11 mode is master mode
    57.     SPI1CON1bits.SMP = 1;           //Sample input data at middle of data output time
    58. //**********************    
    59.     SPI1CON1bits.CKE = 1;         //Serial out data changes on transition from active clock state to idle clock state
    60.       // SPI1CON1bits.CKE = 0;      // Serial out data changes from on transition from active clock state to idle clock state
    61.    SPI1CON1bits.SSEN = 1;      // SSx pin not used by module. Pin controlled by port function
    62. //**********************    
    63.     SPI1CON1bits.CKP = 1;          //Idle state for clock is high level; active is low
    64. //**********************    
    65.     SPI1STATbits.SPIROV = 0;    //Clear SPI1 Receive overflow flag
    66.    SPI1STATbits.SPISIDL = 0;
    67.    SPI1STATbits.SPITBF = 0;
    68.    // SPI1STATbits.SPIRBF = 0;
    69.    SPI1CON2bits.FRMEN = 0;      // Framed SPIx support disabled    
    70.    SPI1STATbits.SPIEN = 1;         //Enable SPI1
    71.  
    72.    IFS0bits.SPI1IF = 0;           //Clear interrupt flag for SPI1.  
    73.      IEC0bits.SPI1IE = 0;           //Disable SPI interrupt
    74.  
    75. }   // 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.
     
Loading...