一、馬達接腳圖
二、FPGA&motor
三、FPGA&motor實際照
四、注意事項
1.delay時間為Count=1000000以上才有正確的訊號輸出
2.此設定FPGA輸出為con4,若改變請修改UCF檔
3.此步進馬達為TECO ELEC.生產,型號:4H4018S0106
4.此馬達最大的承受電壓為12V,最大承受電流為0.2A
五、程式碼
module R2000(CLK,STRTSTOP,CW,CCW,STOP,outBus);
input CLK;
input STRTSTOP;
input CW;
input CCW;
input STOP;
output [3:0] outBus;
reg Run;
reg [3:0] outBus;
//狀態
parameter
clear=2'b00,
fetch=2'b01,
decod=2'b10;
reg [1:0] current_state;
reg [1:0] next_state;
reg OutCount;
reg Outflag;
reg stateflag;
integer Count;
reg [4:0] P1; //按鍵暫存
reg [1:0] dir;
reg [2:0] turn;
reg keydown;
reg clearKey;
reg [4:0] regs[2:0]; //模擬MIPS R2000用之暫存器
reg [31:0] ir;
reg [5:0] op, funct;
reg [4:0] rs, rt, rd;
reg [15:0] address;
reg [25:0] long_address;
reg [31:0] addro,pc;
reg [7:0] rom[51:0]; // 執行之程式碼位址
always@(negedge CLK)
begin
current_state = next_state;
//程式啟動
if(STRTSTOP == 1)//程式啟動
begin
current_state = clear;
Run=1; //啟動按鍵旗標
Count=1;
OutCount=1;
turn=1;
dir=0;
end
// if(Count==1) //除頻,就是延遲時間,不讓馬達動作太快
// begin
// OutCount=1;
// Count=0;
// end
// else
// OutCount=1;
// Count=Count+1;
end
//將鍵盤訊號存入P1暫存器
always@(CW or CCW or STOP or clearKey)
begin
if(CW) //正轉按下
begin
P1=5'b00001;//按鍵訊號暫存
keydown=1; //按鍵按下旗標,後面才會開始馬達動作
end
else if(CCW) //反轉按下
begin
P1=5'b00010;
keydown=1;
end
else if(STOP)
begin
P1=5'b00011;
keydown=1;
end
else if(clearKey)
begin
P1=5'b00000;
keydown=0;
end
end
always@(negedge CLK)
begin
if(Run==1)//程式啟動按鍵旗標
begin
case(current_state)
//程式碼讀入
clear:
begin
pc = 0;
stateflag=1;//判斷目前旗標是否完成,按下後PC才會做加4的動作
addro = pc; //read #1 code from ROM
next_state=fetch;
end
//載入程式
fetch:
begin
next_state=decod;
ir = {rom[addro+3],rom[addro+2],rom[addro+1],rom[addro]};
op = ir[31:26];
rs = ir[25:21];
rt = ir[20:16];
rd = ir[15:11];
funct = ir[5:0];
address = ir[15:0];
long_address = ir[25:0];
if(stateflag)
pc = pc + 4; //calculate next program counter
stateflag=0;
end
//解譯MIPS
decod:
begin
if(op == 2) // MIPS j (Jump)
begin
pc = long_address;
end
else if(op == 4) // MIPS beq
begin //馬達功能Option
if(rt == regs[rs])
begin
pc = address;
clearKey=0;
end
end
else if(op == 50) //自訂 MIPS IO 指令[ 將P1暫存器值存入regs
begin
if(keydown) //按鍵按下旗標
begin
regs[rs]=P1; //P1值放入RS
end
end
else if(op == 60) // 自訂 MIPS IO 指令[轉動馬達]
begin
Outflag=1;//馬達啟動旗標
end
else if(op == 61) // 自訂 MIPS IO 指令[停止馬達]
begin
Outflag=0;
dir=2'b00;
end
else if(op == 62) // 自訂 MIPS IO 指令[馬達正轉]
begin
if(dir==0)
dir=2'b01;
end
else if(op == 63) // 自訂 MIPS IO 指令[馬達反轉]
begin
if(dir==0)
dir=2'b10;
end
stateflag=1;
addro = pc; //setting updated PC to ROM
next_state=fetch;
end
endcase
end
end
always@(negedge CLK)
begin
if(Outflag) //馬達啟動旗標
begin
if(OutCount) //除頻後的信號輸出
begin
if(dir==2) //反轉(OP=63)
begin
if(turn==1) //ture=馬達四項輸出
begin
outBus=4'b0001; //馬達輸出訊號
turn=5;
end
else if(turn==2)
outBus=4'b0010;
else if(turn==3)
outBus=4'b0100;
else if(turn==4)
outBus=4'b1000;
turn=turn-1; //從5開始減1反轉
end
else if(dir==1) //正轉(OP=62)
begin
if(turn==1)
outBus=4'b0001;
else if(turn==2)
outBus=4'b0010;
else if(turn==3)
outBus=4'b0100;
else if(turn==4)
begin
outBus=4'b1000;
turn=0;
end
turn=turn+1; //dir=0
end
end
$display("turn=%d",turn );
$display("outBus=%d",outBus );
end
end
endmodule
六、SynaptiCAD模擬