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

相关文章

Java集合之Iterator迭代器实现代码解析

《Java集合之Iterator迭代器实现代码解析》迭代器Iterator是Java集合框架中的一个核心接口,位于java.util包下,它定义了一种标准的元素访问机制,为各种集合类型提供了一种统一的... 目录一、什么是Iterator二、Iterator的核心方法三、基本使用示例四、Iterator的工

Java 线程池+分布式实现代码

《Java线程池+分布式实现代码》在Java开发中,池通过预先创建并管理一定数量的资源,避免频繁创建和销毁资源带来的性能开销,从而提高系统效率,:本文主要介绍Java线程池+分布式实现代码,需要... 目录1. 线程池1.1 自定义线程池实现1.1.1 线程池核心1.1.2 代码示例1.2 总结流程2. J

JS纯前端实现浏览器语音播报、朗读功能的完整代码

《JS纯前端实现浏览器语音播报、朗读功能的完整代码》在现代互联网的发展中,语音技术正逐渐成为改变用户体验的重要一环,下面:本文主要介绍JS纯前端实现浏览器语音播报、朗读功能的相关资料,文中通过代码... 目录一、朗读单条文本:① 语音自选参数,按钮控制语音:② 效果图:二、朗读多条文本:① 语音有默认值:②

Vue实现路由守卫的示例代码

《Vue实现路由守卫的示例代码》Vue路由守卫是控制页面导航的钩子函数,主要用于鉴权、数据预加载等场景,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着... 目录一、概念二、类型三、实战一、概念路由守卫(Navigation Guards)本质上就是 在路

uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)

《uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)》在uni-app开发中,文件上传和图片处理是很常见的需求,但也经常会遇到各种问题,下面:本文主要介绍uni-app小程序项目中实... 目录方式一:使用<canvas>实现图片压缩(推荐,兼容性好)示例代码(小程序平台):方式二:使用uni

JAVA实现Token自动续期机制的示例代码

《JAVA实现Token自动续期机制的示例代码》本文主要介绍了JAVA实现Token自动续期机制的示例代码,通过动态调整会话生命周期平衡安全性与用户体验,解决固定有效期Token带来的风险与不便,感兴... 目录1. 固定有效期Token的内在局限性2. 自动续期机制:兼顾安全与体验的解决方案3. 总结PS

C#中通过Response.Headers设置自定义参数的代码示例

《C#中通过Response.Headers设置自定义参数的代码示例》:本文主要介绍C#中通过Response.Headers设置自定义响应头的方法,涵盖基础添加、安全校验、生产实践及调试技巧,强... 目录一、基础设置方法1. 直接添加自定义头2. 批量设置模式二、高级配置技巧1. 安全校验机制2. 类型

Python屏幕抓取和录制的详细代码示例

《Python屏幕抓取和录制的详细代码示例》随着现代计算机性能的提高和网络速度的加快,越来越多的用户需要对他们的屏幕进行录制,:本文主要介绍Python屏幕抓取和录制的相关资料,需要的朋友可以参考... 目录一、常用 python 屏幕抓取库二、pyautogui 截屏示例三、mss 高性能截图四、Pill

使用MapStruct实现Java对象映射的示例代码

《使用MapStruct实现Java对象映射的示例代码》本文主要介绍了使用MapStruct实现Java对象映射的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、什么是 MapStruct?二、实战演练:三步集成 MapStruct第一步:添加 Mave

Java抽象类Abstract Class示例代码详解

《Java抽象类AbstractClass示例代码详解》Java中的抽象类(AbstractClass)是面向对象编程中的重要概念,它通过abstract关键字声明,用于定义一组相关类的公共行为和属... 目录一、抽象类的定义1. 语法格式2. 核心特征二、抽象类的核心用途1. 定义公共接口2. 提供默认实