初来乍到,如有bug,那是必然!:D
module ADS(clk,rst_n,XD, RESET, CONVST, RD, CS, BUSY, A0, A1, A2);
input clk; // 50MHz主时钟
input rst_n; //低电平复位信号
input [15:0]XD; //定义AD转换结果的16位数据并行输入端口
wire [15:0]XD; //类型为wire型(默认就是)
output RESET; //复位AD芯片输出信号端口
output CONVST; //启动AD转换输出信号端口
output RD; //读AD转换结果输出信号端口
output CS; //AD芯片片选信号端口(多个AD芯片时有用)
input BUSY; //AD转换完成标志,输入端口。用于判断是否转换完成
output[15:0] A0; //AD芯片是6通道,这里输出三路结果A0 A1 A2
output[15:0] A1;
output[15:0] A2;
reg RESET; //定义数据类型
reg CONVST;
reg RD;
reg CS;
reg[3:0] CONVST_state; //状态机状态定义
reg[7:0] CONVST_2us; //延时2us寄存器
reg[15:0] CONVST_DATA[5:0]; //定义6个16位宽的寄存器,用于存储转换结果
reg[3:0] RD_30ns; //读AD数据延时时间寄存器
reg[3:0] RESET_50ns; //AD芯片复位至少50ns,延时寄存器
reg[3:0] RD_DELAY; //读AD转换结果通道,状态机状态标志寄存器
reg[3:0] channel;
always @ (posedge clk or negedge rst_n)
if(!rst_n)
begin
CONVST_state<=4'd0; //一些复位操作
CONVST_2us<=8'd0;
RD_30ns<=4'd0;
RESET_50ns<=4'd0;
RD_DELAY<=4'd0;
channel<=4'd0;
RESET<=1'b1;//RESET至少保持50ns
CONVST<=1'b0;
RD<=1'b1;
CS<=1'b1;
end
else
case(CONVST_state)
0: begin //启动AD转换
RESET<=1'b0;
CONVST<=1'b1;//延时至少1.2us完成转换
CONVST_2us<=CONVST_2us+1'b1;
if(CONVST_2us==60)
begin
CONVST_state<=4'd1;
CS<=1'b0;
end
else
CONVST_state<=4'd0;
end
1: begin //AD转换完成
CONVST<=1'b0;//
CONVST_2us<=1'b0;
if(BUSY==1'b0)
begin
CONVST_state<=4'd2;
end
else
CONVST_state<=4'd1;
end
2: begin
RD<=1'b0;
CONVST_state<=4'd3;
end
3: begin //读取AD转换结果,每个通道读取时间时间不能少于30ns
RD_30ns<=RD_30ns+1'b1;
case(channel) //channel状态机依次读取AD转换结果的6个通道数据
0: begin
if(RD_30ns==0)
channel<=4'd0;
else
begin
channel<=4'd1;
CONVST_DATA[0]<=XD;
end
end
1: begin
CONVST_DATA[1]<=XD;
if(RD_30ns==0)
channel<=4'd1;
else
channel<=4'd2;
end
2: begin
CONVST_DATA[2]<=XD;
if(RD_30ns==0)
channel<=4'd2;
else
channel<=4'd3;
end
3: begin
CONVST_DATA[3]<=XD;
if(RD_30ns==0)
channel<=4'd3;
else
channel<=4'd4;
end
4: begin
CONVST_DATA[4]<=XD;
if(RD_30ns==0)
channel<=4'd4;
else
channel<=4'd5;
end
5: begin
CONVST_DATA[5]<=XD;
if(RD_30ns==0)
channel<=4'd5;
else
channel<=4'd0;
end
endcase
if(RD_30ns==1)
begin
RD<=1'b1;
RD_30ns<=4'd0;
if(channel==5)
begin
CONVST_state<=4'd4;
CS<=1'b1;
end
else
begin
CONVST_state<=4'd2;
end
end
else
CONVST_state<=4'd3;
end
4: begin
RESET_50ns<=RESET_50ns+1'b1;
if(RESET_50ns==1)
begin
CONVST_state<=4'd0;
RESET_50ns<=4'd0;
end
else
CONVST_state<=4'd4;
end
endcase
assign A0 = CONVST_DATA[0]; //将结果连接至模块输出端口
assign A1 = CONVST_DATA[1];
assign A2 = CONVST_DATA[2];
endmodule