maestros wrote:
> Your code is perfectly working, I have tested it on hardware also.
Nice to hear.
A hint: check out the symmetry of your square wave. When its changing a
little bit from 50:50 to 48:52, then you will get short spikes on the
SquareDetected output, because there is only a little margin on the
counter (from 125 to 127). See WF_SD_DC48.PNG for that.
To get this waveform I changed the test bench a little to invoke the
duty cycle in the 4kHz signal:
1 | :
|
2 | constant dc : real := 0.48;
|
3 | :
|
4 | BEGIN
|
5 | :
|
6 | process begin
|
7 | sq <= '1';
|
8 | wait for dc * 250 us;
|
9 | sq <= '0';
|
10 | wait for (1.0-dc) * 250 us;
|
11 | end process;
|
12 | :
|
To be much more tolerant there it would be best to increase the top
value of the counter to 255:
1 | :
|
2 | signal cnt : integer range 0 to 255 := 0;
|
3 | :
|
4 | if cnt<255 then -- count to highest value
|
5 | :
|
The reaction to detect a missing 4kHz square wave signal is then delayed
by 129us. See WF_SD_DC48_cnt255.PNG at around 2.2ms.