0% found this document useful (0 votes)
48 views

Code Mach To Hop Va Led

Uploaded by

Tấn Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Code Mach To Hop Va Led

Uploaded by

Tấn Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

1/ Mạch tổ hợp

Mux 4 sang 1

module mux_4to1(in,sel,out);

input [3:0] in;

input [1:0] sel;

output out;

reg out;

always @(in,sel)

begin

case (sel)

2'b00: out=in[0];

2'b01: out=in[1];

2'b10: out=in[2];

2'b11: out=in[3];

endcase

end

endmodule

MUX 4 sang 1 HL

module mux_4to1_HL(in,sel,en,HL,out);

input [3:0] in;

input [1:0] sel;

input en, HL;

reg temp;

output out;

always @(in,sel,en,HL)

begin

if(en)

begin

case(sel)
2'b00: temp= in[0];

2'b01: temp= in[1];

2'b10: temp= in[2];

2'b11: temp= in[3];

endcase

end

else temp=0;

end

assign out=(HL==1)?temp:~temp;

endmodule

Encoder 4 sang 2

module encoder_4to2(in,out);

input [3:0] in;

output [1:0] out;

reg out;

always @(in)

begin

case (in)

4'b1000: out=2'b11;

4'b0100: out=2'b10;

4'b0010: out=2'b01;

4'b0001: out=2'b00;

endcase

end

endmodule

Encoder 4 sang 2 HL

module encoder_4to2_HL(in,en,HL,out);

input [3:0] in;

input en,HL;
output [1:0] out;

reg [1:0] temp;

always @(in,en,HL)

begin

if(en)

case(in)

4'b0001: temp=2'b00;

4'b0010: temp=2'b01;

4'b0100: temp=2'b10;

4'b1000: temp=2'b11;

endcase

else temp =2'b00;

end

assign out = (HL==1)?temp:~temp;

endmodule

Demux 1 sang 8

module demux_1to8(in,sel,out);

input in;

input [2:0]sel;

output reg [7:0]out;

always @(in,sel)

begin

case (sel)

3'b000:out={7'b0000000,in};

3'b001:out={6'b000000,in,1'b0};

3'b010:out={5'b00000,in,2'b00};

3'b011:out={4'b0000,in,3'b000};

3'b100:out={3'b000,in,4'b0000};

3'b101:out={2'b00,in,5'b00000};
3'b110:out={1'b0,in,6'b000000};

3'b111:out={in,7'b0000000};

endcase

end

endmodule

Decoder 2 sang 4

module decoder_2to4(in,out);

input [1:0] in;

output [3:0] out;

reg out;

always @(in)

begin

case (in)

2'b00: out=4'b1000;

2'b01: out=4'b0100;

2'b10: out=4'b0010;

2'b11: out=4'b0001;

endcase

end

endmodule

decoder 2 sang 4 en

module decoder_2to4_en(in,en,out);

input [1:0] in;

input en;

output [3:0] out;

reg out;

always @(in,en)

begin

out=4'b0000;
if (en)

begin

case (in)

2'b00: out=4'b1000;

2'b01: out=4'b0100;

2'b10: out=4'b0010;

2'b11: out=4'b0001;

endcase

end

end

endmodule

2/ Mạch LED

Đếm 4 bit

module CNT4b(clk,rst,SS,UD,OUT);

input clk,rst,SS,UD;

output reg [3:0] OUT;

always @(posedge clk,rst)

begin

if(rst==1'b1)

OUT=4'b0;

else

if(SS==1)

if(UD==1)

OUT=OUT + 1;

else

OUT=OUT - 1;

else

OUT=OUT;

end
endmodule

LED sáng dần phải sang trái

module LED_SANG_DAN_PST(clk,rst,LED);

input clk,rst;

output reg [7:0] LED;

always @(posedge clk,rst)

begin

if(rst==1)

LED=8'b0000_0001;

else

if(LED==8'b1111_1111)

LED=8'b0000_0001;

else

LED=LED<<1;

LED[0]=1;

end

endmodule

LED sáng dần trái sang phải

module LED_SANG_DAN_TSP(clk,rst,LED);

input clk,rst;

output reg [7:0] LED;

always @(posedge clk,rst)

begin

if(rst==1)

LED=8'b1000_0000;

else

if(LED==8'b1111_1111)

LED=8'b1000_0000;

else
LED=LED>>1;

LED[7]=1;

end

endmodule

LED sáng dịch phải sang trái

module LED_SANG_DICH_PST(clk,rst,LED);

input clk,rst;

output reg [7:0] LED;

always @(posedge clk,rst)

begin

if(rst==1'b1)

LED=8'b0000_0001;

else

LED=LED<<1;

end

endmodule

LED sáng dịch trái sang phải

module LED_SANG_DICH_TSP(clk,rst,LED);

input clk,rst;

output reg [7:0] LED;

always @(posedge clk,rst)

begin

if(rst==1'b1)

LED=8'b1000_0000;

else

LED=LED>>1;

end

endmodule
LED sáng dịch phải sang trái có REPEAT

module LED_SANG_DICH_PST_REPEAT(clk,rst,LED);

input clk,rst;

output reg [7:0] LED;

always @(posedge clk,rst)

begin

if(rst==1'b1)

LED=8'b0000_0001;

else

if(LED==0)

LED=8'b0000_0001;

else

LED=LED<<1;

end

endmodule

LED sáng dịch trái sang phải có REPEAT

module LED_SANG_DICH_TSP_REPEAT(clk,rst,LED);

input clk,rst;

output reg [7:0] LED;

always @(posedge clk,rst)

begin

if(rst==1'b1)

LED=8'b1000_0000;

else

if(LED==0)

LED=8'b1000_0000;

else

LED=LED>>1;

end
endmodule

LED dịch sáng ngoài vào trong

module LED_SANG_DICH_TNV(clk,rst,LED);

input clk,rst;

output reg [7:0] LED;

always @(posedge clk,rst)

begin

if(rst==1)

LED=8'b1000_0001;

else

if(LED==0)

LED=8'b1000_0001;

else

begin

LED[7:4]=LED[7:4]>>1;

LED[3:0]=LED[3:0]<<1;

end

end

endmodule

LED dịch sáng trong ra ngoài

module LED_SANG_DICH_TTR(clk,rst,LED);

input clk,rst;

output reg [7:0] LED;

always @(posedge clk,rst)

begin

if(rst==1)

LED=8'b0001_1000;

else

if(LED==8'b0000_0000)
LED=8'b0001_1000;

else

begin

LED[7:4]=LED[7:4]<<1;

LED[3:0]=LED[3:0]>>1;

end

end

endmodule

LED dịch sáng 2 chế độ ngoài vào trong – trong ra ngoài điều khiển bằng nút MODE

module SANG_DICH_TTR_TNV(clk,rst,SS,MODE,LED);

input clk,rst,SS,MODE;

output reg [7:0] LED;

always @(posedge clk,rst)

begin

if(rst==1)

LED=0;

else if(SS==1)

begin

if(MODE==1)

if(LED==0)

LED=8'b1000_0001;

else

begin

LED[7:4]=LED[7:4]>>1;

LED[3:0]=LED[3:0]<<1;

end

if(MODE==0)

if(LED==0)

LED=8'b0001_1000;
else

begin

LED[7:4]=LED[7:4]<<1;

LED[3:0]=LED[3:0]>>1;

end

end

else LED=LED;

end

endmodule

LED dịch sáng 2 chết độ trái sang phải – phải sang trái điều khiển bằng nút MODE

module LED_SANG_DICH_TSP_PST(clk,rst,SS,MODE,LED);

input clk,rst,SS,MODE;

output reg [7:0] LED;

always @(posedge clk,rst)

begin

if(rst==1)

LED=0;

else if(SS==1)

begin

if(MODE==1)

if(LED==0)

LED=8'b0000_0001;

else

LED=LED<<1;

if(MODE==0)

if(LED==0)

LED=8'b1000_0000;

else

LED=LED>>1;
end

else

LED=LED;

end

endmodule

You might also like