fpga蜂鸣器实现音乐播放器

本文详细介绍了一种使用蜂鸣器实现两只老虎音乐播放的项目设计,通过Verilog硬件描述语言编程,实现了数字音符到音乐信号的转换,涵盖了状态机设计、频率宏定义及音乐播放逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目名称

音乐播放器

具体要求

用蜂鸣器实现两只老虎的音乐播放,数字音符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

 

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值