码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-04-27 10:28:22      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:串口发送   timer   assign   sign   put   pos   key   art   color   

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=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串口发送数据

标签:串口发送   timer   assign   sign   put   pos   key   art   color   

原文地址:http://www.cnblogs.com/jeavenwong/p/6772320.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!