程序如下:
modulepiano(in,clk_6MHz,clk_4Hz,song,speaker); input in,clk_6MHz,clk_4Hz,song; output speaker; reg speaker; reg[7:0] state; reg song_on; wire[6:0] in; reg[3:0] high,med,low; reg[13:0] divider,origin; reg[7:0] counter; reg out; wire carry; reg[20:0] i; parameter zero=8'b0000_0000, one=8'b0000_0001, two=8'b0000_0010, three=8'b0000_0100, four=8'b0000_1000, five=8'b0001_0000, six=8'b0010_0000, seven=8'b0100_0000; initial begin song_on<=0; end always @(posedge song) begin song_on<=~song_on; end always @(posedge clk_6MHz) begin if(song_on) speaker<=out; else begin case(in) zero:begin speaker<=0; i<=0; end one:begin //do if(i>=11451) begin speaker=!speaker; i<=0; end else i<=i+1; end two:begin //re if(i>=10204) begin speaker=!speaker; i<=0; end else i<=i+1; end three:begin //mi if(i>=9091) begin speaker=!speaker; i<=0; end else i<=i+1; end four:begin // fa if(i>=8596) begin speaker=!speaker; i<=0; end else i<=i+1; end five:begin //so if(i>=7653) begin speaker=!speaker; i<=0; end else i<=i+1; end six:begin //lo if(i>=6818) begin speaker=!speaker; i<=0; end else i<=i+1; end seven:begin //si if(i>=6073) begin speaker=!speaker; i<=0; end else i<=i+1; end default:begin speaker<=0; i<=0; end endcase end end assign carry=(divider==16383); always @(posedge clk_6MHz) begin if(carry) divider<=origin; else divider<=divider+1; end always @(posedge carry) begin out<=~out; //2 分频产生方波信号 end always @(posedge clk_4Hz) begin case({high,med,low}) //分频比预置 'b000000000011:origin<=7281; 'b000000000101:origin<=8730; 'b000000000110:origin<=9565; 'b000000000111: origin<=10310; 'b000000010000:origin<=10647; 'b000000100000:origin<=11272; 'b000000110000:origin<=11831; 'b000001010000:origin<=12556; 'b000001100000:origin<=12974; 'b000100000000:origin<=13516; 'b000000000000:origin<=16383; endcase end always @(posedge clk_4Hz) begin if(counter==63) counter<=0;//计时,以实现循环演奏 else counter<=counter+1; case(counter) //记谱 0:{high,med,low}<='b000000000011; //低音“3” 1: {high,med,low}<='b000000000011; //持续4 个时钟节拍 2:{high,med,low}<='b000000000011; 3:{high,med,low}<='b000000000011; 4:{high,med,low}<='b000000000101; //低音“5” 5:{high,med,low}<='b000000000101; //发3 个时钟节拍 6:{high,med,low}<='b000000000101; 7:{high,med,low}<='b000000000110; //低音“6” 8:{high,med,low}<='b000000010000; //中音“1” 9:{high,med,low}<='b000000010000; //发3 个时钟节拍 10:{high,med,low}<='b000000010000; 11:{high,med,low}<='b000000100000; //中音“2” 12:{high,med,low}<='b000000000110; //低音“6” 13:{high,med,low}<='b000000010000; 14: {high,med,low}<='b000000000101; 15:{high,med,low}<='b000000000101; 16:{high,med,low}<='b000001010000; //中音“5” 17:{high,med,low}<='b000001010000; //发3 个时钟节拍 18:{high,med,low}<='b000001010000; 19:{high,med,low}<='b000100000000; //高音“1” 20:{high,med,low}<='b000001100000; 21:{high,med,low}<='b000001010000; 22:{high,med,low}<='b000000110000; 23: {high,med,low}<='b000001010000; 24:{high,med,low}<='b000000100000; //中音“2” 25:{high,med,low}<='b000000100000; //持续11 个时钟节拍 26:{high,med,low}<='b000000100000; 27:{high,med,low}<='b000000100000; 28:{high,med,low}<='b000000100000; 29:{high,med,low}<='b000000100000; 30:{high,med,low}<='b000000100000; 31:{high,med,low}<='b000000100000; 32:{high,med,low}<='b000000100000; 33:{high,med,low}<='b000000100000; 34:{high,med,low}<='b000000100000; 35:{high,med,low}<='b000000110000; //中音“3” 36:{high,med,low}<='b000000000111; //低音“7” 37: {high,med,low}<='b000000000111; 38:{high,med,low}<='b000000000110; //低音“6” 39:{high,med,low}<='b000000000110; 40:{high,med,low}<='b000000000101; //低音“5” 41:{high,med,low}<='b000000000101; 42:{high,med,low}<='b000000000101; 43:{high,med,low}<='b000000000110; //低音“6” 44:{high,med,low}<='b000000010000; //中音“1” 45:{high,med,low}<='b000000010000; 46: {high,med,low}<='b000000100000;//中音“2” 47:{high,med,low}<='b000000100000; 48:{high,med,low}<='b000000000011; //低音“3” 49:{high,med,low}<='b000000000011; 50:{high,med,low}<='b000000010000; //中音“1” 51:{high,med,low}<='b000000010000; 52:{high,med,low}<='b000000000110; 53:{high,med,low}<='b000000000101; //低音“5” 54:{high,med,low}<='b000000000110; 55:{high,med,low}<='b000000010000; //中音“1” 56:{high,med,low}<='b000000000101; //低音“5” 57:{high,med,low}<='b000000000101; //持续8 个时钟节拍 58:{high,med,low}<='b000000000101; 59:{high,med,low}<='b000000000101; 60: {high,med,low}<='b000000000101; 61:{high,med,low}<='b000000000101; 62:{high,med,low}<='b000000000101; 63:{high,med,low}<='b000000000101; endcase endendmodule
此帖出自
小平头技术问答
有空回去跑跑看再回。
一周热门 更多>