小梅哥和你一起深入學(xué)習(xí)FPGA之DAC驅(qū)動(dòng)
線性序列機(jī)計(jì)數(shù)器Cnt1的控制代碼如下:
本文引用地址:http://m.butianyuan.cn/article/278347.htm以下是代碼片段:
always @(posedge Clk or negedge Rst_n)
if(!Rst_n)
Cnt1 <= 5'd0;
else if(Cnt_State == DO_CNT)
begin
if(Cnt1 == 5'd25)
Cnt1 <= 5'd0;
else if(Cnt2 == Cnt2_Top)
Cnt1 <= Cnt1 + 1'b1;
else
Cnt1 <= Cnt1;
end
else
Cnt1 <= 5'd0;
其中,涉及到了兩個(gè)狀態(tài),當(dāng)Cnt_State = 0時(shí),表示沒(méi)有轉(zhuǎn)換請(qǐng)求,即系統(tǒng)處于空閑狀態(tài),DAC不工作,當(dāng)外部有轉(zhuǎn)換請(qǐng)求時(shí),則系統(tǒng)進(jìn)入轉(zhuǎn)換狀態(tài),每當(dāng)計(jì)數(shù)使能信號(hào)到來(lái)時(shí),Cnt1自加一,當(dāng)Cnt1=25后,表明一次轉(zhuǎn)換完成,將計(jì)數(shù)器清零,同時(shí)狀態(tài)跳回空閑態(tài),等待下一次使能信號(hào)的到來(lái)。具體的狀態(tài)轉(zhuǎn)移圖如下所示:
圖2 系統(tǒng)狀態(tài)轉(zhuǎn)移圖
該狀態(tài)機(jī)的代碼對(duì)應(yīng)如下:
以下是代碼片段:
always @(posedge Clk or negedge Rst_n)
if(!Rst_n)
Cnt_State <= IDEL;
else
begin
case(Cnt_State)
IDEL:
if(Do_DA)
Cnt_State <= DO_CNT;
else
Cnt_State <= IDEL;
DO_CNT:
if(Cnt1 == 5'd25)
Cnt_State <= IDEL;
else
Cnt_State <= DO_CNT;
default:;
endcase
end
因此,我們,只需要將Do_DA給出1個(gè)時(shí)鐘周期的高脈沖,即可啟動(dòng)一次轉(zhuǎn)換。同時(shí),在檢測(cè)到該脈沖時(shí),模塊內(nèi)部會(huì)將數(shù)據(jù)端口Data上的數(shù)據(jù)讀入到內(nèi)部數(shù)據(jù)寄存器中,代碼如下:
以下是代碼片段:
always@(posedge Clk or negedge Rst_n)
if(!Rst_n)
Data_r <= 10'd0;
else if(Do_DA)
Data_r <= Data;
else
Data_r <= Data_r;
同時(shí),為了產(chǎn)生1MHz的時(shí)鐘,系統(tǒng)中使用了一個(gè)計(jì)數(shù)器Cnt2來(lái)專門產(chǎn)生該信號(hào),該計(jì)數(shù)器對(duì)系統(tǒng)時(shí)鐘進(jìn)行計(jì)數(shù),如當(dāng)系統(tǒng)時(shí)鐘為50M(周期為20ns)時(shí),Cnt2計(jì)數(shù)到24,即計(jì)數(shù)了500ns,產(chǎn)生一個(gè)時(shí)鐘周期的標(biāo)志信號(hào),則Cnt1在檢測(cè)到這個(gè)標(biāo)志信號(hào)后,便會(huì)自加1,因此,該標(biāo)志信號(hào)出現(xiàn)兩次則表明計(jì)時(shí)1000ns,對(duì)應(yīng)時(shí)鐘頻率為1Mhz,即DAC芯片數(shù)字接口的時(shí)鐘頻率。該部分代碼如下:
以下是代碼片段:
always @ (posedge Clk or negedge Rst_n)
if(!Rst_n)
Cnt2 <= 5'd0;
else if(Cnt_State == DO_CNT)
begin
if(Cnt2 == Cnt2_Top)
Cnt2 <= 5'd0;
else
Cnt2 <= Cnt2 + 1'b1;
end
else
Cnt2 <= 5'd0;
為了兼容不同的系統(tǒng)時(shí)鐘,這里采用參數(shù)化定制,得出對(duì)應(yīng)的計(jì)數(shù)最大值,具體代碼如下:
以下是代碼片段:
Localparam system_clk = 50_000_000; /*系統(tǒng)時(shí)鐘*/
Localparam Cnt2_Top = system_clk / 1_000_000 / 2 - 1; /*500ns技術(shù)器計(jì)數(shù)最大值*/
系統(tǒng)時(shí)鐘設(shè)置為50M,則計(jì)數(shù)最大值為50000000/1000000/2– 1 = 24,當(dāng)系統(tǒng)時(shí)鐘改變后,只需要修改system_clk的值,即可保證Cnt2計(jì)數(shù)一次的時(shí)間為500ns。
最后,附上主序列中的操作代碼:
以下是代碼片段:
always@(posedge Clk or negedge Rst_n)
if(!Rst_n)
begin
DAC_Dout <= 1;
DAC_Clk <= 0;
DAC_LOAD <= 1;
DAC_LDAC <= 1;
DA_Done <= 1;
end
else
begin
case(Cnt1)
0:
begin
DAC_Dout <= 1;
DAC_Clk <= 0;
DAC_LOAD <= 1;
DAC_LDAC <= 1;
DA_Done <= 1;
end
1:begin DAC_Dout <= Data_r[10]; DAC_Clk <= 1;DA_Done <= 0;end
2:DAC_Clk <= 0;
3:begin DAC_Dout <= Data_r[9]; DAC_Clk <= 1;end
4:DAC_Clk <= 0;
5:begin DAC_Dout <= Data_r[8]; DAC_Clk <= 1;end
6:DAC_Clk <= 0;
7:begin DAC_Dout <= Data_r[7]; DAC_Clk <= 1;end
8:DAC_Clk <= 0;
9:begin DAC_Dout <= Data_r[6]; DAC_Clk <= 1;end
10:DAC_Clk <= 0;
11:begin DAC_Dout <= Data_r[5]; DAC_Clk <= 1;end
12:DAC_Clk <= 0;
13:begin DAC_Dout <= Data_r[4]; DAC_Clk <= 1;end
14:DAC_Clk <= 0;
15:begin DAC_Dout <= Data_r[3]; DAC_Clk <= 1;end
16:DAC_Clk <= 0;
17:begin DAC_Dout <= Data_r[2]; DAC_Clk <= 1;end
18:DAC_Clk <= 0;
19:begin DAC_Dout <= Data_r[1]; DAC_Clk <= 1;end
20:DAC_Clk <= 0;
21:begin DAC_Dout <= Data_r[0]; DAC_Clk <= 1;end
22:DAC_Clk <= 0;
23:DAC_LOAD <= 0;
24:begin DAC_LOAD <= 1; DAC_LDAC <= 0; end
25:begin DAC_LDAC <= 1; DA_Done <= 1; end
default:;
endcase
end
該設(shè)計(jì)的仿真結(jié)果如下如所示:
由該仿真結(jié)果可知,時(shí)鐘頻率為1MHz,滿足芯片工作要求,其它時(shí)序均與手冊(cè)給出的時(shí)序保持一致。為了設(shè)計(jì)簡(jiǎn)潔,這里將LOAD和LDAC的低電平脈沖時(shí)間都設(shè)置為了500ns,而非最小時(shí)間250ns,這里主要是為了方便序列機(jī)的設(shè)計(jì)。當(dāng)然,如此設(shè)計(jì)在一定程度上會(huì)影響DAC 的轉(zhuǎn)換速率,不過(guò)在大多數(shù)應(yīng)用場(chǎng)合已經(jīng)足夠,如需更加高效的設(shè)計(jì),只需要對(duì)代碼稍加修改即可。
本驅(qū)動(dòng)的testbench編寫較為簡(jiǎn)單,這里只附上對(duì)應(yīng)代碼,不做詳細(xì)解釋:
以下是代碼片段:
`timescale 1ns/1ns
module TLC5620_Driver_tb;
reg Clk;
reg Rst_n;
reg Do_DA; /*使能單次轉(zhuǎn)換*/
reg [10:0]Data;/*{Addr1,Addr0,Range,Data_bit[7:0]}*/
wire DAC_Dout; /*DAC數(shù)據(jù)線*/
wire DAC_Clk; /*DAC時(shí)鐘線,最高速度1M*/
wire DAC_LDAC; /**/
wire DAC_LOAD; /**/
wire DA_Done; /*單次轉(zhuǎn)換完成標(biāo)志信號(hào)*/
TLC5620_Driver u1(
.Clk(Clk),
.Rst_n(Rst_n),
.Do_DA(Do_DA),
.Data(Data),
.DAC_Dout(DAC_Dout),
.DAC_Clk(DAC_Clk),
.DAC_LDAC(DAC_LDAC),
.DAC_LOAD(DAC_LOAD),
.DA_Done(DA_Done)
);
initial begin
Clk = 1;
Rst_n = 0;
Do_DA = 0;
Data = 11'd0;
#200;
Rst_n = 1;
#400;
Data = 11'b110_1011_1001;
Do_DA = 1;
@(posedge DA_Done)
Data = 11'b110_0000_1111;
#20
Do_DA = 1;
#20;
Do_DA = 0;
@(posedge DA_Done)
Data = 11'b110_1111_0000;
#20
Do_DA = 1;
#20;
Do_DA = 0;
@(posedge DA_Done)
#400;
$stop;
end
always #10 Clk = ~Clk;
endmodule
因?yàn)闀r(shí)間關(guān)系,這里只開(kāi)發(fā)了該芯片的驅(qū)動(dòng),并用modelsim對(duì)該驅(qū)動(dòng)進(jìn)行了仿真,詳細(xì)的調(diào)試和應(yīng)用,小梅哥將在下一個(gè)實(shí)驗(yàn)中介紹。
fpga相關(guān)文章:fpga是什么
路由器相關(guān)文章:路由器工作原理
路由器相關(guān)文章:路由器工作原理
塵埃粒子計(jì)數(shù)器相關(guān)文章:塵埃粒子計(jì)數(shù)器原理
評(píng)論