Pulse Width input

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
mimiyum
Posts: 94
Joined: Wed Jul 16, 2008 4:39 pm
Location: Philippines
Been thanked: 1 time
Contact:

Pulse Width input

Post 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.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Pulse Width input

Post 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.

mimiyum
Posts: 94
Joined: Wed Jul 16, 2008 4:39 pm
Location: Philippines
Been thanked: 1 time
Contact:

Re: Pulse Width input

Post 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?

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Pulse Width input

Post 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.

mimiyum
Posts: 94
Joined: Wed Jul 16, 2008 4:39 pm
Location: Philippines
Been thanked: 1 time
Contact:

Re: Pulse Width input

Post 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.
Attachments
Pulse Width Input.fcf
(8.5 KiB) Downloaded 382 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Pulse Width input

Post 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

User avatar
Jan Lichtenbelt
Posts: 797
Joined: Tue Feb 17, 2009 8:35 pm
Location: Haren GN, the Netherlands
Has thanked: 128 times
Been thanked: 264 times
Contact:

Re: Pulse Width input

Post 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

Post Reply