1.闪烁灯分频(4个交替闪烁) module light(clk,ledout);//clk是一个信号 intput clk; output[7:0] ledout; reg[7:0] ledout; reg[24:0] count; initial ledout=8’h0f; always@(posedge clk) begin count=count+1; if(count==24000000) begin ledout=~ledout; count=0; end end endmoudule 异或:相同为0,相异为1。 clk在18引脚 2.L1灯以0.5秒周期闪烁(即亮0.25秒,灭0.25秒),L2灯以2秒周期闪烁,其余灯灭。 module ttt(ledout,clk); input clk; output [7:0] ledout; reg[7:0] ledout; reg[22:0] count0;//1/24000000*X=0.25;X=6000000; reg[24:0] count1;·· //reg clk1; initial ledout=8’b11111100; always@(posedge clk) begin count0=count0+1; if(count0==6000000) begin ledout[0]=~ledout[0]; count0=0; //clk1=~clk1; end end always@(posedge clk) begin count1=count1+1; if(count1==12000000) begin ledout[1]=~ledout[1]; end end endmodule 3.(老师) L1灯以0.5秒周期闪烁(即亮0.25秒,灭0.25秒),L2灯以2秒周期闪烁,其余灯灭 ### 方法一: module light(clk,ledout) input clk; output[7:0] ledout; reg[7:0] ledout; reg[22:0] count0; reg[24:0] count1; initial ledout=8’hff; always@(posedge clk) begin count0=count0+1; if(count0==23’d6000000) begin ledout[0]=~ledout[0]; // ledout=ledout^8’h01;//与上面的一样 count0=0; end end alway@(posedge clk) begin count1=count1+1; if(count1==25’d12000000) begin ledout[1]=~ledout[1]; count1=0; end end endmodule ### 方法二: module test(clk,ledout); input clk; output[7:0] ledout; reg[7:0] ledout; reg[23:0] count; reg[3:0] flag; initial begin ledout=8’hff; flag=0; end always@(posedge clk) begin count=count+1; if(count==24’d12000000) begin ledout=ledout<<1; flag=flag+1; if(flag>8) begin ledout=8’hfe;//8’11111111 flag=0; end count=0; end end endmodule 4.流水灯逐个闪烁,其余灭 module test(clk,ledout); input clk; output[7:0] ledout; reg[7:0] ledout; reg[23:0] count; reg[3:0] flag; initial ledout=8’hfe;//高到低移8’h7f always@(posedge clk) begin count=count+1; if(count==24’d12000000) begin ledout=(ledout<<1)+1;//+128 //ledout={ledout[0],ledout[7:1]}//{}拼接运算符 flag=flag+1; if(flag==8) begin ledout=8’hfe;//8’h7f flag=0; end count=0; end end endmodule 5.译码器设计 概念:较少的输入控制较多的输出。 译码器可分为:变量译码器和显示译码器。 module code3_8(sw,ledout,seg); input[2:0] sw; output[7:0] ledout; reg[7:0] ledout; reg[7:0] seg; initial begin seg=8’h00; ledout=8’hff; end always@(sw) begin case(sw) 3’b000:ledout=8’b11111110; 3’b001:ledout=8’b11111101; 3’b010:ledout=8’b11111011; 3’b011:ledout=8’b11110111; 3’b100:ledout=8’b11101111; 3’b101:ledout=8’b11011111; 3’b110:ledout=8’b10111111; 3’b111:ledout=8’b01111111; default:ledout=8’b11111111; endcase case(sw) 3’b000:seg=8’h06;//共阴数码管8’h06 3’b001:seg=8’h5b;//8’h5b 3’b010:seg=8’h4f;//8’h4f 3’b011:seg=8’h66;//8’h66 3’b100:seg=8’h6d;//8’h6d 3’b101:seg=8’h7d;//8’h7d 3’b110:seg=8’h07;//8’h07 3’b111:seg=8’h7f;//8’h7f default:seg=8’h00;//8’h00 endcase end endmodule {1} sw2~0(拨码开关)>>引脚71 72 73 seg0>>引脚124 seg1>>123 seg2>>121 seg3>>120 seg4>>119 seg5>>125 seg6>>127 seg7>>122 6.小灯逐个亮,从左到右,从右到左(自己) module test(clk,ledout); input clk; output[7:0] ledout; reg[7:0] ledout; reg[24:0] count; reg flag; //reg[3:0] flag;2^4=16位 1111 1111 1111 1111 initial begin ledout=8’hfe; flag=0; end always@(posedge clk) begin count=count+1; if(count==24000000) begin if(flag==0) ledout=(ledout<<1)+1; else ledout=(ledout>>1)+128; if(ledout==01111111) flag=~flag; if(ledout==11111110) flag=~flag; count=0; end end endmodule 7.模块化流水灯 module ledwater(clk,ledout);//主模块 input clk; output[7:0] ledout; wire clk_temp; divclk(clk,clk_temp); ledmove(clk_temp,ledout); endmodule module divclk(clkin,clkout);//子模块 input clkin; output clkout; reg clkout; reg[24:0] count; always@(posedge clk)//只记数 begin count=count+1; if(count==24000000) begin clkout=~clkout; count=0; end end endmodule module ledmove(clkin,led)//子模块 input clkin; output[7:0] led; reg[7:0] led; reg[3:0] flag; initial led=8’h7f; always@(posedge clkin)//只流水 begin ledout=(ledout>>1)+128; flag=flag+1; if(flag==8) begin led=8’h7f; flag=0; end end endmodule 8.编写代码,实现流水灯功能(要求:每2秒流水一次,只能亮一个小灯,一个小灯从右向左跑,跑到头后,再从左向右跑,如此循环……) module test(clk,ledout); input clk; output[7:0] ledout; wire clk_temp; divclk(clk,clk_temp); ledmove(clk_temp,ledout); endmodule module divclk(clkin,clkout); input clkin; output clkout; reg clkout; reg[25:0] count; always@(posedge clkin) begin count=count+1; if(count==24000000) begin clkout=~clkout; count=0; end end endmodule module ledmove(clkin,led); input clkin; output[7:0] led; reg[7:0] led; reg flag; initial begin led=8’h7f;//从右往左 flag=0; end always@(posedge clkin) begin if(!flag)//flag==0 led=(led>>1)+128; else //flag==1 led=(led<<1)+1; if(led==11111111) begin if(!flag) begin led=8’hfe; flag=~flag; end else begin led=8’h7f; flag=~flag; end end end endmodule


一个好奇的人