FPGA (2) basic syntax -- button control led (alway@ statement)

Table of contents

1. module file name (port)

2. Declare keywords

3. The always@ statement

the code


1. module file name (port)

 Note: It is best to develop a habit here, only declare pin variables in brackets after the file name, and define input and output, keyword types, etc. later.

//verilog基础语法(always)
module my_and(key1, led1);
//注:一旦在module后面的括号中指定了input/output,后面就不能再增加类型了,
//所以最好不要在括号内定义input/output类型

2. Declare keywords

The input line key1 and the output register led1 are respectively defined here.

input wire key1;		//key1:设置输入、wire型(输入不能用wire型)
output reg led1;		//led1:设置输出,reg型(always语句中需要reg类型变量)

3. The always@ statement

Note: Only reg-type variables can be assigned in the always@ statement (the previous assign can no longer be directly assigned)

always@(key1)			//变量变化一次就执行一次(key1变化)
	begin 
		led1 = key1;	//按键按下执行
		//注:这里led1是reg型,reg型不能再用assign连线直接赋值
	end

the code

//verilog基础语法(always)
module my_and(key1, led1);
//注:一旦在文件名后面的括号中指定了input/output,后面就不能再增加类型了,
//所以最好不要在括号内定义input/output类型

input wire key1;		//key1:设置输入、wire型(输入不能用wire型)
output reg led1;		//led1:设置输出,reg型(always语句中需要reg类型变量)

always@(key1)			//变量变化一次就执行一次(key1变化)
	begin 
		led1 = key1;	//按键按下执行
		//注:这里led1是reg型,reg型不能再用assign连线直接赋值
	end
	
endmodule

Execution effect: press the button, the light is on.

Guess you like

Origin blog.csdn.net/great_yzl/article/details/121374915