CPLD板播放音乐“两只老虎”

来源:互联网 发布:2013至2016年房价数据 编辑:程序博客网 时间:2024/04/28 03:45

module music_1(clock,buzzout);
input clock; //定义基准时钟输入
output buzzout; //定义声响输出口

reg buzzout_reg;        reg clk_4hz;                                            //4Hz信号时钟输入reg[2:0] high,med,low;                              //定义高中低音标注reg[24:0] count1;                                       //定义基准时钟分频寄存器reg[15:0] count_end,count2;                     //定义音谱分频寄存器reg[7:0] counter;                                       //控制印谱播放顺序/*由基准时钟分频得到4Hz信号*/always@(posedge clock)    begin        if(count1<25'd3000000)            count1=count1+1;        else            begin                count1=0;                clk_4hz=~clk_4hz;            end     end /*控制交流蜂鸣器发出各种音乐声*/  always@(posedge clock)    begin        count2=count2+1;        if(count2==count_end)            begin                count2=25'h0;                buzzout_reg=!buzzout_reg;            end     end /*控制相应印谱频率*/    always@(posedge clk_4hz)    begin        case({high,med,low})                9'b000000001:count_end=16'hb32f; //低音1的分频系数值                9'b000000010:count_end=16'h9f9a;    //低音2的分频系数值                9'b000000011:count_end=16'h8e37;    //低音3的分频系数值                9'b000000100:count_end=16'h863c;    //低音4的分频系数值                9'b000000101:count_end=16'h7794;    //低音5的分频系数值                9'b000000110:count_end=16'h6a88;    //低音6的分频系数值                9'b000000111:count_end=16'h5ee8;    //低音7的分频系数值                9'b000001000:count_end=16'h5993;    //中音1的分频系数值                9'b000010000:count_end=16'h4fd0;    //中音2的分频系数值                9'b000011000:count_end=16'h4719;    //中音3的分频系数值                9'b000100000:count_end=16'h431b;    //中音4的分频系数值                9'b000101000:count_end=16'h3bca;    //中音5的分频系数值                9'b000110000:count_end=16'h3544;    //中音6的分频系数值                9'b000111000:count_end=16'h2f74;    //中音7的分频系数值                9'b001000000:count_end=16'h2cca;    //高音1的分频系数值                9'b010000000:count_end=16'h27e7;    //高音2的分频系数值                9'b011000000:count_end=16'h238d;    //高音3的分频系数值                9'b100000000:count_end=16'h218e;    //高音4的分频系数值                9'b101000000:count_end=16'h1de5;    //高音5的分频系数值                9'b110000000:count_end=16'h1aa2;    //高音6的分频系数值                9'b111000000:count_end=16'h17ba;    //高音7的分频系数值                default:count_end=16'hffff;        endcase     end always@(posedge clk_4hz)    begin        if(counter==47)            counter=0;        else            counter=counter+1;        case(counter)            0:{high,med,low}=9'b000001000;      //中音1            1:{high,med,low}=9'b000001000;      //中音1            2:{high,med,low}=9'b000101000;      //中音5            3:{high,med,low}=9'b000101000;      //中音5            4:{high,med,low}=9'b000110000;      //中音6            5:{high,med,low}=9'b000110000;      //中音6            6:{high,med,low}=9'b000101000;      //中音5            7:{high,med,low}=9'b000101000;      //中音5            8:{high,med,low}=9'b000100000;      //中音4            9:{high,med,low}=9'b000100000;      //中音4            10:{high,med,low}=9'b000011000;     //中音3            11:{high,med,low}=9'b000011000;     //中音3            12:{high,med,low}=9'b000010000;     //中音2            13:{high,med,low}=9'b000010000;     //中音2            14:{high,med,low}=9'b000001000;     //中音1            15:{high,med,low}=9'b000001000;     //中音1            16:{high,med,low}=9'b000101000;     //中音5            17:{high,med,low}=9'b000101000;     //中音5            18:{high,med,low}=9'b000100000;     //中音4            19:{high,med,low}=9'b000100000;     //中音4            20:{high,med,low}=9'b000011000;     //中音3            21:{high,med,low}=9'b000011000;     //中音3            22:{high,med,low}=9'b000010000;     //中音2            23:{high,med,low}=9'b000010000;     //中音2            24:{high,med,low}=9'b000101000;     //中音5            25:{high,med,low}=9'b000101000;     //中音5            26:{high,med,low}=9'b000100000;     //中音4            27:{high,med,low}=9'b000100000;     //中音4            28:{high,med,low}=9'b000011000;     //中音3            29:{high,med,low}=9'b000011000;     //中音3            30:{high,med,low}=9'b000010000;     //中音2            31:{high,med,low}=9'b000010000;     //中音2            32:{high,med,low}=9'b000001000;     //中音1            33:{high,med,low}=9'b000001000;     //中音1            34:{high,med,low}=9'b000101000;     //中音5            35:{high,med,low}=9'b000101000;     //中音5            36:{high,med,low}=9'b000110000;     //中音6            37:{high,med,low}=9'b000110000;     //中音6            38:{high,med,low}=9'b000101000;     //中音5            39:{high,med,low}=9'b000101000;     //中音5            40:{high,med,low}=9'b000100000;     //中音4            41:{high,med,low}=9'b000100000;     //中音4            42:{high,med,low}=9'b000011000;     //中音3            43:{high,med,low}=9'b000011000;     //中音3            44:{high,med,low}=9'b000010000;     //中音2            45:{high,med,low}=9'b000010000;     //中音2            46:{high,med,low}=9'b000001000;     //中音1            47:{high,med,low}=9'b000001000;     //中音1        endcase     end     assign buzzout=buzzout_reg;

endmodule

阅读全文
0 0
原创粉丝点击