Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
12 years ago

need 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;

3 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    When you have a look at this section:

    else
      if (pwm_inches > "000") then
        pwm_distance_i <= pwm_inches;
      end if;
      pwm_count := 0;
      pwm_inches <= "000";
    end if;
    The pwm_inches is reset at 000 as soon as the PWM pulse ends. You probably won't see anything on the output because of that. I think that your second process should use the pwm_distance_i signal instead, that should keep the value of the measured distance after the PWM pulse ends:
    process(pwm_distance_i )
    begin
      case pwm_distance_i is
        when "000"=>
    And also in that last process, you assign led to 1 in the others section, but never to 0. If you want the led signal to be 0 when you have a valid measured distance, you can assign a default value before the case:
    process(pwm_distance_i )
    begin
      led <= '0';
      case pwm_distance_i is
        when "000"=>

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    thanks for your reply, i'm already made the correction, but still there are nothing happens to the led when i connect the sensor output to de2 board. what i concern is, am i doing the right way for reading pwm output from the sensor. this is the datasheet for the sensor i used. http://maxbotix.com/documents/mb1010_datasheet.pdf

    there are 3 type of output which is pwm, serial output and analog, but i'm need to use the pwm output.

    thanks,

    regards,

    faizul
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    it looks right. I suggest to use Signaltap to check that the signal you are getting is what you expect and that the counter increments as it should.