Universal Asynchronous Receiver Transmitter-IT CAN WORKING AS A DUPLEX AND Full Duplex. in Uart Some Parameters Are Set by Users Are
Universal Asynchronous Receiver Transmitter-IT CAN WORKING AS A DUPLEX AND Full Duplex. in Uart Some Parameters Are Set by Users Are
Universal Asynchronous Receiver Transmitter-IT CAN WORKING AS A DUPLEX AND Full Duplex. in Uart Some Parameters Are Set by Users Are
FULL DUPLEX.
BAUD RATE:- IT IS DEFINES HOW MUCH RATE THE SERIAL DATA IS TRANSMITTED . LIKE – 19200 BAUD
RATE MEAN IT IS TRANSMITTED 19200 BITS PER SECOND.
PARITY BIT IS CALCULATED USING XOR OPERATION AND SENT AFTER THE DATA BITS.
STOP BITS ITS VALUE IS LOGIC 1 AND IT IS SET BY USER HOW MUCH BIT IS USED FOR REPERSENTAION
OF STOP BIT.
START BIT – WHEN TRANSITION FROM HIGH TO LOW. This first transition indicates the
start bit. Once the beginning of the start bit is found, the FPGA waits for one half of a bit period. This
ensures that the middle of the data bit gets sampled. From then on, the FPGA just needs to wait one
bit period (as specified by the baud rate) and sample the rest of the data. The figure below shows
how the UART receiver works inside of the FPGA. First a falling edge is detected on the serial data
line. This represents the start bit. The FPGA then waits until the middle of the first data bit and
samples the data. It does this for all eight data bits.
out_rx_dv
In_rx_clk
UART_RX
In_rx_serial Out_rx_byte
module UART_rs232_rx (Clk,Rst_n,RxEn,RxData,RxDone,Rx,Tick,NBits); //Define my module
as UART_rs232_rx
output [7:0]RxData; //Define 8 bits output (this will eb the 1byte received data)
//Variabels used for state machine...
parameter IDLE = 1'b0, READ = 1'b1; //We haev 2 states for the State Machine state 0
and 1 (READ adn IDLE)
reg [1:0] State, Next; //Create some registers for the states
reg read_enable = 1'b0; //Variable that will enable or NOT the data in read
reg start_bit = 1'b1; //Variable used to notify when the start bit was detected
(first falling edge of RX)
reg RxDone = 1'b0; //Variable used to notify when the data read process is done
reg [4:0]Bit = 5'b00000; //Variable used for the bit by bit read loop (in this case 8
bits so 8 loops)
reg [3:0] counter = 4'b0000; //Counter variable used to count the tick pulses up to
16
reg [7:0] Read_data= 8'b00000000; //Register where we store the Rx input bits before
assigning it to the RxData output
reg [7:0] RxData; //We register the output as well so we store the value
///////////////////////////////STATE MACHINE////////////////////////////////
//////////////////////////////////////////////////////////////////////////// ////////
///////////////////////////Reset//////////////////////////////////// ////////////////
////////////////////////////////////////////////////////////
always @ (posedge Clk or negedge Rst_n) //It is good to always have a reset always
begin if (!Rst_n) State <= IDLE; //If reset pin is low, we get to the initial state
which is IDLE
end ////////////////////////////////////////////////////////////////////////////
////////////////////////////Next step decision//////////////////////////////
//////////////////////////////////////////////////////////////////////////// /*This
is easy. Each time "State or Rx or RxEn or RxDone" will change their value we decide
which is the next step. - Obviously we get to IDLE only when RxDone is high meaning
that the read process is done. - Also, while we are into IDEL, we get to READ state
only if Rx input gets low meaning we've detected a start bit*/
case(State)
IDLE: if(!Rx & RxEn) Next = READ; //If Rx is low (Start bit detected) we start the
read process
READ: if(RxDone) Next = IDLE; //If RxDone is high, than we get back to IDLE and wait
for Rx input to go low (start bit detect)
endcase
end ////////////////////////////////////////////////////////////////////////////
///////////////////////////ENABLE READ OR NOT///////////////////////////////
////////////////////////////////////////////////////////////////////////////
case (State)
READ: begin read_enable <= 1'b1; //If we are in the Read state, we enable the read
process so in the "Tick always" we start getting the bits
end
IDLE: begin read_enable <= 1'b0; //If we get back to IDLE, we desable the read
process so the "Tick always" could continue without geting Rx bits
end
endcase
if (read_enable) begin
RxDone <= 1'b0; //Set the RxDone register to low since the process is still going
counter <= counter+1; //Increase the counter by 1 with each Tick detected
if ((counter == 4'b1000) & (start_bit)) //Counter is 8? Then we set the start bit to
1.
//We make a loop (8 loops in this case) and we read all 8 bits
end
if ((counter == 4'b1111) & (Bit == NBits) & (Rx)) //Then we count to 16 once again
and detect the stop bit (Rx input must be high)
start_bit <= 1'b1; //We reset all values for next data input and set RxDone to high
end
end
end
end
End
////////////////////////////////////////////////////////////////////
module new_Rx_baud(clk,Rst,Rx_baud);
parameter S0 = 2'b00;
parameter S1 = 2'b01;
parameter S2 = 2'b10;
parameter S3 = 2'b11;
output Rx_baud;
input Rst;
input clk;
reg Rx_baud;
reg [1:0]Pr_state;
reg [1:0]Nx_state;
integer count;
always@(Rst or count or Pr_state)
begin
case(Pr_state)
S0:
begin
if(Rst)
begin
Nx_state = S0;
end
else
begin
Nx_state = S1;
end
end
S1:
begin
if((count >= 0) && (count <= 81))
begin
Nx_state = S1;
end
else
begin
Nx_state = S2;
end
end
S2:
begin
if((count > 81) && (count < 163))
begin
Nx_state = S2;
end
else
begin
Nx_state = S0;
end
end
default:
begin
Nx_state = S0;
end
endcase
end
always@(posedge clk or posedge Rst)
begin
if(Rst)
begin
count = 0;
Pr_state = S0;
end
else
begin
Pr_state = Nx_state;
case(Nx_state)
S0:
begin
count = 0;
end
S1:
begin
Rx_baud = 1;
count = count + 1;
end
S2:
begin
Rx_baud = 0;
count = count + 1;
end
default:
begin
count = 0;
Rx_baud = 1;
end
endcase
end
end
endmodule