Altera_Forum
Honored Contributor
13 years agoneed helps in coding...
hi i'm using an ultrasonic sensor to measure distance which produces PWM output, the pwm output is 147us/inch. i want to implement it in de2 board.
i'm using 50MHz input clock from de2 board and the input from the sensor is 147us/inch. so 147us / 20ns = 7350 counts per inch. i'm already tried to do some coding to read the sensors output, i'm planning to use the sensor to measure distance up to 8 inch. so basically i just count the counter to 7350 to 1 inch and continue counting to 2 inch and so on. this is the coding,am i doing it right or is it violates the way to reads pwm? when i compile it using quartus there are no errors but when i use the sensor nothing happens.. anyone has any suggestion.? thank you, regards, faizul library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; entity maxsonar_pwm_signal_decoder is Port ( clk : in STD_LOGIC; reset : in STD_LOGIC; maxsonar_pwm : in STD_LOGIC; y : out std_logic_vector(0 to 7); led : out std_logic; pwm_distance : out std_logic_vector(2 downto 0)); end maxsonar_pwm_signal_decoder; architecture Behavioral of maxsonar_pwm_signal_decoder is signal pwm_inches : std_logic_vector(2 downto 0); signal pwm_distance_i : std_logic_vector(2 downto 0); begin pwm_distance <= pwm_distance_i; process(clk) variable pwm_count : integer range 0 to 60000; begin if reset = '1' then pwm_count := 0; pwm_distance_i <= "000"; elsif rising_edge(clk) then if maxsonar_pwm = '1' then if pwm_count = 7350 then pwm_inches <= "001"; end if; if pwm_count = 14700 then pwm_inches <= "010"; end if; if pwm_count = 22050 then pwm_inches <= "011"; end if; if pwm_count = 29400 then pwm_inches <= "100"; end if; if pwm_count = 36750 then pwm_inches <= "101"; end if; if pwm_count = 44100 then pwm_inches <= "110"; end if; if pwm_count = 51450 then pwm_inches <= "111"; else pwm_count := pwm_count + 1; end if; else if (pwm_inches > "000") then pwm_distance_i <= pwm_inches; end if; pwm_count := 0; pwm_inches <= "000"; end if; end if; end process; process(pwm_inches ) begin case pwm_inches is when "000"=> y<="10000000"; when "001"=> y<="01000000"; when "010"=> y<="00100000"; when "011"=> y<="00010000"; when "100"=> y<="00001000"; when "101"=> y<="00000100"; when "110"=> y<="10000010"; when "111"=> y<="00000001"; when others=> y<="00000000"; led <= '1'; end case; end process; end Behavioral;