首页 > 代码库 > 利用状态机(FSM)进行简单的uart串口发送数据

利用状态机(FSM)进行简单的uart串口发送数据

module uart_tx(clk,rst_n,key,txd);

input clk;
input rst_n;
input key;
output reg txd;

reg[3:0] state;
reg[15:0] bit_timer;
wire[7:0] tx_data;
assign tx_data=http://www.mamicode.com/8h5a; 


parameter s_idle=4d0;
parameter s_start=4d1;
parameter s_bit0=4d2;
parameter s_bit1=4d3;
parameter s_bit2=4d4;
parameter s_bit3=4d5;
parameter s_bit4=4d6;
parameter s_bit5=4d7;
parameter s_bit6=4d8;
parameter s_bit7=4d9;
parameter s_stop=4d10;

always@(posedge clk or negedge rst_n)
begin
    if(!rst_n)
    begin
         state<=s_idle;
         bit_timer<=16d0;
         txd<=1b1; 
    end
    else
    begin
        case(state)
        
        s_idle:
        begin
            txd<=1b1;
            if(!key)
            state<=s_start;
            else
            state<=state;
        end
        
        
        s_start:
        begin
            txd<=1b0;
            if(bit_timer==16d5208)
            begin
            state<=s_bit0;
            bit_timer<=16d0;
            end
            else
            begin
            state<=state;
            bit_timer=bit_timer+16d1;
            end
        end
        
        s_bit0:
        begin
            txd<=tx_data[0];
            if(bit_timer==16d5208)
            begin
            state<=s_bit1;
            bit_timer<=16d0;
            end
            else
            begin
            state<=state;
            bit_timer=bit_timer+16d1;
            end
        end
        
         s_bit1:
        begin
            txd<=tx_data[1];
            if(bit_timer==16d5208)
            begin
            state<=s_bit2;
            bit_timer<=16d0;
            end
            else
            begin
            state<=state;
            bit_timer=bit_timer+16d1;
            end
        end
        
        
        s_bit2:
        begin
            txd<=tx_data[2];
            if(bit_timer==16d5208)
            begin
            state<=s_bit3;
            bit_timer<=16d0;
            end
            else
            begin
            state<=state;
            bit_timer=bit_timer+16d1;
            end
        end
        
        
         s_bit3:
        begin
            txd<=tx_data[3];
            if(bit_timer==16d5208)
            begin
            state<=s_bit4;
            bit_timer<=16d0;
            end
            else
            begin
            state<=state;
            bit_timer=bit_timer+16d1;
            end
        end
        
        
         s_bit4:
        begin
            txd<=tx_data[4];
            if(bit_timer==16d5208)
            begin
            state<=s_bit5;
            bit_timer<=16d0;
            end
            else
            begin
            state<=state;
            bit_timer=bit_timer+16d1;
            end
        end
        
        
         s_bit5:
        begin
            txd<=tx_data[5];
            if(bit_timer==16d5208)
            begin
            state<=s_bit6;
            bit_timer<=16d0;
            end
            else
            begin
            state<=state;
            bit_timer=bit_timer+16d1;
            end
        end
        
        
         s_bit6:
        begin
            txd<=tx_data[6];
            if(bit_timer==16d5208)
            begin
            state<=s_bit7;
            bit_timer<=16d0;
            end
            else
            begin
            state<=state;
            bit_timer=bit_timer+16d1;
            end
        end
        
        
         s_bit7:
        begin
            txd<=tx_data[7];
            if(bit_timer==16d5208)
            begin
            state<=s_stop;
            bit_timer<=16d0;
            end
            else
            begin
            state<=state;
            bit_timer=bit_timer+16d1;
            end
        end
        
         s_stop:
        begin
            txd<=1b1;
            if(bit_timer==16d5208)
            begin
            state<=s_idle;
            bit_timer<=16d0;
            end
            else
            begin
            state<=state;
            bit_timer=bit_timer+16d1;
            end
        end
        
        default:
        begin
        state<=s_idle;
        end
        
        
        endcase
    end
        
    
end
  
endmodule

 

利用状态机(FSM)进行简单的uart串口发送数据