




已阅读5页,还剩7页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
电子钟二、电子钟 module clock(work_state,clock_on,clk_6MHz,clk_4Hz,in_set,display_set,clk_1024Hz,up,down,out1,out2,out3,out4,out5,out6,out7,out8,speaker); input work_state,clk_1024Hz,up,down,display_set,clk_6MHz,in_set,clock_on,clk_4Hz; output out1,out2,out3,out4,out5,out6,out7,out8,speaker; wire2:0 in_set; reg3:0 out1,out2,out3,out4,out5,out6,out7,out8; reg2:0 display_state; reg7:0 hour,minute,second,year,month,day,day_max; reg7:0 hour_set,minute_set,second_set,day_set,month_set,year_set,day_set_max; integer century=20; integer i=0; reg speaker; reg7:0 clock_hour,clock_minute,clock_second; reg3:0 high,med,low; reg13:0 divider,origin; reg7:0 counter; reg out; wire carry; /*各初始值设置时对应的状态*/ parameter state_yorh=3b100, /设置年或小时对应的按键状态 state_morm=3b010, /设置月或分对应的按键状态 state_dors=3b001; /设置日或秒对应的按键状态 /*初始化时钟:08年08月08日00:00:00*/ initial begin year=8; year_set=8; month=8; month_set=8; day=8; day_set=8; hour=0; minute=0; second=0; clock_hour=0; clock_minute=0; clock_second=0; display_state=1023) begin i=0; if(work_state) begin second=second_set; minute=minute_set; hour=hour_set; day=day_set; month=month_set; year=59) begin second=59) begin minute=23) begin hour=day_max) begin day=12) begin month=99) year=0; else year=year+1; end else month=month+1; end else day=day+1; end else hour=hour+1; end else minute=minute+1; end else second=second+1; end end else i=2) display_state=0; else display_state=display_state+1; end /*闹钟模块设置,用clock_on控制闹钟,闹钟铃声播放时间为一分钟,可手动关闭闹钟*/ always (posedge clk_6MHz) begin if(clock_on) begin if(clock_hour=hour&clock_minute=minute) speaker=out; else speaker=0; end else speaker=0; end /*设置时钟的初值*/ /*用up来触发,当down=0时,一个上升沿触发来时加1,否则减1.*/ always ( posedge up) begin if(!work_state) /时间处于正常工作状态时,使要设置的初始值保持同步 begin second_set=second; minute_set=minute; hour_set=hour; day_set=day; month_set=month; year_set=99) year_set=0; else year_set=12) month_set=1; else month_set=day_set_max) day_set=1; else day_set=day_set+1; end endcase end else begin case(in_set) state_yorh:begin if(year_set=0) year_set=99; else year_set=year_set-1; end state_morm:begin if(month_set=1) month_set=12; else month_set=month_set-1; end state_dors:begin if(day_set=1) day_set=day_set_max; else day_set=23) hour_set=0; else hour_set=59) minute_set=0; else minute_set=59) second_set=0; else second_set=second_set+1; end endcase end else begin case(in_set) state_yorh:begin if(hour_set=0) hour_set=23; else hour_set=hour_set-1; end state_morm:begin if(minute_set=0) minute_set=59; else minute_set=minute_set-1; end state_dors:begin if(second_set=0) second_set=59; else second_set=23) clock_hour=0; else clock_hour=59) clock_minute=59) clock_second=0; else clock_second=clock_second+1; end endcase end else begin case(in_set) state_yorh:begin if(clock_hour=0) clock_hour=23; else clock_hour=clock_hour-1; end state_morm:begin if(clock_minute=0) clock_minute=59; else clock_minute=clock_minute-1; end state_dors:begin if(clock_second=0) clock_second=59; else clock_second=clock_second-1; end endcase end end end end /*本年度当前月的天数的计算*/ always (day or day_set)/当日期变化时,则其所处月份的天数; begin if(month=1|month=3|month=5|month=7|month=8|month=10|month=12) day_max=31; else if(month=4|month=6|month=9|month=11) day_max=30; else begin if(century,year%400=0)|(century,year%4=0)&(century,year%10!=0) day_max=29; else day_max=28; end if(month_set=1|month_set=3|month_set=5|month_set=7|month_set=8|month_set=10|month_set=12) day_set_max=31; else if(month_set=4|month_set=6|month_set=9|month_set=11) day_set_max=30; else begin if(century,year_set%400=0)|(century,year_set%4=0)&(century,year_set%10!=0) day_set_max=29; else day_set_max=28; end end /*数码管显示模块,display_state=1时,显示年月日,否则显示时间/ always (posedge clk_6MHz) begin if(!work_state) begin if(display_state=1) begin out8=year%10; out7=year/10; out6=13; out5=month%10; out4=month/10; out3=13; out2=day%10; out1=day/10; end else if(display_state=0) begin out8=hour%10; out7=hour/10; out6=15; out5=minute%10; out4=minute/10; out3=15; out2=second%10; out1=second/10; end else if(display_state=2) begin out8=clock_hour%10; out7=clock_hour/10; out6=12; out5=clock_minute%10; out4=clock_minute/10; out3=12; out2=clock_second%10; out1=clock_second/10; end end else begin if(display_state=1) begin out8=year_set%10; out7=year_set/10; out6=13; out5=month_set%10; out4=month_set/10; out3=13; out2=day_set%10; out1=day_set/10; end else if(display_state=0) begin out8=hour_set%10; out7=hour_set/10; out6=15; out5=minute_set%10; out4=minute_set/10; out3=15; out2=second_set%10; out1=second_set/10; end else if(display_state=2) begin out8=clock_hour%10; out7=clock_hour/10; out6=12; out5=clock_minute%10; out4=clock_minute/10; out3=12; out2=clock_second%10; out1=clock_second/10; end end end /*用乐曲梁祝作为闹钟铃声*/ assign carry=(divider=16383); always (posedge clk_6MHz) begin if(carry) divider=origin; else divider=divider+1; end always (posedge carry) begin out=out; /2 分频产生方波信号 end always (posedge clk_4Hz) begin case(high,med,low) /分频比预置 b000000000011: origin=7281; b000000000101: origin=8730; b000000000110: origin=9565; b000000000111: origin=10310; b000000010000: origin=10647; b000000100000: origin=11272; b000000110000: origin=11831; b000001010000: origin=12556; b000001100000: origin=12974; b000100000000: origin=13516; b000000000000: origin=16383; endcase end always (posedge clk_4Hz) begin if(counter=63) counter=0; /计时,以实现循环演奏 else counter=counter+1; case(counter) /记谱 0: high,med,low=b000000000011; /低音“3” 1: high,med,low=b000000000011; /持续4 个时钟节拍 2: high,med,low=b000000000011; 3: high,med,low=b000000000011; 4: high,med,low=b000000000101; /低音“5” 5: high,med,low=b000000000101; /发3 个时钟节拍 6: high,med,low=b000000000101; 7: high,med,low=b000000000110; /低音“6” 8: high,med,low=b000000010000; /中音“1” 9: high,med,low=b000000010000; /发3 个时钟节拍 10: high,med,low=b000000010000; 11: high,med,low=b000000100000; /中音“2” 12: high,med,low=b000000000110; /低音“6” 13: high,med,low=b000000010000; 14: high,med,low=b000000000101; 15: high,med,low=b000000000101; 16: high,med,low=b000001010000; /中音“5” 17: high,med,low=b000001010000; /发3 个时钟节拍 18: high,med,low=b000001010000; 19: high,med,low=b000100000000; /高音“1” 20: high,med,low=b000001100000; 21: high,med,low=b000001010000; 22: high,med,low=b000000110000; 23: high,med,low=b000001010000; 24: high,med,low=b000000100000; /中音“2” 25: high,med,low=b000000100000; /持续11 个时钟节拍 26: high,med,low=b000000100000; 27: high,med,low=b000000100000; 28: high,med,low=b000000100000; 29: high,med,low=b000000100000; 30: high,med,low=b000000100000; 31: high,med,low=b000000100000; 32: high,med,low=b000000100000; 33: high,med,low=b000000100000; 34: high,med,low=b000000100000; 35: high,med,low=b000000110000; /中音“3” 36: high,med,low=b000000000111; /低音“7” 37: high,med,low=b000000000111; 38: high,med,low=b000000000110; /低音“6” 39: high,med,low=b000000000110; 40: high,med,low=b000000000101; /低音“5” 41: high,med,low=b000000000101; 42: high,med,low=b0000000001
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 林木种苗培育中心创新创业项目商业计划书
- 保护范围合同(标准版)
- 海参养殖模式创新创业项目商业计划书
- 考安全员证的及答案1
- 车间安全员证考及答案
- 2025活动场地租赁合同范本
- 2025商务合同到期续签申请模板
- 2025学校电力供应管理合同书
- 2025连锁店销售合同范本
- 2025房屋修缮施工合同范本
- 邀请函模板完整
- 2020新译林版高中英语选择性必修二全册课文及翻译(英汉对照)
- 大学物理第14章光的衍射课件
- 家长会 课件(共44张ppt) 九年级上学期
- 钻孔灌注桩施工安全控制培训教材课件
- 福建省莆田市各县区乡镇行政村村庄村名明细
- 大班幼儿随访电访记录表内有内容
- 干细胞精品课件
- 太阳能路灯说明书完整版
- 中国老龄化社会的潜藏价值(中英)
- 初中化学课程标准(修订稿)
评论
0/150
提交评论