【iCore4 雙核心板_FPGA】例程六:觸發器實驗——觸發器的使用

XiaomaGee發表於2017-08-17

實驗現象:

按下按鍵,綠色led亮滅互動;

//--------------------module_rst_n---------------------------//
module trigger_ctrl(
    input clk_25m,
    input rst_n,
    input key,
    output fpga_ledg
);
//--------------------key_in--------------------------------//
parameter ms_10 = 250000;
reg key_r;
reg [17:0]low_cnt;
reg [17:0]hig_cnt;

always @(posedge clk_25m or negedge rst_n)    //按鍵消抖動,提取按鍵狀態
    if (!rst_n)
        begin
            key_r <= 1'd0;
            low_cnt <= 18'd0;
            hig_cnt <= 18'd0;
        end
    else if(key)                                    //檢測按鍵狀態為高時,延時10ms,把按鍵狀態提取出來。
        begin
            low_cnt <= 18'd0;
            if (hig_cnt == ms_10)
                begin
                    key_r <= key;
                    hig_cnt <= hig_cnt;
                end
            else hig_cnt <= hig_cnt + 1'd1;
        end
    else                                                    //按鍵狀態為低時,延時10ms,把按鍵狀態提取出來。
        begin
            hig_cnt <= 18'd0;
            if (low_cnt == ms_10)
                begin
                    key_r <= key;
                    low_cnt <= low_cnt;
                end
            else low_cnt <= low_cnt + 1'd1;
        end
        
wire key_state = key_r;
//--------------------led_ctrl-----------------------------//
reg ledg;

always@(negedge key_state or negedge rst_n)    //按鍵下降沿控制led狀態切換
    if (!rst_n)
        begin
            ledg <= 1'd0;
        end
    else 
        ledg <= ~ledg;
        
assign fpga_ledg = ledg;

//--------------------endmodule-----------------------------//
endmodule

原始碼下載連結:

連結:http://pan.baidu.com/s/1sl3AhvZ 密碼:xz70

iCore4連結:

相關文章