ST micro LIS3LV02DL accelerometer interfacing


Closed Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2008
    Location
    Madhya Pradesh, India
    Posts
    9

    Default ST micro LIS3LV02DL accelerometer interfacing

    hi,

    i am trying to read data from an accelerometer (ST micro LIS3LV02DL) through SPI.
    I used shiftin and shiftout command in picbasic pro but not able to read data. I have read the datasheet quite a few times but not able to get how to use SPI. I am basically confused in using SPI.

    any help is solicited.

    here's the data sheet: http://www.st.com/stonline/products/...e/ds/12094.pdf

    here's my program:

    INCLUDE "modedefs.bas"
    CS VAR PORTB.4
    CLK VAR PORTB.3
    DI VAR PORTB.2
    DO VAR PORTB.2
    BUFF VAR WORD
    ATM CON $32
    MSB CON $AC
    LSB CON $F3

    START
    GOSUB GETDATA
    GOTO START

    GETDATA
    Low CS
    Shiftout DI,CLK,MSBFIRST,[ATM,MSB,LSB]
    Shiftin DO,CLK,MSBPRE,[BUFF]
    High CS
    serout PORTA.2,N2400,[BUFF]
    RETURN
    END

    -Thanks

  2. #2
    skimask's Avatar
    skimask Guest

    Default

    No idea which PIC you're using...therefore no idea if the PIC you are using has any other functions on the PortB pins you are using...

    No colons behind START and GETDATA

  3. #3
    Join Date
    Dec 2005
    Posts
    1,073

    Default

    There's an SPI example on the meLabs web site.

  4. #4
    Join Date
    Mar 2008
    Location
    Madhya Pradesh, India
    Posts
    9

    Default

    Quote Originally Posted by skimask View Post
    No idea which PIC you're using...therefore no idea if the PIC you are using has any other functions on the PortB pins you are using...

    No colons behind START and GETDATA
    I am using 16f84.

    -Thanks

  5. #5
    Join Date
    Mar 2008
    Location
    Madhya Pradesh, India
    Posts
    9

    Default

    Quote Originally Posted by dhouston View Post
    There's an SPI example on the meLabs web site.
    I have already tried it, not able to complie it, lot's of error

    -Thanks

  6. #6
    skimask's Avatar
    skimask Guest

    Default

    Quote Originally Posted by devil6600 View Post
    I have already tried it, not able to complie it, lot's of error
    -Thanks
    And did you add the colons?
    Or are you using Proton basic?

  7. #7
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898

    Default

    colons are not 100% mandatory, it may work without.

    i would suggest to have a look a the following
    http://www.melabs.com/resources/samples/x1/pbp/spix.bas

    If you receive few compilation error, it's probably because that code above is made for a 16F877x. Grandpa 16F84 have no PORTC, PORTD, PORTE & ADCON1 registers.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  8. #8
    Join Date
    Mar 2008
    Location
    Madhya Pradesh, India
    Posts
    9

    Default

    hello,

    here's the code in c.
    Can you help me to port it into picbasic?
    I am not sure how to read an address register and write an address register with some data.

    void Write_SPI_Data(char address, char data)
    {
    ClrBit(PBDR, 2); //Chip select active
    SPIDR = address;
    while(!ValBit(SPISR,7)); //wait the end of tx
    SPIDR=data;
    while(!ValBit(SPISR,7)); //wait the end of tx
    SetBit(PBDR, 2);//Chip select inactive
    return;
    }
    char Read_SPI_Data(char address)
    {
    ClrBit(PBDR, 2); //Chip select active
    SPIDR = address + 0x80; // "write addr" + 0x80 for reading operations
    while(!ValBit(SPISR,7)); //wait the end of rx
    SPIDR = 0x00; //dummy byte
    while(!ValBit(SPISR,7)); //wait the end of rx
    SetBit(PBDR, 2); //Chip select inactive
    return SPIDR;
    }
    void Init_MEMS_device(void)
    {
    //Write_SPI_Data(0x20, 0xC7); // REG1 40Hz
    Write_SPI_Data(0x20, 0xD7); // REG1 160Hz
    Write_SPI_Data(0x21, 0x05); // REG2, data ready enabled, 16 bits
    //Write_SPI_Data(0x21, 0x85); // REG2, data ready enabled, 16 bits. 6g FS
    //Write_SPI_Data(0x21, 0x00); // REG2 //no DataRDY, 12 bit right just
    Write_SPI_Data(0x22, 0x00); // REG3
    Write_SPI_Data(0x38, 0x00); // DD_CFG
    return;
    }
    void acquire(void)
    {
    ACC[0]=((Read_SPI_Data(0x29))<<8)|Read_SPI_Data(0x28);
    ACC[1]=((Read_SPI_Data(0x2B))<<8)|Read_SPI_Data(0x2A);
    ACC[2]=((Read_SPI_Data(0x2D))<<8)|Read_SPI_Data(0x2C);
    return ;
    }

    -Thanks

  9. #9
    skimask's Avatar
    skimask Guest

    Default

    Quote Originally Posted by devil6600 View Post
    I am not sure how to read an address register and write an address register with some data.
    PicBasicPro...da manual...has the information you need to learn how to read an address register and write an address register with some data.

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts