打印

我问一个VHDL的问题大家过来看一下 谢了

[复制链接]
1756|11
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
风中De舞者|  楼主 | 2007-8-24 17:46 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我想实现这样一个逻辑功能 
用4位2进制信号cin来控制一个输出信号  当0000时输出一个10进制数字并且这个数字是最小数字  当1111输出一个10进制数字  这个数字是最大数字  当cin在0000和1111之间时输出从最小数到最大中间的任意一个从小到大的数
具体的大家看我的程序  大家有没什么问题  谢谢了
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;

entity yindiao is
port
(clk  : in  std_logic;
 cin :  in std_logic_vector(3 downto 0);
 q    : out integer range 0 to 10204
);
end yindiao;

architecture behave of yindiao is
begin
  process(clk)
  variable cnt1:integer range 0 to 150000;
  begin 
    if clk'event and clk='1' then
       if cnt1<150000 then         --此数值控制每一个音的长短
          cnt1:=cnt1+1;
       else
          cnt1:=0;
          case cin is
            when "0000"   =>q<=638;
                  
            when "0001"  =>q<=1275;
                      
            when "0010"   =>q<=1913;
                      
            when "0011"   =>q<=2550;
                      
            when "0100"   =>q<=3186;

            when "0101"  =>q<=3823;
                      
            when "0110"   =>q<=4460;
                     
            when "0111"   =>q<=5098;
                      
            when "1000"   =>q<=5700;
                       
            when "1001"  =>q<=6300;
                       
            when "1010"  =>q<=6900;
                     
            when "1011"  =>q<=7600;
                     
            when "1100"  =>q<=8200;
                     
            when "1101"  =>q<=8890;
                  
            when "1110"  =>q<=9500;
                      
            when "1111"  =>q<=10204;
          end case;
       end if;
    end if;
  end process;
end behave;

相关帖子

沙发
dadong| | 2007-8-25 09:33 | 只看该作者

case语句要求列出所以可能

std_logic类型有九种值,你没有列出000z等类型,可以在case后面加上others或者用if语句来编程,我忘了case的语法,所以就改成if语句的了。
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;

entity yindiao is
port
(clk  : in  std_logic;
cin :  in std_logic_vector(3 downto 0);
q    : out integer range 0 to 10204
);
end yindiao;

architecture behave of yindiao is
begin
  process(clk)
  variable cnt1:integer range 0 to 150000;
  begin 
    if clk'event and clk='1' then
       if cnt1<150000 then         --此数值控制每一个音的长短
          cnt1:=cnt1+1;
       else
          cnt1:=0;
        if  
           cin= "0000"   then q<=638;
                  
        elsif cin= "0001" then q<=1275;
                      
         elsif   cin= "0010"  then  q<=1913;
                      
         elsif   cin= "0011"   then q<=2550;
                      
         elsif   cin= "0100"   then q<=3186;

         elsif   cin= "0101"  then  q<=3823;
                      
          elsif  cin= "0110"  then q<=4460;
                     
        elsif    cin= "0111"  then q<=5098;
                      
         elsif   cin= "1000"  then q<=5700;
                       
        elsif    cin= "1001" then q<=6300;
                       
        elsif    cin= "1010" then q<=6900;
                     
         elsif   cin= "1011" then q<=7600;
                     
        elsif    cin= "1100"  then q<=8200;
                     
        elsif    cin= "1101" then  q<=8890;
                  
        elsif    cin= "1110" then  q<=9500;
                      
        elsif    cin= "1111" then  q<=10204;
        end if;
      end if;
    end if;
  end process;
end behave;

使用特权

评论回复
板凳
风中De舞者|  楼主 | 2007-8-25 10:40 | 只看该作者

谢谢这位兄弟了

我试了一下结果还是不行  郁闷啊

使用特权

评论回复
地板
dadong| | 2007-8-25 12:27 | 只看该作者

不行?

我刚才用maxplus编译了一下,没问题啦!?

使用特权

评论回复
5
风中De舞者|  楼主 | 2007-8-27 08:26 | 只看该作者

程序编译通过 但是仿真的时候没结果啊

仿真输出不管输入的是多少都是0啊  这是怎么回事?

使用特权

评论回复
6
waotang| | 2007-8-27 14:22 | 只看该作者

试试

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;

entity yindiao is
port
(clk  : in  std_logic;
 reset: in  std_logic;
cin :  in std_logic_vector(3 downto 0);
q    : out std_logic_vector(13 downto 0)
);
end yindiao;

architecture behave of yindiao is

signal cnt1:integer range 0 to 150000;
signal q_s : integer range 0 to 10204;

begin
  process(clk,reset)
  
  begin 
      if reset='1' then
          cnt1<=0;
      elsif rising_edge(clk) then
       if cnt1<150000 then         --此数值控制每一个音的长短
          cnt1:=cnt1+1;
       else
             cnt1<=0;
          case cin is
            when "0000"   =>q_s<=638;
                  
            when "0001"  =>q_s<=1275;
                      
            when "0010"   =>q_s<=1913;
                      
            when "0011"   =>q_s<=2550;
                      
            when "0100"   =>q_s<=3186;

            when "0101"  =>q_s<=3823;
                      
            when "0110"   =>q_s<=4460;
                     
            when "0111"   =>q_s<=5098;
                      
            when "1000"   =>q_s<=5700;
                       
            when "1001"  =>q_s<=6300;
                       
            when "1010"  =>q_s<=6900;
                     
            when "1011"  =>q_s<=7600;
                     
            when "1100"  =>q_s<=8200;
                     
            when "1101"  =>q_s<=8890;
                  
            when "1110"  =>q_s<=9500;
                      
            when "1111"  =>q_s<=10204;
            when others  =>q_s<=0;
          end case;
       end if;
    end if;
  end process;

  q<=conv_std_logic_vector(q_s,14);
end behave;

使用特权

评论回复
7
风中De舞者|  楼主 | 2007-8-27 15:28 | 只看该作者

cnt1<=0;

             cnt1<=0;
cnt1是个变量怎么能代入呢  肯定不行啦

使用特权

评论回复
8
风中De舞者|  楼主 | 2007-8-27 15:34 | 只看该作者

cnt1:=cnt1+1;

cnt1:=cnt1+1;
cnt1是个信号  怎么能这样赋值  肯定不行啦

使用特权

评论回复
9
伊处| | 2007-8-27 22:20 | 只看该作者

variable cnt1:integer range 0 to 1500 :=4;

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;

entity yindiao is
    port
        (clk      :     in  std_logic;
         cin     :      in std_logic_vector(3 downto 0);
         q        :   out integer range 0 to 10204
        );
end yindiao;

architecture behave of yindiao is

begin
    process(clk)
    variable cnt1:integer range 0 to 1500 :=4;

      begin 
        if clk'event and clk='1' then
           if cnt1<1500 then         --此数值控制每一个音的长短
             cnt1:=cnt1+1;
             else
              cnt1:=0;
            CASE cin IS
                WHEN "0000" =>q<=638;
                WHEN "0001" =>q<=1275;
                WHEN "0010" =>q<=1913;
                WHEN "0011" =>q<=2550;
                WHEN "0100" =>q<=3186;
                WHEN "0101" =>q<=3823;
                WHEN "0110" =>q<=4460;
                WHEN "0111" =>q<=5098;
                WHEN "1000" =>q<=5700;
                WHEN "1001" =>q<=6300;
                WHEN "1010" =>q<=6900;
                WHEN "1011" =>q<=7600;
                WHEN "1100" =>q<=8200;
                WHEN "1101" =>q<=8890;
                WHEN "1110" =>q<=9500;
                WHEN "1111" =>q<=10204;
                WHEN OTHERS =>q<=0;
            END CASE;
             end if;
        end if;
    end process;
end behave;

这个我仿真出来了,下载应该可以的。
变量cnt1声明时,要赋值。
另外为了仿真方便,我把变量cnt1的范围改成了0到1500

使用特权

评论回复
10
风中De舞者|  楼主 | 2007-8-28 09:06 | 只看该作者

大家帮忙看下这个程序里的CNT2变量是干什么用的

这是一个控制蜂鸣器发出友谊天长地久音乐的一个乐谱
用Q来表示的  另外再接一个控制部分就可以控制蜂鸣器演奏了
时钟频率4M
其中的CNT2变量是干什么用的呢?
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;

entity yindiao is
port
(clk  : in  std_logic;
 q    : out integer range 0 to 10204
);
end yindiao;

architecture behave of yindiao is
begin
  process(clk)
  variable cnt1:integer range 0 to 1500000;
  variable cnt2:integer range 0 to 8;
  variable n   :integer range 0 to 45;
  begin 
    if clk'event and clk='1' then
       if cnt1<1500000 then         --此数值控制每一个音的长短
          cnt1:=cnt1+1;
       else
          cnt1:=0;
          case n is
            when 0   =>q<=10204;
                       if cnt2<1 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=1;
                       end if;
            when 1   =>q<=7364;
                       if cnt2<6 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=2;
                       end if;
            when 2   =>q<=6061;
                       if cnt2<1 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=3;
                       end if;
            when 3   =>q<=6803;
                       if cnt2<3 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=4;
                       end if;
            when 4   =>q<=7634;
                       n:=5;
            when 5   =>q<=6803;
                       if cnt2<1 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=6;
                       end if;
            when 6   =>q<=6061;
                       if cnt2<1 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=7;
                       end if;
            when 7   =>q<=7634;
                       if cnt2<4 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=8;
                       end if;
            when 8   =>q<=6061;
                       if cnt2<1 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=9;
                       end if;
            when 9   =>q<=5089;
                       if cnt2<1 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=10;
                       end if;
            when 10  =>q<=4535;
                       if cnt2<9 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=11;
                       end if;
            when 11  =>q<=5089;
                       if cnt2<3 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=12;
                       end if;
            when 12  =>q<=6061;
                       if cnt2<2 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=13;
                       end if;
            when 13  =>q<=7634;
                       if cnt2<1 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=14;
                       end if;
            when 14  =>q<=6803;
                       if cnt2<3 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=15;
                       end if;
            when 15  =>q<=7634;
                       n:=16;
            when 16  =>q<=6803;
                       if cnt2<1 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=17;
                       end if;
            when 17  =>q<=6061;
                       if cnt2<1 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=18;
                       end if;

            when 18  =>q<=7634;
                       if cnt2<3 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=19;
                       end if;
            when 19  =>q<=9050;
                       if cnt2<2 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=20;
                       end if;
            when 20  =>q<=10204;
                       if cnt2<1 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=21;
                       end if;
            when 21  =>q<=7634;
                       if cnt2<7 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=22;
                       end if;
            when 22  =>q<=4535;
                       if cnt2<1 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=23;
                       end if;

            when 23  =>q<=5089;
                       if cnt2<3 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=24;
                       end if;
            when 24  =>q<=6061;
                       if cnt2<2 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=25;
                       end if;
            when 25  =>q<=7634;
                       if cnt2<1 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=26;
                       end if;
            when 26  =>q<=6803;
                       if cnt2<3 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=27;
                       end if;
            when 27  =>q<=7634;
                       n:=28;
            when 28  =>q<=6803;
                       if cnt2<1 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=29;
                       end if;
            when 29  =>q<=4535;
                       if cnt2<1 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=30;
                       end if;
            when 30  =>q<=5089;
                       if cnt2<3 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=31;
                       end if;
            when 31  =>q<=6061;
                       if cnt2<2 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=32;
                       end if;
            when 32  =>q<=5089;
                       if cnt2<1 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=33;
                       end if;
            when 33  =>q<=4535;
                       if cnt2<7 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=34;
                       end if;
            when 34  =>q<=3809;
                       if cnt2<1 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=35;
                       end if;
            when 35  =>q<=5089;
                       if cnt2<3 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=36;
                       end if;
            when 36  =>q<=6061;
                       if cnt2<2 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=37;
                       end if;
            when 37  =>q<=7634;
                       if cnt2<1 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=38;
                       end if;
            when 38  =>q<=6803;
                       if cnt2<3 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=39;
                       end if;
            when 39  =>q<=7634;
                       n:=40;
            when 40  =>q<=6803;
                       if cnt2<1 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=41;
                       end if;
            when 41  =>q<=6061;
                       if cnt2<1 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=42;
                       end if;
            when 42  =>q<=7634;
                       if cnt2<3 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=43;
                       end if;
            when 43  =>q<=9050;
                       if cnt2<2 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=44;
                       end if;
            when 44  =>q<=10204;
                       if cnt2<1 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=45;
                       end if;
            when 45  =>q<=7634;
                       if cnt2<7 then
                          cnt2:=cnt2+1;
                       else
                          cnt2:=0;
                          n:=0;
                       end if;
          end case;
       end if;
    end if;
  end process;
end behave;

使用特权

评论回复
11
风中De舞者|  楼主 | 2007-8-28 13:36 | 只看该作者

9楼的兄弟解决了问题

看来就是因为没有赋初值  我把他的程序弄进去以后确实是跟我的设计结果比较符合了

使用特权

评论回复
12
伊处| | 2007-9-2 13:49 | 只看该作者

cnt2应该是控制每个音节的拍数

每个音符可能会是一拍或两拍等等,我觉得cnt2用于控制这个。
n代表的是已经播放到了第几个音符。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

32

主题

287

帖子

1

粉丝