Accelerometer PWM-measurement
wacko
Posts: 10
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
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
[noparse][[/noparse]Edit:] Okay, it's just me.
Post Edited (sylvie369) : 2/15/2008 7:01:29 PM GMT
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
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.
thanks for the hint. I think the memsic2125 object does what I want to do.
@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..
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?
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...
ctramode := %1000 << 26 + Xin
denO
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).
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
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
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. No. Join the party. The Propeller is great fun when you get acquainted with it.
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.
I will start a new thread, instead of hijacking this old one....
Thanks to all of you for your help.
dennO
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.
When you have doubts this community is the absolute best resource available.