資源描述:
《FPGA小程序-特權(quán)同學(xué)視頻》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、特權(quán)同學(xué)視頻程序第一課:分頻moduleclkdiv(clk,rst_n,clk_div);inputclk;//50MHzinputrst_n;//低電平復(fù)位信號outputclk_div;//分頻信號,連接到蜂鳴器//---------------------------------------------------reg[19:0]cnt;//分頻計數(shù)器always@(posedgeclkornegedgerst_n)//異步復(fù)位if(!rst_n)cnt<=20'd0;elsecnt<=cnt+1'b1;//寄存器cnt20ms循環(huán)計數(shù)//----------------------
2、------------------------------regclk_div_r;//clk_div信號值寄存器always@(posedgeclkornegedgerst_n)if(!rst_n)clk_div_r<=1'b0;elseif(cnt==20'hfffff)clk_div_r<=~clk_div_r;//每20ms讓clk_div_r值翻轉(zhuǎn)一次assignclk_div=clk_div_r;endmodule第二課:按鍵//說明:當(dāng)三個獨立按鍵的某一個被按下后,相應(yīng)的LED被點亮;//再次按下后,LED熄滅,按鍵控制LED亮滅modulesw_debounce(clk,rs
3、t_n,sw1_n,sw2_n,sw3_n,led_d1,led_d2,led_d3);inputclk;//主時鐘信號,50MHzinputrst_n;//復(fù)位信號,低有效inputsw1_n,sw2_n,sw3_n;//三個獨立按鍵,低表示按下outputled_d1,led_d2,led_d3;//發(fā)光二極管,分別由按鍵控制//--------------------------------------------------------reg[2:0]key_rst;always@(posedgeclkornegedgerst_n)if(!rst_n)key_rst<=3'b111;e
4、lsekey_rst<={sw3_n,sw2_n,sw1_n};reg[2:0]key_rst_r;//每個時鐘周期的上升沿將low_sw信號鎖存到low_sw_r中always@(posedgeclkornegedgerst_n)if(!rst_n)key_rst_r<=3'b111;elsekey_rst_r<=key_rst;//當(dāng)寄存器key_rst由1變?yōu)?時,led_an的值變?yōu)楦?,維持一個時鐘周期wire[2:0]key_an=key_rst_r&(~key_rst);//------------------------------------------------------
5、-----------reg[19:0]cnt;//計數(shù)寄存器always@(posedgeclkornegedgerst_n)if(!rst_n)cnt<=20'd0;//異步復(fù)位elseif(key_an)cnt<=20'd0;elsecnt<=cnt+1'b1;reg[2:0]low_sw;always@(posedgeclkornegedgerst_n)if(!rst_n)low_sw<=3'b111;elseif(cnt==20'hfffff)//滿20ms,將按鍵值鎖存到寄存器low_sw中cnt==20'hffffflow_sw<={sw3_n,sw2_n,sw1_n};//--
6、-------------------------------------------------------------reg[2:0]low_sw_r;//每個時鐘周期的上升沿將low_sw信號鎖存到low_sw_r中always@(posedgeclkornegedgerst_n)if(!rst_n)low_sw_r<=3'b111;elselow_sw_r<=low_sw;//當(dāng)寄存器low_sw由1變?yōu)?時,led_ctrl的值變?yōu)楦?,維持一個時鐘周期wire[2:0]led_ctrl=low_sw_r[2:0]&(~low_sw[2:0]);regd1;regd2;regd3;al
7、ways@(posedgeclkornegedgerst_n)if(!rst_n)begind1<=1'b0;d2<=1'b0;d3<=1'b0;endelsebegin//某個按鍵值變化時,LED將做亮滅翻轉(zhuǎn)if(led_ctrl[0])d1<=~d1;if(led_ctrl[1])d2<=~d2;if(led_ctrl[2])d3<=~d3;endassignled_d3=d1?1'b1:1'