Shop OBEX P1 Docs P2 Docs Learn Events
Accelerometer PWM-measurement — Parallax Forums

Accelerometer PWM-measurement

wackowacko Posts: 10
edited 2014-12-16 07:55 in Propeller 1
Hi guys,

in lack of analog/digital converters in the propeller (at least I couldn't find anything about it) I try to read analog data from an accelerometer by pwm signals. The manual says something about using counters for measuring pwm but I don't get it. Can anyone tell me how to do it or where to find more information on that topic for beginners?

Thanks a lot

Comments

  • sylvie369sylvie369 Posts: 1,622
    edited 2008-02-15 14:50
    Perhaps I'm the confused one, but I haven't seen accelerometers that code their output by pulse width, and unless I'm completely and hopelessly confused about this, you can't just read pulse width from a regular analog in variable voltage signal. You need an ADC.

    [noparse][[/noparse]Edit:] Okay, it's just me. blush.gif

    Post Edited (sylvie369) : 2/15/2008 7:01:29 PM GMT
  • JavalinJavalin Posts: 892
    edited 2008-02-15 15:04
    The memisc accelerometers output a variable length pulse to indicate acceleration. To read analog voltages on the propeller - look at the MCP3208 ADC converter objects in the Propeller Object Exchange (obex.parallax.com). Its really easy!

    The propeller is a true General Purpose chip - all pins are equal, except P28-P31 which have special startup features and thus are more equal than others...!

    You can interface almost anything to it!

    James
  • wackowacko Posts: 10
    edited 2008-02-15 15:13
    Hi sylvie369

    I use an ADXL213AE (Analog Devices) which outputs both analog voltage and digital pwm. As far as I know the internal acceleration measurement outputs pwm which is externally smoothed to analog voltage output. I used an Arduino board before which has integrated ADC that worked fine but I don't know how to decode the pwm. I suppose you just have to compare the time (or clock cycles) the signal is high to the time it is low. When getting high again one duty cycle is completed. The resulting ratio is the measured acceleration. So you need a high sampling rate which I hoped to get by using the integrated counters. If I just knew how exactly to do it.
  • wackowacko Posts: 10
    edited 2008-02-15 15:28
    Hi Javalin

    thanks for the hint. I think the memsic2125 object does what I want to do.
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-15 16:05
    Yes it will. It is not even an "algorithm", it is plain simplicity...

    @Kramer et al.: This is very good example where the engagement of TWO COUNTERS simplifies the matter considerably. Could be done with one timer as well, but with a little bit reduced accuracy. As the counters do the work there is is no need for machine code, SPIN will do it JUST AS PRECISE (Exercise: turn the program to SPIN!) However you need a separate COG.

    @wacko: Sorry for hijacking your thread..
    {                               ********************************************
                                    Melexis MXD2125 Dual-Axis Accelerometer V1.0
                                          coded by Paul Baker (Parallax) 
                                    ********************************************}
    
    VAR              'variable which contains the cog identifier and Raw sensor data
    long cog, XVal, YVal    
    
    PUB stop                                                               
        '' Stop driver - frees a cog
        if cog
           cogstop(cog)
    
    PUB start(Xin,Yin):okay
        ctramode := %1000 << 26 + Xin     'construct value for counter A mode (POS accum.)
        ctrbmode := %1000 << 26 + Yin     'construct value for counter B mode (POS accum.)
        pinwaitm := |< Xin + |< Yin       'construct pin mask for waiting
    
         okay := cog := cognew(@MXD2125, @XVal)      
    
    PUB x
      return XVal                                   'return current X axis pulse width
    
    PUB y
      return YVal                                   'return current Y axis pulse width
    
    DAT
    MXD2125       org
                  'set shared memory locations
                  mov Xarg, par                     'store location to write X value 
                  mov Yarg, par                     'store location to write Y value
                  add Yarg, #4                      'adjust location of Y value
                  'establish counter A as pulse width counter
                  mov frqa, #1                      'add 1 to counter A total each cycle pin is high
                  mov ctra, ctramode
                  'establish counter B as pulse width counter
                  mov frqb, #1                      'add 1 to counter B total each cycle pin is high
                  mov ctrb, ctrbmode
    :loop         waitpeq zero, pinwaitm            'wait until both channels have completed cycle
                  mov temp, phsa                    'copy X axis value to hub 
                  wrlong temp, Xarg
                  mov temp, phsb                    'copy Y axis value to hub
                  wrlong temp, Yarg
                  mov phsa, zero                    'reset counter values
                  mov phsb, zero
                  waitpeq pinwaitm, pinwaitm        'wait until both channels start thier cycle
                  jmp #:loop                        'do it again
    
    zero          LONG 0                            'value zero
    temp          LONG 0                            'temporary loaction to store counter value
    Xarg          LONG 0                            'address to store X axis pulse width
    Yarg          LONG 0                            'address to store X axis pulse width
    ctramode      LONG 0                            'counter A mode value
    ctrbmode      LONG 0                            'counter B mode value
    pinwaitm      LONG 0                            'pin mask used in waitpeq instruction
    
  • wackowacko Posts: 10
    edited 2008-02-15 17:45
    Never mind help is always appreciated.

    I think I've got it now. It actually doesn't seem that complicated. But I still have a question about the memory allocation in your example (probably it's obvious but I'm still a newb):

    add Yarg, #4
    -means you're moving the memory address given by XVal by 4 to get a location for Yarg(?)
    -why exactly 4 and why can you access the data by YVal?
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-15 20:45
    Have a look at this definition
    long cog, XVal, YVal
    


    It allocates YVal directly after YVal. As they are LONGs (= 4 bytes) the address difference is 4. When installing the COG by COGNEW the address of XVal is copied to the COG register PAR

    But as I said - you need no assembly code for this problem...
  • dennodenno Posts: 223
    edited 2014-12-13 14:55
    I am trying to teach myself SPIN...after a decade or more using the BS2sx. Can someone please translate the following in simple english...thanks.

    ctramode := %1000 << 26 + Xin

    denO
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-12-13 15:14
    denno wrote: »
    Can someone please translate the following in simple english...thanks.

    ctramode := %1000 << 26 + Xin

    The "%1000" is "8" expressed in binary. This value is being used to set the appropriate bits in the counter A mode setting. The "<< 26" shifts the bits to the desired position. The "Xin" is the pin number connected to the X-axis pulse output of the accelerometer. The pin number occupies the last five bits of the counter setting. Since these bits were zero adding the pin number has the same effect as "or"ing the value to set these bits high.

    I confess to still thinking of the counter bits as being "magic" and I just follow the examples I find in programs and on the forum. There is a section in the Propeller manual about setting up counters and there's an appnote (#1) about counters. I have links to all the appnotes in post #3 of my index (see my signature for a link).
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2014-12-13 16:42
    denno wrote: »
    I am trying to teach myself SPIN...after a decade or more using the BS2sx. Can someone please translate the following in simple english...thanks.

    ctramode := %1000 << 26 + Xin

    denO

    The control register for the A counter is called CTRA has a 5-bit field to set the counter mode and these 5 bits are located in bit positions 26..30. Seeing us programmers are lazy people we just say what 5-bit patten or code we want and then we get the compiler to put it in the right position with the << 26 which will shift that pattern left 26 bits. Finally we add in the pin number that we want it to connect to as this is specified in the control registers as bits 0..4 so we don't need to shift anything to blend it in.

    BTW, as mentioned % is the prefix for binary numbers so %1000 is the same as 01000 and I put a zero in front to pad it out to 5-bits, and in the Propeller manual this tells you that this is the mode for a POS detector.

    Looking at the 32-bits of CTRA (via ctramode holding variable)

    CTRA bit fields
    332222222222111111111110000000000
    10987654321098765432109876543210
    ================================
    00100000000000000000000000000000 = %1000 << 26
    if Xin = 2
    00000000000000000000000000000010
    so ctramode =
    00100000000000000000000000000010
  • dennodenno Posts: 223
    edited 2014-12-14 03:39
    The pin number occupies the last five bits of the counter setting. Since these bits were zero adding the pin number has the same effect as "or"ing the value to set these bits high.

    OK...chapter two...I guess. Color me stupid...but isn;t there a simpler way to do the above? A simpler way of explaining, or perhaps I should just stick with the basic stamp...

    denno
  • Heater.Heater. Posts: 21,230
    edited 2014-12-14 04:21
    deno,
    ...but isn't there a simpler way to do the above? A simpler way of explaining,...
    No, it's about as simple as possible. What we have there in "%1000 << 26 + Xin" is simply three numbers and two operators. So it's no more complex than "x + y + z" which I'm sure you will agree is very simple.

    Admittedly if you have not seen the "<<" operator before it looks a bit scary but one you learn that it just means "shift the bits of the number on the right hand side to the left by the number of places given by the number on the right hand side" it's no harder to understand than "+".

    The "%" thing may look odd as well but it simply means "this number is binary".

    Such shifting, adding, oring, anding operations are very common in all kinds of code in Spin or C or JavaScript or a ton of other languages so it's probably something one should get comfortable with.
    ...or perhaps I should just stick with the basic stamp.
    No. Join the party. The Propeller is great fun when you get acquainted with it.
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2014-12-14 04:29
    denno wrote: »
    The pin number occupies the last five bits of the counter setting. Since these bits were zero adding the pin number has the same effect as "or"ing the value to set these bits high.

    OK...chapter two...I guess. Color me stupid...but isn;t there a simpler way to do the above? A simpler way of explaining, or perhaps I should just stick with the basic stamp...

    denno

    Is there a simpler way of "explaining" 8 * 2^26 + 2? I don't think you can reduce everything down to beyond it's fundamentals. Would you think ctramode = %00100000000000000000000000000010 any simpler?

    Okay, in Forth I would just say 2 APIN %1000 CTRMODE or perhaps having created some definitions I could just say 2 POSDETECT but at some point you have to understand the hardware otherwise nothing makes sense.

    Stick with the Basic Stamp if it does what you want, it is very simple because it is very "basic". Spin itself is simple though, don't confuse the hardware with the software.

    BTW, I just realized you hijacked a very old thread, that is very poor etiquette, definitely start a new one if you want to continue and give it a meaningful thread title like "new to Spin, need help" or something like that.
  • dennodenno Posts: 223
    edited 2014-12-15 12:14
    Wow..we...zow...we...I got it! Thanks to all. As usual I was looking at it from "outside the box", instead of "inside the box. Moving the %1000 << 26 puts it in the right "spot" for the ctramode to indicate that is is a positive pulse detector.and "adding" the Xin puts that value in the Apin location. I am not used to dealing with bit's, as in Basic, I use DEC and lots of CON's and lots of VAR's so that the resulting program reads like a book.

    I will start a new thread, instead of hijacking this old one....

    Thanks to all of you for your help.

    dennO
  • Heater.Heater. Posts: 21,230
    edited 2014-12-15 17:15
    What you describe doing in BASIC sounds like good practice to me.

    Here though we see that sometimes decimal is not a good representation, it hides what we are wanting to do. Setting bit fields in hardware registers is the classic case where we want to see the bits. In binary or hexadecimal. Whatever fits the field width. And those shifts are the simple way to think about getting those bits into the right places.

    You will soon be fluent in this. Have fun.
  • lardomlardom Posts: 1,659
    edited 2014-12-16 07:55
    denno wrote: »
    Wow..we...zow...we...I got it! Thanks to all. As usual I was looking at it from "outside the box", instead of "inside the box. Moving the %1000 << 26 puts it in the right "spot" for the ctramode to indicate that is is a positive pulse detector.and "adding" the Xin puts that value in the Apin location. I am not used to dealing with bit's, as in Basic, I use DEC and lots of CON's and lots of VAR's so that the resulting program reads like a book.

    I will start a new thread, instead of hijacking this old one....

    Thanks to all of you for your help.

    dennO
    I actually smiled when I read that. I can safely assume you won't give up on the Propeller. It is an amazing microcontroller.
    When you have doubts this community is the absolute best resource available.
Sign In or Register to comment.