当前位置: 首页 > news >正文

建设部考试网站医疗软件公司10强

建设部考试网站,医疗软件公司10强,详情页设计方法,网站建设相关新闻一#xff0c;项目要求 二#xff0c;理论原理 通过按键来控制状态机的状态#xff0c;在将状态值传送到各个模块进行驱动#xff0c;在空闲状态下#xff0c;数码管显示基础时钟#xff0c;基础时钟是由7个计数器组合而成#xff0c;当在ADJUST状态下可以调整时间项目要求 二理论原理 通过按键来控制状态机的状态在将状态值传送到各个模块进行驱动在空闲状态下数码管显示基础时钟基础时钟是由7个计数器组合而成当在ADJUST状态下可以调整时间并且基础时间会随基础时钟的改变而改变同过位置使能来确定更改的值在按下确定来更改基础时钟的时间ALARM状态下可以设置闹钟的时间设定方法和更改时钟方法一致随后设置了一个beep_flag来驱动beep当beep_flag为1且到达设定时间就响若beep_flag不为1则停止响动最后的秒表功能不多做赘述之后通过状态机传出来的值驱动数码管显示那个模块的输出值. 三系统架构分析 本次系统架构分为状态机模块数码管驱动模块基础时钟模块调时模块闹钟模块秒表模块 三状态转移图 四源码展示 首先是状态机模块的实现这里对应上边的状态转移图通过传出的使能信号state来控制各个模块 /**************************************功能介绍*********************************** Date : Author : WZY. Version : Description: 这是一个状态机模块用来控制不同状态的转换 *********************************************************************************///---------模块及端口声名------------------------------------------------------ module state_change( input wire clk ,input wire rst_n ,input wire [3:0] key_in ,//按键输入input wire beep ,output reg [1:0] led_on ,//led灯显示用来判断当前在什么状态output reg [1:0] state //状态输出 ); //---------参数定义--------------------------------------------------------- parameter IDLE 4b0001, //空闲状态表示显示基础时钟ADJUST 4b0010,//更改状态可以更改时钟的值ALARM 4b0100,//闹钟状态可以制定闹钟STOP 4b1000; //---------内部信号定义----------------------------------------------------- reg [3:0] cstate;//现态 reg [3:0] nstate;//次态//**************************************************************** //状态机 //**************************************************************** //三段式状态机第一段时序逻辑 always (posedge clk or negedge rst_n) beginif (!rst_n) begincstate IDLE;endelse begincstate nstate;end end//三段式状态机第二段组合逻辑 always (*) begincase (cstate)IDLE :beginif (!key_in[0]) begin //当按键0按下时转到调时状态nstate ADJUST;endelse if (!key_in[1]||!beep) beginnstate ALARM;endelse if (!key_in[2]) beginnstate STOP ;endelse beginnstate cstate; endend ADJUST :beginif (!key_in[0]||!key_in[3]) begin//当按下按键0时转到基础时钟nstate IDLE; endelse if (!beep) begin//当蜂鸣器响立刻跳转到闹钟状态nstate ALARM;endelse beginnstate cstate;endendALARM :beginif (!key_in[0]) begin//当按下按键0时转到基础时钟nstate IDLE; endelse beginnstate cstate;endendSTOP :beginif (!key_in[0]) beginnstate IDLE;endelse if (!beep) begin//当蜂鸣器响立刻跳转到闹钟状态nstate ALARM;endelse beginnstate cstate;endenddefault: nstate IDLE;endcase end //三段式状态机第三段时序逻辑 always (posedge clk or negedge rst_n) beginif (!rst_n) beginstate 2b00;endelse case (cstate)IDLE : state 2b00;ADJUST : state 2b01;ALARM : state 2b10;STOP : state 2b11;default: state 2b00;endcase end//**************************************************************** //led显示状态通过led的亮灭状态来看处在什么状态 //**************************************************************** always (posedge clk or negedge rst_n) beginif (!rst_n) beginled_on 2b00;endelse case (cstate)IDLE : led_on 2b00;ADJUST : led_on 2b01;ALARM : led_on 2b10;STOP : led_on 2b11;default: led_on 2b00;endcase end endmodule接下来是基础时钟模块这个模块我为了后边的修改时钟模块方便所以我选择了使用七个计数器来实现。 /**************************************功能介绍*********************************** Date : Author : WZY. Version : Description: 这是一个基础时钟通过用标准的五个计数器实现 *********************************************************************************///---------模块及端口声名------------------------------------------------------ module counter( input wire clk ,input wire rst_n ,input wire [3:0] key_in ,input wire [1:0] state ,//状态input wire [2:0] flag ,//位置信号input wire [23:0] adjust_time, //时间调整output wire [23:0] times //当前时间寄存区 ); //---------参数定义--------------------------------------------------------- parameter MAX1s 26d49_999_999; reg [25:0] cnt_1s ; wire add_cnt ; wire end_cnt ;//时钟内部参数 reg [3:0] sec_low ; reg [3:0] sec_high ; reg [3:0] mine_low ; reg [3:0] mine_high ; reg [3:0] hour_low ; reg [3:0] hour_high ; wire add_sec_low; wire add_sec_high ; wire add_mine_low ; wire add_mine_high; wire add_hour_low ; wire add_hour_high; wire end_sec_low ; wire end_sec_high ; wire end_mine_low ; wire end_mine_high; wire end_hour_low ; wire end_hour_high; //---------内部信号定义----------------------------------------------------- //**************************************************************** //1s计时器 //**************************************************************** always (posedge clk or negedge rst_n)begin if(!rst_n)begincnt_1s 26d0;end else if(add_cnt(state ! 4b01))begin if(end_cnt)begin cnt_1s 26d0;endelse begin cnt_1s cnt_1s 1b1;end end end assign add_cnt 1; assign end_cnt add_cnt cnt_1s MAX1s;//**************************************************************** //时钟计时 //****************************************************************//**************************************************************** //秒钟计时通过两个计数器来设定秒的个位和十位 //**************************************************************** always (posedge clk or negedge rst_n ) beginif (!rst_n) beginsec_low 4d0;endelse if (add_sec_low) beginif (end_sec_low) beginsec_low 4d0;endelse beginsec_low sec_low 1d1;endendelse if ((state 2b01)(!key_in[3])) beginsec_low adjust_time[3:0];endelse beginsec_low sec_low;end end assign add_sec_low end_cnt((state ! 2b01)(flag ! 3d1)); assign end_sec_low (sec_low 4d9)add_sec_low;always (posedge clk or negedge rst_n ) beginif (!rst_n) beginsec_high 4d0;endelse if (add_sec_high) beginif (end_sec_high) beginsec_high 4d0;endelse beginsec_high sec_high 1d1;endendelse if ((state 2b01)(!key_in[3])) beginsec_high adjust_time[7:4];endelse beginsec_high sec_high;end end assign add_sec_high end_sec_low(flag ! 3d2); assign end_sec_high (sec_high 4d5)add_sec_high; //**************************************************************** //分钟计时器通过两个计数器来控制分钟个位和十位 //**************************************************************** always (posedge clk or negedge rst_n ) beginif (!rst_n) beginmine_low 4d0;endelse if (add_mine_low) beginif (end_mine_low) beginmine_low 4d0;endelse beginmine_low mine_low 1d1;endendelse if ((state 2b01)(!key_in[3])) beginmine_low adjust_time[11:8];endelse beginmine_low mine_low;end end assign add_mine_low end_sec_high(flag ! 3d3); assign end_mine_low (mine_low 4d9) add_mine_low;always (posedge clk or negedge rst_n ) beginif (!rst_n) beginmine_high 4d0;endelse if (add_mine_high) beginif (end_mine_high) beginmine_high 4d0;endelse beginmine_high mine_high 1d1;endendelse if ((state 2b01)(!key_in[3])) beginmine_high adjust_time[15:12];endelse beginmine_high mine_high;end end assign add_mine_high end_mine_low (flag ! 3d4); assign end_mine_high (mine_high 4d5) add_mine_high; //**************************************************************** //小时计时器通过两个计数器来控制小时的个位和十位 //**************************************************************** always (posedge clk or negedge rst_n ) beginif (!rst_n) beginhour_low 4d0;endelse if (end_hour_high) beginhour_low 4d0;endelse if (add_hour_low) beginif (end_hour_low) beginhour_low 4d0;endelse beginhour_low hour_low 1d1;endendelse if ((state 2b01)(!key_in[3])) beginhour_low adjust_time[19:16];endelse beginhour_low hour_low;end end assign add_hour_low end_mine_high(flag ! 3d5) ; assign end_hour_low (hour_low 4d9) add_hour_low;always (posedge clk or negedge rst_n ) beginif (!rst_n) beginhour_high 4d0;endelse if (end_hour_high) beginhour_high 4d0;endelse if (add_hour_high) beginhour_high hour_high 1d1;endelse if ((state 2b01)(!key_in[3])) beginhour_high adjust_time[23:20];endelse beginhour_high hour_high;end end assign add_hour_high end_hour_low(flag ! 3d6); assign end_hour_high (hour_high 4d2)(hour_low 4d4); //拼接输出值 assign times {hour_high , hour_low , mine_high , mine_low , sec_high , sec_low}; endmodule接下来是修改时钟模块这里通过定义了一个位置信号来达到选择到每一位最后把修改的数值重新赋值给基础时钟 /**************************************功能介绍*********************************** Date : Author : WZY. Version : Description: 这是一个调时模块通过位置信号和按键信号来更改 *********************************************************************************///---------模块及端口声名------------------------------------------------------ module adjust_state( input wire clk ,//全局时钟input wire rst_n ,input wire [3:0] key_in ,//按键输入input wire [1:0] state ,//状态input wire [23:0] times ,//基本时钟时间output wire [2:0] flag ,//位置信号output wire [23:0] adjust_time//调整后时间 ); //---------参数定义--------------------------------------------------------- //调时参数定义 reg [2:0] flag_r;//位置信号 //时钟参数定义 reg [3:0] sec_low ; reg [3:0] sec_high ; reg [3:0] mine_low ; reg [3:0] mine_high ; reg [3:0] hour_low ; reg [3:0] hour_high ;//---------内部信号定义-----------------------------------------------------//**************************************************************** //位置信号驱动 //**************************************************************** //控制位置信号 always (posedge clk or negedge rst_n) beginif (!rst_n) beginflag_r 3d0;endelse if ((state 2b01)) beginif (!key_in[1]) beginif (flag_r 3d6) beginflag_r 3d1;endelse beginflag_r flag_r 1b1;endendelse beginflag_r flag_r;end endelse beginflag_r 3d0;end end assign flag flag_r; //**************************************************************** //调时主要模块当不在调时状态时使得值一直和时钟保持相等在调时状态时 //根据位置信号和按键信号来加减值 //**************************************************************** always (posedge clk or negedge rst_n) beginif (!rst_n) beginsec_low times[3:0];sec_high times[7:4];mine_low times[11:8];mine_high times[15:12];hour_low times[19:16];hour_high times[23:20];endelse if (state ! 2b01) beginsec_low times[3:0];sec_high times[7:4];mine_low times[11:8];mine_high times[15:12];hour_low times[19:16];hour_high times[23:20];endelse if (state 2b01) beginif (flag_r 3d1) beginif (!key_in[2]) begin //当在调时状态并且位置信号为1时按下按键2使得分钟个位加1下放同理sec_low sec_low 1b1;endelse if (sec_low 4d10) beginsec_low 4d0;endendelse if (flag_r 3d2) beginif (!key_in[2]) beginsec_high sec_high 1b1;endelse if (sec_high 4d6) beginsec_high 4d0;endendelse if (flag_r 3d3) beginif (!key_in[2]) beginmine_low mine_low 1b1;endelse if (mine_low 4d10) beginmine_low 4d0;endendelse if (flag_r 3d4) beginif (!key_in[2]) beginmine_high mine_high 1b1;endelse if (mine_high 4d6) beginmine_high 4d0;endendelse if (flag_r 3d5) beginif (!key_in[2]) beginhour_low hour_low 1b1;endelse if ((hour_low 4d10)(hour_high 4d1)) beginhour_low 4d0;endelse if ((hour_low 4d4)(hour_high 4d2)) beginhour_low 4d0;endendelse if (flag_r 3d6) beginif (!key_in[2]) beginhour_high hour_high 1b1;endelse if ((hour_high 4d2)(hour_low 4d4)) beginhour_high 4d0;endelse if ((hour_high 4d3)(hour_low 4d4)) beginhour_high 4d0;endendelse beginsec_low sec_low ;sec_high sec_high ;mine_low mine_low ;mine_high mine_high;hour_low hour_low ;hour_high hour_high;endend end //调值后的信号输出 assign adjust_time {hour_high ,hour_low,mine_high , mine_low , sec_high , sec_low} ; endmodule下面是对于闹钟模块的介绍闹钟模块中定时跟修改模块一致只是会让修改后的值一直保持只要基础时钟时间跟定时想同就使使能拉高按下按键或者等待5s使能自动拉低使能拉高切时间达到就使得蜂鸣器响达到闹钟的效果 /**************************************功能介绍*********************************** Date : Author : WZY. Version : Description: 这是一个闹钟模块在调时模块的基础上增加了蜂鸣器驱动信号来控制定时 *********************************************************************************///---------模块及端口声名------------------------------------------------------ module alarm_clock( input wire clk ,input wire rst_n ,input wire [3:0] key_in ,input wire [1:0] state ,//状态input wire [23:0] times ,//基础时钟时间output reg beep ,//蜂鸣器output wire [2:0] flag_alarm,//闹钟位置信号output wire [23:0] adjust_alarm,//设定闹钟时间output wire led_alarm //定时led); //---------参数定义--------------------------------------------------------- parameter MAX1S 26d49_999_999;//1s;//闹钟参数定义 reg [2:0] flag_alarm_r;//位置信号 reg flag_beep_r ;//蜂鸣器使能 reg [3:0] sec_low ; reg [3:0] sec_high ; reg [3:0] mine_low ; reg [3:0] mine_high ; reg [3:0] hour_low ; reg [3:0] hour_high ;//1s计时器参数定义 reg [25:0] cnt ; wire add_cnt ; wire end_cnt ; //5s计数器参数定义 reg [2:0] cnt_5s ; wire add_cnt_5s ; wire end_cnt_5s ;reg led_r ;//led信号寄存器 reg flag ;//计时驱动 //---------内部信号定义-----------------------------------------------------//**************************************************************** //flag驱动控制计时 //**************************************************************** always (posedge clk or negedge rst_n) beginif (!rst_n) beginflag 0;endelse if (end_cnt_5s) beginflag 1b0;endelse if (adjust_alarm timestimes! 0) beginflag 1b1;endelse beginflag flag ;endend //**************************************************************** //1s计时器 //****************************************************************always (posedge clk or negedge rst_n)begin if(!rst_n)begincnt 26d0;end else if(add_cnt)begin if(end_cnt)begin cnt 26d0;endelse begin cnt cnt 1b1;end endelse if (state ! 2b10) begincnt 26d0;endelse begincnt cnt;end end assign add_cnt flag; assign end_cnt add_cnt cnt MAX1S;//**************************************************************** //5s计时器 //****************************************************************always (posedge clk or negedge rst_n)begin if(!rst_n)begincnt_5s 3d0;end else if(add_cnt_5s)begin if(end_cnt_5s)begin cnt_5s 3d0;endelse begin cnt_5s cnt_5s 1b1;end endelse if (state ! 2b10) begincnt_5s 3d0;endelse begincnt_5s cnt_5s;end end assign add_cnt_5s end_cnt; assign end_cnt_5s add_cnt_5s cnt_5s 3d5;//**************************************************************** //位置信号驱动 //**************************************************************** //控制位置信号 always (posedge clk or negedge rst_n) beginif (!rst_n) beginflag_alarm_r 3d0;endelse if ((state 2b10)) beginif (!key_in[1]) beginif (flag_alarm_r 3d6) beginflag_alarm_r 3d1;endelse beginflag_alarm_r flag_alarm_r 1b1;endendelse if (!key_in[3]) beginflag_alarm_r 3d0;endelse beginflag_alarm_r flag_alarm_r;end endelse beginflag_alarm_r 3d0;end end assign flag_alarm flag_alarm_r; //**************************************************************** //是定闹钟的主要模块当不在闹钟状态时使得值一直设定的值一样在闹钟 //时根据位置信号和按键信号来加减值 //**************************************************************** always (posedge clk or negedge rst_n) beginif (!rst_n) beginsec_low 4d0;sec_high 4d0;mine_low 4d0;mine_high 4d0;hour_low 4d0;hour_high 4d0;endelse if (state 2b10) beginif (flag_alarm_r 3d1) beginif (!key_in[2]) beginsec_low sec_low 1b1;endelse if (sec_low 4d10) beginsec_low 4d0;endendelse if (flag_alarm_r 3d2) beginif (!key_in[2]) beginsec_high sec_high 1b1;endelse if (sec_high 4d6) beginsec_high 4d0;endendelse if (flag_alarm_r 3d3) beginif (!key_in[2]) beginmine_low mine_low 1b1;endelse if (mine_low 4d10) beginmine_low 4d0;endendelse if (flag_alarm_r 3d4) beginif (!key_in[2]) beginmine_high mine_high 1b1;endelse if (mine_high 4d6) beginmine_high 4d0;endendelse if (flag_alarm_r 3d5) beginif (!key_in[2]) beginhour_low hour_low 1b1;endelse if ((hour_low 4d10)(hour_high 4d1)) beginhour_low 4d0;endelse if ((hour_low 4d4)(hour_high 4d2)) beginhour_low 4d0;endendelse if (flag_alarm_r 3d6) beginif (!key_in[2]) beginhour_high hour_high 1b1;endelse if ((hour_high 4d2)(hour_low 4d4)) beginhour_high 4d0;endelse if ((hour_high 4d3)(hour_low 4d4)) beginhour_high 4d0;endendelse beginsec_low sec_low ;sec_high sec_high ;mine_low mine_low ;mine_high mine_high;hour_low hour_low ;hour_high hour_high;endend endassign adjust_alarm {hour_high ,hour_low,mine_high , mine_low , sec_high , sec_low} ;//**************************************************************** //闹钟判断和蜂鸣器模块 //**************************************************************** always (posedge clk or negedge rst_n) beginif (!rst_n) beginbeep 1b1;endelse if ((adjust_alarm times)flag_beep_r) begin//当时间达到并且使能为1时beep响beep 1b0;endelse if (!flag_beep_r) begin//当时能为0时beep 1b1;endelse beginbeep beep;end end always (posedge clk or negedge rst_n) beginif (!rst_n) beginflag_beep_r 1b0;endelse if (end_cnt_5s) begin//当计时结束后使得使能自动归0停止闹钟flag_beep_r 1b0;endelse if (!key_in[3](state 2b10)) begin//当按下第四个按键时翻转用来控制开始和结束flag_beep_r ~flag_beep_r;endelse beginflag_beep_r flag_beep_r; end end //**************************************************************** //led显示 //**************************************************************** always (posedge clk or negedge rst_n) beginif (!rst_n) beginled_r 1b0;endelse if (flag_beep_r 1) begin//当使能为1即设定了闹钟led就亮否则不亮led_r 1b1;endelse beginled_r 1b0;end end assign led_alarm led_r; endmodule 下面是数码管驱动模块这里我为了让选择到的哪一位频闪所以采用了一个巧妙的三位运算符的方法来控制大家可以自己看一下 /**************************************功能介绍*********************************** Date : 2023.8.2 Author : WZY. Version : Description: 这是一个数码管显示模块用来显示各个功能的值 *********************************************************************************///---------模块及端口声名------------------------------------------------------ module seg_dirver( input wire clk ,input wire rst_n ,input wire [1:0] state ,//状态input wire [23:0] times , //基础时钟寄存器input wire [5:0] point ,//点控制寄存器input wire [2:0] flag ,//调时位选信号input wire [23:0] adjust_time,//调时显示寄存器input wire [2:0] flag_alarm,//闹钟位选信号input wire [23:0] adjust_alarm,//闹钟显示寄存器input wire [23:0] adjust_clock,//计时器寄存器output reg [5:0] sel ,//位选output reg [7:0] seg //段选 ); //---------参数定义--------------------------------------------------------- parameter MAX20US 10d999; parameter MAX_500MS 25d24_999_999;//500ms //数码管译码参数 parameter ZERO 7b100_0000 ,ONE 7b111_1001 ,TWO 7b010_0100 ,THREE 7b011_0000 ,FOUR 7b001_1001 ,FIVE 7b001_0010 ,SIX 7b000_0010 ,SEVEN 7b111_1000 ,EIGHT 7b000_0000 ,NINE 7b001_0000 ,A 7b000_1000 ,B 7b000_0011 ,C 7b100_0110 ,D 7b010_0001 ,E 7b000_0110 ,F 7b000_1110 ,DARK 7b111_1111 ;//全灭 //---------内部信号定义----------------------------------------------------- //20us计数器 reg [9:0] cnt ; wire add_cnt ; wire end_cnt ; //500ms计数器 reg [24:0] cnt_500ms ; wire add_cnt_500ms ; wire end_cnt_500ms ; reg flash;//闪烁信号reg [23:0] num ;//位选赋值寄存器 reg [4:0] seg_temp;//seg单位值 reg point_r ;//点位控制 //**************************************************************** //20us计数器 //**************************************************************** always (posedge clk or negedge rst_n)begin if(!rst_n)begincnt 10d0;end else if(add_cnt)begin if(end_cnt)begin cnt 10d0;endelse begin cnt cnt 1b1;end end end assign add_cnt 1b1; assign end_cnt add_cnt cnt MAX20US;//**************************************************************** //500ms计数器 //****************************************************************always (posedge clk or negedge rst_n)begin if(!rst_n)begincnt_500ms 25d0;end else if(add_cnt_500ms)begin if(end_cnt_500ms)begin cnt_500ms 25d0;endelse begin cnt_500ms cnt_500ms 1b1;end endelse begincnt_500ms 25d0;end end assign add_cnt_500ms (state 2b01)||(state 2b10); assign end_cnt_500ms add_cnt_500ms cnt_500ms MAX_500MS; //**************************************************************** //驱动闪烁信号 //**************************************************************** always (posedge clk or negedge rst_n) beginif (!rst_n) beginflash 1b0;endelse if (end_cnt_500ms) begin//每500ms翻转一次flash ~flash;endelse if ((state ! 2b01)(state ! 2b10)) begin//当不在调时和闹钟状态归0flash 1b0;endelse beginflash flash;end end //**************************************************************** //seg显示选择.根据状态选择数码管显示的值 //**************************************************************** always (posedge clk or negedge rst_n) beginif (!rst_n) beginnum 24d0;endelse if (state 2b00) beginnum times;endelse if (state 2b01) beginnum adjust_time;endelse if (state 2b10) beginnum adjust_alarm;endelse if (state 2b11) beginnum adjust_clock;endelse beginnum num;end end //**************************************************************** //驱动sel //**************************************************************** always (posedge clk or negedge rst_n)begin if(!rst_n)beginsel 6b111_110;end else if(end_cnt)begin sel {sel[4:0],sel[5]};end else begin sel sel; end end //**************************************************************** //位选赋值当选择到哪一位哪一位进行频闪 //**************************************************************** always (*) begincase (sel)6b011111:begin seg_temp (flash((flag3d1||(flag_alarm 3d1))))?5d15 : num[3:0] ; point_r point[0];end6b101111:begin seg_temp (flash((flag3d2||(flag_alarm 3d2))))?5d15 : num[7:4] ; point_r point[1];end6b110111:begin seg_temp (flash((flag3d3||(flag_alarm 3d3))))?5d15 : num[11:8] ; point_r point[2];end6b111011:begin seg_temp (flash((flag3d4||(flag_alarm 3d4))))?5d15 : num[15:12] ; point_r point[3];end6b111101:begin seg_temp (flash((flag3d5||(flag_alarm 3d5))))?5d15 : num[19:16] ; point_r point[4];end6b111110:begin seg_temp (flash((flag3d6||(flag_alarm 3d6))))?5d15 : num[23:20] ; point_r point[5];enddefault: seg_temp 4d0;endcase end //**************************************************************** //译码 //**************************************************************** always (*) begincase (seg_temp)4d0: seg {point_r,ZERO };4d1: seg {point_r,ONE };4d2: seg {point_r,TWO };4d3: seg {point_r,THREE };4d4: seg {point_r,FOUR };4d5: seg {point_r,FIVE };4d6: seg {point_r,SIX };4d7: seg {point_r,SEVEN };4d8: seg {point_r,EIGHT };4d9: seg {point_r,NINE };4d15:seg {point_r,DARK};default: seg 8b1111_1111;endcase end endmodule最后是消抖和秒表比较简单 消抖 module key_debounce (input wire clk ,input wire rst_n ,input wire [3:0] key_in ,output wire [3:0] key_out );parameter MAX20ms 20d999_999;wire add_cnt;//倒计时开始使能 wire end_cnt;//倒计时结束使能 reg [19:0] cnt_20ms;//20ms计数寄存器 reg [3:0] key_r0;//同步 reg [3:0] key_r1;//打拍 reg start;//下降沿检测寄存器 reg [3:0] flag; reg [3:0] key_out_r;//输出按键信号寄存器 wire nedge;//下降沿检测 always (posedge clk or negedge rst_n) beginif (!rst_n) beginkey_r0 4b1111;key_r1 4b1111;endelse beginkey_r0 key_in;key_r1 key_r0;end endassign nedge (~key_r0[0]key_r1[0])||(~key_r0[1]key_r1[1])||(~key_r0[2]key_r1[2])||(~key_r0[3]key_r1[3]);//20ms计时器 always (posedge clk or negedge rst_n) beginif (!rst_n) begincnt_20ms 20d0;endelse if (nedge) begincnt_20ms 20d0;endelse if (add_cnt) beginif (end_cnt) begincnt_20ms 20d0;endelse begincnt_20ms cnt_20ms 1b1;endendelse begincnt_20ms 20d0;end endassign add_cnt start; assign end_cnt add_cnt (cnt_20ms MAX20ms);//约束start always (posedge clk or negedge rst_n) beginif (!rst_n) beginstart 1b0;endelse if (nedge) beginstart 1b1;endelse if (end_cnt) beginstart 1b0;endelse beginstart start ;end end//约束flag always (posedge clk or negedge rst_n) beginif (!rst_n) beginflag 4b1111;endelse if (nedge) beginflag 4b1111;endelse if (end_cnt) beginflag key_r0;endelse beginflag 4b1111 ;end end //脉冲信号 always (posedge clk or negedge rst_n) beginif (!rst_n) beginkey_out_r 4b1111;endelse if (!flag[0]) beginkey_out_r 4b1110;endelse if (!flag[1]) beginkey_out_r 4b1101;endelse if (!flag[2]) beginkey_out_r 4b1011;endelse if (!flag[3]) beginkey_out_r 4b0111;endelse beginkey_out_r 4b1111;end end// //持续信号 // always (posedge clk or negedge rst_n) begin // if (!rst_n) begin // key_out_r 4b1111; // end // else if (!flag[0]) begin // key_out_r 4b1110; // end // else if (!flag[1]) begin // key_out_r 4b1101; // end // else if (!flag[2]) begin // key_out_r 4b1011; // end // else if (!flag[3]) begin // key_out_r 4b0111; // end // else begin // key_out_r key_out_r; // end // endassign key_out key_out_r; endmodule 秒表cpp /**************************************功能介绍*********************************** Date : Author : WZY. Version : Description: 这是一个秒表 *********************************************************************************///---------模块及端口声名------------------------------------------------------ module stop_watch( input wire clk ,input wire rst_n ,input wire [3:0] key_in ,input wire [1:0] state ,output wire [23:0] adjust_clock//秒表寄存器分钟/秒/毫秒/ ); //---------参数定义--------------------------------------------------------- parameter max1ms 19d499_999;//100ms reg [3:0] ms_low; reg [3:0] ms_high; reg [3:0] s_low; reg [3:0] s_high; reg [3:0] mine_low; reg [3:0] mine_high;reg [18:0] cnt ; wire add_cnt ; wire end_cnt ;wire add_cnt_ms_low; wire end_cnt_ms_low; wire add_cnt_ms_high; wire end_cnt_ms_high; wire add_cnt_s_low; wire end_cnt_s_low; wire add_cnt_s_high; wire end_cnt_s_high; wire add_cnt_mine_low; wire end_cnt_mine_low; wire add_cnt_mine_high; wire end_cnt_mine_high;reg flag_clock; //---------内部信号定义-----------------------------------------------------//**************************************************************** //秒表使能 //**************************************************************** always (posedge clk or negedge rst_n) beginif (!rst_n) beginflag_clock 1b0;endelse if ((!key_in[1])(state 2b11)) beginflag_clock ~flag_clock;endelse if ((state ! 2b11)||(!key_in[2])) beginflag_clock 1b0;endelse beginflag_clock flag_clock;end end//**************************************************************** //100ms计数器 //**************************************************************** always (posedge clk or negedge rst_n)begin if(!rst_n)begincnt 19d0;end else if(add_cnt)begin if(end_cnt)begin cnt 19d0;endelse begin cnt cnt 1b1;end endelse if ((state ! 2b11)||(!key_in[2])) begincnt 19d0;endelse begincnt cnt;end end assign add_cnt (state 2b11)(flag_clock); assign end_cnt add_cnt (cnt max1ms);//**************************************************************** //秒表模块 //**************************************************************** //ms always (posedge clk or negedge rst_n) beginif (!rst_n) beginms_low 4d0;endelse if (add_cnt_ms_low) beginif (end_cnt_ms_low) beginms_low 4d0;endelse beginms_low ms_low 1d1;endendelse if ((state ! 2b11)||((state 2b11)(!key_in[2]))) beginms_low 4d0;endelse beginms_low ms_low;end endassign add_cnt_ms_low end_cnt; assign end_cnt_ms_low add_cnt_ms_low(ms_low 9);always (posedge clk or negedge rst_n) beginif (!rst_n) beginms_high 4d0;endelse if (add_cnt_ms_high) beginif (end_cnt_ms_high) beginms_high 4d0;endelse beginms_high ms_high 1d1;endendelse if ((state ! 2b11)||((state 2b11)(!key_in[2]))) beginms_high 4d0;endelse beginms_high ms_high;end endassign add_cnt_ms_high end_cnt_ms_low; assign end_cnt_ms_high add_cnt_ms_high(ms_high 5);//s always (posedge clk or negedge rst_n) beginif (!rst_n) begins_low 4d0;endelse if (add_cnt_s_low) beginif (end_cnt_s_low) begins_low 4d0;endelse begins_low s_low 1d1;endendelse if ((state ! 2b11)||((state 2b11)(!key_in[2]))) begins_low 4d0;endelse begins_low s_low;end endassign add_cnt_s_low end_cnt_ms_high; assign end_cnt_s_low add_cnt_s_low(s_low 9);always (posedge clk or negedge rst_n) beginif (!rst_n) begins_high 4d0;endelse if (add_cnt_s_high) beginif (end_cnt_s_high) begins_high 4d0;endelse begins_high s_high 1d1;endendelse if ((state ! 2b11)||((state 2b11)(!key_in[2]))) begins_high 4d0;endelse begins_high s_high;end endassign add_cnt_s_high end_cnt_s_low; assign end_cnt_s_high add_cnt_s_high(s_high 5);//mine always (posedge clk or negedge rst_n) beginif (!rst_n) beginmine_low 4d0;endelse if (add_cnt_mine_low) beginif (end_cnt_mine_low) beginmine_low 4d0;endelse beginmine_low mine_low 1d1;endendelse if ((state ! 2b11)||((state 2b11)(!key_in[2]))) beginmine_low 4d0;endelse beginmine_low mine_low;end endassign add_cnt_mine_low end_cnt_s_high; assign end_cnt_mine_low add_cnt_mine_low(mine_low 9);always (posedge clk or negedge rst_n) beginif (!rst_n) beginmine_high 4d0;endelse if (add_cnt_mine_high) beginif (end_cnt_mine_high) beginmine_high 4d0;endelse beginmine_high mine_high 1d1;endendelse if ((state ! 2b11)||((state 2b11)(!key_in[2]))) beginmine_high 4d0;endelse beginmine_high mine_high;end endassign add_cnt_mine_high end_cnt_mine_low; assign end_cnt_mine_high add_cnt_mine_high(mine_high 5);assign adjust_clock {mine_high , mine_low ,s_high , s_low , ms_high,ms_low};endmodule顶层 /**************************************功能介绍*********************************** Date : 2023.8.2 Author : WZY. Version : Description: 这是一个顶层模块 *********************************************************************************///---------模块及端口声名------------------------------------------------------ module top( input wire clk ,input wire rst_n ,input wire [3:0] key_in ,output wire [3:0] led_on ,output wire beep ,output wire [5:0] sel ,output wire [7:0] seg ); //---------参数定义--------------------------------------------------------- wire [3:0] key_debounce; wire [1:0] state; wire [2:0] flag; wire [23:0] times; wire [23:0] adjust_time; wire [2:0] flag_alarm; wire [23:0] adjust_alarm; wire [23:0] adjust_clock; //---------内部信号定义-----------------------------------------------------// // **************************************************************** // // 模块例化 // // **************************************************************** // //消抖模块例化 // key_debounce key_debounce_inst( // .clk (clk), // .rst_n (rst_n), // .key_in (key_in), // .key_out (key_debounce) // );// //状态机例化 // state_change state_change_inst( // .clk (clk), // .rst_n (rst_n), // .key_in (key_debounce),//按键输入 // .beep (beep) , // .led_on (led_on[1:0]),//led灯显示用来判断当前在什么状态 // .state (state) //状态输出 // );// //基础时钟例化 // counter counter_inst( // .clk (clk), // .rst_n (rst_n), // .key_in (key_debounce), // .state (state), // .flag (flag), // .times (times), // .adjust_time (adjust_time) //时间调整 // ); // //调时模块例化 // adjust_state adjust_state_inst( // .clk (clk), // .rst_n (rst_n), // .key_in (key_debounce), // .state (state), // .times (times), // .flag (flag), // .adjust_time (adjust_time) // ); // //闹钟模块例化 // alarm_clock alarm_clock_inst( // .clk (clk), // .rst_n (rst_n), // .key_in (key_debounce), // .state (state), // .times (times), // .beep (beep), // .flag_alarm (flag_alarm), // .adjust_alarm (adjust_alarm), // .led_alarm (led_on[3])// ); // //秒表模块例化 // stop_watch stop_watch_inst( // .clk (clk), // .rst_n (rst_n), // .key_in (key_debounce), // .state (state), // .adjust_clock (adjust_clock)//秒表寄存器分钟/秒/毫秒/ // ); // //数码管驱动例化 // seg_dirver seg_dirver_inst( // .clk (clk), // .rst_n (rst_n), // .state (state), // .times (times), // .point (6b101011), // .flag (flag),//调时位选信号 // .adjust_time (adjust_time),//调时显示寄存器 // .flag_alarm (flag_alarm),//闹钟位选信号 // .adjust_alarm (adjust_alarm),//闹钟显示寄存器 // .adjust_clock (adjust_clock),//秒表显示寄存器 // .sel (sel), // .seg (seg) // );// **************************************************************** // 模块例化 // **************************************************************** //消抖模块例化 //状态机例化 state_change state_change_inst( .clk (clk),.rst_n (rst_n),.key_in (key_in),//按键输入.beep (beep) ,.led_on (led_on[1:0]),//led灯显示用来判断当前在什么状态.state (state) //状态输出 );//基础时钟例化 counter counter_inst( .clk (clk),.rst_n (rst_n),.key_in (key_in),.state (state),.flag (flag),.times (times),.adjust_time (adjust_time) //时间调整 ); //调时模块例化 adjust_state adjust_state_inst( .clk (clk),.rst_n (rst_n),.key_in (key_),.state (state),.times (times),.flag (flag),.adjust_time (adjust_time) ); //闹钟模块例化 alarm_clock alarm_clock_inst( .clk (clk),.rst_n (rst_n),.key_in (key_in),.state (state),.times (times),.beep (beep),.flag_alarm (flag_alarm),.adjust_alarm (adjust_alarm),.led_alarm (led_on[3])); //秒表模块例化 stop_watch stop_watch_inst( .clk (clk),.rst_n (rst_n),.key_in (key_in),.state (state),.adjust_clock (adjust_clock)//秒表寄存器分钟/秒/毫秒/ ); //数码管驱动例化 seg_dirver seg_dirver_inst( .clk (clk),.rst_n (rst_n),.state (state),.times (times),.point (6b101011),.flag (flag),//调时位选信号.adjust_time (adjust_time),//调时显示寄存器.flag_alarm (flag_alarm),//闹钟位选信号.adjust_alarm (adjust_alarm),//闹钟显示寄存器.adjust_clock (adjust_clock),//秒表显示寄存器.sel (sel),.seg (seg) ); endmodule四测试文件 timescale 1ns/1nsmodule top_tb();//激励信号定义 reg clk ; reg rst_n ; reg [3:0] key_in ;//输出信号定义 wire [3:0] led_on ; wire beep ; wire [5:0] sel ; wire [7:0] seg ; //时钟周期参数定义 parameter CYCLE 20; defparam top_inst.counter_inst.MAX1s 10*CYCLE,top_inst.seg_dirver_inst.MAX20US CYCLE,top_inst.seg_dirver_inst.MAX_500MS 5*CYCLE, top_inst.alarm_clock_inst.MAX1S 10*CYCLE;//模块例化top top_inst( .clk (clk),.rst_n (rst_n),.key_in (key_in),.led_on (led_on),.beep (beep),.sel (sel),.seg (seg) ); //产生时钟initial clk 1b0;always #(CYCLE/2) clk ~clk;//产生激励// //调值模块仿真 // initial begin // rst_n 1b1; // key_in 4b1111; // #(CYCLE*2); // rst_n 1b0; // #(CYCLE*20); // rst_n 1b1; // #(CYCLE*10000) //延迟10000个周期来观察基础时钟 // key_in 4b1110;//按下key0进入调时状态 // #CYCLE // key_in 4b1111; // #(CYCLE*20) // key_in 4b1101; //按下key1选择第一位 // #CYCLE // key_in 4b1111; // #(CYCLE*20) // repeat(5)begin // key_in 4b1011;//连续按下key2使得秒的个位1 // #(CYCLE) // key_in 4b1111; // #(CYCLE*20); // end // #(CYCLE*100) // key_in 4b0111;//按下key3确定更改时间 // #(CYCLE) // key_in 4b1111; // #(CYCLE*10000) // $stop; // end//调值模块仿真initial begin rst_n 1b1;key_in 4b1111;#(CYCLE*2);rst_n 1b0;#(CYCLE*20);rst_n 1b1;key_in 4b1101;//按下key1进入闹钟状态#CYCLEkey_in 4b1111;#(CYCLE*20)key_in 4b1101; //按下key1选择第一位#CYCLEkey_in 4b1111;#(CYCLE*20)key_in 4b1101; //按下key1选择第二位#CYCLEkey_in 4b1111;#(CYCLE*20)repeat(5)beginkey_in 4b1011;//连续按下key2使得秒的个位1使得计时50s#(CYCLE)key_in 4b1111;#(CYCLE*20);endkey_in 4b0111;//按下key3确定设定闹钟#(CYCLE)key_in 4b1111;#(CYCLE*10000) //延迟10000个周期等待闹钟触发#(CYCLE*10000)$stop;endendmodule 波形 这是基础时钟的仿真波形可以看到基础功能实现 这是修改时间模块的波形可以看到当按键按下时状态改变并且当按下key1时位置信号变为001表示控制个位之后按下key2个位数字1并且按下key3时基础时钟的times变更为更改时间adjust_time的值说明更改成功基本功能实现 下面是闹钟模块的仿真波形可以看到当设置闹钟后等到基础时钟到达设定值蜂鸣器拉低开始5s计时当计时结束蜂鸣器拉高停止响这里我也同样做了按键停止但是效果差不多就只展示计时停止 六结果展示 上板验证
文章转载自:
http://www.morning.xhlht.cn.gov.cn.xhlht.cn
http://www.morning.fhqdb.cn.gov.cn.fhqdb.cn
http://www.morning.kmjbs.cn.gov.cn.kmjbs.cn
http://www.morning.zgdnd.cn.gov.cn.zgdnd.cn
http://www.morning.hgsylxs.com.gov.cn.hgsylxs.com
http://www.morning.qzzmc.cn.gov.cn.qzzmc.cn
http://www.morning.cnqdn.cn.gov.cn.cnqdn.cn
http://www.morning.xckqs.cn.gov.cn.xckqs.cn
http://www.morning.tkzqw.cn.gov.cn.tkzqw.cn
http://www.morning.jbysr.cn.gov.cn.jbysr.cn
http://www.morning.gnzsd.cn.gov.cn.gnzsd.cn
http://www.morning.sqlh.cn.gov.cn.sqlh.cn
http://www.morning.gthc.cn.gov.cn.gthc.cn
http://www.morning.prgdy.cn.gov.cn.prgdy.cn
http://www.morning.clbgy.cn.gov.cn.clbgy.cn
http://www.morning.bsqth.cn.gov.cn.bsqth.cn
http://www.morning.dqxnd.cn.gov.cn.dqxnd.cn
http://www.morning.wrwcf.cn.gov.cn.wrwcf.cn
http://www.morning.rwlsr.cn.gov.cn.rwlsr.cn
http://www.morning.ctsjq.cn.gov.cn.ctsjq.cn
http://www.morning.ksbmx.cn.gov.cn.ksbmx.cn
http://www.morning.mkrjf.cn.gov.cn.mkrjf.cn
http://www.morning.nhrkl.cn.gov.cn.nhrkl.cn
http://www.morning.qzqfq.cn.gov.cn.qzqfq.cn
http://www.morning.qqnp.cn.gov.cn.qqnp.cn
http://www.morning.fhsgw.cn.gov.cn.fhsgw.cn
http://www.morning.jfcbz.cn.gov.cn.jfcbz.cn
http://www.morning.smdiaosu.com.gov.cn.smdiaosu.com
http://www.morning.zcqbx.cn.gov.cn.zcqbx.cn
http://www.morning.lxcwh.cn.gov.cn.lxcwh.cn
http://www.morning.nstml.cn.gov.cn.nstml.cn
http://www.morning.tfwr.cn.gov.cn.tfwr.cn
http://www.morning.jtfcd.cn.gov.cn.jtfcd.cn
http://www.morning.rljr.cn.gov.cn.rljr.cn
http://www.morning.gsdbg.cn.gov.cn.gsdbg.cn
http://www.morning.jltmb.cn.gov.cn.jltmb.cn
http://www.morning.zynjt.cn.gov.cn.zynjt.cn
http://www.morning.thpzn.cn.gov.cn.thpzn.cn
http://www.morning.jpgfq.cn.gov.cn.jpgfq.cn
http://www.morning.bdypl.cn.gov.cn.bdypl.cn
http://www.morning.fqcdh.cn.gov.cn.fqcdh.cn
http://www.morning.hpnhl.cn.gov.cn.hpnhl.cn
http://www.morning.tnbas.com.gov.cn.tnbas.com
http://www.morning.fgkwh.cn.gov.cn.fgkwh.cn
http://www.morning.ydrml.cn.gov.cn.ydrml.cn
http://www.morning.fchkc.cn.gov.cn.fchkc.cn
http://www.morning.rszbj.cn.gov.cn.rszbj.cn
http://www.morning.xqxlb.cn.gov.cn.xqxlb.cn
http://www.morning.kxypt.cn.gov.cn.kxypt.cn
http://www.morning.xcnwf.cn.gov.cn.xcnwf.cn
http://www.morning.pggkr.cn.gov.cn.pggkr.cn
http://www.morning.mspqw.cn.gov.cn.mspqw.cn
http://www.morning.bnrff.cn.gov.cn.bnrff.cn
http://www.morning.pltbd.cn.gov.cn.pltbd.cn
http://www.morning.rnrwq.cn.gov.cn.rnrwq.cn
http://www.morning.tkxyx.cn.gov.cn.tkxyx.cn
http://www.morning.wqjpl.cn.gov.cn.wqjpl.cn
http://www.morning.jjxnp.cn.gov.cn.jjxnp.cn
http://www.morning.tgxrm.cn.gov.cn.tgxrm.cn
http://www.morning.dnycx.cn.gov.cn.dnycx.cn
http://www.morning.hnrls.cn.gov.cn.hnrls.cn
http://www.morning.fhcwm.cn.gov.cn.fhcwm.cn
http://www.morning.cpwmj.cn.gov.cn.cpwmj.cn
http://www.morning.thmlt.cn.gov.cn.thmlt.cn
http://www.morning.gkjnz.cn.gov.cn.gkjnz.cn
http://www.morning.gllhx.cn.gov.cn.gllhx.cn
http://www.morning.bgdk.cn.gov.cn.bgdk.cn
http://www.morning.fktlg.cn.gov.cn.fktlg.cn
http://www.morning.lnrr.cn.gov.cn.lnrr.cn
http://www.morning.sdamsm.com.gov.cn.sdamsm.com
http://www.morning.hwnqg.cn.gov.cn.hwnqg.cn
http://www.morning.rhchr.cn.gov.cn.rhchr.cn
http://www.morning.hphqy.cn.gov.cn.hphqy.cn
http://www.morning.gywxq.cn.gov.cn.gywxq.cn
http://www.morning.rqlbp.cn.gov.cn.rqlbp.cn
http://www.morning.qfgxk.cn.gov.cn.qfgxk.cn
http://www.morning.rnpt.cn.gov.cn.rnpt.cn
http://www.morning.mzkn.cn.gov.cn.mzkn.cn
http://www.morning.rglp.cn.gov.cn.rglp.cn
http://www.morning.lgwjh.cn.gov.cn.lgwjh.cn
http://www.tj-hxxt.cn/news/248908.html

相关文章:

  • 广东省住房和城乡建设局网站动态ip服务器可以做网站吗
  • 企业大型网站开发设计建站流程水果网络营销方案
  • 漳州网站开发点博大a优门头沟营销型网站建设
  • 丰城市城乡规划建设局网站杭州网站建设网页制作
  • 英文二手汽车网站建设汉南公司网站建设
  • 最好的商业网站泉州做网站多少钱
  • 国外著名的网站设计公司网站信息内容建设管理
  • 固安建设局网站中小企业网站优化
  • 注册网站流程河南新乡做网站公司
  • wordpress 过滤html代码google优化排名
  • 贵州桥梁集团建设有限公司网站全球域名
  • 企业网站建设的方式有哪些方式wordpress 3.9.2 下载
  • 昆明网站建设工作室有哪些网站的形成
  • 网站seo描述网站建设标语
  • 网站flash音乐播放器营销内容包括哪些方面
  • 乔拓云智能建站官网登录入口360优化大师下载安装
  • 网站制作的分割线怎么做上海网站建设求职简历
  • 建设网站培训内存数据库 网站开发
  • 如何优化网站性能产品软文范例大全
  • wordpress 多站点配置文件宁波网站建设费用是多少钱
  • dedecms部署两个网站建设德育网站的意义
  • 建设p2p网站注册科技有限公司经营范围
  • soho做网站二级域名网站查询入口
  • 深圳宝安医院的网站建设设计制作小车一微课
  • 中小企业网站建设与管理 王耀在那些免费网站做宣传效果好
  • 河南省网站制作公司阿里巴巴logo发展史
  • 搭建网站都需要什么线上赚钱正规平台
  • 服装私人订制网站济南公司快速建站
  • 郑州做食用菌配送的网站中国建设招标网官方网站
  • 网站备案WordPress添加用户组