Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 12:51 13 Jul 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

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: Sweden
Posts: 36
Posted: 09:47pm 27 Mar 2015
Copy link to clipboard 
Print this post

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.
Edited by bobo2 2015-03-29
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4038
Posted: 10:52pm 27 Mar 2015
Copy link to clipboard 
Print this post

Well 1kHz is 1ms kind of time so MMBasic should be plenty fast enough.

John
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2933
Posted: 11:10pm 27 Mar 2015
Copy link to clipboard 
Print this post

@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!

WWEdited by WhiteWizzard 2015-03-29
 
bobo2
Newbie

Joined: 26/10/2012
Location: Sweden
Posts: 36
Posted: 11:28pm 27 Mar 2015
Copy link to clipboard 
Print this post

  JohnS said   Well 1kHz is 1ms kind of time so MMBasic should be plenty fast enough.

John

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: Sweden
Posts: 36
Posted: 11:32pm 27 Mar 2015
Copy link to clipboard 
Print this post

  WhiteWizzard said   @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


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 Kingdom
Posts: 2933
Posted: 11:46pm 27 Mar 2015
Copy link to clipboard 
Print this post

@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: Australia
Posts: 935
Posted: 11:54pm 27 Mar 2015
Copy link to clipboard 
Print this post

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: Sweden
Posts: 36
Posted: 12:16am 28 Mar 2015
Copy link to clipboard 
Print this post

  WhiteWizzard said   @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

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: Sweden
Posts: 36
Posted: 12:21am 28 Mar 2015
Copy link to clipboard 
Print this post

  BobD said   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.

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 Kingdom
Posts: 10246
Posted: 02:22am 28 Mar 2015
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 2933
Posted: 03:04am 28 Mar 2015
Copy link to clipboard 
Print this post

Which version of MMBasic do you have there on your DuinoMite??
 
bobo2
Newbie

Joined: 26/10/2012
Location: Sweden
Posts: 36
Posted: 05:51am 28 Mar 2015
Copy link to clipboard 
Print this post

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!Edited by bobo2 2015-03-29
 
bobo2
Newbie

Joined: 26/10/2012
Location: Sweden
Posts: 36
Posted: 07:04am 28 Mar 2015
Copy link to clipboard 
Print this post

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 Kingdom
Posts: 2933
Posted: 07:34am 28 Mar 2015
Copy link to clipboard 
Print this post

Take a look at the bottom of this page!


Special Version of V4.5 with Support for CAN (including DuinoMite version) . . . Edited by WhiteWizzard 2015-03-29
 
bobo2
Newbie

Joined: 26/10/2012
Location: Sweden
Posts: 36
Posted: 08:35am 28 Mar 2015
Copy link to clipboard 
Print this post

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: Australia
Posts: 6269
Posted: 11:04am 28 Mar 2015
Copy link to clipboard 
Print this post

  bobo2 said   but just ran the dos version and PULSIN results in "Error: Unknown command"...

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: Sweden
Posts: 36
Posted: 11:44am 28 Mar 2015
Copy link to clipboard 
Print this post

  Quote  DuinoMite MMBasic Version 4.5 with CAN
Copyright 2011-2014 Geoff Graham

> pulsin
Error: Unknown command
>


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: Australia
Posts: 935
Posted: 11:58am 28 Mar 2015
Copy link to clipboard 
Print this post

  bobo2 said  
  Quote  DuinoMite MMBasic Version 4.5 with CAN
Copyright 2011-2014 Geoff Graham

> pulsin
Error: Unknown command
>


So I updated the duinomite and you can see the result above.

Back to PULSIN - what would it take to make a CFUNCTION?

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 States
Posts: 147
Posted: 12:05pm 28 Mar 2015
Copy link to clipboard 
Print this post

What about a comparator circuit with the other side coming from your mite at 1khz 50% duty?
 
bobo2
Newbie

Joined: 26/10/2012
Location: Sweden
Posts: 36
Posted: 12:15pm 28 Mar 2015
Copy link to clipboard 
Print this post

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    
Print this page
The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025