Best method to Measure Pulse Width in PASM
SailerMan
Posts: 337
I'm working in PASM and I am learning as much as I can on my own with some help from various users on here.
I have finally been able to write a program that sends data to a 74hc595 shift register..it was a great day for me.. [noparse]:)[/noparse]
Now I want to read data from a Maxbotix Sonar sensor... using the pulse width method.. I'm just wondering if someone can point me in the right direction.
I was playing with WaitPEQ and WaitPNE command and measuring the time difference... Is it that simple? Any Guidance would be appreciated.
Regards,
Eric
PS oh yeah.. What about dividing CNT by 1000 ? Division in PASM is well beyond me at this point in time.
I have finally been able to write a program that sends data to a 74hc595 shift register..it was a great day for me.. [noparse]:)[/noparse]
Now I want to read data from a Maxbotix Sonar sensor... using the pulse width method.. I'm just wondering if someone can point me in the right direction.
I was playing with WaitPEQ and WaitPNE command and measuring the time difference... Is it that simple? Any Guidance would be appreciated.
Regards,
Eric
PS oh yeah.. What about dividing CNT by 1000 ? Division in PASM is well beyond me at this point in time.
Comments
Use counter A to count in case pin is low
Use counter B to count in case pin is high
From time to time (the time should be close to the frequency of the PWM signal) you read the counters and set them to zero again.
The ratio of the read counter values is equal to the pulse/pause ratio.
Of course you can do other things while the counters count or you wait with a waitcnt ...
As for dividing cnt by 1000, the easiest way is to have the SPIN portion of your code do the dividing (before or after the PASM is called, depending on your requirements).
If you want to do it in PASM, I haven't used this in a while, but I don't see why I would have broke the code any time recently. Put numerator in par1, denominator in par2, and the result will be stored in ret_val. The inputs are limited to 16 bits. I know there is better dividing code out there
Spin uses this code (32-bit compatible), but it is a bit more of mess:
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
April, 2008: when I discovered the answers to all my micro-computational-botherations!
Some of my objects:
MCP3X08/4 ADC Driver - Programmable Schmitt inputs, frequency reading, and more!
Simple Propeller-based Database - Making life easier and more readable for all your EEPROM storage needs.
String Manipulation Library - Don't make strings the bane of the Propeller, bend them to your will!
Post Edited (Bobb Fwed) : 10/28/2009 3:48:19 PM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
April, 2008: when I discovered the answers to all my micro-computational-botherations!
Some of my objects:
MCP3X08/4 ADC Driver - Programmable Schmitt inputs, frequency reading, and more!
Simple Propeller-based Database - Making life easier and more readable for all your EEPROM storage needs.
String Manipulation Library - Don't make strings the bane of the Propeller, bend them to your will!
Lawson
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Lunch cures all problems! have you had lunch?
Yes, that is by far the easiest, fastest method. But then you are forced into binary division. But seeing as you now using a type of "binary" division, it limits your output. I would guess you would want the dividing by 1000 to return your results as some multiple of a second (milliseconds, microseconds, etc.).
It all really depends on your application of the dividing by 1000. Can't wait until the Prop II, it is supposed to have some type of value dividing circuitry (not single-cycle instruction). Sounds interesting, but would be very nice to have.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
April, 2008: when I discovered the answers to all my micro-computational-botherations!
Some of my objects:
MCP3X08/4 ADC Driver - Programmable Schmitt inputs, frequency reading, and more!
Simple Propeller-based Database - Making life easier and more readable for all your EEPROM storage needs.
String Manipulation Library - Don't make strings the bane of the Propeller, bend them to your will!
shpos is bits to shift (in this case 10 is probably what you'll want to set it to)
EDIT: fixed typo
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
April, 2008: when I discovered the answers to all my micro-computational-botherations!
Some of my objects:
MCP3X08/4 ADC Driver - Programmable Schmitt inputs, frequency reading, and more!
Simple Propeller-based Database - Making life easier and more readable for all your EEPROM storage needs.
String Manipulation Library - Don't make strings the bane of the Propeller, bend them to your will!
Post Edited (Bobb Fwed) : 10/28/2009 8:25:51 PM GMT
I decided to follow Jeff Martin's ASM web seminar example and have the ASM code measure the High Time of a pulse from a function generator attached to pin 10.
I am using the Pro Deveopment Board.
The high time measurement using a cog's counter A ended up being the same as that of the BS2 Functions code.
Looking at that was a good learning exercise, my original stab at it did not wait for the signal to be low before clearing the counter. That did not work.
I was giving the counter function more credit for watching for that than I should have.
The best examples of using the counters is in PELabsFunBook-v1.1.pdf.
Next version will be to add a Command = 2 to use counter B to measure the low time. Just for leaning.
I also attached a spin only using the BS2 Functions, at the time I typed this in my code was doing nothing and this told me
the counter can do it I just did not have it figured out.
Using the counter is a clean way to do it with microsecond displayed results.
Tom