﻿{"id":611,"date":"2022-07-14T18:38:47","date_gmt":"2022-07-14T10:38:47","guid":{"rendered":"https:\/\/dt9025a.xyz\/?p=611"},"modified":"2022-07-14T18:39:21","modified_gmt":"2022-07-14T10:39:21","slug":"hdlbits-count-clock","status":"publish","type":"post","link":"https:\/\/dt9025a.top\/?p=611","title":{"rendered":"HDLBits &#8211; Count clock"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"462\" src=\"https:\/\/dt9025a.top\/wp-content\/uploads\/2022\/07\/image-1024x462.png\" alt=\"\" class=\"wp-image-612\" srcset=\"https:\/\/dt9025a.top\/wp-content\/uploads\/2022\/07\/image-1024x462.png 1024w, https:\/\/dt9025a.top\/wp-content\/uploads\/2022\/07\/image-300x135.png 300w, https:\/\/dt9025a.top\/wp-content\/uploads\/2022\/07\/image-768x346.png 768w, https:\/\/dt9025a.top\/wp-content\/uploads\/2022\/07\/image-1536x693.png 1536w, https:\/\/dt9025a.top\/wp-content\/uploads\/2022\/07\/image.png 1590w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/hdlbits.01xz.net\/wiki\/Count_clock\" target=\"_blank\">HDLBits\u9898\u76ee\u94fe\u63a5<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u9898\u76ee\u8981\u6c42<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>\u6784\u5efa\u4e00\u4e2a\u5e26\u6709AM\/PM\u6307\u793a\u6807\u5fd7\u768412\u5c0f\u65f6\u65f6\u949f\uff0c<code>clk<\/code>\u8f93\u5165\u4e3a1 pulse\/sec\u3002<\/li><li>\u590d\u4f4d\u8f93\u5165<code>reset<\/code>\u5c06\u65f6\u949f\u590d\u4f4d\u81f3AM 12:00\uff0c\u4f18\u5148\u7ea7\u9ad8\u4e8e\u4f7f\u80fd\u8f93\u5165<code>enable<\/code>\u3002<\/li><li>PM\u6307\u793a\u6807\u5fd7<code>pm<\/code>\u4e3a0\u65f6\u8868\u793aAM\uff0c1\u8868\u793aPM\u3002<\/li><li>hh\uff0cmm\uff0css\u7684\u6570\u636e\u8868\u793a\u65b9\u5f0f\u4e3aBCD\u7801\u3002<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u9898\u76ee\u5206\u6790<\/h2>\n\n\n\n<p>\u9996\u5148\uff0c12\u5c0f\u65f6\u5236\u4e0e24\u5c0f\u65f6\u5236\u7684\u6240\u8868\u793a\u7684\u65f6\u95f4\u6bb5\u6709\u5982\u4e0b\u5bf9\u5e94\u89c4\u5219\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table aligncenter\"><table><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">12HR<\/td><td class=\"has-text-align-center\" data-align=\"center\">24HR<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">12:00AM &#8211; 11:59AM<\/td><td class=\"has-text-align-center\" data-align=\"center\">0:00 &#8211; 11:59<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">12:00PM &#8211; 11:59PM<\/td><td class=\"has-text-align-center\" data-align=\"center\">12:00 &#8211; 23:59<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u6240\u4ee5\uff0c\u5176\u5b9e\u54b1\u4eec\u4e0d\u7528\u505a\u8fd9\u4e2a\u5bf9\u5e94\uff0c\u53ea\u9700\u8981\u50bb\u50bb\u8ba1\u65f6\u4ee5\u53ca\u7ef4\u62a4<code>pm<\/code>\u6307\u793a\u6807\u5fd7\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u7531\u4e8e\u65f6\u5206\u79d2\u7684\u8ba1\u65f6\u8303\u56f4\u5206\u522b\u4e3a1-12\uff0c0-59\uff0c\u56e0\u6b64\u53ef\u4ee5\u8bbe\u8ba1\u4e24\u4e2aBCD\u8ba1\u65f6\u6a21\u5757\uff0c\u5206\u522b\u7ef4\u62a41-12\u7684\u8ba1\u65f6\u548cpm\u6807\u5fd7\uff0c\u4ee5\u53ca0-59\u7684\u8ba1\u65f6\u3002<\/p>\n\n\n\n<p>\u5199\u51fa\u4e24\u6a21\u5757\u5206\u522b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"verilog\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">module cnt12\n(\n    input clk,\n    input reset,\n    input ena,\n    output reg [7:0] counter,\n    output reg pm\n);\n    \/\/ \u521d\u59cb\u4e3a\u4e0a\u534812\u65f6\n    initial pm = 1'b0;\n    initial counter = 8'h12;\n    \n    always @(posedge clk) begin\n        \/\/ reset\u9ad8\u4f18\u5148\u7ea7\n        if (reset) begin\n            counter &lt;= 8'h12;\n            pm &lt;= 1'b0;\n        end\n        else begin\n            if (ena) begin\n                if (counter == 8'h11) begin\n                    \/\/ \u8be5\u6539\u53d8PM\u6807\u5fd7\u4e86\uff0c\u8981\u4e0d\u7b498'h112\u505a\u975e\u963b\u585e\u8d4b\u503c\u5c31\u4f1a\u6709\u4e00\u4e2a\u6267\u884c\u5468\u671f\u7684\u5ef6\u8fdf\n                    pm &lt;= !pm;\n                    counter &lt;= counter + 8'h1;\n                end\n                else if (counter == 8'h12) begin\n                    \/\/ \u8ba1\u65f6\u5faa\u73af\u7ed3\u675f\n                    counter &lt;= 8'h1;\n                end\n                else if (counter == 8'h9) begin\n                    \/\/ \u8fd9\u91cc\u5341\u4f4d\u6700\u9ad8\u4e3a1\uff0c\u4ec5\u9700if\u5224\u65ad\u5373\u53ef\uff0c\u4e0d\u9700\u8981\u4f7f\u7528vector\u64cd\u4f5c\n                    counter &lt;= 8'h10;\n                end\n                else begin\n                    \/\/ \u8ba1\u6570+1\n                    counter &lt;= counter + 8'h1;\n                end\n            end\n        end\n    end\n\nendmodule\n\n\nmodule cnt60\n(\n    input clk,\n    input reset,\n    input ena,\n    output reg [7:0] counter\n);\n\n    always @(posedge clk) begin\n        \/\/ reset\u9ad8\u4f18\u5148\u7ea7\n        if (reset) begin\n            counter &lt;= 8'h0;\n        end\n        else begin\n            if (ena) begin\n                if (counter == 8'h59) begin\n                    \/\/ \u8ba1\u65f6\u5faa\u73af\u7ed3\u675f\n                    counter &lt;= 8'h0;\n                end\n                else if (counter[3:0] == 4'd9) begin\n                    \/\/ \u8fd9\u91cc\u8fdb\u4f4d\u4f7f\u7528vector\u64cd\u4f5c\u66f4\u65b9\u4fbf\uff0c\u4e0d\u9700\u8981\u5927\u91cfif\u5224\u65ad\n                    counter[7:4] &lt;= counter[7:4] + 4'd1;\n                    counter[3:0] &lt;= 4'd0;\n                end\n                else begin\n                    \/\/ \u8ba1\u6570+1\n                    counter[3:0] &lt;= counter[3:0] + 4'd1;\n                end\n            end\n        end\n    end\n\nendmodule<\/pre>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u5728\u9876\u5c42\u6a21\u5757\u4e2d\u4f8b\u5316\u4e24\u4e2a\u8ba1\u65f6\u6a21\u5757\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"verilog\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">module top_module(\n    input clk,\n    input reset,\n    input ena,\n    output pm,\n    output [7:0] hh,\n    output [7:0] mm,\n    output [7:0] ss);\n\n    wire ena_mm, ena_hh;\n    \n    \/\/ \u5206\u949f\u8fdb\u4f4d\uff1a59\u79d2+1\n    assign ena_mm = ena &amp; (ss == 8'h59);\n    \/\/ \u5c0f\u65f6\u8fdb\u4f4d\uff1a59\u520659\u79d2+1\uff0c\u4f7f\u7528ena_mm\u8fdb\u884c\u7b80\u5316\n    assign ena_hh = ena_mm &amp; (mm == 8'h59);\n    \n    cnt60 u_cnt60_ss(clk, reset, ena, ss);\n    cnt60 u_cnt60_mm(clk, reset, ena_mm, mm);\n    cnt12 u_cnt12_hh(clk, reset, ena_hh, hh, pm);\n\nendmodule<\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity is-style-wide\"\/>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"56\" height=\"56\" src=\"https:\/\/dt9025a.top\/wp-content\/uploads\/2020\/08\/kuaikule.gif\" alt=\"\" class=\"wp-image-33\"\/><\/figure>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>HDLBits\u9898\u76ee\u94fe\u63a5 \u9898\u76ee\u8981\u6c42 \u6784\u5efa\u4e00\u4e2a\u5e26\u6709AM\/PM\u6307\u793a\u6807\u5fd7\u768412\u5c0f\u65f6\u65f6\u949f\uff0cclk\u8f93\u5165\u4e3a1 pulse\/s&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/dt9025a.top\/?p=611\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,9],"tags":[],"class_list":["post-611","post","type-post","status-publish","format-standard","hentry","category-verilog-and-fpga","category-9"],"_links":{"self":[{"href":"https:\/\/dt9025a.top\/index.php?rest_route=\/wp\/v2\/posts\/611","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dt9025a.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dt9025a.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dt9025a.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dt9025a.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=611"}],"version-history":[{"count":0,"href":"https:\/\/dt9025a.top\/index.php?rest_route=\/wp\/v2\/posts\/611\/revisions"}],"wp:attachment":[{"href":"https:\/\/dt9025a.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dt9025a.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dt9025a.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}