3 axis digital accelerometer reading with Stamp (ST Microelectronics LIS302DL)
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
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
Post Edited (Caleb) : 1/9/2008 12:41:39 AM GMT
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
Post Edited (Caleb) : 1/10/2008 6:45:34 AM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
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
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
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