Forum Discussion
Altera_Forum
Honored Contributor
15 years agoVHDL timer
hi,
how can I write a program for a timer which Measures a pulse width that represents of range.i'm using un ultrasonic sensor, The distance can be calculated using the scale factor of 147uS per inch. thanks in advance20 Replies
- Altera_Forum
Honored Contributor
From what I read the circuit will measure the number of clock pulses.
Each clock pulse has a fixed duration (T = 1/fclk). First try to understand what your circuit is. This means input and output signals and internal circuitry. You need: 1) A signal that enables counting (input) 2) A clock (input) 3) A reset (input) 4) The duration of the measured pulse (output: how many bit?) The circuit could be a simple counter wich counts the high duration of a signal. The Altera example for a counter (Verilog) is:
I think this is a good start.module counter ( clk, reset, result, ena ); input clk; input reset; input ena; output result; reg result; always @(posedge clk or posedge reset) begin if (reset) result = 0; else if (ena) result = result + 1; end endmodule - Altera_Forum
Honored Contributor
I just saw you want VHDL.
The Altera code for VHDL is:library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all; entity count is port ( clk : in std_logic; reset : in std_logic; enable : in std_logic; q : out integer range 0 to 255 ); end entity; architecture rtl of count is begin process (clk) variable cnt : integer range 0 to 255; begin if (rising_edge(clk)) then if reset = '1' then -- Reset the counter to 0 cnt := 0; elsif enable = '1' then -- Increment the counter if counting is enabled cnt := cnt + 1; end if; end if; -- Output the current count q <= cnt; end process; end rtl; - Altera_Forum
Honored Contributor
ok....i think it's ok
thx alot and i think the clk input will be the sensor output (pulse width) am i right? after that i have to add some operations to the code cuz this value is gonna be converted to distance... - Altera_Forum
Honored Contributor
no, the clock is always a clock input
the sensor input will be enable. - Altera_Forum
Honored Contributor
do u mean enternal clk on de2 kit?
and the output sensor (pulse width)will be "enable" signal? - Altera_Forum
Honored Contributor
yes.
You may need some separate logic to reset the counter and register it. - Altera_Forum
Honored Contributor
I also would like to add a note.
This simple scheme will not work if your sensor output has glitches. If the pulse is like this: .........|*|..|****************| .........|.|..|................| **********.****................|********* you risk to measure the initial glitch and get wrong results. In this case you need a more complex circuit or some other control signal. - Altera_Forum
Honored Contributor
Assuming the sensor output has no glitches, it still can't drive the enable signal or the result register directly, because it's asynchronous to the system clock. It must be synchronized first, otherwise you'll get a certain amount of incorrect readings.
- Altera_Forum
Honored Contributor
this is exactly what happened...
there is no output signals.. i think i'm gonna change the way.... this sensor have 3 outputs...(analog voltage,asynchronous serial with an RS232 format,BW) what do u think ? what's the best? - Altera_Forum
Honored Contributor
hi
how can i write a program for a timer count 20 second in vhdl