AD芯片TLC549采集模拟信号实验代码
module ADC_TLC549
(
clk,//系统50MHZ时钟
adc_sclk,//AD TLC549的时钟
data,//AD TLC549的数据口
cs,//AD TLC549的片选择
wei,//数码管的为选择
duan //数码管的7段码
);
input clk;
input data;
output cs;
output adc_sclk;
output[7:0] duan;
output[3:0] wei;
reg cs,adc_sclk,clk1k,clk1ms;
reg[15:0] count;
reg[24:0] count1ms;
reg[3:0] cnt;
reg[2:0] number;
reg[1:0] state;
reg[3:0] wei;
reg ledcs;
reg[7:0] duan;
reg[7:0] dataout;
reg[16:0] tenvalue;
parameter sample=2‘b00,
display=2’b01;
/**********产生100k的采集时钟信号*********/
always@(posedge clk)
begin
if(count《=250)
count《=count+1‘b1;
else
begin
count《=0;
adc_sclk《=~adc_sclk;
end
end
/*******产生周期为1ms即1kHz的信号*********/
always@(posedge clk)
begin
if(count1ms》25’d25000)
begin
clk1ms《=~clk1ms;
count1ms《=0;
end
else
count1ms《=count1ms+1;
end
/*********AD采样程序**************/
always@(negedge adc_sclk)
begin
case(state)
sample:
begin
cs《=0;
dataout[7:0]《={dataout[6:0],data};
if(cnt》4‘d7)
begin
cnt《=0;
state《=display;
end
else
begin
cnt《=cnt+1;
state《=sample;
end
end
display:
begin
cs《=1;//关AD片选
tenvalue《=(tendata((dataout》》4)&8’b0000_1111)*16+tendata(dataout&8‘b0000_1111))*129;//
//得到采集的数据
state《=sample;
end
default: state《=display;
endcase
end
/***********2进制转十进制函数*************/
function[7:0] tendata;//返回一个4位的数字
input[7:0] datain;
begin
case(datain)
4’b00000000: tendata=4‘d0;//0
4’b00000001: tendata=4‘d1;//1
4’b00000010: tendata=4‘d2;//2
4’b00000011: tendata=4‘d3;//3
4’b00000100: tendata=4‘d4;//4
4’b00000101: tendata=4‘d5;//5
4’b00000110: tendata=4‘d6;//6
4’b00000111: tendata=4‘d7;//7
4’b00001000: tendata=4‘d8;//8
4’b00001001: tendata=4‘d9;//9
4’b00001010: tendata=4‘d10;//
4’b00001011: tendata=4‘d11;//
4’b00001100: tendata=4‘d12;
4’b00001101: tendata=4‘d13;
4’b00001110: tendata=4‘d14;
4’b00001111: tendata=4‘d15;
default:tendata=4’bzzzz_zzzz;
endcase
end
endfunction
/*********十进制转LED段选函数*********/
function[7:0] leddata;//返回一个8位的数字
input[3:0] datain;
begin
case(datain)
4‘d0: leddata=8’b11000000;//0
4‘d1: leddata=8’b11111001;//1
4‘d2: leddata=8’b10100100;//2
4‘d3: leddata=8’b10110000;//3
4‘d4: leddata=8’b10011001;//4
4‘d5: leddata=8’b10010010;//5
4‘d6: leddata=8’b10000010;//6
4‘d7: leddata=8’b11111000;//7
4‘d8: leddata=8’b10000000;//8
4‘d9: leddata=8’b10010000;//9
4‘d10: leddata=8’b10111111;//-
4‘d11: leddata=8’b01111111;//。
default:leddata=8‘bzzzz_zzzz;
endcase
end
endfunction
/********数码管扫描函数*************/
always@(posedge clk1ms)
begin
if(number==5)
number《=0;
else
begin
number《=number+1;
case(number)
4’d0:
begin
duan《=leddata((tenvalue/10)%10);//个位
wei《=4‘b1110;
end
4’d1:
begin
duan《=leddata((tenvalue/100)%10);//十位
wei《=4‘b1101;
end
4’d2:
begin
duan《=leddata((tenvalue/1000)%10);//百位
wei《=4‘b1011;
end
4’d3:
begin
duan《=leddata(tenvalue/10000);//千位
wei《=4‘b0111;
end
4’d4:
begin
duan《=leddata(4‘d11);//。 显示小数点
wei《=4’b0111;
end
endcase
end
end
endmodule
评论
查看更多