项目名称
音乐播放器
具体要求
用蜂鸣器实现两只老虎的音乐播放,数字音符1231 1231 345 345 565431 565431 151 151
代码设计
`include "state.v"
module beep(
input clk,
input rst_n,
output reg beep
);
//250ms跳转一次,需要计数12500000
localparam state_top=24'd12500000-1;
reg [23:0] state_cnt;
always@(posedge clk or negedge rst_n)
if(!rst_n)
state_cnt<=0;
else if(state_cnt<state_top)
state_cnt<=state_cnt+1;
else
state_cnt<=0;
//状态跳转标志信号
wire state_cnt_done=(state_cnt==state_top)?1:0;
reg [4:0] state;
reg [20:0] cnt_top;
always@(posedge clk or negedge rst_n)
if(!rst_n)
state<=0;
else if(state_cnt_done)begin
if(state<31)
state<=state+1;
else
state<=0;
end
else
state<=state;
always@(*)
begin
case(state)
0 :cnt_top<=`C1;
1 :cnt_top<=`C2;
2 :cnt_top<=`C3;
3 :cnt_top<=`C1;
4 :cnt_top<=`C1;
5 :cnt_top<=`C2;
6 :cnt_top<=`C3;
7 :cnt_top<=`C1;
8 :cnt_top<=`C3;
9 :cnt_top<=`C4;
10 :cnt_top<=`C5;
11 :cnt_top<=`C3;
12 :cnt_top<=`C4;
13 :cnt_top<=`C5;
14 :cnt_top<=`C5;
15 :cnt_top<=`C6;
16 :cnt_top<=`C5;
17 :cnt_top<=`C4;
18 :cnt_top<=`C3;
19 :cnt_top<=`C1;
20 :cnt_top<=`C5;
21 :cnt_top<=`C6;
22 :cnt_top<=`C5;
23 :cnt_top<=`C4;
24 :cnt_top<=`C3;
25 :cnt_top<=`C1;
26 :cnt_top<=`C1;
27 :cnt_top<=`C5;
28 :cnt_top<=`C1;
29 :cnt_top<=`C1;
30 :cnt_top<=`C5;
31 :cnt_top<=`C1;
default:;
endcase
end
reg [26:0] cnt;
always@(posedge clk or negedge rst_n)
if(!rst_n)
cnt<=0;
else if(cnt<50_000_000/cnt_top-1)
cnt<=cnt+1;
else
cnt<=0;
always@(posedge clk or negedge rst_n)
if(!rst_n)
beep<=0;
else
beep<=(cnt<cnt_top/2)?1:0;
endmodule
参数宏定义
//高低中音宏定义
`define L1 262 //低音1
`define L2 294 //低音2
`define L3 330 //低音3
`define L4 349 //低音4
`define L5 392 //低音5
`define L6 440 //低音6
`define L7 494 //低音7
`define C1 523 //中音1
`define C2 587 //中音2
`define C3 659 //中音3
`define C4 699 //中音4
`define C5 784 //中音5
`define C6 880 //中音6
`define C7 988 //中音7
`define H1 1046 //高音1
`define H2 1175 //高音2
`define H3 1319 //高音3
`define H4 1397 //高音4
`define H5 1568 //高音5
`define H6 1760 //高音6
`define H7 1976 //高音7