//需 Import pin assignments DE2_115_pin_assignments
module Encoder_if(
//input CLOCK_50, // 50 MHz clock
//input [3:0] KEY, // Pushbutton[3:0]
input [17:0] SW, // Toggle Switch[17:0]
//output [6:0] HEX0,HEX1,HEX2,HEX3,HEX4,HEX5,HEX6,HEX7, // Seven Segment Digits
output [8:0] LEDG, // LED Green
output [17:0] LEDR // LED Red
// inout [35:0] GPIO_0,GPIO_1, // GPIO Connections
// LCD Module 16X2
/*
output LCD_ON, // LCD Power ON/OFF
output LCD_BLON, // LCD Back Light ON/OFF
output LCD_RW, // LCD Read/Write Select, 0 = Write, 1 = Read
output LCD_EN, // LCD Enable
output LCD_RS, // LCD Command/Data Select, 0 = Command, 1 = Data
inout [7:0] LCD_DATA, // LCD Data bus 8 bits
input [2:0] mess, // MESSAGE STATUS (see lcd_test)
input [1:0] isServer // SERVER STATUS (see lcd_test)
*/
);
// All inout port turn to tri-state
//assign GPIO_0 = 36'hzzzzzzzzz;
//assign GPIO_1 = 36'hzzzzzzzzz;
// turn LCD ON
//assign LCD_ON = 1'b1;
//assign LCD_BLON = 1'b1;
// blank unused 7-segment digits
// blank unused 7-segment digits
//assign HEX0 = 7'b111_1111;
//assign HEX1 = 7'b111_1111;
//assign HEX2 = 7'b111_1111;
//assign HEX3 = 7'b111_1111;
//assign HEX4 = 7'b111_1111;
//assign HEX5 = 7'b111_1111;
//assign HEX6 = 7'b111_1111;
//assign HEX7 = 7'b111_1111;
assign LEDR=SW;
M_encoder( LEDG[3:0],SW[15:0],SW[17] );
endmodule
//-----------------------------------------------------
// Design Name : encoder_using_if
// File Name : encoder_using_if.v
// Function : Encoder using If
// Coder : Deepak Kumar Tala
//-----------------------------------------------------
module M_encoder(
binary_out , // 4 bit binary output
encoder_in , // 16-bit input
enable // Enable for the encoder
);
//-----------Output Ports---------------
output [3:0] binary_out ;
//-----------Input Ports---------------
input enable ;
input [15:0] encoder_in ;
//------------Internal Variables--------
reg [3:0] binary_out ;
//-------------Code Start-----------------
always @ (enable or encoder_in)
begin
binary_out = 0;
if (enable) begin
if (encoder_in[1]==1'b1) begin
binary_out = 1;
end if (encoder_in[2] == 1'b1) begin
binary_out = 2;
end if (encoder_in[3] == 1'b1) begin
binary_out = 3;
end if (encoder_in[4] == 1'b1) begin
binary_out = 4;
end if (encoder_in[5] == 1'b1) begin
binary_out = 5;
end if (encoder_in[6] == 1'b1) begin
binary_out = 6;
end if (encoder_in[7] == 1'b1) begin
binary_out = 7;
end if (encoder_in[8] == 1'b1) begin
binary_out = 8;
end if (encoder_in[9] == 1'b1) begin
binary_out = 9;
end if (encoder_in[10] == 1'b1) begin
binary_out = 10;
end if (encoder_in[11] == 1'b1) begin
binary_out = 11;
end if (encoder_in[12] == 1'b1) begin
binary_out = 12;
end if (encoder_in[13] == 1'b1) begin
binary_out = 13;
end if (encoder_in[14] == 1'b1) begin
binary_out = 14;
end if (encoder_in[15] == 1'b1) begin
binary_out = 15;
end
end
end
endmodule
https://www.youtube.com/watch?v=P5yuUIx2W-c