本文主要是介绍FPGA——可调时时钟设计(verilog),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
可调时时钟 verilog模块
1、该设计的主要点在于调整信号的产生,即按键的处理。在100khz的时钟下,产生按键前后的变化信号,此方法可代替检测按键边沿信号并消抖的模式。
//-------------------------生成调整信号-----------------------------wire hour_adj ;wire sec_adj ;wire min_adj ;reg hour_d1 = 0;reg hour_d2 = 0;reg min_d1 = 0;reg min_d2 = 0;reg sec_d1 = 0;reg sec_d2 = 0;always @ ( posedge clock_100k ) beginhour_d1 <= key[2];hour_d2 <= hour_d1;min_d1 <= key[1];min_d2 <= min_d1;sec_d1 <= key[0];sec_d2 <= sec_d1;endassign hour_adj = key[3]&&( hour_d1 & (~hour_d2) );assign min_adj = key[3]&&(min_d1 & (~min_d2));assign sec_adj = key[3]&&(sec_d1 & (~sec_d2));
2、时钟计数器设计,生成时分秒信号,若按键信号有效,则按照相应按键处理。
//---------------------sec计数器处理----------------------always @ ( posedge clock_100k ,negedge reset) begin if (! reset )cnt_sec_r <= 0;else if ( cnt_sec_r <=9 )beginif ( sec_adj ) cnt_sec_r <= cnt_sec_r + 1;else if ( cnt == 99999 )//99999cnt_sec_r <= cnt_sec_r + 1;endelsecnt_sec_r <= 0;end always @ ( posedge clock_100k ,negedge reset)begin if ( !reset)cnt_sec_rr <= 0;else if ( cnt_sec_rr <= 5 ) beginif ( sec_adj && cnt_sec_r ==9) cnt_sec_rr <= cnt_sec_rr + 1; else if ( cnt_sec_r == 9 && cnt == 99999 )// && cnt == 0cnt_sec_rr <= cnt_sec_rr + 1;endelsecnt_sec_rr <= 0;end assign sec_cnt = cnt_sec_rr*10+cnt_sec_r ;
3、整体代码如下
module clock_cal( input reset , input clock_100k ,input [7:0]key,output [4:0]hour_cnt ,output [5:0]min_cnt ,output [5:0]sec_cnt );//-------------------------生成调整信号---------------------------------wire hour_adj ;wire sec_adj ;wire min_adj ;reg hour_d1 = 0;reg hour_d2 = 0;reg min_d1 = 0;reg min_d2 = 0;reg sec_d1 = 0;reg sec_d2 = 0;always @ ( posedge clock_100k ) beginhour_d1 <= key[2];hour_d2 <= hour_d1;min_d1 <= key[1];min_d2 <= min_d1;sec_d1 <= key[0];sec_d2 <= sec_d1;endassign hour_adj = key[3]&&( hour_d1 & (~hour_d2) );assign min_adj = key[3]&&(min_d1 & (~min_d2));assign sec_adj = key[3]&&(sec_d1 & (~sec_d2));//-------------------------------------------------reg [16:0] cnt = 0;always @ ( posedge clock_100k ,negedge reset )beginif ( !reset )cnt <= 0;else if ( cnt < 99999 ) cnt <= cnt + 1;else cnt <= 0;end reg [3:0] cnt_sec_r ;reg [3:0] cnt_sec_rr ; reg [3:0] cnt_min_r ;reg [3:0] cnt_min_rr ; reg [3:0] cnt_hour_r;reg [3:0] cnt_hour_rr; //---------------------sec----------------------always @ ( posedge clock_100k ,negedge reset) begin if (! reset )cnt_sec_r <= 0;else if ( cnt_sec_r <=9 )beginif ( sec_adj ) cnt_sec_r <= cnt_sec_r + 1;else if ( cnt == 99999 )//99999cnt_sec_r <= cnt_sec_r + 1;endelsecnt_sec_r <= 0;end always @ ( posedge clock_100k ,negedge reset)begin if ( !reset)cnt_sec_rr <= 0;else if ( cnt_sec_rr <= 5 ) beginif ( sec_adj && cnt_sec_r ==9) cnt_sec_rr <= cnt_sec_rr + 1; else if ( cnt_sec_r == 9 && cnt == 99999 )// && cnt == 0cnt_sec_rr <= cnt_sec_rr + 1;endelsecnt_sec_rr <= 0;end
//-----------------------min---------------------- always @ ( posedge clock_100k ,negedge reset) begin if ( !reset)cnt_min_r <= 0;else if ( cnt_min_r <= 9 ) beginif ( min_adj ) cnt_min_r <= cnt_min_r + 1;else if ( cnt_sec_r == 9 && cnt_sec_rr == 5 && cnt == 99999 )cnt_min_r <= cnt_min_r + 1;endelsecnt_min_r <= 0;end always @ ( posedge clock_100k ,negedge reset )begin if ( !reset)cnt_min_rr <= 0;else if ( cnt_min_rr <= 5 ) beginif ( min_adj && cnt_min_r == 9 ) cnt_min_rr <= cnt_min_rr + 1;else if ( cnt_min_r == 9 && cnt_sec_r == 9 && cnt_sec_rr == 5 && cnt == 99999 )cnt_min_rr <= cnt_min_rr + 1;endelsecnt_min_rr <= 0;end
//-----------------------hour---------------------- always @ ( posedge clock_100k ,negedge reset) begin if (! reset)cnt_hour_r <= 0;else if(cnt_hour_r==3 && cnt_hour_rr==2) beginif(hour_adj )cnt_hour_r<=0;else if( cnt_min_r == 9 && cnt_min_rr == 5 && cnt_sec_r == 9 && cnt_sec_rr == 5 && cnt == 99999 )cnt_hour_r<=0;endelse if ( cnt_hour_r <= 9 ) beginif ( hour_adj ) cnt_hour_r <= cnt_hour_r + 1;else if ( cnt_min_r == 9 && cnt_min_rr == 5 && cnt_sec_r == 9 && cnt_sec_rr == 5 && cnt == 99999 )cnt_hour_r <= cnt_hour_r + 1;endelsecnt_hour_r <= 0;end always @ ( posedge clock_100k ) begin if ( !reset )cnt_hour_rr <= 0;else if ( cnt_hour_rr == 2 &&cnt_hour_r==3)beginif(hour_adj)cnt_hour_rr<=0;else if( cnt_min_r == 9 && cnt_min_rr == 5 && cnt_sec_r == 9 && cnt_sec_rr == 5 && cnt == 99999 )cnt_hour_rr<=0;endelse if ( cnt_hour_rr <= 2 )beginif ( hour_adj && cnt_hour_r==9 ) cnt_hour_rr <= cnt_hour_rr + 1;else if ( cnt_hour_r == 9 && cnt_min_r == 9 && cnt_min_rr == 5 && cnt_sec_r == 9 && cnt_sec_rr == 5 && cnt == 99999 )cnt_hour_rr <= cnt_hour_rr + 1;endelsecnt_hour_rr <= 0;end assign hour_cnt = cnt_hour_rr*10+cnt_hour_r;assign min_cnt = cnt_min_rr*10+cnt_min_r ;assign sec_cnt = cnt_sec_rr*10+cnt_sec_r ;endmodule
这篇关于FPGA——可调时时钟设计(verilog)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!