clock.txt
上传用户:tianzi0744
上传日期:2009-09-14
资源大小:2k
文件大小:4k
开发平台:

MultiPlatform

  1. module clock(CLK,CLK_1K,MODE,CHANGE,TURN,ALERT,HOU,MIN,SEC,LD_AL,LD_H,LD_M);
  2. input CLK,CLK_1K,MODE,CHANGE,TURN;
  3. output [7:0] HOU,MIN,SEC;
  4. output ALERT,LD_AL,LD_H,LD_M;
  5. reg [7:0] HOU,MIN,SEC,THOU,TMIN,TSEC,AHOU,AMIN;
  6. reg [1:0] m,fm,sound;
  7. reg LD_H,LD_M;
  8. reg CLK_1Hz,CLK_2Hz,mclk,hclk;
  9. reg alert1,alert2,ear;
  10. reg count1,count2,lcount1,lcount2;
  11. wire ct1,ct2,lct1,lct2,m_clk,h_clk;
  12. always @(posedge CLK)
  13. begin
  14.    CLK_2Hz<=~CLK_2Hz;
  15.    if(sound==3)
  16.       begin sound<=0;ear<=1;end
  17.             //ear信号用于产生或屏蔽声音
  18.    else 
  19.       begin sound<=sound+1;ear<=0;end
  20. end
  21. always @(posedge CLK_2Hz)
  22.    CLK_1Hz<=~CLK_1Hz; //产生1Hz的时基信号
  23. always @(posedge MODE)   //MODE信号控制3钟功能的转换
  24.    begin if(m==2) m<=0;  //m=0:计时功能
  25.          else m<=m+1;    //m=1:闹钟功能
  26.    end                   //m=2:手动校时
  27. always @(TURN)
  28.    fm<=~fm;     //校时时选择调整分钟还是小时
  29. always
  30. begin case(m)
  31.       2:begin if(fm)
  32.                  begin
  33.                    count1<=CHANGE;
  34.                    {LD_H,LD_M}<=2'b01;
  35.                  end
  36.               else
  37.                  begin
  38.                    count2<=CHANGE;
  39.                    {LD_H,LD_M}<=2'b10;
  40.                  end
  41.               {count1,count2}<=0;
  42.         end
  43.       1:begin if(fm)
  44.                  begin
  45.                    lcount1<=CHANGE;
  46.                    {LD_H,LD_M}<=2'b01;
  47.                  end
  48.               else
  49.                  begin
  50.                    lcount2<=CHANGE;
  51.                    {LD_H,LD_M}<=2'b10;
  52.                  end
  53.               {lcount1,lcount2}<=0;
  54.         end
  55.       default:{count1,count2,lcount1,lcount2,LD_H,LD_M}<=0;
  56.      endcase
  57. end
  58. always @(posedge CLK_1Hz)  //秒计时和秒调整
  59.   if(!(TSEC^8'h59)|TURN&(!m))
  60.     begin TSEC<=0;
  61.           if(!(TURN&(!m)))
  62.             mclk<=1;
  63.     end
  64.   else begin
  65.      if(TSEC[3:0]==9)
  66.            begin TSEC[3:0]<=0;
  67.                  TSEC[7:4]<=TSEC[7:4]+1;
  68.            end
  69.      else TSEC[3:0]<=TSEC[3:0]+1;
  70.        mclk<=0;
  71.        end
  72. assign m_clk=mclk|count1;
  73. assign h_clk=hclk|count2;
  74. assign ct1=CLK|m_clk;    //ct1用于计时、校时中的分钟计数
  75. assign ct2=CLK|h_clk;    //ct2用于计时、校时中的小时计数 
  76. assign lct1=CLK|lcount1; //lct1用于定时状态下调整分钟信号
  77. assign lct2=CLK|lcount2; //lct2用于定时状态下调整小时信号
  78. always @(posedge ct1)    //分计时和分调整
  79.   begin if(TMIN==8'h59)
  80.           begin TMIN<=0;
  81.                 hclk<=1;
  82.           end
  83.         else begin
  84.           if(TMIN[3:0]==9)
  85.              begin TMIN[3:0]<=0;
  86.                    TMIN[7:4]<=TMIN[7:4]+1;
  87.              end
  88.          else TMIN[3:0]<=TMIN[3:0]+1;
  89.             hclk<=0;
  90.             end
  91.   end
  92. always @(posedge ct2)    //小时计时和小时调整
  93.    if(THOU==8'h23)
  94.         THOU<=0;
  95.    else begin
  96.           if(THOU[3:0]==9)
  97.              begin THOU[3:0]<=0;
  98.                  THOU[7:4]<=THOU[7:4]+1;
  99.              end
  100.           else THOU[3:0]<=THOU[3:0]+1;
  101.         end
  102. always @(posedge lct1)    //闹钟定时中分钟调整
  103.    if(AMIN==8'h59)
  104.         AMIN<=0;
  105.    else if(AMIN[3:0]==9)
  106.              begin AMIN[3:0]<=0;
  107.                  AMIN[7:4]<=AMIN[7:4]+1;
  108.              end
  109.           else AMIN[3:0]<=AMIN[3:0]+1;
  110.         
  111. always @(posedge lct2)    //闹钟定时中小时调整
  112.    if(AHOU==8'h23)
  113.         AHOU<=0;
  114.    else begin
  115.           if(AHOU[3:0]==9)
  116.              begin AHOU[3:0]<=0;
  117.                  AHOU[7:4]<=AHOU[7:4]+1;
  118.              end
  119.           else AHOU[3:0]<=AHOU[3:0]+1;
  120.        end
  121. always    //闹钟功能:产生闹铃信号
  122.    if((THOU==AHOU)&&(TMIN==AMIN)&&(AHOU|AMIN)&&(!CHANGE))
  123.      //若按住"CHANGE"键不放就可以屏蔽闹铃
  124.       if(TSEC<8'h30)  alert1<=1; //设定闹铃声的长短,这里设为30秒
  125.       else alert1<=0;
  126.    else alert1<=0;
  127. always
  128.   case(m)
  129.   0:begin HOU<=THOU;  //计时状态下时、分、秒显示
  130.           MIN<=TMIN;
  131.           SEC<=TSEC;
  132.     end
  133.   1:begin HOU<=AHOU;  //定时状态下时、分、秒显示
  134.           MIN<=AMIN;
  135.           SEC<=8'hzz;
  136.     end
  137.   2:begin HOU<=THOU;  //校时状态下时、分、秒显示
  138.           MIN<=TMIN;
  139.           SEC<=8'hzz;
  140.     end
  141.  endcase
  142. assign LD_AL=(AHOU|AMIN)?1:0;
  143. always     //整点报时功能:产生整点报时信号
  144.    begin if((TMIN==8'h59)&&(TSEC>8'h54)||(!(TMIN|TSEC)))
  145.             if(TSEC>8'h54) 
  146.                alert2<=ear & CLK_1K; //产生短音
  147.             else
  148.                alert2<=!ear & CLK_1K; //产生长音
  149.         else alert2<=0;
  150.    end
  151. assign ALERT=((alert1)?CLK_1K & CLK:0)|alert2;
  152.              //闹铃音和整点报时信号输出
  153. endmodule