[资源共享] (FPGA实验五):Verilog HDL语言数码管时钟(时分秒)

bqgup   2019-4-26 19:49 楼主
Verilog HDL语言写得数码管时钟
  1. module smg(
  2. input ext_clk_50M,
  3. inout reg div_clk_1K,
  4. output reg[7:0]wei_xuan,
  5. output reg[7:0]duan_xuan
  6. );
  7. reg [31:0]cnt;
  8. reg [31:0]cnt1;//time
  9. reg div_clk_1Hz;
  10. reg [7:0]sec;
  11. reg [7:0]min;
  12. reg [7:0]h;
  13. reg [2:0]duan_num;
  14. always [url=home.php?mod=space&uid=775551]@[/url] (posedge ext_clk_50M)//scan clk
  15. if(cnt == 32'd10_000)
  16. begin
  17. cnt <= 32'd0;
  18. div_clk_1K <= ~div_clk_1K;
  19. end
  20. else cnt <= cnt + 32'd1;
  21. always @ (posedge ext_clk_50M)//time clk
  22. if(cnt1 == 32'd25_000_000)
  23. begin
  24. cnt1 <= 32'd0;
  25. div_clk_1Hz <= ~div_clk_1Hz;
  26. end
  27. else cnt1 <= cnt1 + 32'd1;
  28. ////////////////////////////////////////////////////////////////
  29. always @(posedge div_clk_1Hz)
  30. if(sec == 8'd59)
  31. begin
  32. sec <= 8'd0;
  33. min <= min + 8'd1;
  34. if(min == 8'd59)
  35. begin
  36. min <= 8'd0;
  37. h <= h + 8'd1;
  38. if(h == 8'd23)
  39. h <= 8'd0;
  40. end
  41. end
  42. else sec <= sec + 8'd1;
  43. ////////////////////////////////////////////////////////////////
  44. always @ (posedge div_clk_1K)
  45. if(duan_num == 3'b110) duan_num <= 3'b0;
  46. else duan_num <= duan_num + 3'b1;
  47. always @ (sec or min or h or duan_num)//encoder
  48. if(duan_num == 3'b001)
  49. begin
  50. wei_xuan <= 8'b0111_1111;
  51. case(sec % 10)
  52. 8'd0: duan_xuan <= 8'b1100_0000;//0
  53. 8'd1: duan_xuan <= 8'b1111_1001;//1
  54. 8'd2: duan_xuan <= 8'b1010_0100;//2
  55. 8'd3: duan_xuan <= 8'b1011_0000;//3
  56. 8'd4: duan_xuan <= 8'b1001_1001;//4
  57. 8'd5: duan_xuan <= 8'b1001_0010;//5
  58. 8'd6: duan_xuan <= 8'b1000_0011;//6
  59. 8'd7: duan_xuan <= 8'b1111_1000;//7
  60. 8'd8: duan_xuan <= 8'b1000_0000;//8
  61. 8'd9: duan_xuan <= 8'b1001_0000;//9
  62. endcase
  63. end
  64. else if(duan_num == 3'b010)
  65. begin
  66. wei_xuan <= 8'b1011_1111;
  67. case(sec / 10)
  68. 8'd0: duan_xuan <= 8'b1100_0000;//0
  69. 8'd1: duan_xuan <= 8'b1111_1001;//1
  70. 8'd2: duan_xuan <= 8'b1010_0100;//2
  71. 8'd3: duan_xuan <= 8'b1011_0000;//3
  72. 8'd4: duan_xuan <= 8'b1001_1001;//4
  73. 8'd5: duan_xuan <= 8'b1001_0010;//5
  74. 8'd6: duan_xuan <= 8'b1000_0011;//6
  75. 8'd7: duan_xuan <= 8'b1111_1000;//7
  76. 8'd8: duan_xuan <= 8'b1000_0000;//8
  77. 8'd9: duan_xuan <= 8'b1001_0000;//9
  78. endcase
  79. end
  80. else if(duan_num == 3'b011)
  81. begin
  82. wei_xuan <= 8'b1110_1111;
  83. case(min % 10)
  84. 8'd0: duan_xuan <= 8'b1100_0000;//0
  85. 8'd1: duan_xuan <= 8'b1111_1001;//1
  86. 8'd2: duan_xuan <= 8'b1010_0100;//2
  87. 8'd3: duan_xuan <= 8'b1011_0000;//3
  88. 8'd4: duan_xuan <= 8'b1001_1001;//4
  89. 8'd5: duan_xuan <= 8'b1001_0010;//5
  90. 8'd6: duan_xuan <= 8'b1000_0011;//6
  91. 8'd7: duan_xuan <= 8'b1111_1000;//7
  92. 8'd8: duan_xuan <= 8'b1000_0000;//8
  93. 8'd9: duan_xuan <= 8'b1001_0000;//9
  94. endcase
  95. end
  96. else if(duan_num == 3'b100)
  97. begin
  98. wei_xuan <= 8'b1111_0111;
  99. case(min / 10)
  100. 8'd0: duan_xuan <= 8'b1100_0000;//0
  101. 8'd1: duan_xuan <= 8'b1111_1001;//1
  102. 8'd2: duan_xuan <= 8'b1010_0100;//2
  103. 8'd3: duan_xuan <= 8'b1011_0000;//3
  104. 8'd4: duan_xuan <= 8'b1001_1001;//4
  105. 8'd5: duan_xuan <= 8'b1001_0010;//5
  106. 8'd6: duan_xuan <= 8'b1000_0011;//6
  107. 8'd7: duan_xuan <= 8'b1111_1000;//7
  108. 8'd8: duan_xuan <= 8'b1000_0000;//8
  109. 8'd9: duan_xuan <= 8'b1001_0000;//9
  110. endcase
  111. end
  112. else if(duan_num == 3'b101)//h
  113. begin
  114. wei_xuan <= 8'b1111_1101;
  115. case(h % 10)
  116. 8'd0: duan_xuan <= 8'b1100_0000;//0
  117. 8'd1: duan_xuan <= 8'b1111_1001;//1
  118. 8'd2: duan_xuan <= 8'b1010_0100;//2
  119. 8'd3: duan_xuan <= 8'b1011_0000;//3
  120. 8'd4: duan_xuan <= 8'b1001_1001;//4
  121. 8'd5: duan_xuan <= 8'b1001_0010;//5
  122. 8'd6: duan_xuan <= 8'b1000_0011;//6
  123. 8'd7: duan_xuan <= 8'b1111_1000;//7
  124. 8'd8: duan_xuan <= 8'b1000_0000;//8
  125. 8'd9: duan_xuan <= 8'b1001_0000;//9
  126. endcase
  127. end
  128. else if(duan_num == 3'b110)//h
  129. begin
  130. wei_xuan <= 8'b1111_1110;
  131. case(h / 10)
  132. 8'd0: duan_xuan <= 8'b1100_0000;//0
  133. 8'd1: duan_xuan <= 8'b1111_1001;//1
  134. 8'd2: duan_xuan <= 8'b1010_0100;//2
  135. 8'd3: duan_xuan <= 8'b1011_0000;//3
  136. 8'd4: duan_xuan <= 8'b1001_1001;//4
  137. 8'd5: duan_xuan <= 8'b1001_0010;//5
  138. 8'd6: duan_xuan <= 8'b1000_0011;//6
  139. 8'd7: duan_xuan <= 8'b1111_1000;//7
  140. 8'd8: duan_xuan <= 8'b1000_0000;//8
  141. 8'd9: duan_xuan <= 8'b1001_0000;//9
  142. endcase
  143. end
  144. endmodule
代码写完使用JTAG(联合测试行动组)下载观看效果
HDL语言数码管时钟.mp4 (1.15 MB)
(下载次数: 11, 2019-4-26 19:37 上传)
数码管的效果是这样: TIM图片20190426194806.jpg 效果是这样的,看起来有点不太符合我们日常的观看习惯,我们对代码做个修改,让效果看起来更舒适。
  1. module smg(
  2. input ext_clk_50M,
  3. inout reg div_clk_1K,
  4. output reg[7:0]wei_xuan,
  5. output reg[7:0]duan_xuan
  6. );
  7. reg [31:0]cnt;
  8. reg [31:0]cnt1;//time
  9. reg div_clk_1Hz;
  10. reg [7:0]sec;
  11. reg [7:0]min;
  12. reg [7:0]h;
  13. reg [3:0]duan_num;
  14. always @ (posedge ext_clk_50M)//scan clk
  15. if(cnt == 32'd10_000)
  16. begin
  17. cnt <= 32'd0;
  18. div_clk_1K <= ~div_clk_1K;
  19. end
  20. else cnt <= cnt + 32'd1;
  21. always @ (posedge ext_clk_50M)//time clk
  22. if(cnt1 == 32'd25_000_000)
  23. begin
  24. cnt1 <= 32'd0;
  25. div_clk_1Hz <= ~div_clk_1Hz;
  26. end
  27. else cnt1 <= cnt1 + 32'd1;
  28. ////////////////////////////////////////////////////////////////
  29. always @(posedge div_clk_1Hz)
  30. if(sec == 8'd59)
  31. begin
  32. sec <= 8'd0;
  33. min <= min + 8'd1;
  34. if(min == 8'd59)
  35. begin
  36. min <= 8'd0;
  37. h <= h + 8'd1;
  38. if(h == 8'd23)
  39. h <= 8'd0;
  40. end
  41. end
  42. else sec <= sec + 8'd1;
  43. ////////////////////////////////////////////////////////////////
  44. always @ (posedge div_clk_1K)
  45. if(duan_num == 4'b1000) duan_num <= 4'b0;
  46. else duan_num <= duan_num + 4'b1;
  47. always @ (sec or min or h or duan_num)//encoder
  48. if(duan_num == 4'b0001)
  49. begin
  50. wei_xuan <= 8'b0111_1111;
  51. case(sec % 10)
  52. 8'd0: duan_xuan <= 8'b1100_0000;//0
  53. 8'd1: duan_xuan <= 8'b1111_1001;//1
  54. 8'd2: duan_xuan <= 8'b1010_0100;//2
  55. 8'd3: duan_xuan <= 8'b1011_0000;//3
  56. 8'd4: duan_xuan <= 8'b1001_1001;//4
  57. 8'd5: duan_xuan <= 8'b1001_0010;//5
  58. 8'd6: duan_xuan <= 8'b1000_0011;//6
  59. 8'd7: duan_xuan <= 8'b1111_1000;//7
  60. 8'd8: duan_xuan <= 8'b1000_0000;//8
  61. 8'd9: duan_xuan <= 8'b1001_0000;//9
  62. endcase
  63. end
  64. else if(duan_num == 4'b0010)
  65. begin
  66. wei_xuan <= 8'b1011_1111;
  67. case(sec / 10)
  68. 8'd0: duan_xuan <= 8'b1100_0000;//0
  69. 8'd1: duan_xuan <= 8'b1111_1001;//1
  70. 8'd2: duan_xuan <= 8'b1010_0100;//2
  71. 8'd3: duan_xuan <= 8'b1011_0000;//3
  72. 8'd4: duan_xuan <= 8'b1001_1001;//4
  73. 8'd5: duan_xuan <= 8'b1001_0010;//5
  74. 8'd6: duan_xuan <= 8'b1000_0011;//6
  75. 8'd7: duan_xuan <= 8'b1111_1000;//7
  76. 8'd8: duan_xuan <= 8'b1000_0000;//8
  77. 8'd9: duan_xuan <= 8'b1001_0000;//9
  78. endcase
  79. end
  80. else if(duan_num == 4'b0011)
  81. begin
  82. wei_xuan <= 8'b1110_1111;
  83. case(min % 10)
  84. 8'd0: duan_xuan <= 8'b1100_0000;//0
  85. 8'd1: duan_xuan <= 8'b1111_1001;//1
  86. 8'd2: duan_xuan <= 8'b1010_0100;//2
  87. 8'd3: duan_xuan <= 8'b1011_0000;//3
  88. 8'd4: duan_xuan <= 8'b1001_1001;//4
  89. 8'd5: duan_xuan <= 8'b1001_0010;//5
  90. 8'd6: duan_xuan <= 8'b1000_0011;//6
  91. 8'd7: duan_xuan <= 8'b1111_1000;//7
  92. 8'd8: duan_xuan <= 8'b1000_0000;//8
  93. 8'd9: duan_xuan <= 8'b1001_0000;//9
  94. endcase
  95. end
  96. else if(duan_num == 4'b0100)
  97. begin
  98. wei_xuan <= 8'b1111_0111;
  99. case(min / 10)
  100. 8'd0: duan_xuan <= 8'b1100_0000;//0
  101. 8'd1: duan_xuan <= 8'b1111_1001;//1
  102. 8'd2: duan_xuan <= 8'b1010_0100;//2
  103. 8'd3: duan_xuan <= 8'b1011_0000;//3
  104. 8'd4: duan_xuan <= 8'b1001_1001;//4
  105. 8'd5: duan_xuan <= 8'b1001_0010;//5
  106. 8'd6: duan_xuan <= 8'b1000_0011;//6
  107. 8'd7: duan_xuan <= 8'b1111_1000;//7
  108. 8'd8: duan_xuan <= 8'b1000_0000;//8
  109. 8'd9: duan_xuan <= 8'b1001_0000;//9
  110. endcase
  111. end
  112. else if(duan_num == 4'b0101)//h
  113. begin
  114. wei_xuan <= 8'b1111_1101;
  115. case(h % 10)
  116. 8'd0: duan_xuan <= 8'b1100_0000;//0
  117. 8'd1: duan_xuan <= 8'b1111_1001;//1
  118. 8'd2: duan_xuan <= 8'b1010_0100;//2
  119. 8'd3: duan_xuan <= 8'b1011_0000;//3
  120. 8'd4: duan_xuan <= 8'b1001_1001;//4
  121. 8'd5: duan_xuan <= 8'b1001_0010;//5
  122. 8'd6: duan_xuan <= 8'b1000_0011;//6
  123. 8'd7: duan_xuan <= 8'b1111_1000;//7
  124. 8'd8: duan_xuan <= 8'b1000_0000;//8
  125. 8'd9: duan_xuan <= 8'b1001_0000;//9
  126. endcase
  127. end
  128. else if(duan_num == 4'b0110)//h
  129. begin
  130. wei_xuan <= 8'b1111_1110;
  131. case(h / 10)
  132. 8'd0: duan_xuan <= 8'b1100_0000;//0
  133. 8'd1: duan_xuan <= 8'b1111_1001;//1
  134. 8'd2: duan_xuan <= 8'b1010_0100;//2
  135. 8'd3: duan_xuan <= 8'b1011_0000;//3
  136. 8'd4: duan_xuan <= 8'b1001_1001;//4
  137. 8'd5: duan_xuan <= 8'b1001_0010;//5
  138. 8'd6: duan_xuan <= 8'b1000_0011;//6
  139. 8'd7: duan_xuan <= 8'b1111_1000;//7
  140. 8'd8: duan_xuan <= 8'b1000_0000;//8
  141. 8'd9: duan_xuan <= 8'b1001_0000;//9
  142. endcase
  143. end
  144. else if(duan_num == 4'b0111)//-
  145. begin
  146. wei_xuan <= 8'b1111_1011;
  147. duan_xuan <= 8'b1011_1111;//-
  148. end
  149. else if(duan_num == 4'b1000)//-
  150. begin
  151. wei_xuan <= 8'b1101_1111;
  152. duan_xuan <= 8'b1011_1111;//-
  153. end
  154. endmodule
修改完的效果如下所示,个人感觉看出来比较舒服:
8位数码管.mp4 (1.16 MB)
(下载次数: 9, 2019-4-26 19:46 上传)
最后分享一下工程,方便大家使用下载:
4.4、数码管动态显示(时分秒).rar (1.15 MB)
(下载次数: 35, 2019-4-26 19:48 上传)
4.5、数码管动态显示(时分秒8段).rar (1.17 MB)
(下载次数: 68, 2019-4-26 19:48 上传)
欢迎大家讨论交流。 本帖最后由 bqgup 于 2019-4-26 19:51 编辑
  • TIM图片20190426194806.jpg

回复评论

暂无评论,赶紧抢沙发吧
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复