您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > Verilog--数字钟设计代码
数字钟一、简介此数字钟为时分秒可调,带有闹钟设置。各引脚功能入下:clk:输入时钟信号,为50mhz;(clk1k:产生闹铃音、报时音的时钟信号,)mode:功能控制信号;为0:计时功能;为1:闹钟功能;为2:手动校时功能;turn:接按键,在手动校时功能时,选择是调整小时,还是分钟;若长时间按住该键,还可使秒信号清零,用于精确调时;change:接按键,手动调整时,每按一次,计数器加1;如果长按,则连续快速加1,用于快速调时和定时;seg:此信号分别输出显示数据;采用BCD码计数,分别驱动6个数码管显示时间;scan:数码管位选择信号输出alert:输出到扬声器的信号,用于产生闹铃音和报时音;闹铃音为持续20秒的急促的“嘀嘀嘀”音,若按住“change”键,则可屏蔽该音;整点报时音为“嘀嘀嘀嘀—嘟”四短一长音;LD_alert:接发光二极管,指示是否设置了闹钟功能;LD_hour:接发光二极管,指示当前调整的是小时信号;LD_min:接发光二极管,指示当前调整的是分钟信号。二、程序如下moduleshuzizhong(clk,mode,change,turn,alert,seg,scan,LD_alert,LD_hour,LD_min);inputclk,mode,change,turn;outputalert,LD_alert,LD_hour,LD_min;output[7:0]scan,seg;reg[7:0]scan,seg;reg[7:0]hour,min,sec,hour1,min1,sec1,ahour,amin;reg[1:0]m,fm,num1,num2,num3,num4;reg[1:0]loop1,loop2,loop3,loop4,sound;regLD_hour,LD_min;regclk1hz,clk4hz,clk2hz,clk1khz,minclk,hclk;regalert1,alert2,ear;regcount1,count2,counta,countb;reg[3:0]data;reg[2:0]cnt8;reg[14:0]count3;reg[6:0]count4;wirect1,ct2,cta,ctb,m_clk,h_clk;//-------------------1khz,2hz--,1hz------------------------------------always@(posedgeclk)beginif(count3=='d25000)beginclk1khz=~clk1khz;count3=0;endelsebegincount3=count3+1'b1;endendalways@(posedgeclk1khz)//4hzbeginif(count4=='d125)beginclk4hz=~clk4hz;count4=0;endelsebegincount4=count4+1'b1;endendalways@(posedgeclk4hz)//2hzbeginclk2hz=~clk2hz;endalways@(posedgeclk2hz)//1hzbeginclk1hz=~clk1hz;end//-----------------------------------------------------------always@(posedgeclk4hz)beginif(sound==3)beginsound=0;ear=1;endelsebeginsound=sound+1'b1;ear=0;endendalways@(negedgemode)beginif(m==2)m=0;elsem=m+1'b1;endalways@(negedgeturn)beginfm=~fm;end//----------------------------------------------------------------alwaysbegincase(m)2:beginif(fm)begincount1=change;{LD_min,LD_hour}=2;endelsebegincounta=change;{LD_min,LD_hour}=1;end{count2,countb}=2'b11;end1:beginif(fm)begincount2=change;{LD_min,LD_hour}=2;endelsebegincountb=change;{LD_min,LD_hour}=1;end{count1,counta}=2'b11;enddefault:{count1,count2,counta,countb,LD_min,LD_hour}=6'b111111;endcaseendalways@(negedgeclk4hz)beginif(!count2)beginif(loop1==3)num1=1;elsebeginloop1=loop1+1'b1;num1=0;endendelsebeginloop1=0;num1=0;endendalways@(negedgeclk4hz)beginif(!countb)beginif(loop2==3)num2=1;elsebeginloop2=loop2+1'b1;num2=0;endendelsebeginloop2=0;num2=0;endendalways@(negedgeclk4hz)beginif(!count1)beginif(loop3==3)num3=1;elsebeginloop3=loop3+1'b1;num3=0;endendelsebeginloop3=0;num3=0;endendalways@(negedgeclk4hz)beginif(!counta)beginif(loop4==3)num4=1;elsebeginloop4=loop4+1'b1;num4=0;endendelsebeginloop4=0;num4=0;endendassignct1=(num3&clk)|(!num3&m_clk);assignct2=(num1&clk)|(!num1&(!count2));assigncta=(num4&clk)|(!num4&h_clk);assignctb=(num2&clk)|(!num2&(!countb));//-----------------------------------------------------------------------------always@(posedgeclk1hz)beginif(!(sec1^8'h59)|(!turn)&(!m))beginsec1=0;if(!((!turn)&(!m)))minclk=1;endelsebeginif(sec1[3:0]==4'b1001)beginsec1[3:0]=4'b0000;sec1[7:4]=sec1[7:4]+1'b1;endelsesec1[3:0]=sec1[3:0]+1'b1;minclk=0;endendassignm_clk=minclk||(!count1);always@(posedgect1)beginif(min1==8'h59)beginmin1=0;hclk=1;endelsebeginif(min1[3:0]==9)beginmin1[3:0]=0;min1[7:4]=min1[7:4]+1'b1;endelsemin1[3:0]=min1[3:0]+1'b1;hclk=0;endendassignh_clk=hclk||(!counta);always@(posedgecta)//Сʱ¼ÆʱºÍСʱµ÷Õû½ø³Ìif(hour1==8'h23)hour1=0;elseif(hour1[3:0]==9)beginhour1[7:4]=hour1[7:4]+1'b1;hour1[3:0]=0;endelsehour1[3:0]=hour1[3:0]+1'b1;always@(posedgect2)if(amin==8'h59)amin=0;elseif(amin[3:0]==9)beginamin[3:0]=0;amin[7:4]=amin[7:4]+1'b1;endelseamin[3:0]=amin[3:0]+1'b1;always@(posedgectb)if(ahour==8'h23)ahour=0;elseif(ahour[3:0]==9)beginahour[3:0]=0;ahour[7:4]=ahour[7:4]+1'b1;endelseahour[3:0]=ahour[3:0]+1'b1;//---------------------------------------------------------------------------alwaysif((min1==amin)&&(hour1==ahour)&&(amin|ahour)&&(change))if(sec18'h20)alert1=1;elsealert1=0;elsealert1=0;alwaysbegincase(m)3'b00:beginhour=hour1;min=min1;sec=sec1;end3'b01:beginhour=ahour;min=amin;sec=8'bx;end3'b10:beginhour=hour1;min=min1;sec=8'bx;enddefault:beginhour=8'bx;min=8'bx;sec=8'bx;endendcaseendassignLD_alert=(ahour|amin)?1'b1:1'b0;assignalert=((alert1)?clk1khz&clk4hz:1'b0)|alert2;alwaysbeginif((min1==8'h59)&&(sec18'h54)||(!(min1|sec1)))if(sec18'h54)alert2=ear&clk1khz;elsealert2=!ear&clk1khz;elsealert2=0;end//-------------------------------------------------------------always@(posedgeclk1khz)begincnt8=cnt8+1'b1;endalwaysbegincase(cnt8[2:0])3'b000:beginscan=8'b01111111;data[3:0]=hour[7:4];end3'b001:beginscan=8'b10111111;data[3:0]=hour[3:0];end3'b010:beginscan=8'b11011111;data[3:0]=min[7:4];end3'b011:beginscan=8'b11101111;data[3:0]=min[3:0];end3'b100:beginscan=8'b11110111;data[3:0]=sec[7:4];end3'b101:beginscan=8'b11111011;data[3:0]=sec[3:0];end//3'b110:beginscan='b11111101;data[3:0]=4'b1011;end//3'b111:beginscan='b11111110;data[3:0]=4'b1010;enddefault:beginscan=8'b11111111;data[3:0]=4'bx;endendcasecase(data[3:0])4'b0000:seg[7:0]
本文标题:Verilog--数字钟设计代码
链接地址:https://www.777doc.com/doc-5898107 .html