这个作业没有完全按照51FPGA的要求做,之前一直想能控制,
功能:实现3位拨码开关控制LED的样式,包含闪烁灯,流水灯以及花样灯(花样灯参考有51的程序)
视屏:
视频地址:http://union.bokecc.com/flash/player.swf?vid=6BC2B1CD78955E89&siteid=290666218ACBA694&playerid=EEA982EE6B20F4D1&playertype=1
代码如下:
module led_z(clk,
rst_n,
LED,
SW);
input clk; //50MHZ
input rst_n; //系统复位,低电平有效
input [2:0]SW; //3位拨码开关输入
output[7:0] LED; //8位LED_R输出
reg[7:0] LED_R; //8位LED_R输出寄存器
reg[24:0] delay; //LED_R计数器
//-----------------------------------
reg [7:0] flicker;
reg [7:0] run;
reg [7:0] water;
reg [4:0] state;
//--------------延时计数器-------------------------
always @(posedge clk or negedge rst_n)
if(!rst_n) delay<=25'd0;
else if(delay==25'd24999999)
begin
delay<=25'd0;
end
else begin
delay<=delay+1'd1;
end
//---------------流水灯程序-------------------------
always @(posedge clk or negedge rst_n)
if(!rst_n) water <= 8'b0;
else if(delay==25'd2499999)
begin
case(state)
5'b00000: water=8'b11111111;
5'b00001: water=8'b00000000;
5'b00010: water=8'b10000000;
5'b00011: water=8'b11000000;
5'b00100: water=8'b11100000;
5'b00101: water=8'b11110000;
5'b00110: water=8'b11111000;
5'b00111: water=8'b11111100;
5'b01000: water=8'b11111110;
5'b01001: water=8'b11111111;
5'b01010: water=8'b00000000;
5'b01011: water=8'b11111111;
5'b01100: water=8'b00000001;
5'b01101: water=8'b00000010;
5'b01110: water=8'b00000100;
5'b01111: water=8'b00001000;
5'b10000: water=8'b00010000;
5'b10001: water=8'b00100000;
5'b10010: water=8'b01000000;
5'b10011: water=8'b10000000;
5'b10100: water=8'b11111111;
5'b10101: water=8'b00000000;
5'b10110: water=8'b11111111;
5'b10111: water=8'b10000001;
5'b11000: water=8'b11000011;
5'b11001: water=8'b11100111;
5'b11010: water=8'b11111111;
5'b11011: water=8'b00011000;
5'b11100: water=8'b00111100;
5'b11101: water=8'b01111110;
5'b11110: water=8'b11111111;
5'b11111: water=8'b00000000;
endcase
state=state+1;
end
//-------------跑马灯程序---------------------
always @(posedge clk or negedge rst_n)
if(!rst_n) run<=8'h7f;
else
begin
if(delay==25'd2499999)
run<={run[0],run[7:1]};
else
run<=run;
end
//----------------闪烁灯程序----------------------
always @(posedge clk or negedge rst_n)
begin
if(!rst_n) flicker <= 8'hff;
else
begin
if(delay == 25'd249999)
flicker <= ~flicker;
else
flicker <= flicker;
end
end
//----------------LED控制--------------------------
always @(posedge clk)
begin
case(SW[2:0])
3'b0: LED_R<=8'hff;
3'b001: LED_R<=flicker;
3'b010: LED_R<=run;
3'b100: LED_R<=water;
endcase
end
assign LED=LED_R;
endmodule