AD芯片TLC549采集模擬信號(hào)實(shí)驗(yàn)代碼
module ADC_TLC549
?。?/p>
clk,//系統(tǒng)50MHZ時(shí)鐘
adc_sclk,//AD TLC549的時(shí)鐘
data,//AD TLC549的數(shù)據(jù)口
cs,//AD TLC549的片選擇
wei,//數(shù)碼管的為選擇
duan //數(shù)碼管的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;
/**********產(chǎn)生100k的采集時(shí)鐘信號(hào)*********/
always@(posedge clk)
begin
if(count《=250)
count《=count+1‘b1;
else
begin
count《=0;
adc_sclk《=~adc_sclk;
end
end
/*******產(chǎn)生周期為1ms即1kHz的信號(hào)*********/
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;//關(guān)AD片選
tenvalue《=(tendata((dataout》》4)&8’b0000_1111)*16+tendata(dataout&8‘b0000_1111))*129;//
//得到采集的數(shù)據(jù)
state《=sample;
end
default: state《=display;
endcase
end
/***********2進(jìn)制轉(zhuǎn)十進(jìn)制函數(shù)*************/
function[7:0] tendata;//返回一個(gè)4位的數(shù)字
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
/*********十進(jìn)制轉(zhuǎn)LED段選函數(shù)*********/
function[7:0] leddata;//返回一個(gè)8位的數(shù)字
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
/********數(shù)碼管掃描函數(shù)*************/
always@(posedge clk1ms)
begin
if(number==5)
number《=0;
else
begin
number《=number+1;
case(number)
4’d0:
begin
duan《=leddata((tenvalue/10)%10);//個(gè)位
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);//。 顯示小數(shù)點(diǎn)
wei《=4’b0111;
end
endcase
end
end
endmodule
評(píng)論
查看更多