External Trigger pulse reading

Thread Starter

maanas.adi

Joined Nov 3, 2011
12
I have been using PIC18 micro-controller for generating reference signal and a charging signal at 100 Hz rate max. Now I have to generate this charging signal on sensing an external trigger pulse of width 15us with frequency in between 1 to 100 Hz. I am already using all the 5 timers of PIC18 for different purposes.
I am thinking of using external interrupt on rising edge but how to check 15uS pulse width. I mean what if the external pulse only rises to logic high and doesn't go to logic low. How would I know that?
How to verify that the input pulses are in integer format only i.e. 1Hz, 2Hz and not 1.5Hz or any other values?
How to mark limit for sensing up to 100 Hz only?
 

AlbertHall

Joined Jun 4, 2014
12,346
Depending how the timers are set up you may be able to use one of them to time the input pulses as well as whatever it does already. As long as the timer is running continuously and not reset then you can read the value on each input edge and get the time between edges. Possibly you could use the CCP module to do that more automatically.
 

John P

Joined Oct 14, 2008
2,026
I question whether the use of so many timers is a good idea. I acknowledge that you might need to do it if the intervals are short, but I would try to use only a single one, running at some fast rate, with counters to calculate longer intervals. Some of these might not even need execution in an interrupt: you could set a flag there and when it's found in the main() loop, you'd clear it and do whatever the task is. It all depends on what you need concerning priority and accuracy of timing etc. But by using only a single timer, or just a few, you aren't limited to what the chip provides.

If the issue is measuring a pulse width, PIC chips can do this in hardware using the capture input and one of the timers. Would that solve the problem?
 

Thread Starter

maanas.adi

Joined Nov 3, 2011
12
This is what I am thinking of doing. After getting the external interrupt at the rising edge, I'll wait for 15us in the interrupt routine and then check for the pin status, if it does not go low then there is some error. Is this OK.
I may spare one timer but how do use that for checking external signal frequency.
 

spinnaker

Joined Oct 29, 2009
7,830
How to verify that the input pulses are in integer format only i.e. 1Hz, 2Hz and not 1.5Hz or any other values?
What?

Floating point is a human concept. Your timers are all integers or to be more exact bytes. How you present that to the user is entirely up to you.
 
Last edited:

ErnieM

Joined Apr 24, 2011
8,377
I agree. To what limits are you validating the input signal?

You cannot test this to be 15 us, you can only test to be within some tolerance of 15 us. Same is true with the period: is 1.0001 Hz acceptable? How about 1.01? Or 1.1 Hz?

And what do you do with an out of tolerance input? Ignore it?
 

Thread Starter

maanas.adi

Joined Nov 3, 2011
12
That is correct. What I meant to say regarding integer values of frequency is that suppose user has set a value of 2Hz and the input pulse frequency is of 3 Hz. How do I check that when I have to generate a signal at every rising edge.
 

ErnieM

Joined Apr 24, 2011
8,377
That is correct. What I meant to say regarding integer values of frequency is that suppose user has set a value of 2Hz and the input pulse frequency is of 3 Hz. How do I check that when I have to generate a signal at every rising edge.
How is the user setting the value? How is the input frequency generated?

What element of your system can detect both the user setting AND the input frequency?

These questions remain a mystery.

If you wish to limit the "sensing" the frequency to 100 Hz or less simply do not sample the input until 10 mS have passed. You may get some weird pulses out, but you have limited your output rate.

You may also want to lock out the output if you get a second edge during the 10mS delay until you get more reasonable inputs.
 

MrSoftware

Joined Oct 29, 2013
2,200
I believe to sample a signal you have to sample at least at Nyquist rate, which is 2x the Nyquest frequency (highest frequency in the system). So to sample a 100Hz sine wave, which is 50% duty cycle with highest frequency being 100Hz, you need to sample at 200Hz. The catch is your signal is only 15us long. You can fit 66666.7 of those into a second. So to accurately catch the pulse, you need to sample at (66666.7 x 2) or 133333.4 Hz, or one sample every 7.5us. So if you interrupt on the rising edge, you would then need to sample every 7.5us to catch the end of the pulse, or start of the next pulse. Assuming that's what you're trying to do? You can then either wait for another interrupt, or keep sampling every 7.5us to catch the next pulse.

Edit - Actually now that I think about it... If you're getting pulses that can be 15us long, but back to back, then you're going to have to sample higher than the Nyquist rate, otherwise you might just catch the rising or high state of every signal, making it appear that your signal is staying high. What's the minimum pause, or minimum low-state between 15us long pulses?
 
Last edited:

Thread Starter

maanas.adi

Joined Nov 3, 2011
12
If you wish to limit the "sensing" the frequency to 100 Hz or less simply do not sample the input until 10 mS have passed. You may get some weird pulses out, but you have limited your output rate
Yes that's a good option. After generating the signal on the first rising edge, a timer of 10ms will be started and till the timer completes no further pulses will be checked.
But how to check for exact set frequency i.e. 1Hz 1sec, 2Hz 500ms, 5Hz 200ms ..... and so on.
 

ErnieM

Joined Apr 24, 2011
8,377
First you need to define "exact," because if you go by the dictionary definition your unit will never do anything since nothing is exactly exact in this world.

If you have a timer to use then you can measure the input frequency (perdiod actually) and use that. You could use the same timer to validate both the high time and the low time to insure the pulse width and the rep rate.

MrSoftware: Nyquist sampling is not applicable to this issue. The goal is to check pulse widths, not to somehow reproduce the input wave.
 
Top