實驗現象:
iCore1s 雙核心板上與FPGA相連的三色LED(PCB上標示為FPGA·LED),按鍵按下紅燈點亮,鬆開按鍵紅燈熄滅。
核心原始碼:
module KEY( input CLK_12M, input FPGA_KEY, output FPGA_LEDR, output FPGA_LEDG, output FPGA_LEDB ); //----------------------rst_n-----------------------// //產生復位訊號 reg rst_n; reg [3:0]cnt_rst; always@(posedge CLK_12M) if(cnt_rst==4'd10) begin rst_n <= 1'd1; cnt_rst <= 4'd10; end else cnt_rst <= cnt_rst + 1'd1; //-----------------------KEY_CLK--------------------// //將按鍵的按下與鬆開轉換為KEY_CLK,按下後鬆開為上升沿 reg KEY_CLK; reg [7:0]cnt_key0; reg [7:0]cnt_key1; always@(posedge CLK_12M or negedge rst_n ) begin if(!rst_n) begin KEY_CLK <= 1'd0; cnt_key0 <= 8'd0; cnt_key1 <= 8'd0; end else if(!FPGA_KEY) begin cnt_key0 <= cnt_key0 + 1'd1; if(cnt_key0==8'd200)//消抖 begin if(!FPGA_KEY) begin KEY_CLK <= 1'd0; cnt_key0 <= 8'd0; end end end else if(FPGA_KEY) begin cnt_key1 <= cnt_key1 + 1'd1; if(cnt_key1==8'd200)//消抖 begin if(FPGA_KEY) begin KEY_CLK <= 1'd1; cnt_key1 <= 8'd0; end end end end //-----------------------led-------------------------// reg [1:0]led_state; reg ledr,ledg,ledb; always@(posedge KEY_CLK or negedge rst_n) begin if(!rst_n) begin led_state <= -2'd1;//初始化使燈熄滅 ledr <= 1'd1; ledg <= 1'd1; ledb <= 1'd1; end else begin led_state <= led_state + 1'd1; if(led_state > 2'd2) begin led_state <= 2'd0; end case(led_state) 2'd0: //紅燈亮 begin ledr <= 1'd0; ledg <= 1'd1; ledb <= 1'd1; end 2'd1: //綠燈亮 begin ledr <= 1'd1; ledg <= 1'd0; ledb <= 1'd1; end 2'd2: //藍燈亮 begin ledr <= 1'd1; ledg <= 1'd1; ledb <= 1'd0; end default : //燈熄滅 begin ledr <= 1'd1; ledg <= 1'd1; ledb <= 1'd1; end endcase end end assign FPGA_LEDR = ledr; assign FPGA_LEDG = ledg; assign FPGA_LEDB = ledb; //--------------------endmodule------------------// endmodule
程式碼包下載:
連結:http://pan.baidu.com/s/1kUWAJC7 密碼:13jp