提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
提示:这里可以添加本文要记录的大概内容:
第一次写博客。。。如果有什么问题大家可以在评论区告诉我,谢谢大家。
提示:以下是本篇文章正文内容,下面案例可供参考
一、项目设计目标
(1)项目综合描述
本项目要求设计并实现一个数字电压表的装置,该装置能够对0~200V范围的直流电压进行测量。测量分为4挡:200mV、2V、20V和200V。输入为模拟直流电压,输出为数字量,并在必要的辅助输出显示设备上显示。同时具有正、负电压极性显示,小数点显示。能判读并显示被测量信号超出所选择的量程范围。并根据不同的量程能自动调整小数点。
(2)任务要求
① 数字电压表有4个测量挡:200mV、2V、20V和200V,能将被测的模拟直流电压在显示设备上显示出来。
② 数字电压表以基本量程为基础,同时设计衰减器进行量程的扩展。
③ 具有位(三位半)显示:有3位完整的显示,另外最高位只显示0或1。
④ 能够判读并显示被测电压的极性。 1
⑤ 测量速度为2~5 次/秒,分辨率为0.1mV,测量误差<0.1%。
(3)发挥部分
①设计并调试自动量程转换电路。
②设计并调试小数点自动切换电路。
二、系统总体设计
系统总体框架如下图所示:
主要有五个模块:衰减电路、放大电路、反向电路、A/D转换模块、FPGA处理及显示。
(1)衰减电路:将不同电压分别送入不同的档位,通过开关控制档位的选择,最后四档电压都衰减到-0.2v-0.2v,再送入放大电路;
(2)放大电路:对衰减电路送入的电压进行放大,利用NE5532放大器进行放大10倍;
(3)反向电路:对于输入的负电压,电压经衰减和放大后,还需要经过反向电路进行反向,这里利用NE5532放大器放大-1倍进行反向;
(4)A/D转换模块:对处理得到的0-2V电压进行12位的模数转换,转换输出为0-4095的二进制电平;
(5)FPGA处理及显示模块:利用FPGA里面烧入的程序对A/D转换后的数据进行处理,还原到其输入值,并通过手动控制FPGA上的开关进行量程选择,控制小数点的显示,以及控制正负号的显示,最后通过七段数码管显示输入的电压值。
#三、程序代码
1.顶层模块(Top.v)
`timescale 1ns / 1ps
module Top(clk,sw,led,flag, ADC_sdata, ADC_sclk,ADC_csn,slec_wei,slec_duan);
input clk;
input [3:0]sw;
output reg [7:0] led;
input flag;
input ADC_sdata;
output ADC_sclk,ADC_csn;
output [7:0] slec_wei;
output [7:0] slec_duan;
wire [11:0] adc_res;
wire adc_valid;
wire [19:0]cout;
always@(posedge clk)if(adc_valid) led<=adc_res[11:4];
PmodAD1 U0(
.clk(clk),
.rst(1’b0),
.ADC_sdata(ADC_sdata),
.ADC_sclk(ADC_sclk),
.ADC_csn(ADC_csn),
.adc_res(adc_res),
.adc_valid(adc_valid)
);
data_ad_pro U1(
.sys_clk(clk),
.rst_n(1’b1),
.pre_data(adc_res[11:4]),
.cout(cout)
);
display U2(
.sys_clk(clk),
.rst_n(1’b1),
.cout(cout),
.sw(sw),
.flag(flag),
.slec_wei(slec_wei),
.slec_duan(slec_duan)
);
endmodule
2.A/D转换模块(PmodAD1.v)
module PmodAD1( clk,rst, ADC_sdata,ADC_sclk,ADC_csn,adc_res,adc_valid);
input clk,rst, ADC_sdata;
output reg ADC_sclk,ADC_csn;
output reg [11:0] adc_res;
output reg adc_valid;
reg [7:0] cntr;
always@(posedge clk)
if(rst)cntr<=0;else if(cntr==34)cntr<=0;else cntr<=cntr+1;
always@(posedge clk)
case (cntr)
0: ADC_csn<=0;
33: ADC_csn<=1;
endcase
always@(posedge clk)
case(cntr)
34,0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,33:ADC_sclk<=1;
default ADC_sclk<=0;
endcase
always@(posedge clk)
case(cntr)
8: adc_res[11]<= ADC_sdata;
10:adc_res[10]<= ADC_sdata;
12:adc_res[9]<= ADC_sdata;
14:adc_res[8]<= ADC_sdata;
16:adc_res[7]<= ADC_sdata;
18:adc_res[6]<= ADC_sdata;
20:adc_res[5]<= ADC_sdata;
22:adc_res[4]<= ADC_sdata;
24:adc_res[3]<= ADC_sdata;
26:adc_res[2]<= ADC_sdata;
28:adc_res[1]<= ADC_sdata;
30:adc_res[0]<= ADC_sdata;
endcase
always@(posedge clk)adc_valid<=cntr==32;
endmodule
3.数据处理模块(data_ad_pro.v)
`timescale 1ns / 1ps
//
//
module data_ad_pro(
//input
sys_clk, //输入系统时钟50MHz
rst_n, //输入复位信号
pre_data, //输入AD采样模块传来的数据
//output
cout //输出处理后的12位电压数据
);
input sys_clk;
input rst_n;
input [7:0] pre_data;
output [19:0] cout;
reg [19:0] L;
always @ (posedge sys_clk)
case(pre_data[3:0]) //pre_data[3:0]=adc_res[7:4],0.0016
4'h1: L <= 20'h00260;
4'h2: L <= 20'h00520;
4'h3: L <= 20'h00780;
4'h4: L <= 20'h01039;
4'h5: L <= 20'h01300;
4'h6: L <= 20'h01559;
4'h7: L <= 20'h01819;
4'h8: L <= 20'h02079;
4'h9: L <= 20'h02339;
//
4'ha: L <= 20'h02598;
4'hb: L <= 20'h02858;
4'hc: L <= 20'h03118;
4'hd: L <= 20'h03378;
4'he: L <= 20'h03638;
4'hf: L <= 20'h03898;
default: L <= 20'h00000;
endcase
reg [19:0] H;
always @ (posedge sys_clk)
case(pre_data[7:4]) //H = n * 12'h080(n=1、2、3、、f),pre_data[7:4]=adc_res[11:8]
4'h1: H <= 20'h04157;
4'h2: H <= 20'h08315;
4'h3: H <= 20'h12472;
4'h4: H <= 20'h16630;
4'h5: H <= 20'h20787;
//只需要显示0-2V
4'h6: H <= 20'h00000;
4'h7: H <= 20'h00000;
4'h8: H <= 20'h00000;
4'h9: H <= 20'h00000;
//
4'ha: H <= 20'h00000;
4'hb: H <= 20'h00000;
4'hc: H <= 20'h00000;
4'hd: H <= 20'h00000;
4'he: H <= 20'h00000;
4'hf: H <= 20'h00000;
default: H <= 20'h00000;
endcase
reg c0;
always @ (posedge sys_clk)
begin
if(H[3:0] + L[3:0] > 4'd9)
c0 <= 1;
else
c0 <= 0;
end
reg c1;
always @ (posedge sys_clk)
begin
if(H[7:4] + L[7:4] > 4'd9)
c1 <= 1;
else
c1 <= 0;
end
reg c2;
always @ (posedge sys_clk)
begin
if(H[11:8] + L[11:8] > 4'd9)
c2 <= 1;
else
c2 <= 0;
end
reg c3;
always @ (posedge sys_clk)
begin
if(H[15:12] + L[15:12] > 4'd9)
c3 <= 1;
else
c3 <= 0;
end
reg [19:0] cout;
always @(c1 or c0 or c2 or c3 )
begin
case({c3,c2,c1,c0})
4'b0000: begin
cout[19:16] <= H[19:16] + L[19:16];
cout[15:12] <= H[15:12] + L[15:12];
cout[11:8] <= H[11:8] + L[11:8];
cout[7:4] <= H[7:4] + L[7:4];
cout[3:0] <= H[3:0] + L[3:0];
end
4'b0001: begin
if(((H[7:4] + L[7:4] + 4'b0001) > 9)&&((H[11:8] + L[11:8] + 4'b0001) > 9)&&((H[15:12] + L[15:12] + 4'b0001) > 9)) begin
cout[19:16] <= H[19:16] + L[19:16]+4'b0001;
cout[15:12] <= H[15:12] + L[15:12]+4'b0111;
cout[11:8] <= H[11:8] + L[11:8]+4'b0111;
cout[7:4] <= H[7:4] + L[7:4]+4'b0111;
cout[3:0] <= H[3:0] + L[3:0]+ 4'b0110;//加上6
end
else if(((H[7:4] + L[7:4] + 4'b0001) > 9)&&((H[11:8] + L[11:8] + 4'b0001) > 9)&&((H[15:12] + L[15:12] + 4'b0001) <= 9)) begin
cout[19:16] <= H[19:16] + L[19:16];
cout[15:12] <= H[15:12] + L[15:12]+4'b0001;
cout[11:8] <= H[11:8] + L[11:8]+4'b0111;
cout[7:4] <= H[7:4] + L[7:4]+4'b0111;
cout[3:0] <= H[3:0] + L[3:0]+ 4'b0110;//加上6
end
else if(((H[7:4] + L[7:4] + 4'b0001) > 9)&&((H[11:8] + L[11:8] + 4'b0001) <= 9)) begin
cout[19:16] <= H[19:16] + L[19:16];
cout[15:12] <= H[15:12] + L[15:12];
cout[11:8] <= H[11:8] + L[11:8]+4'b0001;
cout[7:4] <= H[7:4] + L[7:4]+4'b0111;
cout[3:0] <= H[3:0] + L[3:0]+ 4'b0110;//加上6
end
else if(((H[7:4] + L[7:4] + 4'b0001)<= 9)&&((H[11:8] + L[11:8] + 4'b0001)<=9)) begin
cout[19:16] <= H[19:16] + L[19:16];
cout[15:12] <= H[15:12] + L[15:12];
cout[11:8] <= H[11:8] + L[11:8];
cout[7:4] <= H[7:4] + L[7:4]+4'b0001;
cout[3:0] <= H[3:0] + L[3:0]+ 4'b0110;//加上6
end
end
4'b0010:begin
if(((H[11:8] + L[11:8] + 4'b0001) > 9)&&((H[15:12] + L[15:12] + 4'b0001) > 9)) begin
cout[19:16] <= H[19:16] + L[19:16]+4'b0001;
cout[15:12] <= H[15:12] + L[15:12]+4'b0111;
cout[11:8] <= H[11:8] + L[11:8]+4'b0111;
cout[7:4] <= H[7:4] + L[7:4]+4'b0110;
cout[3:0] <= H[3:0] + L[3:0];//加上6
end
else if(((H[11:8] + L[11:8] + 4'b0001) > 9)&&((H[15:12] + L[15:12] + 4'b0001) <= 9)) begin
cout[19:16] <= H[19:16] + L[19:16];
cout[15:12] <= H[15:12] + L[15:12]+4'b0001;
cout[11:8] <= H[11:8] + L[11:8]+4'b0111;
cout[7:4] <= H[7:4] + L[7:4]+4'b0110;
cout[3:0] <= H[3:0] + L[3:0];//加上6
end
else if(((H[11:8] + L[11:8] + 4'b0001) <= 9)) begin
cout[19:16] <= H[19:16] + L[19:16];
cout[15:12] <= H[15:12] + L[15:12];
cout[11:8] <= H[11:8] + L[11:8]+4'b0001;
cout[7:4] <= H[7:4] + L[7:4]+4'b0110;
cout[3:0] <= H[3:0] + L[3:0];//加上6
end
end
4'b0011:begin
if(((H[11:8] + L[11:8] + 4'b0001) > 9)&&((H[15:12] + L[15:12] + 4'b0001) > 9)) begin
cout[19:16] <= H[19:16] + L[19:16]+4'b0001;
cout[15:12] <= H[15:12] + L[15:12]+4'b0111;
cout[11:8] <= H[11:8] + L[11:8]+4'b0111;
cout[7:4] <= H[7:4] + L[7:4]+4'b0111;
cout[3:0] <= H[3:0] + L[3:0]+4'b0110;//加上6
end
else if(((H[11:8] + L[11:8] + 4'b0001) > 9)&&((H[15:12] + L[15:12] + 4'b0001) <= 9)) begin
cout[19:16] <= H[19:16] + L[19:16];
cout[15:12] <= H[15:12] + L[15:12]+4'b0001;
cout[11:8] <= H[11:8] + L[11:8]+4'b0111;
cout[7:4] <= H[7:4] + L[7:4]+4'b0111;
cout[3:0] <= H[3:0] + L[3:0]+4'b0110;//加上6
end
else if(((H[11:8] + L[11:8] + 4'b0001) <= 9)) begin
cout[19:16] <= H[19:16] + L[19:16];
cout[15:12] <= H[15:12] + L[15:12];
cout[11:8] <= H[11:8] + L[11:8]+4'b0001;
cout[7:4] <= H[7:4] + L[7:4]+4'b0111;
cout[3:0] <= H[3:0] + L[3:0]+4'b0110;//加上6
end
end
4'b0100:begin
if((H[15:12] + L[15:12] + 4'b0001) > 9) begin
cout[19:16] <= H[19:16] + L[19:16]+4'b0001;
cout[15:12] <= H[15:12] + L[15:12]+4'b0111;
cout[11:8] <= H[11:8] + L[11:8]+4'b0110;
cout[7:4] <= H[7:4] + L[7:4];
cout[3:0] <= H[3:0] + L[3:0];//加上6
end
else if((H[15:12] + L[15:12] + 4'b0001) <= 9) begin
cout[19:16] <= H[19:16] + L[19:16];
cout[15:12] <= H[15:12] + L[15:12]+4'b0001;
cout[11:8] <= H[11:8] + L[11:8]+4'b0110;
cout[7:4] <= H[7:4] + L[7:4];
cout[3:0] <= H[3:0] + L[3:0];//加上6
end
end
4'b0101:begin
if(((H[7:4] + L[7:4] + 4'b0001) > 9)&&((H[15:12] + L[15:12] + 4'b0001) > 9)) begin
cout[19:16] <= H[19:16] + L[19:16]+4'b0001;
cout[15:12] <= H[15:12] + L[15:12]+4'b0111;
cout[11:8] <= H[11:8] + L[11:8]+4'b0111;
cout[7:4] <= H[7:4] + L[7:4]+4'b0111;
cout[3:0] <= H[3:0] + L[3:0]+4'b0110;//加上6
end
else if(((H[7:4] + L[7:4] + 4'b0001) > 9)&&((H[15:12] + L[15:12] + 4'b0001) <= 9)) begin
cout[19:16] <= H[19:16] + L[19:16];
cout[15:12] <= H[15:12] + L[15:12]+4'b0001;
cout[11:8] <= H[11:8] + L[11:8]+4'b0111;
cout[7:4] <= H[7:4] + L[7:4]+4'b0111;
cout[3:0] <= H[3:0] + L[3:0]+4'b0110;//加上6
end
else if(((H[7:4] + L[7:4] + 4'b0001)<=9)&&((H[15:12] + L[15:12] + 4'b0001) > 9)) begin
cout[19:16] <= H[19:16] + L[19:16]+4'b0001;
cout[15:12] <= H[15:12] + L[15:12]+4'b0111;
cout[11:8] <= H[11:8] + L[11:8]+4'b0110;
cout[7:4] <= H[7:4] + L[7:4]+4'b0001;
cout[3:0] <= H[3:0] + L[3:0]+4'b0110;//加上6
end
else if(((H[11:8] + L[11:8] + 4'b0001) <= 9)&&((H[15:12] + L[15:12] + 4'b0001) <= 9)) begin
cout[19:16] <= H[19:16] + L[19:16];
cout[15:12] <= H[15:12] + L[15:12]+4'b0001;
cout[11:8] <= H[11:8] + L[11:8]+4'b0110;
cout[7:4] <= H[7:4] + L[7:4]+4'b0001;
cout[3:0] <= H[3:0] + L[3:0]+4'b0110;//加上6
end
end
4'b0110:begin
if((H[15:12] + L[15:12] + 4'b0001) > 9) begin
cout[19:16] <= H[19:16] + L[19:16]+4'b0001;
cout[15:12] <= H[15:12] + L[15:12]+4'b0111;
cout[11:8] <= H[11:8] + L[11:8]+4'b0111;
cout[7:4] <= H[7:4] + L[7:4]+4'b0110;
cout[3:0] <= H[3:0] + L[3:0];//加上6
end
else if((H[15:12] + L[15:12] + 4'b0001) <= 9) begin
cout[19:16] <= H[19:16] + L[19:16];
cout[15:12] <= H[15:12] + L[15:12]+4'b0001;
cout[11:8] <= H[11:8] + L[11:8]+4'b0111;
cout[7:4] <= H[7:4] + L[7:4]+4'b0110;
cout[3:0] <= H[3:0] + L[3:0];//加上6
end
end
4'b0111:begin
if((H[15:12] + L[15:12] + 4'b0001) > 9) begin
cout[19:16] <= H[19:16] + L[19:16]+4'b0001;
cout[15:12] <= H[15:12] + L[15:12]+4'b0111;
cout[11:8] <= H[11:8] + L[11:8]+4'b0111;
cout[7:4] <= H[7:4] + L[7:4]+4'b0111;
cout[3:0] <= H[3:0] + L[3:0]+4'b0110;//加上6
end
else if((H[15:12] + L[15:12] + 4'b0001) <= 9) begin
cout[19:16] <= H[19:16] + L[19:16];
cout[15:12] <= H[15:12] + L[15:12]+4'b0001;
cout[11:8] <= H[11:8] + L[11:8]+4'b0111;
cout[7:4] <= H[7:4] + L[7:4]+4'b0111;
cout[3:0] <= H[3:0] + L[3:0]+4'b0110;//加上6
end
end
4'b1000:begin
cout[19:16] <= H[19:16] + L[19:16]+4'b0001;
cout[15:12] <= H[15:12] + L[15:12]+ 4'b0110;
cout[11:8] <= H[11:8] + L[11:8];
cout[7:4] <= H[7:4] + L[7:4];
cout[3:0] <= H[3:0] + L[3:0];
end
4'b1001:begin
if(((H[7:4] + L[7:4] + 4'b0001) > 9)&&((H[11:8] + L[11:8] + 4'b0001) > 9)) begin
cout[19:16] <= H[19:16] + L[19:16]+4'b0001;
cout[15:12] <= H[15:12] + L[15:12]+4'b0111;
cout[11:8] <= H[11:8] + L[11:8]+4'b0111;
cout[7:4] <= H[7:4] + L[7:4]+4'b0111;
cout[3:0] <= H[3:0] + L[3:0]+ 4'b0110;//加上6
end
else if(((H[7:4] + L[7:4] + 4'b0001) >9)&&((H[11:8] + L[11:8] + 4'b0001) <= 9)) begin
cout[19:16] <= H[19:16] + L[19:16]+4'b0001;
cout[15:12] <= H[15:12] + L[15:12]+4'b0110;
cout[11:8] <= H[11:8] + L[11:8]+4'b0001;
cout[7:4] <= H[7:4] + L[7:4]+4'b0111;
cout[3:0] <= H[3:0] + L[3:0]+ 4'b0110;//加上6
end
else if(((H[7:4] + L[7:4] + 4'b0001) <=9)) begin
cout[19:16] <= H[19:16] + L[19:16]+4'b0001;
cout[15:12] <= H[15:12] + L[15:12]+4'b0110;
cout[11:8] <= H[11:8] + L[11:8];
cout[7:4] <= H[7:4] + L[7:4]+4'b0001;
cout[3:0] <= H[3:0] + L[3:0]+ 4'b0110;//加上6
end
end
4'b1010:begin
if(((H[11:8] + L[11:8] + 4'b0001) > 9)) begin
cout[19:16] <= H[19:16] + L[19:16]+4'b0001;
cout[15:12] <= H[15:12] + L[15:12]+4'b0111;
cout[11:8] <= H[11:8] + L[11:8]+4'b0111;
cout[7:4] <= H[7:4] + L[7:4]+4'b0110;
cout[3:0] <= H[3:0] + L[3:0];//加上6
end
else if(((H[11:8] + L[11:8] + 4'b0001) <= 9)) begin
cout[19:16] <= H[19:16] + L[19:16]+4'b0001;
cout[15:12] <= H[15:12] + L[15:12]+4'b0110;
cout[11:8] <= H[11:8] + L[11:8]+4'b0001;
cout[7:4] <= H[7:4] + L[7:4]+4'b0110;
cout[3:0] <= H[3:0] + L[3:0];//加上6
end
end
4'b1011:begin
if(((H[11:8] + L[11:8] + 4'b0001) > 9)) begin
cout[19:16] <= H[19:16] + L[19:16]+4'b0001;
cout[15:12] <= H[15:12] + L[15:12]+4'b0111;
cout[11:8] <= H[11:8] + L[11:8]+4'b0111;
cout[7:4] <= H[7:4] + L[7:4]+4'b0111;
cout[3:0] <= H[3:0] + L[3:0]+4'b0110;//加上6
end
else if(((H[11:8] + L[11:8] + 4'b0001) <= 9)) begin
cout[19:16] <= H[19:16] + L[19:16]+4'b0001;
cout[15:12] <= H[15:12] + L[15:12]+4'b0110;
cout[11:8] <= H[11:8] + L[11:8]+4'b0001;
cout[7:4] <= H[7:4] + L[7:4]+4'b0111;
cout[3:0] <= H[3:0] + L[3:0]+4'b0110;//加上6
end
end
4'b1100:begin
cout[19:16] <= H[19:16] + L[19:16]+4'b0001;
cout[15:12] <= H[15:12] + L[15:12]+4'b0111;
cout[11:8] <= H[11:8] + L[11:8]+4'b0110;
cout[7:4] <= H[7:4] + L[7:4];
cout[3:0] <= H[3:0] + L[3:0];
end
4'b1101:begin
if(((H[7:4] + L[7:4] + 4'b0001) > 9)) begin
cout[19:16] <= H[19:16] + L[19:16]+4'b0001;
cout[15:12] <= H[15:12] + L[15:12]+4'b0111;
cout[11:8] <= H[11:8] + L[11:8]+4'b0111;
cout[7:4] <= H[7:4] + L[7:4]+4'b0111;
cout[3:0] <= H[3:0] + L[3:0]+4'b0110;//加上6
end
else if(((H[7:4] + L[7:4] + 4'b0001) <= 9)) begin
cout[19:16] <= H[19:16] + L[19:16]+4'b0001;
cout[15:12] <= H[15:12] + L[15:12]+4'b0111;
cout[11:8] <= H[11:8] + L[11:8]+4'b0110;
cout[7:4] <= H[7:4] + L[7:4]+4'b0001;
cout[3:0] <= H[3:0] + L[3:0]+4'b0110;//加上6
end
end
4'b1110:begin
cout[19:16] <= H[19:16] + L[19:16]+4'b0001;
cout[15:12] <= H[15:12] + L[15:12]+4'b0111;
cout[11:8] <= H[11:8] + L[11:8]+4'b0111;
cout[7:4] <= H[7:4] + L[7:4]+4'b0110;
cout[3:0] <= H[3:0] + L[3:0];
end
4'b1111:begin
cout[19:16] <= H[19:16] + L[19:16]+4'b0001;
cout[15:12] <= H[15:12] + L[15:12]+4'b0111;
cout[11:8] <= H[11:8] + L[11:8]+4'b0111;
cout[7:4] <= H[7:4] + L[7:4]+4'b0111;
cout[3:0] <= H[3:0] + L[3:0]+4'b0110;
end
endcase
end
endmodule
4.电压显示模块(display.v)
`timescale 1ns / 1ps
//
module display(
//input
sys_clk, //输系统时钟50MHz
rst_n, //输入复位信号
cout, //输入数据处理模块传来的12位电压数据
sw,
flag,
//output
slec_wei, //输出数码管位选信号
slec_duan //输出数码管段选信号
);
input sys_clk;
input rst_n;
input [19:0] cout;
input [3:0] sw;
input flag;
output [7:0] slec_wei;
output [7:0] slec_duan;
parameter SEG_NUM0 = 8'b1100_0000, //数码管显示0
SEG_NUM1 = 8'b1111_1001, //数码管显示1
SEG_NUM2 = 8'b1010_0100, //数码管显示2
SEG_NUM3 = 8'b1011_0000, //数码管显示3
SEG_NUM4 = 8'b1001_1001, //数码管显示4
SEG_NUM5 = 8'b1001_0010, //数码管显示5
SEG_NUM6 = 8'b1000_0010, //数码管显示6
SEG_NUM7 = 8'b1111_1000, //数码管显示7
SEG_NUM8 = 8'b1000_0000, //数码管显示8
SEG_NUM = 8'b1111_1111,
SEG_NUM9 = 8'b1001_0000, //数码管显示9
SEG_NUMF = 8'b1011_1111;
parameter T1MS = 16'd49999; //1ms计数
//1ms计数器
reg [15:0] cnt;
always @(posedge sys_clk or negedge rst_n)
if(!rst_n)
cnt <= 16'd0;
else if(cnt == T1MS)
cnt <= 16'd0;
else
cnt <= cnt + 1'b1;
/**********************************************/
//数码管轮流导通
reg [2:0] i;
reg [7:0] slec_wei;
reg [7:0] slec_duan;
reg [7:0] data0;
always @ (posedge sys_clk)
case(cout[19:16]) //进行编码 高
4'h0: data0 <= SEG_NUM0;
4'h1: data0 <= SEG_NUM1;
4'h2: data0 <= SEG_NUM2;
4'h3: data0 <= SEG_NUM3;
4'h4: data0 <= SEG_NUM4;
4'h5: data0 <= SEG_NUM5;
4'h6: data0 <= SEG_NUM6;
4'h7: data0 <= SEG_NUM7;
4'h8: data0 <= SEG_NUM8;
4'h9: data0 <= SEG_NUM9;
default:data0 <= SEG_NUM0;
endcase
reg [7:0] data1;
always @ (posedge sys_clk)
case(cout[15:12]) //进行编码 高
4'h0: data1 <= SEG_NUM0;
4'h1: data1 <= SEG_NUM1;
4'h2: data1 <= SEG_NUM2;
4'h3: data1 <= SEG_NUM3;
4'h4: data1 <= SEG_NUM4;
4'h5: data1 <= SEG_NUM5;
4'h6: data1 <= SEG_NUM6;
4'h7: data1 <= SEG_NUM7;
4'h8: data1 <= SEG_NUM8;
4'h9: data1 <= SEG_NUM9;
default:data1 <= SEG_NUM0;
endcase
reg [7:0] data2;
always @ (posedge sys_clk)
case(cout[11:8]) //进行编码 高
4'h0: data2 <= SEG_NUM0;
4'h1: data2 <= SEG_NUM1;
4'h2: data2 <= SEG_NUM2;
4'h3: data2 <= SEG_NUM3;
4'h4: data2 <= SEG_NUM4;
4'h5: data2 <= SEG_NUM5;
4'h6: data2 <= SEG_NUM6;
4'h7: data2 <= SEG_NUM7;
4'h8: data2 <= SEG_NUM8;
4'h9: data2 <= SEG_NUM9;
default:data2 <= SEG_NUM0;
endcase
/*****************************************/
reg [7:0] data3;
always @ (posedge sys_clk)
case(cout[7:4]) //进行编码 中
4'h0: data3 <= SEG_NUM0;
4'h1: data3 <= SEG_NUM1;
4'h2: data3 <= SEG_NUM2;
4'h3: data3 <= SEG_NUM3;
4'h4: data3 <= SEG_NUM4;
4'h5: data3 <= SEG_NUM5;
4'h6: data3 <= SEG_NUM6;
4'h7: data3 <= SEG_NUM7;
4'h8: data3 <= SEG_NUM8;
4'h9: data3 <= SEG_NUM9;
default:data3 <= SEG_NUM0;
endcase
/*****************************************/
reg [7:0] data4;
always @ (posedge sys_clk)
case(cout[3:0]) //进行编码 低
4'h0: data4 <= SEG_NUM0;
4'h1: data4 <= SEG_NUM1;
4'h2: data4 <= SEG_NUM2;
4'h3: data4 <= SEG_NUM3;
4'h4: data4 <= SEG_NUM4;
4'h5: data4 <= SEG_NUM5;
4'h6: data4 <= SEG_NUM6;
4'h7: data4 <= SEG_NUM7;
4'h8: data4 <= SEG_NUM8;
4'h9: data4 <= SEG_NUM9;
default:data4 <= SEG_NUM0;
endcase
always @ (posedge sys_clk or negedge rst_n)
if( !rst_n )begin
i <= 4'd0;
slec_wei <= 8'b11111111;
end
else
case( i )
0: begin
if( cnt == T1MS ) i <= i + 1'b1;
else if (( cnt != T1MS )&&(sw==4'b0010))begin
slec_wei <= 8'b11101111;
slec_duan <= data0 + 8'b1000_0000;
end //data0是整数位,加上小数点
else if (( cnt != T1MS )&&(sw!=4'b0010))begin
slec_wei <= 8'b11101111;
slec_duan <= data0;
end
end
1: begin
if( cnt == T1MS ) i <= i + 1'b1;
else if (( cnt != T1MS )&&(sw==4'b0100))begin
slec_wei <= 8'b11110111;
slec_duan <= data1 + 8'b1000_0000;
end //data1是整数位,加上小数点
else if (( cnt != T1MS )&&(sw!=4'b0100))begin
slec_wei <= 8'b11110111;
slec_duan <= data1;
end
end
2: begin
if( cnt == T1MS ) i <= i + 1'b1;
else if (( cnt != T1MS )&&(sw==4'b1000))begin
slec_wei <= 8'b11111011;
slec_duan <= data2 + 8'b1000_0000;
end //data2是整数位,加上小数点
else if (( cnt != T1MS )&&(sw!=4'b1000))begin
slec_wei <= 8'b11111011;
slec_duan <= data2;
end
end
3: if( cnt == T1MS ) i <= i + 1'b1;
else begin
slec_wei <= 8'b11111101;
slec_duan <= data3;
end
4: begin
if( cnt == T1MS ) i <= i + 1'b1;
else if (( cnt != T1MS )&&(sw==4'b0001))begin
slec_wei <= 8'b11011111;
slec_duan <= SEG_NUM0 + 8'b1000_0000;
end //0-200mv,加上小数点
else if (( cnt != T1MS )&&(sw!=4'b0001))begin
slec_wei <= 8'b11011111;
slec_duan <= SEG_NUM0;
end
end
5: begin
if( cnt == T1MS ) i <= i + 1'b1;
else if(( cnt != T1MS )&&(flag==0))begin
slec_wei <= 8'b10111111;
slec_duan <= SEG_NUM;
end
else if(( cnt != T1MS )&&(flag==1))begin
slec_wei <= 8'b10111111;
slec_duan <= SEG_NUMF;
end
end
6: if( cnt == T1MS ) i <= i + 1'b1;
else begin
slec_wei <= 8'b01111111;
slec_duan <= SEG_NUM;
end
7: if( cnt == T1MS ) i <= 5'd0;
else begin
slec_wei <= 8'b11111110;
slec_duan <= data4;
end
endcase
endmodule
5.引脚约束文件(这里使用Nexys4ddr,注意:数码管是共阳极显示!!)
## 7 segment display
NET "slec_duan<0>" LOC=T10 | IOSTANDARD=LVCMOS33; #IO_L24N_T3_A00_D16_14
NET "slec_duan<1>" LOC=R10 | IOSTANDARD=LVCMOS33; #IO_25_14
NET "slec_duan<2>" LOC=K16 | IOSTANDARD=LVCMOS33; #IO_25_15
NET "slec_duan<3>" LOC=K13 | IOSTANDARD=LVCMOS33; #IO_L17P_T2_A26_15
NET "slec_duan<4>" LOC=P15 | IOSTANDARD=LVCMOS33; #IO_L13P_T2_MRCC_14
NET "slec_duan<5>" LOC=T11 | IOSTANDARD=LVCMOS33; #IO_L19P_T3_A10_D26_14
NET "slec_duan<6>" LOC=L18 | IOSTANDARD=LVCMOS33; #IO_L4P_T0_D04_14
NET "slec_duan<7>" LOC=H15 | IOSTANDARD=LVCMOS33; #IO_L19N_T3_A21_VREF_15
NET "slec_wei<0>" LOC=J17 | IOSTANDARD=LVCMOS33; #IO_L23P_T3_FOE_B_15
NET "slec_wei<1>" LOC=J18 | IOSTANDARD=LVCMOS33; #IO_L23N_T3_FWE_B_15
NET "slec_wei<2>" LOC=T9 | IOSTANDARD=LVCMOS33; #IO_L24P_T3_A01_D17_14
NET "slec_wei<3>" LOC=J14 | IOSTANDARD=LVCMOS33; #IO_L19P_T3_A22_15
NET "slec_wei<4>" LOC=P14 | IOSTANDARD=LVCMOS33; #IO_L8N_T1_D12_14
NET "slec_wei<5>" LOC=T14 | IOSTANDARD=LVCMOS33; #IO_L14P_T2_SRCC_14
NET "slec_wei<6>" LOC=K2 | IOSTANDARD=LVCMOS33; #IO_L23P_T3_35
NET "slec_wei<7>" LOC=U13 | IOSTANDARD=LVCMOS33; #IO_L23N_T3_A02_D18_14
#量程开关
NET "sw<0>" LOC=J15 | IOSTANDARD=LVCMOS33; #IO_L24N_T3_RS0_15
NET "sw<1>" LOC=L16 | IOSTANDARD=LVCMOS33; #IO_L3N_T0_DQS_EMCCLK_14
NET "sw<2>" LOC=M13 | IOSTANDARD=LVCMOS33; #IO_L6N_T0_D08_VREF_14
NET "sw<3>" LOC=R15 | IOSTANDARD=LVCMOS33; #IO_L13N_T2_MRCC_14
##判断正负
NET "flag" LOC=V10 | IOSTANDARD=LVCMOS33; #IO_L21P_T3_DQS_14
##时钟
NET "clk" LOC=E3 | IOSTANDARD=LVCMOS33;
##pmodad1
NET "ADC_csn" LOC=C17 | IOSTANDARD=LVCMOS33; #IO_L20N_T3_A19_15
NET "ADC_sdata" LOC=D18 | IOSTANDARD=LVCMOS33; #IO_L21N_T3_DQS_A18_15
NET "ADC_sclk" LOC=G17 | IOSTANDARD=LVCMOS33; #IO_L18N_T2_A23_15
##
NET "led<0>" LOC=H17 | IOSTANDARD=LVCMOS33; #IO_L18P_T2_A24_15
NET "led<1>" LOC=K15 | IOSTANDARD=LVCMOS33; #IO_L24P_T3_RS1_15
NET "led<2>" LOC=J13 | IOSTANDARD=LVCMOS33; #IO_L17N_T2_A25_15
NET "led<3>" LOC=N14 | IOSTANDARD=LVCMOS33; #IO_L8P_T1_D11_14
NET "led<4>" LOC=R18 | IOSTANDARD=LVCMOS33; #IO_L7P_T1_D09_14
NET "led<5>" LOC=V17 | IOSTANDARD=LVCMOS33; #IO_L18N_T2_A11_D27_14
NET "led<6>" LOC=U17 | IOSTANDARD=LVCMOS33; #IO_L17P_T2_A14_D30_14
NET "led<7>" LOC=U16 | IOSTANDARD=LVCMOS33; #IO_L18P_T2_A12_D28_14
三.参考资料
(1)罗杰,谢自美 电子线路设计.实验.测试,电子工业出版社,2015
(2)康华光,张林《电子技术基础:模拟部分(第七版)》,高等教育出版社,2021
(3)康华光,张林《电子技术基础:数字部分(第七版)》,高等教育出版社,2021
(4) FPGA(Nexy4ddr)、Analog Discovery2以及PMODAD1和AD7476A芯片的相关资料。
www.analog.com/AD7476A
www.digilentinc.com
本文代码参考了大佬的设计:https://blog.youkuaiyun.com/weixin_43586860/article/details/107331277?spm=1001.2014.3001.5506
感谢大佬,侵删。