FPGA-02 FPGA Key Control LED Lamp

Keyboard is a common control device. In our life, we can see all kinds of buttons. Because of their simple structure and low cost, they are widely used in household appliances, digital products, toys and so on. In this chapter, we will introduce how to use keys to control the brightness and extinction of multiple LED s.

This chapter includes the following parts:

(1) Brief introduction of keys

The key switch is an electronic switch, which belongs to the category of electronic components. There are two kinds of button switches on our development board: the first is the light touch button switch used in this experiment, referred to as the light touch switch. When used, the internal circuit is closed and connected by applying pressure to the operation direction of the switch. When the pressure is withdrawn, the switch is disconnected. The internal structure of the switch is realized by the deformation of the metal shrapnel after the force is exerted. The second kind is self-locking button, which keeps the switch on after the first press, i.e. self-locking, and after the second press, the switch. Disconnect, while the switch button pops up, the power key on the development board is such a switch.

(2) Experiments

Four keys on the development board are used to control four LED lights. When different keys are pressed, four LED lights show different effects.

(3) Hardware design

When the key is not pressed, the output is high, and when pressed, the output is low.

          led[3]                output              F9                          LED       

(4) Programming

The final results are as follows: when no button is pressed, the LED lights are all out; when button 1 is pressed, the LED lights show the water flow effect from right to left; when button 2 is pressed, the LED lights show the water flow effect from left to right; when button 3 is pressed, four LED lights flash at the same time; when button 4 is pressed, the LED lights are all on.

LED in the pipeline effect and scintillation effect in the time interval is 0.2 seconds, so it is necessary to define a 0.2S counter in the program, that is, every 0.2s, state counter plus one. According to the status of the current button, different display modes are selected. The four LED lights in different display modes change with the value of the state counter, thus showing different display effects.

The code is as follows:

//===============================================
//Keyboard controls the state of LED lamp
//===============================================
module key_led(
	input    sys_clk,
	input    sys_rst_n,
	
	input      [3:0] key,
	output reg [3:0] led
);
reg [23:0] counter;
reg [1:0]  led_control;

//0.2s counter 
always @(posedge sys_clk or negedge sys_rst_n)begin
	if(!sys_rst_n)
		counter <= 24'd0;
	else if(counter < 24'd1000_0000)
		counter <= counter + 1'd1;
	else
		counter <= 24'd0;
end

//led status 
always @(posedge sys_clk or negedge sys_rst_n)begin
	if(!sys_rst_n)
		led_control <= 2'b00;
	else if(counter == 24'd1000_0000)
		led_control <= led_control + 1'b1;
	else
		led_control <= led_control;
end

//key 
always @(posedge sys_clk or negedge sys_rst_n)begin
	if(!sys_rst_n)
		led <= 4'b0000;
	else if(key[0] == 0)//Streamlamp effect from right to left when button 1 is pressed
		case(led_control)
			2'b00  : led <= 4'b1000;
			2'b01  : led <= 4'b0100;
			2'b10  : led <= 4'b0010;
			2'b11  : led <= 4'b0001;
			default: led <= 4'b0000;
		endcase
	else if(key[1] == 0)//Streamlamp effect from left to right when button 2 is pressed
		case(led_control)
			2'b00  : led <= 4'b0001;
			2'b01  : led <= 4'b0010;
			2'b10  : led <= 4'b0100;
			2'b11  : led <= 4'b1000;
			default: led <= 4'b0000;
		endcase
	else if(key[2] == 0)//LED flashes when key 3 is pressed
		case(led_control)
			2'b00  : led <= 4'b1111;
			2'b01  : led <= 4'b0000;
			2'b10  : led <= 4'b1111;
			2'b11  : led <= 4'b0000;
			default: led <= 4'b0000;
		endcase
	else if(key[3] == 0)//When the key 4 is pressed, the LED is all bright.
		led <= 4'b1111;
	else
		led <= 4'b0000;//LED goes out when no button is pressed
end
endmodule

The code is divided into three parts. Lines 12 to 20 count the system clock. When the counting time reaches 0.2s, the counter clears and the led_control changes in four states (00, 01, 10, 11). Lines 33 to 65 use case statements to detect the status of keys. When different keys are pressed, LED is assigned different values with the change of led_control. You can find that the counting time of this experiment and the flow lamp experiment is 0.2s. The maximum counter in this experiment can count 9_999_999, while the maximum counter in the flow lamp experiment can count to 10_000_000. In fact, the two experimental counters are counted from 0. This experiment counts from 0 to 9_999_999, which requires 10_000_000 clock cycles, while the system clock is 20 ns, so the counting time is 0.2 s. The running light experiment counts from 0 to 10_000_000, which requires 10_000_001 clock cycles. Therefore, the counting time is actually more than the counting time. 0.2S is 20 ns more.

(5) Download Verification

 

Keywords: Programming

Added by Grizzzzzzzzzz on Wed, 28 Aug 2019 12:32:57 +0300