Page 1 of 1
Pulse Width input
Posted: Wed Jul 13, 2011 10:55 am
by mimiyum
How can we receive pulse width signal INPUT and measure it?
I want to receive two pulse width signals and compare them. How can this be done in Flowcode?
I am using PIC16F877A. I used to do this using the ADC components and comparing the two voltage inputs. My goal is to shift to Digital inputs instead.
Re: Pulse Width input
Posted: Wed Jul 13, 2011 11:26 am
by Benj
Hello,
To correctly time a incoming pulse I would do something like this.
Reset my count variable and then wait for the input to go high.
Once the input is high I then start a timer interrupt which calls a macro which increments my count variable.
I then sit and wait for the input to go low.
Once the input has gone low I then stop the timer interrupt and the count variable contains the number of timer interrupt delays which can be used to work out the mark time.
Using two timers you could measure the mark or space of two incoming channels and then compare your two count variables.
If the two PWM signals can have different periods then you may need to measure the space portion of the signal as well as the mark.
Re: Pulse Width input
Posted: Thu Jul 14, 2011 9:40 am
by mimiyum
I was about to work in this direction but I have another question.
What if the pulse width is on 100% of the time, then it will never stop counting, right? or if the pulse is never on, then it will never start counting. How can this be worked out.
I will still need the 0% or 100% pulses as information.
Period (time for 1 cycle) for both PW inputs are the same. So, it's really just the time on that I will need.
Should we start a timer for say a period of 8ms and time how long out of the 8ms it is on?
Re: Pulse Width input
Posted: Thu Jul 14, 2011 11:06 am
by Benj
Hello Mimiyum,
Ah yes your right I hadn't thought about cases including 100% on or 100% off.
Your suggestion to time and record sections of time using the timer should also work.
You could basically have two counters for each input PWM signal, one counter for the active mark time and one for the active space time. This way if you are not aligned with the period correctly you should still get a good reading. You can also get away with only using one timer interrupt. Your timer interrupt macro would then read the first input, increment the correct counter and then repeat for the second channel.
Re: Pulse Width input
Posted: Tue Jul 19, 2011 3:54 am
by mimiyum
I made a crude program (attached) which needs some tweaking.
To see if I am really able to read the PWM input, I send it back out to PWM output.
Since I am using a 2MHz crystal, I set the PWM output to have a period of 8.192ms. I therefore count how long in 8ms is the pin on. For example, if it is on 4 counts then 4/8=50%.
This looks ok but it may affect accuracy because I can get only increments of 12.5%! I need something more accurate than that. I can only think of lengthening the time to do this but I can't afford to spend time on reading the PWM input. Everything has to happen within a few milliseconds.
Btw, please ignore the 5sec delay I inserted in the program. I put that there for simulation purposes only.
Re: Pulse Width input
Posted: Tue Jul 19, 2011 2:28 pm
by Benj
Hello,
How about changing the delay in the timer macro from 1ms to 8 microseconds or 81 microseconds and then counting these. This should give you more range in your input.
8.1ms / 8us = 1012 counts per period
8.1ms / 81us = 100 counts per period
Re: Pulse Width input
Posted: Sat Sep 17, 2011 8:10 pm
by Jan Lichtenbelt
Hello mimiyum and Ben
I'm afraid that the Pulse_Width_input.fcf does not work correctly due to the PWM intterupts (with TRM2 timer). Any delay, included the example of 81 microseconds will be longer dependend on the number of intterrupts.
Kind reagrds
Jan