Shop OBEX P1 Docs P2 Docs Learn Events
Sensor SPI connection with my BS2 — Parallax Forums

Sensor SPI connection with my BS2

Frank_newbeeFrank_newbee Posts: 6
edited 2009-03-14 18:25 in BASIC Stamp
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!!!

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-02-22 15:32
    First of all, the application note indicates that the accelerometer uses I2C protocol which is completely different from SPI. There are some Nuts and Volts Columns on I2C and how to do I2C protocol using a BS2. The BS2p series of Stamps have built-in I2C statements, but the BS2 does not. Try #85 and #79 here: www.parallax.com/Resources/NutsVoltsColumns/tabid/272/Default.aspx.

    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
  • Frank_newbeeFrank_newbee Posts: 6
    edited 2009-02-22 18:07
    First of all thanks mike for your reply.

    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.
  • Mike GreenMike Green Posts: 23,101
    edited 2009-02-22 18:39
    First of all, the data in the SHIFTOUT statement won't work. If you're going to split up the bits, you will need a "%" in front of the middle 6 bit portion. As you've written it, it gets interpreted as a decimal "1111", not a binary "%001111".

    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).
  • ArnoNLArnoNL Posts: 15
    edited 2009-02-23 08:03
    I've tried this also a few weeks ago with·an ADC. Got it working with a similar code you used, except mike's remark I don't see any errors. Are you sure the 00011110 is the right binary number?




    Post Edited (ArnoNL) : 2/23/2009 10:07:38 AM GMT
  • Frank_newbeeFrank_newbee Posts: 6
    edited 2009-02-23 11:12
    Hello Mike,
    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
  • Frank_newbeeFrank_newbee Posts: 6
    edited 2009-02-24 09:20
    Could it be that my supply voltage is giving this problem of is it something within the code?

    ·
  • Frank_newbeeFrank_newbee Posts: 6
    edited 2009-03-14 18:25
    Below you can find the code, I don't see what's wrong with it, can somebody please help me out? Do I maybe have to do something with the interrupt outputs?



    '
    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
Sign In or Register to comment.