key_debounce key_u1(
.clk(clk),
.rst_n(rst_n),
.key(key),
.key_flag(key_flag),
.key_value(key_value)
);
/*
always@(posedge clk or negedge rst_n) begin
if(!rst_n) begin
key_reg0 <= 1'b0;
key_reg1 <= 1'b0;
end
else begin
key_reg0 <= key_value;
key_reg1 <= key_reg0;
end
end
assign key_edge = (~key_reg0) & key_reg1;
*/
assign wr_en = rxd_done; // 接收端数据和完成信号是同步的,没有延迟时钟周期,直接写第一个数据地址为零?
//根据接收端完成信息后进行赋值写地址
always@(posedge clk or negedge rst_n) begin
if(!rst_n) begin
wraddr <= 8'd0;
end
else if(rxd_done)
wraddr <= wraddr + 1'b1;
else
wraddr <= wraddr;
end
always@(posedge clk or negedge rst_n) begin
if(!rst_n)
reg_txd_done <= 1'b0;
else
reg_txd_done <= key_flag & (~key_value);
end
/*
always@(posedge clk or negedge rst_n) begin
if(!rst_n)
rdaddr <= 8'd0;
else if((key_flag & (~key_value))& txd_done)
rdaddr <= rdaddr + 1'b1;
else
rdaddr <= rdaddr;
end
*/
always@(posedge clk or negedge rst_n) begin
if(!rst_n) begin
txd_en <= 1'b0;
end
else if( key_flag & (~key_value))
txd_en <= 1'b1; //txd_done完成的时间和数据发送完同步不需要延迟一个周期赋值txd_en?
// else if (reg_txd_done)
// txd_en <= 1'b1;
else
txd_en <= 1'b0;
end
reg [7:0] uart_data;
always@(posedge clk or negedge rst ) begin
if(!rst) begin
txd_reg0 <= 1'b0;
txd_reg1 <= 1'b0;
end
else begin
txd_reg0 <= txd_en;
txd_reg1 <= txd_reg0;
end
end
assign start_flag = txd_reg0 & (~txd_reg1);
//计算传输的数据位
always@(posedge clk or negedge rst) begin
if(!rst) begin
data_cnt <= 4'd0;
bit_cnt <= 16'd0;
end
else if(txd_flag) begin
if(bit_cnt < bps_cnt - 1'b1) begin
data_cnt <= data_cnt;
bit_cnt <= bit_cnt + 1'b1;
end
else begin
data_cnt <= data_cnt + 1'b1;
bit_cnt <= 16'd0;
end
end
else begin
data_cnt <= 4'd0;
bit_cnt <= 16'd0;
end
end
//计算flag数据结束位
always@(posedge clk or negedge rst) begin
if(!rst) begin
txd_flag <= 1'b0;
uart_data <= 8'd0;
end
else begin
if(start_flag) begin
txd_flag <= 1'b1;
uart_data <= uart_din;
end
else if((data_cnt == 4'd9)&& (bit_cnt == bps_cnt/2)) begin //检测最后一个数据中间位置,因为传输数据是连续的下一位为数据起始位
txd_flag <= 1'b0;
uart_data <= 8'd0;
end
else begin
txd_flag <= txd_flag;
uart_data <= uart_data;
end
end
end
//并串转换
always@(posedge clk or negedge rst) begin
if(!rst) begin
txd_data <= 1'd1;
// txd_done <= 1'b0;
end
else if(txd_flag) begin
case(data_cnt)
4'd0 : txd_data <= 1'b0;
4'd1 : txd_data <= uart_data[0];
4'd2 : txd_data <= uart_data[1];
4'd3 : txd_data <= uart_data[2];
4'd4 : txd_data <= uart_data[3];
4'd5 : txd_data <= uart_data[4];
4'd6 : txd_data <= uart_data[5];
4'd7 : txd_data <= uart_data[6];
4'd8 : txd_data <= uart_data[7];
4'd9 : txd_data <= 1'b1; //停止位
default : ;
endcase
end
else begin
txd_data <= 1'b1;
// txd_done <= 1'b0;
end
end
always@(posedge clk or negedge rst) begin
if(!rst) begin
txd_done <= 1'b0;
end
else if(data_cnt == 4'd9) begin
txd_done <= 1'b1;
end
else begin
txd_done <= 1'b0;
end
end
endmodule