作业(自动演奏音乐)
源代码:
module music(clk,beep,led);
input clk;
output beep;
output [7:0]led;
reg beep_r;
reg[7:0] state;
reg[16:0]count_end;
reg [16:0]count;
reg[23:0]count1;
reg [7:0]led;
parameter L1=17'd95566,
L2=17'd85121,
L3=17'd75850,
L4=17'd71592,
L5=17'd85616,
L6=17'd56818,
L7=17'd50618,
M1=17'd47774,
M2=17'd42568,
M3=17'd37919,
M4=17'd35791,
M5=17'd31888,
M6=17'd28409,
M7=17'd25309,
H1=17'd23912,
H2=17'd21282,
H3=17'd18961,
H4=17'd17897,
H5=17'd15994,
H6=17'd14025,
H7=17'd12655;
parameter TIME = 12000000;
assign beep = beep_r;
always@(posedge clk)
begin
count <= count + 1'b1;
if(count == count_end)
begin
count <= 17'd0;
beep_r <= !beep_r;
end
end
always @(posedge clk)
begin
if(count1 < TIME)
count1 = count1 + 1'b1;
else
begin
count1 = 24'd0;
if(state == 8'd86)
state = 8'd0;
else
state = state + 1'b1;
case(state)
8'd0,8'd1 :count_end=M7;
8'd2,8'd3 :count_end=H1;
8'd4,8'd5,8'd6 :count_end=H2;
8'd7,8'd8,8'd9,8'd10 :count_end=H3;
8'd11,8'd12,8'd13,8'd14,8'd15 :count_end=H2;
8'd17 :count_end=M7;
8'd18 :count_end=M5;
8'd19,8'd20,8'd21 :count_end=M6;
8'd22, :count_end=M3;
8'd23 :count_end=M2;
8'd24,8'd25,8'd26,8'd27 :count_end=M3;
8'd28,8'd29,8'd30 :count_end=M5;
8'd31,8'd32,8'd33 :count_end=M6;
8'd34 :count_end=H1;
8'd35 :count_end=M7;
8'd36,8'd37 :count_end=H1;
8'd38,8'd39 :count_end=H7;
8'd40,8'd41,8'd42,8'd43 :count_end=H1;
8'd44,8'd45 :count_end=M7;
8'd46,8'd47 :count_end=H1;
8'd48,8'd49,8'd50 :count_end=H2;
8'd51,8'd52,8'd53,8'd54 :count_end=H3;
8'd55,8'd56,8'd57,8'd58,8'd59 :count_end=H2;
8'd60 :count_end=H1;
8'd61 :count_end=M7;
8'd62,8'd63 :count_end=H1;
8'd64,8'd65,8'd66,8'd67 :count_end=M7;
8'd68 :count_end=M3;
8'd69 :count_end=M2;
8'd70,8'd71,8'd73 :count_end=M3;
8'd74,8'd75,8'd76 :count_end=M5;
8'd77,8'd78,8'd79 :count_end=M6;
8'd80 :count_end=L5;
8'd81 :count_end=L6;
8'd82 :count_end=M1;
8'd83 :count_end=M2;
8'd84 :count_end=M3;
8'd85 :count_end=M5;
8'd86,8'd87,8'd88,8'd89 :count_end=H1;
endcase
end
end
always@(count_end)
begin
case(count_end)
L5 : led=8'b1111_1111;
L6 : led=8'b0111_1111;
M1,L7: led=8'b0011_1111;
M2,M3: led=8'b0001_1111;
M4,M5: led=8'b0000_1111;
M6,M7: led=8'b0000_0111;
H1 : led=8'b0000_0011;
H2 : led=8'b0000_0001;
H3 : led=8'b0000_0000;
default :led=8'bx;
endcase
end
endmodule
实现视频:
视频地址:http://union.bokecc.com/flash/single/290666218ACBA694_5654B80E28DB3436_false_EEA982EE6B20F4D1_1/player.swf
分享源代码:music2.rar