Sensor SPI connection with my BS2
Frank_newbee
Posts: 6
Dear all,
I'm Frank, I am a mechanical engineer from Belgium. Always been a happy RC and diorama building enthousiast but just started to explore the DIY robotica hobby. Of course bought some basic stamp stuff (starter kit and experiment kit with 2 spare stamps). I'm really enthousiastic about this, what a great hobby. I can see this hobby detaching me from my other hobbies.
I'm 35 years old happily married with 2 small·kids. What more for an introduction....I also like swimming, and that's about it. If you want to know more just PM me.
Reading this forum for almost a year I know have a question of my own. As I'm now trying to establish a SPI connection between my stamp and a freescale accelerometer I encounter a lot of problems. I have made some SPI interfaces (just write to a device) in the past ith succes but know I'm completely lost.
I hope somebody of this forum is willingly to use some of his/her precious time to get me started with this.
Thus....·I'm trying to establish a connection between my BS2 and a MMA7745L accelerometer sensor. This is the link to the sensor datasheet: http://www.freescale.com/files/sensors/doc/data_sheet/MMA7455L.pdf
An application note·(AN3468) of the sensor evaluation board I bought for my prototype. In this a start up of SPI connection is explaned: http://www.freescale.com/files/sensors/doc/app_note/AN3468.pdf?fpsp=1&WT_TYPE=Application%20Notes&WT_VENDOR=FREESCALE&WT_FILE_FORMAT=pdf&WT_ASSET=Documentation
All example codes are in C and I don't know this language.
Thank you so much for your help!!!
I'm Frank, I am a mechanical engineer from Belgium. Always been a happy RC and diorama building enthousiast but just started to explore the DIY robotica hobby. Of course bought some basic stamp stuff (starter kit and experiment kit with 2 spare stamps). I'm really enthousiastic about this, what a great hobby. I can see this hobby detaching me from my other hobbies.
I'm 35 years old happily married with 2 small·kids. What more for an introduction....I also like swimming, and that's about it. If you want to know more just PM me.
Reading this forum for almost a year I know have a question of my own. As I'm now trying to establish a SPI connection between my stamp and a freescale accelerometer I encounter a lot of problems. I have made some SPI interfaces (just write to a device) in the past ith succes but know I'm completely lost.
I hope somebody of this forum is willingly to use some of his/her precious time to get me started with this.
Thus....·I'm trying to establish a connection between my BS2 and a MMA7745L accelerometer sensor. This is the link to the sensor datasheet: http://www.freescale.com/files/sensors/doc/data_sheet/MMA7455L.pdf
An application note·(AN3468) of the sensor evaluation board I bought for my prototype. In this a start up of SPI connection is explaned: http://www.freescale.com/files/sensors/doc/app_note/AN3468.pdf?fpsp=1&WT_TYPE=Application%20Notes&WT_VENDOR=FREESCALE&WT_FILE_FORMAT=pdf&WT_ASSET=Documentation
All example codes are in C and I don't know this language.
Thank you so much for your help!!!
Comments
I re-read the datasheet and it looks like the device uses either SPI or I2C. Sorry.
You might look at some of the existing sample code for other SPI devices that Parallax sells.
Post Edited (Mike Green) : 2/22/2009 3:41:43 PM GMT
Yes the datasheets says that SPI is possible. I really have downloaded all SPI code in relation to the BS2 on the internet nuts and volts, earlier froum discussions, app notes etc.). still I can't get it to work. I came up with the following:
' {$STAMP BS2}
' {$PBASIC 2.5}
clk PIN 0
cs PIN 1
DataIn PIN 2
DataOut PIN 3
result VAR Byte
read_acc:
DO
LOW cs
SHIFTOUT DataOut, clk, MSBFIRST, [noparse][[/noparse]%0\1, 001111\6, 0\1]··· 'who am I address of the accelerometer
SHIFTIN DataIn, clk, MSBPRE, [noparse][[/noparse]result\8]
HIGH CS
DEBUG result
LOOP
I end up having a feedback reading of strange signs like·question marks, rectangles and percentages, no real reading.
You could also just transmit the whole 8 bit portion as a single value like: SHIFTOUT DataOut,clk,MSBFIRST,[noparse][[/noparse]%00011110]
In this case, you don't need the "\8" since that's the default (also for the SHIFTIN).
Post Edited (ArnoNL) : 2/23/2009 10:07:38 AM GMT
yes you're right. I tried the %00011110 statement before this and that also didn't work. What I also find confusing is the fact that the code (in C???) found in the application note (http://www.freescale.com/files/sensors/doc/app_note/AN3468.pdf?WT_TYPE=Application%20Notes&WT_VENDOR=FREESCALE&WT_FILE_FORMAT=pdf&WT_ASSET=Documentation) (pages 12 and 13) is much much more longer and complex than the one I've written in PBASIC. I'm afraid that I mis out on a lot of the needed code. Or is it really that "easy" (relatively, as oppossed to C) in PBASIC that you can capture all in a few code lines as oppossed to C were you need a whole page of code.
Thank you all in advance for your replies.
Anyhow, I changed the code as follows:
' {$STAMP BS2}
' {$PBASIC 2.5}
clk PIN 0
cs PIN 1
DataIn PIN 2
DataOut PIN 3
result VAR Byte
read_acc:
DO
·LOW cs
·SHIFTOUT DataOut, clk, MSBFIRST, [noparse][[/noparse]%00011110]
·SHIFTIN DataIn, clk, MSBPOST, [noparse][[/noparse]result]
·HIGH CS
·DEBUG "= ", DEC result
LOOP
I get a reading, however this is far from stable. When leaving the accelerometer chip in rest I get the following readings:
0= 255= 0= 255= 0= 255= 0= 255= 0= 15 0= 255= 0= 255= 0= 255= 31= 0= 255= 0= 255= 0= 255= 63= 0= 255= 0= 255= 0= 255= 127= etc. etc. etc.
I really don't get it, I see the pattern (in between the 0 and 255 readings I see it's counting up from 15 to·31 to 63 to 127 etc. etc.).
Thanks again guys/girls for the help you offer.
Post Edited (Frank_newbee) : 2/23/2009 1:03:40 PM GMT
·
'
in-/outputs
clk PIN 0
cs PIN 1
DataIn PIN 2
DataOut PIN 3
'
variables
result VAR Byte
'
main
read_acc:
DO
·LOW cs
·SHIFTOUT DataOut, clk, MSBFIRST, [noparse][[/noparse]%00011110]
·SHIFTIN DataIn, clk, MSBPost, [noparse][[/noparse]result]
·HIGH CS
·DEBUG "= ", DEC result
LOOP