![]() |
Forum Index : Microcontroller and PC projects : (MM-DM) Measuring duty cycle on a 1kHz PW
Page 1 of 2 ![]() ![]() |
|||||
Author | Message | ||||
bobo2 Newbie ![]() Joined: 26/10/2012 Location: SwedenPosts: 36 |
For my project I am trying to measure duty cycle on a 1kHz PWM signal. The duty cycle can be anything between 10% and 90%. I have been looking at this for implementation ideas: http://www.benripley.com/diy/arduino/three-ways-to-read-a-pwm-signal-with-arduino/ I also know that the interrupts on MMBasic can take from 35us to 100us to trigger (the time it takes for a basic statement to process) so I am wondering what would be the optimal strategy to read a duty cycle in MMBasic? I have been eyeballing the CFUNCTION statement in some of the samples - would a C function help? How? Or do I need to implement built in duty cycle PWM interrupts and rebuild MMBasic from source? Any ideas are appreciated! I am using Duinomite with CAN bus enabled MMBasic, but I guess this applies to MMBasic, rather than particular hardware implementation. |
||||
JohnS Guru ![]() Joined: 18/11/2011 Location: United KingdomPosts: 4038 |
Well 1kHz is 1ms kind of time so MMBasic should be plenty fast enough. John |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2933 |
@bobo2, I don't know if the frequency is high enough to somehow use an RC filter to turn the PWM into a proportional DC voltage (read in on an analogue input). Will need calibrating, but once done should be pretty accurate! WW |
||||
bobo2 Newbie ![]() Joined: 26/10/2012 Location: SwedenPosts: 36 |
Thanks for a quick answer John! True - it is 1000us, but 10% duty cycle is then 100us, isn't this a bit too close to the 30-100us a basic function takes? What would be a good strategy to measure the duty cycle? 2 interrupts - 1 on raising and 1 on falling edge and measure time? |
||||
bobo2 Newbie ![]() Joined: 26/10/2012 Location: SwedenPosts: 36 |
I tried this already, with some success... but the voltage of the wave can change too, and that makes the resultant DC voltage change. |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2933 |
@bobo2 Could you not 'zener' the pwm output so that the voltage into the RC remain fixed? Just an idea - but this is starting to get too involved with analogue electronics (an area where I can't offer too much knowledge) ![]() Good luck anyway in solving this one. Out of interest, are there any low-cost 'support' chips that can measure this for you which then output the data on I2C/SPI?? WW |
||||
BobD![]() Guru ![]() Joined: 07/12/2011 Location: AustraliaPosts: 935 |
What about inputting your signal into an analogue to digital converter then sampling the digital value at a fixed high rate. Then measure the time from 0 to 0. |
||||
bobo2 Newbie ![]() Joined: 26/10/2012 Location: SwedenPosts: 36 |
I can't, the voltage can go from 12V to 3V... I can do an op-amp and zener to make a high/low difference... but don't know enough electronics to condition the signal to stable say 0 to 5V amplitude. So no way to read it digitally with interrupts like in the link I posted on my first post? |
||||
bobo2 Newbie ![]() Joined: 26/10/2012 Location: SwedenPosts: 36 |
I am not familiar with this technique - how would this measurement from 0 to 0 work with the MMBasic when the input pulse is say 100us? And how would sampling at a high rate work on the MMBasic - the board is doing at least 4 other things... I am looking at using a ATTiny85 board with simple sample loop and output the measured value either digitally (serial) or on an output pin. Just not looking forward at having one more board to program... |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10246 |
Any reason PULSIN won't work? This waits on the required edge, then times and returns the answer accurate to 0.1uS. Then a simple calc gives you duty cycle |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2933 |
Which version of MMBasic do you have there on your DuinoMite?? |
||||
bobo2 Newbie ![]() Joined: 26/10/2012 Location: SwedenPosts: 36 |
I was not aware of PULSIN. It waits? What if there is no pulse? Where do I find more info about this? I think I have MMBasic 4.1 on this duinomite, I just saw that there is a 4.3 with support for CAN bus commands, so I can try this one. What version do I find the PULSIN in? Edit: I found this: Info on PULSIN Now I am looking to see if I can find CAN bus implementation with V4.5 Edit2: I found a v4.5 with CAN bus commands - fabulous! You guys ROCK! |
||||
bobo2 Newbie ![]() Joined: 26/10/2012 Location: SwedenPosts: 36 |
Close but no cigar... :( The latest version is 4.5, while the PULSIN seems to be in v4.5D... and I am not able to find any such version with CAN Bus... Any ideas? Can I take the PULSIN command as a CFUNCTION somehow? |
||||
WhiteWizzard Guru ![]() Joined: 05/04/2013 Location: United KingdomPosts: 2933 |
Take a look at the bottom of this page! Special Version of V4.5 with Support for CAN (including DuinoMite version) . . . ![]() |
||||
bobo2 Newbie ![]() Joined: 26/10/2012 Location: SwedenPosts: 36 |
Thanks but as I wrote I don't think the PULSIN made it in the current v4.5. I will try to update the firmware tonight, but just ran the dos version and PULSIN results in "Error: Unknown command"... I suspect the same is going to happen on the duinomite. There is no PULSIN command in the documentation or release notes either. I assume v4.5 is earlier than v4.5D - which is the version containing the PULSIN command. Any other ideas? |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6269 |
The DOS versions will give that error for any command that uses the IO pins. Jim VK7JH MMedit |
||||
bobo2 Newbie ![]() Joined: 26/10/2012 Location: SwedenPosts: 36 |
So I updated the duinomite and you can see the result above. I reckon that if the documentation for 4.5 says there is no PULSIN while the documentation for 4.6 lists PULSIN and the actual 4.5 firmware on a duinomite says "Unknown command" then maybe there is no PULSIN on the "official" v4.5. What do you think? Back to PULSIN - what would it take to make a CFUNCTION? |
||||
BobD![]() Guru ![]() Joined: 07/12/2011 Location: AustraliaPosts: 935 |
No Cfunctions available on that hardware. I think there is insufficient flash memory. If you know C then you could "always roll your own". The source code is available for the asking and agreeing to an NDA. |
||||
cwilt Senior Member ![]() Joined: 20/03/2012 Location: United StatesPosts: 147 |
What about a comparator circuit with the other side coming from your mite at 1khz 50% duty? |
||||
bobo2 Newbie ![]() Joined: 26/10/2012 Location: SwedenPosts: 36 |
Any word on availability of 4.6 for duinomite? I find the documentation for 4.6, but it seemed made for the MKII. |
||||
Page 1 of 2 ![]() ![]() |
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |