回复 8# kalote module music(clk,ret,fm,fm1,fm2,div);
input clk;
input ret;
output fm,fm1,fm2;
output [12:0] div;
reg clk1;
reg clk2;
reg h;
reg [12:0] cnt;
reg [23:0] cnt1;
reg [2:0] cnt2;
reg [4:0] cnt3;
reg [12:0] div;
parameter
x=13'd0,
m1=13'd4777,
m2=13'd4257,
m3=13'd3792,
m5=13'd3189,
m6=13'd2841,
h1=13'd2389,
h2=13'd2128,
h3=13'd1896;
always @(posedge clk or negedge ret)
if(!ret)
begin
clk1<=0;
cnt1<=0;
end
else
if(cnt1==24'd6249999)
begin
clk1<=~clk1;
cnt1<=0;
end
else
cnt1<=cnt1+1;
always @(posedge clk or negedge ret)
if(!ret)
begin
clk2<=0;
cnt2<=0;
end
else
if(cnt2==3'd4)
begin
clk2<=~clk2;
cnt2<=0;
end
else
cnt2<=cnt2+1;
always @(posedge clk2 or negedge ret)
if(!ret)
begin
h<=0;
cnt<=0;
end
else if(cnt==div)
begin
h<=~h;
cnt<=0;
end
else
cnt<=cnt+1;
always @( posedge clk1 or negedge ret)
begin
if(!ret)
begin
cnt3<=0;
end
else
if(cnt3==5'd19)
cnt3<=0;
else
cnt3<=cnt3+1;
case(cnt3)
0: div=x;
1: div=x;
2: div=m6;
3: div=m6;
4: div=m5;
5: div=m5;
6: div=m6;
6: div=m6;
7: div=m3;
8: div=m3;
9: div=m3;
10: div=m5;
11: div=m2;
12: div=m2;
13: div=m1;
14: div=m1;
15: div=m1;
16: div=m2;
17: div=m2;
18: div=m3;
19: div=m2;
default : div=12'hfff;
endcase
end
assign fm1=clk1;
assign fm2=clk2;
assign fm=h;
endmodule
这个是我修改后的 fm接蜂鸣器 可以演奏音乐的 |