www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當(dāng)前位置:首頁 > > FPGA開源工作室


PWM 采用任意寬度的輸入值,并創(chuàng)建只有一位寬度的輸出。使用自由運(yùn)行計(jì)數(shù)器的 PWM,這是能做的最簡(jiǎn)單的 PWM。


module PWM( input clk, input rst_n, input [3:0] PWM_in, output PWM_out);  reg [3:0] cnt;always @(posedge clk or negedge rst_n)  if(!rst_n) cnt<=0; else cnt <= cnt + 1'b1; // free-running counter  assign PWM_out = (PWM_in > cnt)?1'b1:1'b0;  // comparatorendmodule


選擇了一個(gè)4位的 PWM 這里,所以 PWM 周期是16。輸入可以從0到15,因此 PWM 輸出比從0% 到15/16 = 93% 。如果需要能夠達(dá)到100% ,輸入需要有一個(gè)額外的bit位。

這段代碼工作得很好,盡管當(dāng)前形式的代碼有點(diǎn)幼稚,因?yàn)檩斎氡仨毷枪潭ǖ?或者只有當(dāng)計(jì)數(shù)器溢出 = 返回到0時(shí)才會(huì)更改)。否則輸出將出現(xiàn)故障。因此,很可能需要一些額外的邏輯(通常是在正確的時(shí)間捕獲輸入的閂鎖)

使用可加載的上下計(jì)數(shù)器的 PWM,這是一個(gè)稍微復(fù)雜一點(diǎn)的設(shè)計(jì)。


module PWM2( input clk, input rst_n, input [3:0] PWM_in, output PWM_out);  reg [3:0] cnt;reg cnt_dir;  // 0 to count up, 1 to count downwire [3:0] cnt_next = cnt_dir ? cnt-1'b1 : cnt+1'b1;wire cnt_end = cnt_dir ? cnt==4'b0000 : cnt==4'b1111;  always @(posedge clk or negedge rst_n )  if(!rst_n) cnt <= 0; else cnt <= cnt_end ? PWM_in : cnt_next;always @(posedge clk or negedge rst_n) if(!rst_n) cnt_dir<=1'b0; else cnt_dir <= cnt_dir ^ cnt_end;assign PWM_out = cnt_dir;endmodule 


它使用一個(gè)可加載的上下計(jì)數(shù)器,不需要輸出比較器。有趣的是,它并不完全等同于第一個(gè)設(shè)計(jì),因?yàn)檩敵鲋芷谟?7個(gè)狀態(tài)而不是16個(gè)(輸出從1/17 = 6% 到16/17 = 94%)。

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
關(guān)閉