PWM read with ADC stm8

Thread Starter

Prajeet Anand

Joined Aug 26, 2014
21
I want to read PWM signal with an A/D converter. Is it reliable to take 100 samples of adc data and average it out.Im still seeing some spurious values in-between my captured data. What would be a reliable way to acquire precise output?
 

JohnInTX

Joined Jun 26, 2012
4,787
What would be a reliable way to acquire precise output?
You can do it directly without the ADC.

Assuming a PWM that is high for a percentage of the total time:

First determine the base time by counting the time between successive low to high transitions of the PWM signal.
Then count the high time of the signal.
Compute the duty cycle as high time / base time.
Use that fraction to compute the % of full scale of whatever variable the PWM represents.
 
Last edited:

Thread Starter

Prajeet Anand

Joined Aug 26, 2014
21
You can do it directly without the ADC.

Assuming a PWM that is high for a percentage of the total time:

First determine the base time by counting the time between successive low to high transitions of the PWM signal.
Then count the high time of the signal.
Compute the duty cycle as high time / base time.
Use that fraction to compute the % of full scale of whatever variable the PWM represents.
Thank you. Sorry I should have been more clear. I'm trying to read pulses with my adc which gives me random values in between. I'd like to acquire data only when there's high in the output.
 

JohnInTX

Joined Jun 26, 2012
4,787
Thank you. Sorry I should have been more clear. I'm trying to read pulses with my adc which gives me random values in between. I'd like to acquire data only when there's high in the output.
Ahh.. OK. You could just poll the PWM input and start conversions when it is high. When the conversion is done, poll again before starting the next conversion to make sure it is still high. If not, you know that the last reading should be discarded and you have to wait for the next PWM cycle to begin again. You could use the external interrupt to sync the PWM output to your code to ensure that you start the conversion near the beginning of the ON pulse.
 

Thread Starter

Prajeet Anand

Joined Aug 26, 2014
21
Ahh.. OK. You could just poll the PWM input and start conversions when it is high. When the conversion is done, poll again before starting the next conversion to make sure it is still high. If not, you know that the last reading should be discarded and you have to wait for the next PWM cycle to begin again. You could use the external interrupt to sync the PWM output to your code to ensure that you start the conversion near the beginning of the ON pulse.
Thank you. I'll try that
 
Top