NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Support Forum » Pulse counting, what am I actually seeing?
January 05, 2011 by Ralphxyz |
This is in reference to my Calculating RPM thread. What exactly is DutyCycle? This originates here And here is the working code:
So my question is what is DutyCycle? Is it processor clock ticks?
Thanks, Ralph |
---|---|
January 05, 2011 by bretm |
Duty cycle is the percentage of time that a periodic digital signal is in the high state. It's the "on" time divided by the "total" time. A square wave has a duty cycle of 50%. A signal that is on 1/4 of the time and off 3/4 of the time has a duty cycle of 25%. The key bit of code here is (Capt2-Capt1)/(Capt3-Capt1). The numerator is the "on" time and the denominator is the total time. |
January 05, 2011 by Ralphxyz |
Thanks bretm I really need that, but what is DutyCycle in the code? When I see on the LCD "DutyCycle = 455" 455 what?
Is this giving a percentage and therefore a duty cycle? I do not understand the logic or the math or why the cast are needed. Using my RPMsimulator code which has the DutyCycle code I see this:
I am using the tempsensor code (modified) to generate LED flash (pulse) and capturing the pulse with the Input Capture Pin (PB0) which the DutyCycle code is using as it's basis. Ralph |
January 05, 2011 by bretm |
DutyCycle is a uint8_t. It's impossible for it to be more than 255 because of that. I'd guess that what you're seeing is 39, 41, 71, 48, and not 395, 415, 715, 485. The '5' at the end seems to be display garbage coming from somewhere else. You should add two spaces before the closing quote, e.g. "Duty Cycle: %d " to see if that helps. That way if a 100 is followed by a single-digit number, the two 0's will get erased. If it does help, I expect there's some timing issue causing a capture event to be missed and wrong values calculated, e.g. "105" which would write the "5" in the third digit position. |
January 05, 2011 by bretm |
The casts force the compiler to do 32-bit multiplication, which is necessary because the two multiplicands are 16 bits each so the compiler would normally use 16-bit multiplication. But these intermediate results would overflow a 16-bit value. |
January 06, 2011 by Ralphxyz |
bretm thank you so much, this is actually starting to make sense!! I added the two spaces before the closing quote and now I see much better results.
It is interesting that DutyCycle is not linear to the ADC: input but that is not what I need to be concerned with at the moment. Thanks for explaining the cast now I see it. I wish I had a scope to see the pulses but just flashing a LED I can grasp the time/cycle. Maybe in my spare time I'll send this to my pc and use Python to generate a graph. Ralph |
Please log in to post a reply.
Did you know that multiple MOSFETs can be used in parallel to switch bigger currents on and off? Learn more...
|