Verilog | HDL 音乐盒设计(代码类)

2024-06-05 19:58

本文主要是介绍Verilog | HDL 音乐盒设计(代码类),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

博主github:https://github.com/MichaelBeechan
博主CSDN:https://blog.csdn.net/u011344545

module music(clk,beep,pause,stop,switch,led);   //定义时钟及几个功能变量
input clk,pause,switch,stop;                //输入变量
output beep;                                //输出变量,即蜂鸣器发声
output[8:1]led;
reg[8:1]led;
reg beep_r;                       //定义几个中间变量
reg a;
reg b;
reg c;
reg[7:0]state;
reg[7:0]state1;
reg[7:0]state2;
reg[15:0]count,count_end;
reg[23:0]count1;
parameter L_5=16'd61224,L_6=16'd54545,M_1=16'd45863,M_2=16'd40864,M_3=16'd36402,M_5=16'd30612,M_6=16'd27273,H_1=16'd22956;
parameter TIME=12000000;         //时钟频率
assign beep=beep_r;
always@(posedge pause)
begin
a=!a; //暂停
end
always@(posedge stop)
begin
b=!b; //停止
end
always@(posedge switch)
begin
c=!c; //切换
end
always@(posedge clk)
begin if(a==1||b==1)
beep_r=1'b0;else
count<=count+1'b1;if(count==count_end)
begin
count<=16'h0;
beep<=!beep_r;
end
end
always@(posedge clk)
begin
if(count1<TIME )count=count1+1'b1;
else
begin
count1=24'd0;
if(state1==8'd147||state2==8'd60)
state=8'd0;
if(a==1&b==0)
begin
state=state;
end
if(a==0&b==0)  
state=state+1'b1; 
if(a==0&b==1)
begin
state=8'd0;
end
if(a==1&b==1)
state=state+1'b1;
if(c==1)    //曲目1
begin
state1=state;
case(state1)
8'd0,8'd1:                           count_end=L_5;
8'd2,8'd3,8'd4,8'd5,8'd6,8'd7,8'd8:  count_end=M_1;
8'd9,8'd10:                          count_end=M_3;
8'd11,8'd12,8'd13,8'd14:             count_end=M_2;
8'd15:                               count_end=M_1;
8'd16,8'd17:                         count_end=M_2;
8'd18,8'd19:                         count_end=M_3;
8'd20,8'd21,8'd22,8'd23,8'd24:       count_end=M_1;
8'd25,8'd26:                         count_end=M_3;
8'd27,8'd28:                         count_end=M_5;
8'd29,8'd30,8'd31,8'd32,8'd33:       count_end=M_6;
8'd34,8'd35,8'd36,8'd37,8'd38:       count_end=M_6;
8'd39,8'd40,8'd41,8'd42:             count_end=M_5;
8'd43,8'd44,8'd45:                   count_end=M_3;
8'd46,8'd47:                         count_end=M_1;
8'd48,8'd49,8'd50,8'd51:             count_end=M_2;
8'd52:                               count_end=M_1;
8'd53,8'd54:                         count_end=M_2;
8'd55,8'd56:                         count_end=M_3;
8'd57,8'd58,8'd59,8'd60:             count_end=M_1;
8'd61,8'd62,8'd63:                   count_end=L_6;
8'd64,8'd65:                         count_end=M_5;
8'd66,8'd67,8'd68,8'd69:             count_end=M_1;
8'd70,8'd71,8'd72,8'd73:             count_end=M_1;
8'd74,8'd75:                         count_end=M_6;
8'd76,8'd77,8'd78,8'd79:             count_end=M_5;
8'd80,8'd81,8'd82:                   count_end=M_3;
8'd83,8'd84:                         count_end=M_1;
8'd85,8'd86,8'd87,8'd88:             count_end=M_2;
8'd89:                               count_end=M_1;
8'd90,8'd91:                         count_end=M_2;
8'd92,8'd93:                         count_end=M_6;
8'd94,8'd95,8'd96,8'd97:             count_end=M_5;
8'd98,8'd99,8'd100:                  count_end=M_3;
8'd101,8'd102:                       count_end=M_5;
8'd103,8'd104,8'd105,8'd106:         count_end=M_6;
8'd107,8'd108,8'd109,8'd110:         count_end=M_6;
8'd111,8'd112:                       count_end=H_1;
8'd113,8'd114,8'd115,8'd116:         count_end=M_5;
8'd117,8'd118,8'd119:                count_end=M_3;
8'd120,8'd121:                       count_end=M_1;
8'd122,8'd123,8'd124,8'd125:         count_end=M_2;
8'd126:                              count_end=M_1;
8'd127,8'd128:                       count_end=M_2;
8'd129,8'd130:                       count_end=M_3;
8'd131,8'd132,8'd133,8'd134:         count_end=M_1;
8'd135,8'd136,8'd137:                count_end=L_6;
8'd138,8'd139:                       count_end=M_5;
8'd140,8'd141,8'd142,8'd143:         count_end=M_1;
8'd144,8'd145,8'd146,8'd147:         count_end=M_1;
default:count_end=16'hffff;
endcase
end
if(c==0)       //曲目2
begin
state2=state;
case(state)
8'd0,8'd1,8'd2:                       count_end=M_6;
8'd3:    count_end=M_5;
8'd4,8'd5:                            count_end=M_3;
8'd6,8'd7,8'd8:              count_end=M_5;
8'd9:                                 count_end=H_1;
8'd10:                          count_end=M_6;
8'd11:                          count_end=M_5;
8'd12,8'd13,8'd14,8'd15:        count_end=M_6;
8'd16,8'd17:                          count_end=M_3;
8'd18:                          count_end=M_5;
8'd19:        count_end=M_6;
8'd20,8'd21:        count_end=M_5;
8'd22:              count_end=M_3;
8'd23:                    count_end=M_2;
8'd24:                          count_end=M_1;
8'd25:              count_end=L_6;
8'd26:                                count_end=M_5;
8'd27:                          count_end=M_3;
8'd28,8'd29,8'd30,8'd31,8'd32,8'd33,8'd34:count_end=M_2;
8'd35:              count_end=M_3;
8'd36,8'd37,8'd38:                    count_end=M_5;
8'd39:                          count_end=M_6;
8'd40,8'd41,8'd42:              count_end=M_3;
8'd43,8'd44:              count_end=M_2;
8'd45,8'd46,8'd47,8'd48:              count_end=M_1;
8'd49,8'd50,8'd51:              count_end=M_5;
8'd52:                    count_end=M_3;
8'd53:                          count_end=M_2;
8'd54:  count_end=M_1;
8'd55:              count_end=L_6;
8'd56:                                count_end=M_1;
8'd57,8'd58,8'd59,8'd60:              count_end=L_5;
default:count_end=16'hffff;
endcase
end
end
end
always @(count)
begin
case(count)
L_5:led=8'b1111_1111;
L_6:led=8'b0111_1111;
M_1:led=8'b0011_1111;
M_2:led=8'b0001_1111;
M_3:led=8'b0000_1111;
M_5:led=8'b0000_0111;
M_6:led=8'b0000_0011;
H_1:led=8'b0000_0000;
default : led=8'b0;
endcase
end
endmodule

 

这篇关于Verilog | HDL 音乐盒设计(代码类)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1034029

相关文章

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

Python从Word文档中提取图片并生成PPT的操作代码

《Python从Word文档中提取图片并生成PPT的操作代码》在日常办公场景中,我们经常需要从Word文档中提取图片,并将这些图片整理到PowerPoint幻灯片中,手动完成这一任务既耗时又容易出错,... 目录引言背景与需求解决方案概述代码解析代码核心逻辑说明总结引言在日常办公场景中,我们经常需要从 W

使用Spring Cache本地缓存示例代码

《使用SpringCache本地缓存示例代码》缓存是提高应用程序性能的重要手段,通过将频繁访问的数据存储在内存中,可以减少数据库访问次数,从而加速数据读取,:本文主要介绍使用SpringCac... 目录一、Spring Cache简介核心特点:二、基础配置1. 添加依赖2. 启用缓存3. 缓存配置方案方案

MySQL的配置文件详解及实例代码

《MySQL的配置文件详解及实例代码》MySQL的配置文件是服务器运行的重要组成部分,用于设置服务器操作的各种参数,下面:本文主要介绍MySQL配置文件的相关资料,文中通过代码介绍的非常详细,需要... 目录前言一、配置文件结构1.[mysqld]2.[client]3.[mysql]4.[mysqldum

Python多线程实现大文件快速下载的代码实现

《Python多线程实现大文件快速下载的代码实现》在互联网时代,文件下载是日常操作之一,尤其是大文件,然而,网络条件不稳定或带宽有限时,下载速度会变得很慢,本文将介绍如何使用Python实现多线程下载... 目录引言一、多线程下载原理二、python实现多线程下载代码说明:三、实战案例四、注意事项五、总结引

IDEA与MyEclipse代码量统计方式

《IDEA与MyEclipse代码量统计方式》文章介绍在项目中不安装第三方工具统计代码行数的方法,分别说明MyEclipse通过正则搜索(排除空行和注释)及IDEA使用Statistic插件或调整搜索... 目录项目场景MyEclipse代码量统计IDEA代码量统计总结项目场景在项目中,有时候我们需要统计

MySQL设置密码复杂度策略的完整步骤(附代码示例)

《MySQL设置密码复杂度策略的完整步骤(附代码示例)》MySQL密码策略还可能包括密码复杂度的检查,如是否要求密码包含大写字母、小写字母、数字和特殊字符等,:本文主要介绍MySQL设置密码复杂度... 目录前言1. 使用 validate_password 插件1.1 启用 validate_passwo

MySQL实现多源复制的示例代码

《MySQL实现多源复制的示例代码》MySQL的多源复制允许一个从服务器从多个主服务器复制数据,这在需要将多个数据源汇聚到一个数据库实例时非常有用,下面就来详细的介绍一下,感兴趣的可以了解一下... 目录一、多源复制原理二、多源复制配置步骤2.1 主服务器配置Master1配置Master2配置2.2 从服

Go语言使用net/http构建一个RESTful API的示例代码

《Go语言使用net/http构建一个RESTfulAPI的示例代码》Go的标准库net/http提供了构建Web服务所需的强大功能,虽然众多第三方框架(如Gin、Echo)已经封装了很多功能,但... 目录引言一、什么是 RESTful API?二、实战目标:用户信息管理 API三、代码实现1. 用户数据

Java对接MQTT协议的完整实现示例代码

《Java对接MQTT协议的完整实现示例代码》MQTT是一个基于客户端-服务器的消息发布/订阅传输协议,MQTT协议是轻量、简单、开放和易于实现的,这些特点使它适用范围非常广泛,:本文主要介绍Ja... 目录前言前置依赖1. MQTT配置类代码解析1.1 MQTT客户端工厂1.2 MQTT消息订阅适配器1.