本次设计使用EGO1开发板,使用软件为vivado,代码是Verilog语言,实现具有多功能蓝牙密码锁的设计。功能包括,通过APP无线输入密码、开锁、修改密码、连续输错密码后锁定40秒并报警。
部分代码:
//串口接收模块程序
module uart_rx(
input rxd,
input clk,
input clear,
input d,
output receive_ack,
output reg[7:0] data_i, //接收的信息,8位ASCII
output reg [3:0] num //接收的个数
);
//串口接收状态机分为三个状态:等待、接收、接收完成
localparam IDLE = 0,
RECEIVE = 1,
RECEIVE_END = 2;
reg[3:0] current_state, next_state; // 状态机变量
reg[4:0] count;
always @(posedge clk)
current_state <= next_state;
always @(*)
begin
next_state = current_state;
case(current_state)
IDLE: if (!rxd) next_state = RECEIVE;
RECEIVE: begin if (count == 7) next_state = RECEIVE_END; end
RECEIVE_END: begin next_state = IDLE; end
default: next_state = IDLE;
endcase
end
always @(posedge clk)
begin
if (current_state == RECEIVE)
count <= count + 1;
else if (current_state == IDLE | current_state == RECEIVE_END)
count <= 0;
end
always @(posedge clk)
begin
if (current_state == RECEIVE)
begin
data_i[6:0] <= data_i[7:1];
data_i[7] <= rxd;
end
end
assign receive_ack = (current_state == RECEIVE_END)?1:0;
//对接收的信息进行记数
always @(posedge clk)
if(clear==1)
num<=1;
else if(receive_ack==1&&num<=4'd12&&d==0)
num<=num+1;
else
num=num;
endmodule
直接上实物图,欢迎探讨,代码后续补充。
EGO1开发板:
APP界面:
输入密码正确,开锁。
连续三次输入错误,进入锁定,40s内无法进行操作。