Hi,
I am getting a square wave input as shown below which i need to be counted.
I am giving this to external interrupt of microcontroller. Since in the negative cycle i am getting noises as shown in red color, i am getting multiple interrupt WHICH IS NOT FIXED VALUE.
The frequency of wave is 0 - 500 Hz.
What should i do in HARDWARE so that i can get single pulse and hence single interrupt?
As suggested Kurenai_ryu add RC filter. or
To make sure the interrupts are valid and not only products of spikes on the pin, add a filter in the interrupt routine. The filter samples the level on the interrupt
line three/five times and compares the values to see if the value is stable. If the value is not the same for all the samples, discard.
Don't add an RC filter, there is a simpler way to do this.
When you get an interrupt:
- Disable the port interrupts;
- Fire a timer with a duration greater than your instability period;
- Once this timer is over, re-enable interrupts.
Well, this assumes that the period of instability has a fixed duration, but
that's usually the case, right? I suspect the squarewave comes from some
mechanical switch, therefore you have multiple interrupts.
Hi doraemon,
I have done like this only. I want to do in hardware itself instead running a separate timer for this. Because i am going to handle three interrupts at a same time. It will be better if single pulse is coming and counting it.
But there is no problem at all doing it with a timer. At least if you have a remaining
timer that you could use for it.
A small correction to what I said:
- Disable the port interrupts; (only the port pin where the square wave comes)
- Fire a timer with a duration greater than your instability period;
- Once this timer is over, re-enable port interrupts. (only the port pin where the square wave comes)
This way it will be absolutely transparent and the program would behave exactly as if you
had input a clean square wave to it.
**broken link removed**I suppose your fall time(Red duration) remains same from 0-500Hz and here is hardware solution in my opinion.
use an RC filter having time constant slightly greater then the then fall time...
example:
Red-Duration=1nSec
Let R=100R
C=1nSec/100R= 10uF