PwmIn's precision

24 May 2012

hello everyone. I would like to read an input frequency from a humidity sensor on my mbed; So I dowloaded the PwmIn library, but I don't now the current accuracy of the pin which is configurate with the PwmIn. I read a period by using the fonction period() in second, and i would like now to read a period in microsecond. Is it possible to increase the precision of the PwmIn entry? Is the mbed enough precise to detect variations in order to microsecond. Because i want to detect little frequencie's variations. I'have already try to use the fonction period_ms() or period_us() but they don't work. Thank Mat

29 May 2012

anyone?

30 May 2012

PwmIn (at least the library I found) uses the timer.read function, which returns the time in seconds, but with floating point precision. In other words, if you want the period in microseconds, simply multiply it by one million.

If you already tried that, then I assume you accidently did a conversion to integer (too soon). If you want to work with integers, make sure you first multiply it by a million, and then convert it to integer, otherwise it will first round the float. Lets say your period is 20ms, so 0.02 seconds. If you round that first, the result is zero.

30 May 2012

I think you should consider using the capture inputs of the timer to get the best possible accuracy. From what I read the PWMin used the external interrupt inputs so there is some delay from the edge of the input to the actual reading.

30 May 2012

True, I considered mentioning that, however from his post I got the impression that the period of the input is not very small. So then the delay is equal for both edges, and should not be a problem.

If I am wrong and you are sampling a high frequency signal, then the InterruptIn functionality is indeed not ideal for you.

01 Jun 2012

thank for reply . I had a signal from 48kHz to 58 KHZ, so I used to have a period of 0.00002 seconds. I put it in a variable called frequency=1.0/period_red. But the accuracy wasn't so good, so I change my signal by replacing resistors. I have now a signal from 1.5khz to 2.5 Khz, the accuracy is better but that is not perfect. What is the capture input ? Sounds good

01 Jun 2012

Quote:

A transition on a capture pin can be configured to load one of the Capture Registers with the value in the Timer Counter and optionally generate an interrupt.

You can read about it in the user manual, basically you keep the timer running an when the programmed edge occurs (rising, falling or both) the timer counter value is copied in the capture register so you can get the counter value of two events and subtract them to calculate the frequency. The good part is that the counter value is stored in hardware level immediately when the event occurs so the time it takes from the event until you get to an interrupt to read the value doesn't matter as long as you can read the stored value before the next event that will overwrite it.

Alex