Shop OBEX P1 Docs P2 Docs Learn Events
3 axis digital accelerometer reading with Stamp (ST Microelectronics LIS302DL) — Parallax Forums

3 axis digital accelerometer reading with Stamp (ST Microelectronics LIS302DL)

CalebCaleb Posts: 18
edited 2008-01-11 06:14 in BASIC Stamp
I am trying to read the data from 3 axis digital output accelerometer from ST Microelectronics (LIS302DL)·which using I2C. Has anyone done this or had the Stamp program which I can try with circuit diagram? Any inforamtion will be appreciated.

Comments

  • ZootZoot Posts: 2,227
    edited 2008-01-08 13:35
    That looks like a cool chip. I looked over the datasheet -- this chip supports both SPI (serial protocol interface) *and* I2C, so my first question for you would be -- what kind of Stamp are you using? If you are using a BS2, you might want to use SPI for this chip so you can use SHIFTIN/SHIFTOUT. If you have a Stamp that has native support for I2C, then use either. If you really want to use I2C on the BS2, there are code routines that emulate the I2CIN/I2COUT commands (which are not available on the BS2). I can point you to this code if you want.

    Wiring is simple -- if using SPI you'll have 3 wires from 3 Stamp pins to the chip -- CS, SDI/SDO, SCLCK. CS is pulled LOW to shift data in and out of the chip, then set HIGH again after data is clocked in/out. See SHIFTIN/SHIFTOUT in the Stamp manual.

    If using I2C, you need to tie CS HIGH on the chip (to keep it in I2C mode) and then run two wires to the Stamp -- one to SDA and one to SCL. You will need to put two pull-up resistors on the two I2C lines -- one end of the resistors to +5v, the other ends to the I2C lines. Values between 1.8 and 4.7k should be fine. Again, see I2CIN/I2COUT in the Stamp manual.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST
  • CalebCaleb Posts: 18
    edited 2008-01-09 00:35
    Thanks for the comments. I am really beginner for MCU. I purchased Basic Stamp 2E module. upon your comments, I found that it doesn't have I2C support. I really want to go with easy way first. I will try spi. If there is any code done or I can use to read the data which will be fantastic for me to get involved with MCU study. Anyway, I will try to read the manual and try to come up with something hoping that I could get help. By the way, I wonder if there is any introduction web site for I2C and SPI...showing difference, purpose, example to communicate. adv and disadv....

    Post Edited (Caleb) : 1/9/2008 12:41:39 AM GMT
  • ZootZoot Posts: 2,227
    edited 2008-01-09 01:27
    Actually, the explanations in the Stamp manual are *really* good. Aside from the technical details here are some pros and cons:

    I2C -- the same two lines (wires) can be run to multiple devices from your micro. I2C uses an addressing scheme to address/read each device.

    Pros -- many devices, two wires. Standard interface. Very immune to noise. Cons -- more complex code (or built-in I2C routines) required to use, more bytes need to be sent each way for communication. I'm a sucker for I2C myself, but I use it either on the Stamps that support it natively or on the SX. I have one Stamp 2 project that uses the emulation code. For example in addition to your device you could add an external eeprom (for more memory), a real-time clock, etc. all on the same two wires.

    SPI -- two lines are clock and data, one line is "enable". This way many devices can share the same clock and data line and each one gets it's own enable line.

    Pros -- easy coding. Standard interface. Cons -- more pins (or some sort of shift register) for the enable lines are required.

    Since these sound like they are both new, I woud use SHIFTIN, SHIFTOUT on your Stamp first. Get communicating with the device and make sure you can write/read to it properly. Then if you decide to use the I2C code for the Stamp 2, you'll know what you're supposed to be sending and receiving from the device, and all you'll have to change is *how* you are doing that (rather than figuring it all out at once).

    Check out SHIFTIN/SHIFTOUT in the manual and then compare to the requirements in the data sheet. The most common things to look for:

    - how many bits/bytes will need to be shifted out to the device to control it, and in what order

    - ditto for incoming data

    - will you need to format what you read.

    If you get stumped, come on back here.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST
  • CalebCaleb Posts: 18
    edited 2008-01-10 05:46
    Thanks for the good comments. I am really slow to lean and will try. By the way, I noticed that stamp has 5V Vdd, but the device spec says 2.16V~3.6V max (-0.3~6V for absolute max). Can I use 5V Vdd directly? Also, can I use this 5V for Vdd_IO which I am not quite sure for the purpose... Or can I just leave Vdd_IO unconnected? Or is there way to get 3.6V in the stamp?



    Post Edited (Caleb) : 1/10/2008 6:45:34 AM GMT
  • FranklinFranklin Posts: 4,747
    edited 2008-01-10 15:40
    Take a look at the propeller forum and there is a topic on interfacing 5v and 3.3v devices. In your case it is the other device that needs the lower voltage and with the prop it is the prop itself that is the low voltage device. Also the device may be 5v tolerant in which case a resistor to limit current may be all you need. The data sheet should say.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • ZootZoot Posts: 2,227
    edited 2008-01-10 18:37
    Franklin is right. For this device, SPI would probably be easier if interfacing with 3.3 v. You'll surely need a separate 3.3 v regulator for the device; power the stamp with 5v (if using external regulator) or 6-9v (if using stamp on-board regulator).

    Resistors on the 3 SPI spins should safely drop the 5v OUT from the Stamp to a level the device can handle (minimum components) and the stamp will see a "high" from the device as long as it's above ~1.4 volts.

    With I2C you will probably need true level shifting -- there are plenty of examples in the stickies at the top of the Propeller forum (you are in luck -- the Prop is 3.3v and many devices are 5v, so there is a wealth of info there about what you can and can not get away with).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST
  • CalebCaleb Posts: 18
    edited 2008-01-11 06:00
    I happened to find the code to read the acceleration from the LIS3LV02DQ using propeller chip (http://forums.parallax.com/forums/default.aspx?f=25&m=151211&g=239573#m239573).

    Does anyone can guide me if or how I can modify this code so I can use basic stamp(2E module)? Or can modify the codes for me ..... as I am really beginner....and this will give me good experience and motivation?


    {{

    LIS3LV02DQ Test

    LIS3LV02DQ Vdd -> Prop Vdd -> +3v
    LIS3LV02DQ Gnd -> Prop Vss -> -gnd.
    LIS3LV02DQ SDA -> Prop pin 10 (pulled high) seems to work fine without pullup.
    LIS3LV02DQ SCL -> Prop pin 11 (pulled high) seems to work fine without pullup.
    LIS3LV02DQ CS -> Prop pin 12
    LIS3LV02DQ INT -> NC
    LIS3LV02DQ SDO -> NC
    LIS3LV02DQ CK -> Nc

    }}

    CON
    _CLKMODE = XTAL1 + PLL16X
    _XINFREQ = 5_000_000

    'OLED
    ISTEXT = 0
    ISNBR = 1
    TINY_FONT = 0
    WITH_AMB = 1
    RAW = 0
    O_TX = 7
    O_RX = 6
    O_RS = 5

    'accelerometer
    A_SDA = 10
    A_SCL = 11
    A_CS = 12
    A_Addr = %00111010

    OBJ
    OLED : "uOLED-128-GMD1"
    A_I2C : "i2cobject"
    DELAY : "Clock"

    VAR
    byte dispX
    byte dispY
    byte reds[noparse][[/noparse]255]
    byte greens[noparse][[/noparse]255]
    byte blues[noparse][[/noparse]255]

    PUB main
    OLED.INIT(O_TX,O_RX,O_RS)
    OLED_SETUP
    cls
    print(TINY_FONT,0,255,0,string("Testing..."),ISTEXT,0)
    A_I2C.Init(A_SDA,A_SCL,false)
    dira[noparse][[/noparse]A_CS]~~
    outa[noparse][[/noparse]A_CS]:=1 ' SPI=0 , I2C=1
    delay.pausesec(1)
    writeRegister($20,%01000111) ' power up! ctr reg1
    printRegister($20,true) ' ctr reg1
    delay.pausesec(3)
    repeat
    printRegister($28,true) ' high byte of x output
    printRegister($29,true) ' low byte of x output
    printRegister($2A,true) ' high byte of y output
    printRegister($2B,true) ' low byte of y output
    printRegister($2C,true) ' high byte of z output
    printRegister($2D,true) ' low byte of z output
    delay.pausesec(1)
    cls

    pri printRegister(register, isNewLine) | c
    register |= %10000000
    c:=A_I2C.readLocation(A_ADDR,register,8,8)
    print(c,TINY_FONT,0,255,0,ISNBR,isNewLine)

    pri writeRegister(register,value)
    register |= %00000000
    A_I2C.writeLocation(A_ADDR,register,value, 8,8)

    pri showAccDevicePresent | c
    c:=a_i2c.devicePresent(A_ADDR)
    print(c,TINY_FONT,0,255,0,ISNBR,true)

    pri OLED_SETUP
    OLED.ERASE
    DELAY.PauseMSec(20)
    OLED.BACKGROUND(0,0,0)

    pri cls
    oled.erase
    dispX:=0
    dispY:=0

    pri print (text,FONT, R,G,B, dTYPE,ISNEWLINE)
    OLED.FTEXT(dispX,dispY,FONT,R, G,B,text,dTYPE)
    dispX += strsize(text)
    if(ISNEWLINE)
    newLine
    handleCursor

    pri newLine
    dispY++
    dispX:=0

    pri handleCursor
    if(dispX > 20)
    dispY++
    dispX:=0
    if(dispY > 14)
    delay.pausesec(1)
    cls
  • ZootZoot Posts: 2,227
    edited 2008-01-11 06:14
    The Propeller code is very different from what you would do with a Stamp. I wouldn't convert it. The posts sending you to the Prop forums were regarding interfacing 3.3v and 5v devices.

    I would revisit the SHIFTIN/SHIFTOUT sections of the Pbasic manual, then check your data sheet. There aren't too many parameters for shifting in or out to the device -- bit-size (e.g. byte, word, 11 bits, etc.), if the commands/data are MSB or LSB (most-significant bit or least-significant bit) and if data is clocked before or after the clock pulse.

    Your device includes a register (memory address) called "WHO_AM_I" -- a first task might be to successfully read from that address and get the correct data. This way you know that you are sending a command and then reading the response back correctly. Then you can expand on that to read (and interpret) the data you want. But communicating with the device is the first step.

    See my note above regarding using just resistors to protect the device from the 5v out of the Stamp. Probably your best bet.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
Sign In or Register to comment.