当前位置:   article > 正文

ADC + 数码管显示

ADC + 数码管显示

REVIEW

1.  今日摸鱼计划

按键启动ADC

将结果显示在数码管上

2.  key + ADC

key.v

  1. module key_one(
  2. input clk ,
  3. input reset_n,
  4. input key,
  5. output reg key_flag,
  6. output reg key_state
  7. );
  8. // nedge_key pedge_key
  9. reg dff_k_0 , dff_k_1 ;
  10. reg r_key;
  11. wire nedge_key, pedge_key;
  12. always@(posedge clk )
  13. dff_k_0 <= key ;
  14. always@(posedge clk )
  15. dff_k_1 <= dff_k_0 ;
  16. always@(posedge clk )
  17. r_key <= dff_k_1 ;
  18. assign nedge_key = (r_key == 1)&&(dff_k_1 == 0);
  19. assign pedge_key = (r_key == 0)&&(dff_k_1 == 1);
  20. // key_now 0:IDLE 1:FILTER0 2:DOWN 3:FILTER1
  21. // cnt 20ms/20ns = 1000000 ;
  22. reg [1:0]key_now;
  23. reg [19:0] cnt;
  24. parameter cnt_N = 1000; //测试使用
  25. always@(posedge clk or negedge reset_n )
  26. if(!reset_n)
  27. begin
  28. key_now <= 0 ;
  29. cnt <= 0;
  30. key_flag <= 0;
  31. key_state <= 1;
  32. end
  33. else
  34. begin
  35. key_flag <= 0;
  36. case(key_now)
  37. 0:
  38. if(!nedge_key) key_now <= 0;
  39. else
  40. begin
  41. cnt <= 0 ;
  42. key_now <= 1;
  43. end
  44. 1:
  45. if(pedge_key) key_now <= 0;
  46. else if(cnt >= cnt_N - 1)
  47. begin
  48. cnt <= 0 ;
  49. key_now <= 2;
  50. key_flag <= 1;
  51. key_state <= 0;
  52. end
  53. else cnt <= cnt + 1'b1;
  54. 2:
  55. if(!pedge_key) key_now <= 2;
  56. else
  57. begin
  58. cnt <= 0 ;
  59. key_now <= 3;
  60. end
  61. 3:
  62. if(nedge_key) key_now <= 2;
  63. else if(cnt >= cnt_N - 1)
  64. begin
  65. cnt <= 0 ;
  66. key_now <= 0;
  67. key_flag <= 1;
  68. key_state <= 1;
  69. end
  70. else cnt <= cnt + 1'b1;
  71. endcase
  72. end
  73. endmodule

adc128s102.v

  1. module adc128s102(
  2. input clk,
  3. input reset_n,
  4. input Conv_Go,//使能信号
  5. input [2:0]Addr,
  6. input ADC_DOUT,
  7. output reg ADC_SCLK,
  8. output reg ADC_CS_N,
  9. output reg ADC_DIN,
  10. output reg Conv_Done,
  11. output reg[11:0]Data
  12. );
  13. parameter CLOCK_FREQ = 50_000_000;
  14. parameter SCLK_FREQ = 12_500_000;
  15. parameter MCNT_DIV_CNT = CLOCK_FREQ/(SCLK_FREQ * 2) - 1;
  16. reg[7:0]DIV_CNT;
  17. reg [5:0]LSM_CNT;
  18. reg [11:0]Data_r;
  19. reg [2:0]r_Addr;
  20. always@(posedge clk)
  21. if(Conv_Go)
  22. r_Addr <= Addr;
  23. else
  24. r_Addr <= r_Addr;
  25. reg Conv_En; //转换使能
  26. always@(posedge clk or negedge reset_n )
  27. if(!reset_n )
  28. Conv_En <= 1'd0;
  29. else if(Conv_Go)
  30. Conv_En <= 1'd1;
  31. else if((LSM_CNT == 6'd34) && (DIV_CNT == MCNT_DIV_CNT))
  32. Conv_En <= 1'd0;
  33. else
  34. Conv_En <= Conv_En;
  35. always@(posedge clk or negedge reset_n)
  36. if(!reset_n)
  37. DIV_CNT <= 0;
  38. else if(Conv_En)
  39. begin
  40. if(DIV_CNT == MCNT_DIV_CNT)
  41. DIV_CNT <= 0;
  42. else
  43. DIV_CNT <= DIV_CNT + 1'd1;
  44. end
  45. else
  46. DIV_CNT <= 0;
  47. always@(posedge clk or negedge reset_n)
  48. if(!reset_n)
  49. LSM_CNT <= 6'd0;
  50. else if(DIV_CNT == MCNT_DIV_CNT)
  51. begin
  52. if(LSM_CNT == 6'd34)
  53. LSM_CNT <= 6'd0;
  54. else
  55. LSM_CNT <= LSM_CNT + 1'd1;
  56. end
  57. else
  58. LSM_CNT <= LSM_CNT;
  59. always@(posedge clk or negedge reset_n )
  60. if(!reset_n )begin
  61. Data_r <= 12'd0;
  62. ADC_SCLK <= 1'd1;
  63. ADC_DIN <= 1'd1;
  64. ADC_CS_N <= 1'd1;
  65. end
  66. else if(DIV_CNT == MCNT_DIV_CNT)begin
  67. case(LSM_CNT)
  68. 0 : begin ADC_CS_N <= 1'd1; ADC_SCLK <= 1'd1;end
  69. 1 : begin ADC_CS_N <= 1'd0;end
  70. 2 : begin ADC_SCLK <= 1'd0;end
  71. 3 : begin ADC_SCLK <= 1'd1;end
  72. 4 : begin ADC_SCLK <= 1'd0;end
  73. 5 : begin ADC_SCLK <= 1'd1;end
  74. 6 : begin ADC_SCLK <= 1'd0;ADC_DIN <= r_Addr[2]; end
  75. 7 : begin ADC_SCLK <= 1'd1;end
  76. 8 : begin ADC_SCLK <= 1'd0;ADC_DIN <= r_Addr[1]; end
  77. 9 : begin ADC_SCLK <= 1'd1;end
  78. 10 :begin ADC_SCLK <= 1'd0;ADC_DIN <= r_Addr[0]; end
  79. 11: begin ADC_SCLK <= 1'd1;Data_r[11] <= ADC_DOUT; end
  80. 12: begin ADC_SCLK <= 1'd0;ADC_DIN <= 1'd1;end
  81. //这里跟之前有一点不一样,将ADC_DIN <= 1'd1;
  82. 13: begin ADC_SCLK <= 1'd1;Data_r[10] <= ADC_DOUT; end
  83. 14: begin ADC_SCLK <= 1'd0;end
  84. 15: begin ADC_SCLK <= 1'd1;Data_r[9] <= ADC_DOUT; end
  85. 16: begin ADC_SCLK <= 1'd0;end
  86. 17: begin ADC_SCLK <= 1'd1;Data_r[8] <= ADC_DOUT; end
  87. 18: begin ADC_SCLK <= 1'd0;end
  88. 19: begin ADC_SCLK <= 1'd1;Data_r[7] <= ADC_DOUT; end
  89. 20: begin ADC_SCLK <= 1'd0;end
  90. 21: begin ADC_SCLK <= 1'd1;Data_r[6] <= ADC_DOUT; end
  91. 22: begin ADC_SCLK <= 1'd0;end
  92. 23: begin ADC_SCLK <= 1'd1;Data_r[5] <= ADC_DOUT; end
  93. 24: begin ADC_SCLK <= 1'd0;end
  94. 25: begin ADC_SCLK <= 1'd1;Data_r[4] <= ADC_DOUT; end
  95. 26: begin ADC_SCLK <= 1'd0;end
  96. 27: begin ADC_SCLK <= 1'd1;Data_r[3] <= ADC_DOUT; end
  97. 28: begin ADC_SCLK <= 1'd0;end
  98. 29: begin ADC_SCLK <= 1'd1;Data_r[2] <= ADC_DOUT; end
  99. 30: begin ADC_SCLK <= 1'd0;end
  100. 31: begin ADC_SCLK <= 1'd1;Data_r[1] <= ADC_DOUT; end
  101. 32: begin ADC_SCLK <= 1'd0;end
  102. 33: begin ADC_SCLK <= 1'd1;Data_r[0] <= ADC_DOUT; end
  103. 34: begin ADC_SCLK <= 1'd1;ADC_CS_N <= 1'd1; end
  104. default: ADC_CS_N <= 1'd1;
  105. endcase
  106. end
  107. always@(posedge clk or negedge reset_n )
  108. if(!reset_n )
  109. begin
  110. Data <= 12'd0;
  111. Conv_Done <= 0;
  112. end
  113. else if((LSM_CNT == 34) && (DIV_CNT == MCNT_DIV_CNT))
  114. begin
  115. Conv_Done <= 1'd1;
  116. Data <= Data_r;
  117. end
  118. else
  119. begin
  120. Conv_Done <= 1'd0;
  121. Data <= Data;
  122. end
  123. endmodule

key_adc.v

  1. module key_adc(
  2. input clk ,
  3. input reset_n,
  4. input key,
  5. input [2:0]Addr,
  6. input ADC_DOUT,
  7. output ADC_SCLK,
  8. output ADC_CS_N,
  9. output ADC_DIN,
  10. output Conv_Done,
  11. output [11:0]Data
  12. );
  13. wire key_flag , key_state ;
  14. key_one key_one_(
  15. . clk(clk) ,
  16. . reset_n(reset_n),
  17. . key(key),
  18. . key_flag(key_flag),
  19. . key_state(key_state)
  20. );
  21. reg Conv_Go ;
  22. always@(posedge clk or negedge reset_n )
  23. if(!reset_n )
  24. Conv_Go <= 1'b0 ;
  25. else if((key_flag)&&(key_state==1))
  26. //(key_flag)&&(key_state==1)代表按键按下后松开
  27. //之前都是按下作为判定,本次有一点不同
  28. Conv_Go <= 1'b1 ;
  29. else
  30. Conv_Go <= 1'b0 ;
  31. adc128s102 adc_(
  32. . clk(clk),
  33. . reset_n(reset_n),
  34. . Conv_Go(Conv_Go),//使能信号
  35. . Addr(Addr),
  36. . ADC_DOUT(ADC_DOUT),
  37. . ADC_SCLK(ADC_SCLK),
  38. . ADC_CS_N(ADC_CS_N),
  39. . ADC_DIN(ADC_DIN),
  40. . Conv_Done(Conv_Done),
  41. . Data(Data)
  42. );
  43. endmodule

key_adc_tb.v

  1. `timescale 1ns / 1ns
  2. module key_adc_tb( );
  3. reg clk;
  4. reg reset_n;
  5. reg key ;
  6. reg [2:0]Addr;
  7. reg ADC_DOUT;
  8. wire ADC_SCLK;
  9. wire ADC_CS_N;
  10. wire ADC_DIN;
  11. wire Conv_Done;
  12. wire[11:0]Data;
  13. key_adc key_adc_(
  14. . clk(clk) ,
  15. . reset_n(reset_n),
  16. . key(key),
  17. . Addr(Addr),
  18. . ADC_DOUT(ADC_DOUT),
  19. . ADC_SCLK(ADC_SCLK),
  20. . ADC_CS_N(ADC_CS_N),
  21. . ADC_DIN(ADC_DIN),
  22. . Conv_Done(Conv_Done),
  23. . Data(Data)
  24. );
  25. initial clk = 1;
  26. always #10 clk = ~clk;
  27. initial
  28. begin
  29. reset_n = 0;
  30. Addr = 2;
  31. key = 1 ;
  32. #201;
  33. reset_n = 1;
  34. #200;
  35. key_press(2);
  36. wait(!ADC_CS_N);
  37. //16'h0A58
  38. @(negedge ADC_SCLK);
  39. ADC_DOUT = 0; //DB15
  40. @(negedge ADC_SCLK);
  41. ADC_DOUT = 0; //DB14
  42. @(negedge ADC_SCLK);
  43. ADC_DOUT = 0; //DB13
  44. @(negedge ADC_SCLK);
  45. ADC_DOUT = 0; //DB12
  46. @(negedge ADC_SCLK);
  47. ADC_DOUT = 1; //DB11
  48. @(negedge ADC_SCLK);
  49. ADC_DOUT = 0; //DB10
  50. @(negedge ADC_SCLK);
  51. ADC_DOUT = 1; //DB9
  52. @(negedge ADC_SCLK);
  53. ADC_DOUT = 0; //DB8
  54. @(negedge ADC_SCLK);
  55. ADC_DOUT = 0; //DB7
  56. @(negedge ADC_SCLK);
  57. ADC_DOUT = 1; //DB6
  58. @(negedge ADC_SCLK);
  59. ADC_DOUT = 0; //DB5
  60. @(negedge ADC_SCLK);
  61. ADC_DOUT = 1; //DB4
  62. @(negedge ADC_SCLK);
  63. ADC_DOUT = 1; //DB3
  64. @(negedge ADC_SCLK);
  65. ADC_DOUT = 0; //DB2
  66. @(negedge ADC_SCLK);
  67. ADC_DOUT = 0; //DB1
  68. @(negedge ADC_SCLK);
  69. ADC_DOUT = 0; //DB0
  70. wait(ADC_CS_N);
  71. #2000;
  72. Addr = 7;
  73. #20;
  74. key_press(2);
  75. wait(!ADC_CS_N);
  76. //16'h0893
  77. @(negedge ADC_SCLK);
  78. ADC_DOUT = 0;
  79. @(negedge ADC_SCLK);
  80. ADC_DOUT = 0;
  81. @(negedge ADC_SCLK);
  82. ADC_DOUT = 0;
  83. @(negedge ADC_SCLK);
  84. ADC_DOUT = 0;
  85. @(negedge ADC_SCLK);
  86. ADC_DOUT = 1;
  87. @(negedge ADC_SCLK);
  88. ADC_DOUT = 0;
  89. @(negedge ADC_SCLK);
  90. ADC_DOUT = 0;
  91. @(negedge ADC_SCLK);
  92. ADC_DOUT = 0;
  93. @(negedge ADC_SCLK);
  94. ADC_DOUT = 1;
  95. @(negedge ADC_SCLK);
  96. ADC_DOUT = 0;
  97. @(negedge ADC_SCLK);
  98. ADC_DOUT = 0;
  99. @(negedge ADC_SCLK);
  100. ADC_DOUT = 1;
  101. @(negedge ADC_SCLK);
  102. ADC_DOUT = 0;
  103. @(negedge ADC_SCLK);
  104. ADC_DOUT = 0;
  105. @(negedge ADC_SCLK);
  106. ADC_DOUT = 1;
  107. @(negedge ADC_SCLK);
  108. ADC_DOUT = 1;
  109. wait(ADC_CS_N);
  110. #200;
  111. #100000;
  112. $stop;
  113. end
  114. reg [13:0] rand;
  115. task key_press;
  116. input[3:0]seed;
  117. begin
  118. key = 1 ;
  119. #100000;
  120. repeat(10)
  121. begin
  122. rand = {$random(seed)} % 10000;
  123. #rand;
  124. key=~key;
  125. end
  126. key = 0 ;
  127. #100000;
  128. key = 1 ;
  129. end
  130. endtask
  131. endmodule

分析

adc中做了这样一个小改动,没什么别的作用,这个就是摸鱼怪觉得看起来好看一点。

关于按键的判断,这里是因为在调试按键按下后,发现wait等不到CS下降沿信号,从而DOUT未触发。

测试结果如下:

经过分析,测试文件一直走不到 wait(!ADC_CS_N);

是因为!ADC_CS_N 在 key_press(2);结束之前已经发生了

因此为了测试分析,本次选择使用按键松开为判定

注意:本次测试为了可以 wait(!ADC_CS_N);

按键松开后,不要停留过长时间!

此时看以看出,进行了两次正常的读取。

3.  key + ADC + 数码管

hex_8.v

  1. module hex_8(
  2. input clk,
  3. input reset_n,
  4. input [31:0]disp_data,
  5. output reg [7:0]sel,
  6. output reg [7:0]seg
  7. );
  8. //[31:0]disp_data 16hex 4*8
  9. //[7:0]sel 位选信号
  10. //[7:0]seg 段选信号
  11. // 1kHz分频时钟
  12. reg [14:0]div_clk;
  13. always@(posedge clk or negedge reset_n)
  14. if(!reset_n)
  15. div_clk <= 1'b0;
  16. else if(div_clk == 24999)
  17. div_clk <= 1'b0;
  18. else
  19. div_clk <= div_clk + 1'b1;
  20. reg disp_en;
  21. always@(posedge clk or negedge reset_n)
  22. if(!reset_n)
  23. disp_en <= 1'b0;
  24. else if(div_clk == 24999)
  25. disp_en <= 1'b1;
  26. else
  27. disp_en <= 1'b0;
  28. // 位选sel
  29. reg[2:0]sel_num;
  30. always@(posedge clk or negedge reset_n)
  31. if(!reset_n)
  32. sel_num <= 3'b000;
  33. else if(disp_en)
  34. sel_num <= sel_num + 1'b1;
  35. always@(posedge clk or negedge reset_n)
  36. if(!reset_n)
  37. sel <= 8'b0000_0000;
  38. else case(sel_num)
  39. 0:sel <= 8'b0000_0001;
  40. 1:sel <= 8'b0000_0010;
  41. 2:sel <= 8'b0000_0100;
  42. 3:sel <= 8'b0000_1000;
  43. 4:sel <= 8'b0001_0000;
  44. 5:sel <= 8'b0010_0000;
  45. 6:sel <= 8'b0100_0000;
  46. 7:sel <= 8'b1000_0000;
  47. endcase
  48. // 段选seg [31:0]disp_data 16hex 4*8
  49. reg [3:0] dis_tmp;
  50. always@(posedge clk )
  51. case(sel_num) //高位放前面
  52. 7:dis_tmp <= disp_data[31:28];
  53. 6:dis_tmp <= disp_data[27:24];
  54. 5:dis_tmp <= disp_data[23:20];
  55. 4:dis_tmp <= disp_data[19:16];
  56. 3:dis_tmp <= disp_data[15:12];
  57. 2:dis_tmp <= disp_data[11:8];
  58. 1:dis_tmp <= disp_data[7:4];
  59. 0:dis_tmp <= disp_data[3:0];
  60. endcase
  61. always@(posedge clk )
  62. case(dis_tmp)
  63. 0:seg <= 8'hc0;
  64. 1:seg <= 8'hf9;
  65. 2:seg <= 8'ha4;
  66. 3:seg <= 8'hb0;
  67. 4:seg <= 8'h99;
  68. 5:seg <= 8'h92;
  69. 6:seg <= 8'h82;
  70. 7:seg <= 8'hf8;
  71. 8:seg <= 8'h80;
  72. 9:seg <= 8'h90;
  73. 4'ha:seg <= 8'h88;
  74. 4'hb:seg <= 8'h83;
  75. 4'hc:seg <= 8'hc6;
  76. 4'hd:seg <= 8'ha1;
  77. 4'he:seg <= 8'h86;
  78. 4'hf:seg <= 8'h8e;
  79. endcase
  80. endmodule

hc595_driver.v

  1. module hc595_driver(
  2. input clk,
  3. input reset_n,
  4. input [15:0]data ,
  5. input s_en ,
  6. output reg sh_cp ,
  7. output reg st_cp ,
  8. output reg ds
  9. );
  10. //接收到s_en才改变r_data
  11. reg [15:0]r_data;
  12. always@(posedge clk)
  13. if(s_en)
  14. r_data <= data;
  15. //分频计数器;
  16. parameter CNT_MAX = 2;
  17. reg [7:0]divider_cnt;
  18. always@(posedge clk or negedge reset_n)
  19. if(!reset_n)
  20. divider_cnt <= 0;
  21. else if(divider_cnt == CNT_MAX - 1'b1)
  22. divider_cnt <= 0;
  23. else
  24. divider_cnt <= divider_cnt + 1'b1;
  25. wire sck_plus;
  26. assign sck_plus = (divider_cnt == CNT_MAX - 1'b1);
  27. reg [5:0]SHCP_EDGE_CNT;
  28. always@(posedge clk or negedge reset_n)
  29. if(!reset_n)
  30. SHCP_EDGE_CNT <= 0;
  31. else if(sck_plus)
  32. begin
  33. if(SHCP_EDGE_CNT == 6'd32)
  34. SHCP_EDGE_CNT <= 0;
  35. else
  36. SHCP_EDGE_CNT <= SHCP_EDGE_CNT + 1'b1;
  37. end
  38. else
  39. SHCP_EDGE_CNT <= SHCP_EDGE_CNT;
  40. always@(posedge clk or negedge reset_n)
  41. if(!reset_n)
  42. begin
  43. st_cp <= 1'b0;
  44. ds <= 1'b0;
  45. sh_cp <= 1'd0;
  46. end
  47. else
  48. begin
  49. case(SHCP_EDGE_CNT)
  50. 0: begin sh_cp <= 0; st_cp <= 1'd0;ds <= r_data[15];end
  51. 1: begin sh_cp <= 1; st_cp <= 1'd0;end
  52. 2: begin sh_cp <= 0; ds <= r_data[14];end
  53. 3: begin sh_cp <= 1; end
  54. 4: begin sh_cp <= 0; ds <= r_data[13];end
  55. 5: begin sh_cp <= 1; end
  56. 6: begin sh_cp <= 0; ds <= r_data[12];end
  57. 7: begin sh_cp <= 1; end
  58. 8: begin sh_cp <= 0; ds <= r_data[11];end
  59. 9: begin sh_cp <= 1; end
  60. 10: begin sh_cp <= 0; ds <= r_data[10];end
  61. 11: begin sh_cp <= 1; end
  62. 12: begin sh_cp <= 0; ds <= r_data[9];end
  63. 13: begin sh_cp <= 1; end
  64. 14: begin sh_cp <= 0; ds <= r_data[8];end
  65. 15: begin sh_cp <= 1; end
  66. 16: begin sh_cp <= 0; ds <= r_data[7];end
  67. 17: begin sh_cp <= 1; end
  68. 18: begin sh_cp <= 0; ds <= r_data[6];end
  69. 19: begin sh_cp <= 1; end
  70. 20: begin sh_cp <= 0; ds <= r_data[5];end
  71. 21: begin sh_cp <= 1; end
  72. 22: begin sh_cp <= 0; ds <= r_data[4];end
  73. 23: begin sh_cp <= 1; end
  74. 24: begin sh_cp <= 0; ds <= r_data[3];end
  75. 25: begin sh_cp <= 1; end
  76. 26: begin sh_cp <= 0; ds <= r_data[2];end
  77. 27: begin sh_cp <= 1; end
  78. 28: begin sh_cp <= 0; ds <= r_data[1];end
  79. 29: begin sh_cp <= 1; end
  80. 30: begin sh_cp <= 0; ds <= r_data[0];end
  81. 31: begin sh_cp <= 1; end
  82. 32: st_cp <= 1'd1;
  83. default:
  84. begin
  85. st_cp <= 1'b0;
  86. ds <= 1'b0;
  87. sh_cp <= 1'd0;
  88. end
  89. endcase
  90. end
  91. endmodule

key_adc_hex8.v

  1. module key_adc_hex8(
  2. input clk ,
  3. input reset_n,
  4. input key,
  5. input [2:0]Addr,
  6. input ADC_DOUT,
  7. output ADC_SCLK,
  8. output ADC_CS_N,
  9. output ADC_DIN,
  10. output Conv_Done,
  11. output sh_cp,
  12. output st_cp,
  13. output ds
  14. );
  15. wire key_flag , key_state ;
  16. key_one key_one_(
  17. . clk(clk) ,
  18. . reset_n(reset_n),
  19. . key(key),
  20. . key_flag(key_flag),
  21. . key_state(key_state)
  22. );
  23. reg Conv_Go ;
  24. always@(posedge clk or negedge reset_n )
  25. if(!reset_n )
  26. Conv_Go <= 1'b0 ;
  27. else if((key_flag)&&(key_state==1))
  28. Conv_Go <= 1'b1 ;
  29. else
  30. Conv_Go <= 1'b0 ;
  31. wire [11:0] Data;
  32. adc128s102 adc_(
  33. . clk(clk),
  34. . reset_n(reset_n),
  35. . Conv_Go(Conv_Go),//使能信号
  36. . Addr(Addr),
  37. . ADC_DOUT(ADC_DOUT),
  38. . ADC_SCLK(ADC_SCLK),
  39. . ADC_CS_N(ADC_CS_N),
  40. . ADC_DIN(ADC_DIN),
  41. . Conv_Done(Conv_Done),
  42. . Data(Data)
  43. );
  44. reg [31:0]disp_data;
  45. wire [7:0] sel;//数码管位选(选择当前要显示的数码管)
  46. wire [7:0] seg;//数码管段选(当前要显示的内容)
  47. always@(posedge clk or negedge reset_n )
  48. if(!reset_n )
  49. disp_data <= 0 ;
  50. else
  51. disp_data <= 32'h0000_0000 + Data ;
  52. hc595_driver hc595_driver(
  53. .clk(clk),
  54. .reset_n(reset_n),
  55. .data({seg,sel}), //将段选与位选信号拼接在一起
  56. .s_en(1'b1),
  57. .sh_cp(sh_cp),
  58. .st_cp(st_cp),
  59. .ds(ds)
  60. );
  61. hex_8 hex8(
  62. .clk(clk),
  63. .reset_n(reset_n),
  64. .disp_data(disp_data),
  65. .sel(sel),
  66. .seg(seg)
  67. );
  68. endmodule

key_adc_hex8_tb.v

  1. `timescale 1ns / 1ns
  2. module key_adc_hex8_tb( );
  3. reg clk;
  4. reg reset_n;
  5. reg key ;
  6. reg [2:0]Addr;
  7. reg ADC_DOUT;
  8. wire ADC_SCLK;
  9. wire ADC_CS_N;
  10. wire ADC_DIN;
  11. wire Conv_Done;
  12. wire sh_cp,st_cp, ds;
  13. key_adc_hex8 k_hex8(
  14. clk ,
  15. reset_n,
  16. key,
  17. Addr,
  18. ADC_DOUT,
  19. ADC_SCLK,
  20. ADC_CS_N,
  21. ADC_DIN,
  22. Conv_Done,
  23. sh_cp,
  24. st_cp,
  25. ds
  26. );
  27. initial clk = 1;
  28. always #10 clk = ~clk;
  29. initial
  30. begin
  31. reset_n = 0;
  32. Addr = 2;
  33. key = 1 ;
  34. #201;
  35. reset_n = 1;
  36. #200;
  37. key_press(2);
  38. wait(!ADC_CS_N);
  39. //16'h0A58
  40. @(negedge ADC_SCLK);
  41. ADC_DOUT = 0; //DB15
  42. @(negedge ADC_SCLK);
  43. ADC_DOUT = 0; //DB14
  44. @(negedge ADC_SCLK);
  45. ADC_DOUT = 0; //DB13
  46. @(negedge ADC_SCLK);
  47. ADC_DOUT = 0; //DB12
  48. @(negedge ADC_SCLK);
  49. ADC_DOUT = 1; //DB11
  50. @(negedge ADC_SCLK);
  51. ADC_DOUT = 0; //DB10
  52. @(negedge ADC_SCLK);
  53. ADC_DOUT = 1; //DB9
  54. @(negedge ADC_SCLK);
  55. ADC_DOUT = 0; //DB8
  56. @(negedge ADC_SCLK);
  57. ADC_DOUT = 0; //DB7
  58. @(negedge ADC_SCLK);
  59. ADC_DOUT = 1; //DB6
  60. @(negedge ADC_SCLK);
  61. ADC_DOUT = 0; //DB5
  62. @(negedge ADC_SCLK);
  63. ADC_DOUT = 1; //DB4
  64. @(negedge ADC_SCLK);
  65. ADC_DOUT = 1; //DB3
  66. @(negedge ADC_SCLK);
  67. ADC_DOUT = 0; //DB2
  68. @(negedge ADC_SCLK);
  69. ADC_DOUT = 0; //DB1
  70. @(negedge ADC_SCLK);
  71. ADC_DOUT = 0; //DB0
  72. wait(ADC_CS_N);
  73. #2000;
  74. #1000000;
  75. Addr = 7;
  76. #20;
  77. key_press(2);
  78. wait(!ADC_CS_N);
  79. //16'h0893
  80. @(negedge ADC_SCLK);
  81. ADC_DOUT = 0;
  82. @(negedge ADC_SCLK);
  83. ADC_DOUT = 0;
  84. @(negedge ADC_SCLK);
  85. ADC_DOUT = 0;
  86. @(negedge ADC_SCLK);
  87. ADC_DOUT = 0;
  88. @(negedge ADC_SCLK);
  89. ADC_DOUT = 1;
  90. @(negedge ADC_SCLK);
  91. ADC_DOUT = 0;
  92. @(negedge ADC_SCLK);
  93. ADC_DOUT = 0;
  94. @(negedge ADC_SCLK);
  95. ADC_DOUT = 0;
  96. @(negedge ADC_SCLK);
  97. ADC_DOUT = 1;
  98. @(negedge ADC_SCLK);
  99. ADC_DOUT = 0;
  100. @(negedge ADC_SCLK);
  101. ADC_DOUT = 0;
  102. @(negedge ADC_SCLK);
  103. ADC_DOUT = 1;
  104. @(negedge ADC_SCLK);
  105. ADC_DOUT = 0;
  106. @(negedge ADC_SCLK);
  107. ADC_DOUT = 0;
  108. @(negedge ADC_SCLK);
  109. ADC_DOUT = 1;
  110. @(negedge ADC_SCLK);
  111. ADC_DOUT = 1;
  112. wait(ADC_CS_N);
  113. #200;
  114. #1000000;
  115. $stop;
  116. end
  117. reg [13:0] rand;
  118. task key_press;
  119. input[3:0]seed;
  120. begin
  121. key = 1 ;
  122. #100000;
  123. repeat(10)
  124. begin
  125. rand = {$random(seed)} % 10000;
  126. #rand;
  127. key=~key;
  128. end
  129. key = 0 ;
  130. #100000;
  131. key = 1 ;
  132. end
  133. endtask
  134. endmodule

.xdc

  1. set_property IOSTANDARD LVCMOS33 [get_ports clk]
  2. set_property IOSTANDARD LVCMOS33 [get_ports reset_n]
  3. set_property IOSTANDARD LVCMOS33 [get_ports st_cp]
  4. set_property IOSTANDARD LVCMOS33 [get_ports sh_cp]
  5. set_property IOSTANDARD LVCMOS33 [get_ports ds]
  6. set_property PACKAGE_PIN U18 [get_ports clk]
  7. set_property PACKAGE_PIN D20 [get_ports ds]
  8. set_property PACKAGE_PIN E19 [get_ports sh_cp]
  9. set_property PACKAGE_PIN F17 [get_ports st_cp]
  10. set_property IOSTANDARD LVCMOS33 [get_ports {Addr[2]}]
  11. set_property IOSTANDARD LVCMOS33 [get_ports {Addr[1]}]
  12. set_property IOSTANDARD LVCMOS33 [get_ports {Addr[0]}]
  13. set_property IOSTANDARD LVCMOS33 [get_ports ADC_CS_N]
  14. set_property IOSTANDARD LVCMOS33 [get_ports ADC_DIN]
  15. set_property IOSTANDARD LVCMOS33 [get_ports ADC_DOUT]
  16. set_property IOSTANDARD LVCMOS33 [get_ports ADC_SCLK]
  17. set_property IOSTANDARD LVCMOS33 [get_ports Conv_Done]
  18. set_property IOSTANDARD LVCMOS33 [get_ports key]
  19. set_property PACKAGE_PIN H18 [get_ports key]
  20. set_property PACKAGE_PIN H20 [get_ports reset_n]
  21. set_property PACKAGE_PIN K14 [get_ports {Addr[0]}]
  22. set_property PACKAGE_PIN L15 [get_ports {Addr[1]}]
  23. set_property PACKAGE_PIN G14 [get_ports {Addr[2]}]
  24. set_property PACKAGE_PIN M19 [get_ports ADC_DIN]
  25. set_property PACKAGE_PIN M20 [get_ports ADC_DOUT]
  26. set_property PACKAGE_PIN L19 [get_ports ADC_SCLK]
  27. set_property PACKAGE_PIN M18 [get_ports ADC_CS_N]
  28. set_property PACKAGE_PIN G17 [get_ports Conv_Done]

分析

测试代码符合预期

板级验证:

拨码开关选取地址

测量短路时,电压为0

测量小电池,电压比较准(手边没万用表,划划水啦~)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/767209
推荐阅读