3-Axis Accelerometer (LIS302DL)


Closed Thread
Results 1 to 21 of 21

Hybrid View

  1. #1
    Join Date
    Dec 2007
    Location
    Finland
    Posts
    191


    Did you find this post helpful? Yes | No

    Post Couple of corrections to code

    I noticed couple of misstakes in the code.

    One in the two's complement calculation formula. Previously posted formula gave result as: 255 = 0, 254 = (-)1, etc ... Correct formula should give 255 = (-)1, 254 = (-)2, etc ...
    Error was minor, but here is updated code (only change is 255 -> 256).

    Other in Acceleration_Menu -loop. Accelerometer_Init required only once you start.

    Code:
    Acceleratio     var word
    Acceleratio_max var word
    X_Data          var Byte
    Y_Data          var Byte
    COUNTER         VAR BYTE
    TEMP            VAR BYTE
    
    '-------------------------------------------------------------------------------
    Accelerometer_Init:
        Acceleratio = 0      
        Acceleratio_max = 0
        COUNTER = 0         
        
    'Axes
    I2CAddress_3D = $20     'CTRL_REG1
    TEMP = %01000011        'DR=0, PD=1, FS=0, STP=00, Zen=0, Yen=1, Xen=1 (X,Y enable, Output 100Hz)
    gosub I2C_Write_3D      '
    
    'Interrupts
    I2CAddress_3D = $22     'CTRL_REG3
    TEMP = %00000100        'IHL = 0, PP_OD = 0, I2CFG2-0 = 000, I1CFG2-0 = 100 (DataReady = INT1)
    gosub I2C_Write_3D
    
    Return
    
    '-------------------------------------------------------------------------------
    I2C_Write_3D:
    I2CWRITE SDA_3D, SCL_3D, I2CDevice_3D_write, I2CAddress_3D,[ TEMP ]
    Return
    
    I2C_Read_3D:
    I2cread SDA_3D, SCL_3D, I2CDevice_3D_read, I2CAddress_3D,[ TEMP ]
    Return
    
    '-------------------------------------------------------------------------------
    Acceleration_Menu:
        gosub Accelerometer_Init                'Start from here
    
    Acceleration_Loop:                                 
        IF Int1_3D = 1 THEN                     'Int1_3D is IO pin for Interrup coming from 3D sensor, DataReady in device     
            COUNTER = COUNTER + 1
            '---------------------------
                I2CAddress_3D = $29             'Read OUTX value
                gosub I2C_Read_3D
                if TEMP > 127 then              'Two's complementary changed to amplitude information
                        X_Data = 256 - TEMP     'Acceleration data presented in two's complement
                    else      
                        X_Data = TEMP
                ENDIF
            '---------------------------
                I2CAddress_3D = $2B             'Read OUTY value 
                gosub I2C_Read_3D
                if TEMP > 127 then              'Two's complementary changed to amplitude information
                        Y_Data = 256 - TEMP     'Acceleration data presented in two's complement
                    else      
                        Y_Data = TEMP
                ENDIF
            '---------------------------
            Acceleratio = Acceleratio + (18 * SQR((X_Data * X_Data) + (Y_Data * Y_Data)))   'Average factor 5x
            '---------------------------                                                    'Calculate resultant force
                IF COUNTER = 5 then
                    COUNTER = 0
                    Acceleratio = Acceleratio/5    
                    IF Acceleratio > Acceleratio_max then
                        Acceleratio_max = Acceleratio
                    endif
                    'ADD YOUR CODE HERE TO SHOW ACCELERATIO AND ACCELERATIO_MAX ON YOUR DISPLAY
                    Acceleratio = 0
                endif
            '---------------------------
            Gosub Accelerometer_Clear
        endif
        goto Acceleration_Loop
    '-------------------------------------------------------------------------------
    Accelerometer_Clear:            'Clear interrupt
        I2CAddress_3D = $31
        gosub I2C_Read_3D
        Return
    Quote Originally Posted by Glenn View Post
    Looks really good, if I knowed this before I started myself I probably would have ordered a I2C version instead

    ..But I might borrow some of your code anyway

    I was thinking of using a timer and calculate speed from it, but I dont know how accurate it would be.
    Thanks! Feel free to borrow, therefore I posted it here
    I would guess that digi output device would give overall better accuracy, but I might be wrong.

    BR,
    -Gusse-
    Last edited by Gusse; - 19th January 2009 at 11:45. Reason: It never goes right at once ....

  2. #2
    Join Date
    Dec 2007
    Location
    Finland
    Posts
    191


    Did you find this post helpful? Yes | No

    Post Some pictures

    It seems to work quite nicely.
    1) Start value ~0.07 - 0.10G. X-axis = 0 deg, Y-axis = 0 deg.
    2) Y-axis tilted 90 deg. Should show 1G in this position. OK.
    3) X-axis tilted ~45 deg.
    BR,
    -Gusse-

  3. #3
    Join Date
    Sep 2008
    Location
    Stockholm
    Posts
    80


    Did you find this post helpful? Yes | No

    Default

    Nice!

    And a LCD that looks identical as the one that I have cannibalized out of a nokia phone and going to solder wires on to use with my PIC's

    Do you want to post the updated code too ?

    ..This is how far I got..

    http://www.repulsiv.se/pics/accelero...breadboard.jpg

    Your setup is much more elegant

    ..Note that I already connected a cable for an Z-axis, I bought a prototypeboard with a 3-axis accelerometer from sure electronics that I'm going to use too.

    The "normal" value seem to be 120, and using 8bit AD-converter that would be.. lets se..

    5v Vref /256 = 0,01953125
    120 * 0,01953125 = 2,34375

    With a Vref at 5v 1G should be 2.5V, so its a bit strange..

    I have to make some effort at making PBP do that math and display the "real" G-value too.. readed Melanies thread about it but.. well
    Last edited by Glenn; - 21st January 2009 at 00:52.

  4. #4
    Join Date
    Dec 2007
    Location
    Finland
    Posts
    191


    Did you find this post helpful? Yes | No

    Post More you know, more painfull it is....

    Display is from Nokia 3310 and it is easy to play with. Yeah!

    I could try to make a code with all neccesary LCD srcipts too. At the moment this accelerometer is just one part of a bigger system and a part of a bigger code bunch. It will take some time, but I'll come back with the code, no problem. Anyway, origin of the display driver is from this forum, therefore I would like to thank those who have done the pioneer work! Thanks!

    As said earlier, my primary use case was motion detection (alarm). For that purpose LIS302DL works fine, but then I started to look more advanced solutions; acceleration, speed, range, etc. Nice features, but one problem is causing grey hairs to my.

    If device is at fully horisontal level (Z-axis is pointing to center of the earth), then everything is OK. But if it is rotated/tilted X, Y, Z or any combinations (pitch, roll, yaw), then readings are not any more right. Spec also says so.

    There are quite lot of discussions about how to get rid of this limitation, but no really simply solution. This is one good post, which describes more accurately the problem http://www.electronicspoint.com/acce...n-t119225.html

    BR,
    -Gusse-

    EDIT: Here is also code for LIS302DL with Nokia 3310 LCD. This one shows captured acceleration values on screen: 1st X, 2nd Y, 3rd Z. No bargraphics included because grachics are located in external EEPROM. LCD is connected to PortB, accelerometer to PortC & D (partial), see file for more details. Change pins to fit you application (I'm using 16F877A).
    Attached Files Attached Files
    Last edited by Gusse; - 21st January 2009 at 18:03. Reason: Code added for Nokia 3310 LCD and LIS302DL

  5. #5
    Join Date
    Dec 2007
    Location
    Finland
    Posts
    191


    Did you find this post helpful? Yes | No

    Post LIS302DL demo with bar graphics. Var so god!

    Here is an updated demo code for LIS302DL with Nokia 3310 LCD. This shows all three axis with bar graphics and numbers.

    Also small video clip, which shows how the system works (I'm rotation PIC and LIS302DL in my hand) http://kotiweb.kotiportti.fi/karl-er...C/LIS302DL.wmv.

    My test bench for accelerometer (add-on board) http://kotiweb.kotiportti.fi/karl-er...d_LIS302DL.gif.

    And here is the code.

    BR,
    -Gusse-

    EDIT: If your screen is too light or too dark then tune "Contrast" value. I have noticed that there are differences between new and old 3310 LCD displays (tested both).
    Attached Files Attached Files
    Last edited by Gusse; - 22nd January 2009 at 13:27. Reason: Guidance added

  6. #6
    Join Date
    Feb 2009
    Posts
    4


    Did you find this post helpful? Yes | No

    Default lis302dl not working

    my lis302dl is not working properly.....no matter which register i tried to read, the data is always 0xFF....shouldnt be right according to the datasheet....even the Who_AM_I register gave me 0xFF...BTW the sensor was working few days a ago. is it detected?help.....

  7. #7
    Join Date
    Feb 2009
    Posts
    4


    Did you find this post helpful? Yes | No

    Default

    I am using PIC16f877 to interface with LIS302DL. I browsed through some threads available over the net. They said we need to add pullups at the data lines and SCL. why? I dont include any pullups. i just directly connect the sensor to the PIC. anything wrong?

Similar Threads

  1. Replies: 4
    Last Post: - 15th April 2009, 01:54
  2. 3 HPWM channels
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 4th April 2006, 02:43
  3. having problems with Hantronix 20x4 lcd
    By Rhatidbwoy in forum mel PIC BASIC Pro
    Replies: 16
    Last Post: - 22nd December 2005, 12:22
  4. Someone help me to simplify this
    By thrix in forum General
    Replies: 4
    Last Post: - 21st February 2004, 05:01
  5. Help wanted..
    By thrix in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 17th February 2004, 23:44

Members who have read this thread : 2

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