{"id":294,"date":"2015-01-26T21:06:27","date_gmt":"2015-01-27T05:06:27","guid":{"rendered":"http:\/\/www.beyond-circuits.com\/wordpress\/?page_id=294"},"modified":"2019-03-07T08:01:40","modified_gmt":"2019-03-07T16:01:40","slug":"tutorial5","status":"publish","type":"page","link":"https:\/\/www.beyond-circuits.com\/wordpress\/tutorial\/tutorial5\/","title":{"rendered":"Tutorial 5: The Other Digit"},"content":{"rendered":"<p>In this tutorial, we&#8217;re going to drive the second digit of the PmodSSD display. To do this, we have\u00a0to switch the cathode signal <em>ssdcat<\/em>\u00a0at a rate around 500Hz. While this is taking place, we&#8217;ll also switch between <em>switch[3:0]<\/em> and <em>switch[7:4]<\/em>.<\/p>\n<h1>Counting to 1ms<\/h1>\n<p>The input clk runs at 100MHz. If we make a counter that counts 100,000 cycles, we can generate a pulse when the counter is at a particular value. We do this using a reg and an always block like this:<\/p>\n<pre><code>integer count = 0;\r\nreg ms_pulse = 0;\r\nalways @(posedge clk)\r\n  if (count == 99999)\r\n    begin\r\n      count &lt;= 0;\r\n      ms_pulse &lt;= 1;\r\n    end\r\n  else\r\n    begin\r\n      count &lt;= count+1;\r\n      ms_pulse &lt;= 0;\r\n    end<\/code><\/pre>\n<p>This will give us a pulse one clock period in length every millisecond. From this, we can make our <em>ssdcat<\/em> signal:<\/p>\n<pre><code>always @(posedge clk)\r\n  if (ms_pulse) ssdcat &lt;= ~ssdcat;<\/code><\/pre>\n<p>Of course, we&#8217;ll also need to initialize <em>ssdcat<\/em> to zero too:<\/p>\n<pre><code>initial ssdcat = 0;<\/code><\/pre>\n<p>When you run this design, you should see both digits light up, although not quite as bright as before. However, the digits will be the same, since\u00a0we&#8217;re driving the <em>ssd<\/em> output with the value from <em>switch[3:0]<\/em> only.<\/p>\n<h1>Making the Digits Different<\/h1>\n<p>We&#8217;re ping-ponging back and forth between the two digits now. We need to do the same thing with the input to our case statement. To do that, we declare a 4-bit input to the case statement called <em>digit<\/em>.<\/p>\n<pre><code>wire [3:0] digit;<\/code><\/pre>\n<p>Then we\u00a0set the digit value to the appropriate switch bits. We&#8217;ll use an assignment statement and a conditional operator.<\/p>\n<pre><code>assign digit = ssdcat ? switch[3:0] : switch[7:4];<\/code><\/pre>\n<p>Note that the declaration of the <em>digit<\/em> signal must come before you can use it\u00a0in the case statement, and before you make an assignment. Try your design out and verify that it&#8217;s possible to\u00a0make two independent digits.<\/p>\n<h1>Fixing the Bug<\/h1>\n<p>Did you notice that we have a bug? The least significant bits on the switches control the left digit. They need to be switched around so that they control the right digit. There are a couple of\u00a0simple solutions for this problem. I&#8217;m going to show you the first one. Can you find the other?<\/p>\n<p>Change the assignment to the digit value by switching around the conditional operator like this:<\/p>\n<pre><code>assign digit = ssdcat ? switch[7:4] : switch[3:0];<\/code><\/pre>\n<p>Now everything looks better. In the next tutorial, we&#8217;ll make a counter for\u00a0counting out seconds.<\/p>\n<p>Here&#8217;s a link to my version of the Verilog code for this tutorial:\u00a0<a title=\"top.v\" href=\"https:\/\/www.beyond-circuits.com\/wordpress\/wp-content\/uploads\/tutorials\/tutorial5\/top.v\">top.v<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we&#8217;re going to drive the second digit of the PmodSSD display. To do this, we have\u00a0to switch the cathode signal ssdcat\u00a0at a rate around 500Hz. While this is taking place, we&#8217;ll also switch between switch[3:0] and switch[7:4]. &hellip; <a href=\"https:\/\/www.beyond-circuits.com\/wordpress\/tutorial\/tutorial5\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":226,"menu_order":5,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-294","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/pages\/294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/types\/page"}],"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=294"}],"version-history":[{"count":13,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/pages\/294\/revisions"}],"predecessor-version":[{"id":1241,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/pages\/294\/revisions\/1241"}],"up":[{"embeddable":true,"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/pages\/226"}],"wp:attachment":[{"href":"https:\/\/www.beyond-circuits.com\/wordpress\/wp-json\/wp\/v2\/media?parent=294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}