http://www.worlduc.com/blog2012.aspx?bid=16824765
/*多功能流水燈shift_led.v
1、單鍵實現0.1秒/位、0.2秒/位、1秒/位元3種速度模式切換;
2、用1位元共陽數碼管顯示速度模式值。
*/
module shift_led(clk_50m,kb1,
led,seg_code,seg_s);
input clk_50m,kb1;
output[3:0] seg_s;
output[7:0] led,seg_code;
reg clk_10,clk_5,clk_1;
reg[7:0] led,seg_code;
led,seg_code,seg_s);
input clk_50m,kb1;
output[3:0] seg_s;
output[7:0] led,seg_code;
reg clk_10,clk_5,clk_1;
reg[7:0] led,seg_code;
//1/5000000分频模块,产生10HZ时钟,供0.1秒/位速度模式用
reg[21:0] cnt1;
always@(posedge clk_50m)
if(cnt1==22'd2499999)
begin
cnt1<=0;
clk_10<=~clk_10;
end
else
cnt1<=cnt1+1'b1;
reg[21:0] cnt1;
always@(posedge clk_50m)
if(cnt1==22'd2499999)
begin
cnt1<=0;
clk_10<=~clk_10;
end
else
cnt1<=cnt1+1'b1;
//1/2分频模块,产生5HZ时钟,供0.2秒/位速度模式用
always@(posedge clk_10)
clk_5<=~clk_5;
//1/10分频模块,产生1HZ时钟,供1秒/位速度模式用
reg[2:0] cnt2;
always@(posedge clk_10)
if(cnt2==3'd4)
begin
cnt2<=0;
clk_1<=~clk_1;
end
else
cnt2<=cnt2+1'b1;
always@(posedge clk_10)
clk_5<=~clk_5;
//1/10分频模块,产生1HZ时钟,供1秒/位速度模式用
reg[2:0] cnt2;
always@(posedge clk_10)
if(cnt2==3'd4)
begin
cnt2<=0;
clk_1<=~clk_1;
end
else
cnt2<=cnt2+1'b1;
//按键识别及模式计数模块
reg A,B;
wire C;
always@(posedge clk_10)
begin
A<=kb1; B<=A;
end
assign C=A&&(~B);
reg[1:0] mode;
always@(posedge C)
if(mode==2'b10 )
mode<=2'b00;
else
mode<=mode+1'b1;
reg A,B;
wire C;
always@(posedge clk_10)
begin
A<=kb1; B<=A;
end
assign C=A&&(~B);
reg[1:0] mode;
always@(posedge C)
if(mode==2'b10 )
mode<=2'b00;
else
mode<=mode+1'b1;
//数码管显示驱动模块
always@ (mode)
begin
if (mode==2'b00)
seg_code <=8'h3F;//显示0,0的段码"h3F"
else if (mode ==2'b01)
seg_code <=8'h06;//显示1,1的段码"h06"
else
seg_code <=8'h5b;//显示2,2的段码"h5b"
end
assign seg_s=4'b1110;//位码
always@ (mode)
begin
if (mode==2'b00)
seg_code <=8'h3F;//显示0,0的段码"h3F"
else if (mode ==2'b01)
seg_code <=8'h06;//显示1,1的段码"h06"
else
seg_code <=8'h5b;//显示2,2的段码"h5b"
end
assign seg_s=4'b1110;//位码
//LED循环移位速度选择模块
reg shift_clk;
always@(mode)
if(mode==2'b00)
shift_clk<=clk_10;
else if(mode==2'b01)
shift_clk<=clk_5;
else
shift_clk<=clk_1;
reg shift_clk;
always@(mode)
if(mode==2'b00)
shift_clk<=clk_10;
else if(mode==2'b01)
shift_clk<=clk_5;
else
shift_clk<=clk_1;
//LED循环移位控制模块
always@(posedge shift_clk)
if(led==8'b00000000||led==8'b10000000)
led<=8'b00000001;
else
led<=led<<1;
always@(posedge shift_clk)
if(led==8'b00000000||led==8'b10000000)
led<=8'b00000001;
else
led<=led<<1;
endmodule
沒有留言:
張貼留言