{"id":900,"date":"2016-03-10T13:49:53","date_gmt":"2016-03-10T21:49:53","guid":{"rendered":"http:\/\/www.beyond-circuits.com\/wordpress\/?p=900"},"modified":"2016-10-31T13:39:11","modified_gmt":"2016-10-31T21:39:11","slug":"detecting-the-rising-edge-of-a-short-pulse","status":"publish","type":"post","link":"https:\/\/www.beyond-circuits.com\/wordpress\/2016\/03\/detecting-the-rising-edge-of-a-short-pulse\/","title":{"rendered":"Detecting the rising edge of a short pulse"},"content":{"rendered":"<p>A reader is going through my ZedBoard tutorial and had some questions about detecting the rising edge of a pulse. The <a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/tutorial\/tutorial8\/\">tutorial<\/a> in question is using a ZedBoard to make a stopwatch. Kind of overkill in terms of hardware, but you have to start somewhere when you&#8217;re\u00a0learning to code.<\/p>\n<p>TRAN MINHHAI&#8217;s question asked: what do you do when the rising edge might just be a pulse, and the pulse might last less than a single clock cycle? The answer is to use a flip-flop with the input signal going to an asynchronous set input. The data input is just zero, and the clock signal is the one we are synchronizing to.<\/p>\n<p>Here is the code:<\/p>\n<pre><code>\r\n`timescale 1ns\/1ns\r\nmodule edge_detect\r\n  (\r\n   input clk,\r\n   input btnl,\r\n   output btnl_rise\r\n   );\r\n\r\n  reg btnl_held = 0;\r\n  always @(posedge clk or posedge btnl)\r\n    if (btnl)\r\n      btnl_held &lt;= 1;\r\n    else\r\n      btnl_held &lt;= 0;\r\n\r\n  reg [1:0] btnl_shift = 0;\r\n  always @(posedge clk)\r\n    btnl_shift &lt;= {btnl_shift,btnl_held};\r\n  \r\n  assign btnl_rise = btnl_shift == 2'b01;\r\nendmodule\r\n<\/code><\/pre>\n<p>I also wrote a little test to go with it. Notice how short little pulses can come anywhere with respect to the clock edge:<\/p>\n<pre><code>\r\n`timescale 1ns\/1ns\r\nmodule edge_detect_test;\r\n\r\n  reg clk = 0;\r\n  always #10 clk = ~clk;\r\n\r\n  reg btnl = 0;\r\n\r\n  wire btnl_rise;\r\n\r\n  edge_detect edge_detect(clk,btnl,btnl_rise);\r\n\r\n  initial\r\n    begin\r\n      $dumpvars(0);\r\n      \r\n      @(posedge clk)\r\n\tbtnl &lt;= 1;\r\n      repeat (10) @(posedge clk);\r\n      btnl &lt;= 0;\r\n      repeat (4) @(posedge clk);\r\n      #10 btnl &lt;= 1;\r\n      #1 btnl &lt;= 0;\r\n      repeat (4) @(posedge clk);\r\n      #19 btnl &lt;= 1;\r\n      #1 btnl &lt;= 0;\r\n      repeat (4) @(posedge clk);\r\n      #20 btnl &lt;= 1;\r\n      #1 btnl &lt;= 0;\r\n      repeat (4) @(posedge clk);\r\n      $finish;\r\n    end\r\nendmodule\r\n<\/code><\/pre>\n<h2>The long pulse case<\/h2>\n<p>In the case of a long pulse, the design works just like it would\u00a0without the asynchronous set flip-flop. Here&#8217;s a timing diagram:<\/p>\n<p><script type=\"WaveDrom\">\n{ signal : [\n{ name: \"clk\",       wave: \"010101010101\" },\n{ name: \"btnl\",      wave: \"0.1......0..\" },\n{ name: \"btnl_rise\", wave: \"0..1.0......\" },\n]}\n<\/script><\/p>\n<h2>The short pulse case<\/h2>\n<p>But if there is a short pulse, the asynchronous set flip-flop holds the input value until there is a clock edge.<\/p>\n<p>If the pulse appears in the middle of the clock cycle, then the timing diagram looks like this:<\/p>\n<p><script type=\"WaveDrom\">\n{ signal : [\n{ name: \"clk\",       wave: \"0...1...0...1\" },\n{ name: \"btnl\",      wave: \"010..........\" },\n{ name: \"btnl_rise\", wave: \"0...1.......0\" },\n]}\n<\/script><\/p>\n<p>If the pulse appears right before the clock edge, then the timing diagram looks like this:<\/p>\n<p><script type=\"WaveDrom\">\n{ signal : [\n{ name: \"clk\",       wave: \"0...1...0...1.\" },\n{ name: \"btnl\",      wave: \"0..10.........\" },\n{ name: \"btnl_rise\", wave: \"0...1.......0.\" },\n]}\n<\/script><\/p>\n<p>Now you know how to synchronize the rising edge of a pulse even if you have a slow clock.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A reader is going through my ZedBoard tutorial and had some questions about detecting the rising edge of a pulse. The tutorial in question is using a ZedBoard to make a stopwatch. Kind of overkill in terms of hardware, but &hellip; <a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/2016\/03\/detecting-the-rising-edge-of-a-short-pulse\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,1,4],"tags":[],"class_list":["post-900","post","type-post","status-publish","format-standard","hentry","category-hdl","category-uncategorized","category-verilog"],"_links":{"self":[{"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/posts\/900","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/comments?post=900"}],"version-history":[{"count":14,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/posts\/900\/revisions"}],"predecessor-version":[{"id":1165,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/posts\/900\/revisions\/1165"}],"wp:attachment":[{"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/media?parent=900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/categories?post=900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/tags?post=900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}