是用有限状态机,我做过的<br>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>
设计三相磁阻式转子步进电机控制器<br>
产生对A/B/C三相绕组的控制信号<br>
在控制信号控制下分别完成三相双三拍正转和三相六拍反转<br>
做出状态跳变图(通电为1,不通电为0<br>
<br>
输入信号:rst_.复位信号,低电平有效<br>
clk,时钟信号<br>
mode,控制信号,1三相双三拍正转,0三 <br>
相六拍反转<br>
输出信号:outa,a绕组驱动信号<br>
outb,b绕组驱动信号<br>
outc,c绕组驱动信号<br>
<br>
<br>
代码:<br>
module step_engineer(clk,rst_,mode,outa,outb,outc);<br>
input clk,rst_,mode;<br>
output outa,outb,outc;<br>
reg outa,outb,outc;<br>
reg [2:0] state,next_state;<br>
parameter IDLE=3'b000, A=3'b100, B=3'b010, C=3'b001, AB=3'b110, BC=3'b011, CA=3'b101;<br>
always@(posedge clk or negedge rst_)<br>
if(!rst_)<br>
state<=IDLE;<br>
else<br>
state<=next_state;<br>
always@(state or rst_ or mode)<br>
if(!rst_)<br>
next_state=IDEL;<br>
else if(mode)<br>
case(state)<br>
IDLE: next_state=AB;<br>
AB: next_state=BC;<br>
BC: next_state=CA;<br>
CA: next_state=AB;<br>
default: next_state=IDLE;<br>
endcase<br>
else if(!mode)<br>
case(state)<br>
IDLE: next_state=A;<br>
A: next_state=CA;<br>
CA: next_state=C;<br>
C: next_state=BC;<br>
BC: next_state=B;<br>
B: next_state=AB;<br>
AB: next_state=A;<br>
default: next_state=IDLE;<br>
endcase<br>
<br>
else if(!mode)<br>
case(state)<br>
IDLE: next_state=A;<br>
A: next_state=CA;<br>
CA: next_state=C;<br>
C: next_state=BC;<br>
BC: next_state=B;<br>
B: next_state=AB;<br>
AB: next_state=A;<br>
default: next_state=IDLE;<br>
endcase<br>
<br>
else if(!mode)<br>
case(state)<br>
IDLE: next_state=A;<br>
A: next_state=CA;<br>
CA: next_state=C;<br>
C: next_state=BC;<br>
BC: next_state=B;<br>
B: next_state=AB;<br>
AB: next_state=A;<br>
default: next_state=IDLE;<br>
endcase<br>
<br>
else if(!mode)<br>
case(state)<br>
IDLE: next_state=A;<br>
A: next_state=CA;<br>
CA: next_state=C;<br>
C: next_state=BC;<br>
BC: next_state=B;<br>
B: next_state=AB;<br>
AB: next_state=A;<br>
default: next_state=IDLE;<br>
endcase<br>
<br>
else if(!mode)<br>
case(state)<br>
IDLE: next_state=A;<br>
A: next_state=CA;<br>
CA: next_state=C;<br>
C: next_state=BC;<br>
BC: next_state=B;<br>
B: next_state=AB;<br>
AB: next_state=A;<br>
default: next_state=IDLE;<br>
endcase<br>
<br>
else if(!mode)<br>
case(state)<br>
IDLE: next_state=A;<br>
A: next_state=CA;<br>
CA: next_state=C;<br>
C: next_state=BC;<br>
BC: next_state=B;<br>
B: next_state=AB;<br>
AB: next_state=A;<br>
default: next_state=IDLE;<br>
endcase<br>
<br>
else if(!mode)<br>
case(state)<br>
IDLE: next_state=A;<br>
A: next_state=CA;<br>
CA: next_state=C;<br>
C: next_state=BC;<br>
BC: next_state=B;<br>
B: next_state=AB;<br>
AB: next_state=A;<br>
default: next_state=IDLE;<br>
endcase<br>
<br>
else if(!mode)<br>
case(state)<br>
IDLE: next_state=A;<br>
A: next_state=CA;<br>
CA: next_state=C;<br>
C: next_state=BC;<br>
BC: next_state=B;<br>
B: next_state=AB;<br>
AB: next_state=A;<br>
default: next_state=IDLE;<br>
endcase<br>
<br>
else if(!mode)<br>
case(state)<br>
IDLE: next_state=A;<br>
A: next_state=CA;<br>
CA: next_state=C;<br>
C: next_state=BC;<br>
BC: next_state=B;<br>
B: next_state=AB;<br>
AB: next_state=A;<br>
default: next_state=IDLE;<br>
endcase<br>
<br>
else if(!mode)<br>
case(state)<br>
IDLE: next_state=A;<br>
A: next_state=CA;<br>
CA: next_state=C;<br>
C: next_state=BC;<br>
BC: next_state=B;<br>
B: next_state=AB;<br>
AB: next_state=A;<br>
default: next_state=IDLE;<br>
endcase<br>
<br>
else if(!mode)<br>
case(state)<br>
IDLE: next_state=A;<br>
A: next_state=CA;<br>
CA: next_state=C;<br>
C: next_state=BC;<br>
BC: next_state=B;<br>
B: next_state=AB;<br>
AB: next_state=A;<br>
default: next_state=IDLE;<br>
endcase<br>
<br>
else if(!mode)<br>
case(state)<br>
IDLE: next_state=A;<br>
A: next_state=CA;<br>
CA: next_state=C;<br>
C: next_state=BC;<br>
BC: next_state=B;<br>
B: next_state=AB;<br>
AB: next_state=A;<br>
default: next_state=IDLE;<br>
endcase |