|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
//*******************************FILE HEAD**************************************
//// FILE NAME : uart_transmitter.v
// FUNCTION : UART数据发送模块
// AUTHOR :
// DATE & REVISION :
// COMPANY :
// UPDATE :
//******************************************************************************
`timescale 1ns/1ns
module uart_transmitter
(
input i_rst_n,
input i_clk,
input i_tx_order, //UART数据发送指令
input [7 : 0] i_tx_data, //UART发送的数据
output o_uart_tx_busy, //UART发送模块忙
output o_uart_tx //UART数据输出
);
reg r_tx_order_buf;
reg r_tx_order_rising;
reg [4 : 0] r_div_cnt; //时钟分频计数器
reg r_div_clk; //分频时钟信号
reg [1 : 0] cstate, nstate;
parameter [1 : 0] idle = 2'b00;
parameter [1 : 0] load_data = 2'b01;
parameter [1 : 0] shift_data = 2'b10;
reg [4 : 0] r_shift_cnt;
reg [9 : 0] r_shift;
assign o_uart_tx = r_shift[0];
assign o_uart_tx_busy = (cstate == idle)? 1'b0 : 1'b1;
//*********************************PROCESS**************************************
// FUNCTION :产生分频时钟
//******************************************************************************
always @(posedge i_clk, negedge i_rst_n)
begin
if(1'b0 == i_rst_n)
begin
r_div_cnt <= 4'd0;
r_div_clk <= 1'b0;
end
else
begin
r_div_cnt <= r_div_cnt + 4'd1;
r_div_clk <= r_div_cnt[3];
end
end
//*********************************PROCESS**************************************
// FUNCTION :捕获UART发送指令的上升沿
//******************************************************************************
always @(posedge r_div_clk, negedge i_rst_n)
begin
if(1'b0 == i_rst_n)
begin
r_tx_order_buf <= 1'b0;
r_tx_order_rising <= 1'b0;
end
else
begin
r_tx_order_buf <= i_tx_order;
r_tx_order_rising <= i_tx_order & (~r_tx_order_buf);
end
end
//*********************************PROCESS**************************************
// FUNCTION :UART下个状态转化到现状态
//******************************************************************************
always @(posedge r_div_clk, negedge i_rst_n)
begin
if(1'b0 == i_rst_n)
cstate <= idle;
else
cstate <= nstate;
end
//*********************************PROCESS**************************************
// FUNCTION :UART下个状态转化进程
//******************************************************************************
always @(*)
begin
case(cstate)
idle :
if(1'b1 == r_tx_order_rising)
nstate = shift_data;
else
nstate = idle;
shift_data :
if(4'd9 == r_shift_cnt)
nstate = idle;
else
nstate = shift_data;
default :
nstate = idle;
endcase
end
//*********************************PROCESS**************************************
// FUNCTION :UART在各个现状态下的操作
//******************************************************************************
always @(posedge r_div_clk, negedge i_rst_n)
begin
if(1'b0 == i_rst_n)
begin
r_shift <= 10'b1111111111;
r_shift_cnt <= 4'd0;
end
else
begin
if(idle == cstate && 1'b1 == r_tx_order_rising)
begin
r_shift_cnt <= 4'd0;
r_shift <= {1'b1, i_tx_data, 1'b0};
end
else if(shift_data == cstate)
begin
r_shift_cnt <= r_shift_cnt + 4'd1;
r_shift <= {1'b1, r_shift[9 : 1]};
end
else
begin
r_shift <= 10'b1111111111;
r_shift_cnt <= 4'd0;
end
end
end
endmodule
// END OF uart_transmitter.v FILE *************************************************** |
|