| 
 | 
 
 本帖最后由 fpgaw 于 2010-11-18 15:58 编辑  
 
米勒型有限状态机实现的串行口发送源程序 
module s_tx(clk,en,dain,txd); 
input clk,en; 
input[7:0] dain; 
output txd; 
reg [7:0] da_temp; 
reg txd; 
reg [3:0] state; 
parameter swait=4'b0000, star=4'b0001,  s1=4'b0010,  s2=4'b0011, 
 s3=4'b0100,  s4=4'b0101,  s5=4'b0110,  s6=4'b0111,s7=4'b1000, 
 s8=4'b1001, stop=4'b1010; 
always @(posedge en) 
      da_temp<=dain; 
always @(posedge clk) 
    if (!en) 
       begin 
         state<=swait; 
         txd<=1; 
       end 
    else 
       case(state) 
         swait: begin state<=star;txd<=1; end 
         star: begin state<=s1; txd<=0; end 
         s1: begin state<=s2; txd<=da_temp[7]; end 
         s2: begin state<=s3; txd<=da_temp[6]; end 
         s3: begin state<=s4; txd<=da_temp[5]; end 
         s4: begin state<=s5; txd<=da_temp[4]; end 
         s5: begin state<=s6; txd<=da_temp[3]; end 
         s6: begin state<=s7; txd<=da_temp[2]; end 
         s7: begin state<=s8; txd<=da_temp[1]; end 
         s8: begin state<=stop; txd<=da_temp[0]; end 
         stop: begin state<=stop; txd<=1; end 
       endcase 
endmodule |   
 
 
 
 |