资源描述:
《基于Verilog的FPGA步进电机控制》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、基于FPGA步进电机控制:电机为四相步进电机,单四拍工作.Speed为电机运行状态输入;Direct为电机转动方向输入;Out为电机控制信号输出;程序控制电机加速减速,采用计数原理,不同计数值控制电机旋转速度,再利用一个另外计数值控制特定速度运行时间,时间到则转到下一速度,设定有最大速度值,加速到最大速度自动进入匀速过程.modulemotor1(speed,rst,out,direct,clk,);//运行状态输入input[1:0]speed;//加速parameterups=2'b01;//减速paramet
2、erdns=2'b10;//停止parameterstop=2'b11;//最大速度parameteraver=4'b1000;//电机运行方向,复位,时钟输入inputdirect,rst,clk;//控制电机信号输出output[3:0]out;reg[3:0]out;//加速速度转换计数reg[3:0]count1;//减速速度转换计数reg[3:0]count2;//加速速度数字代表reg[3:0]countup;//减速速度数字代表reg[3:0]countdn;//特定速度运行时间计数reg[6:0]c
3、ount;//特定速度运行时间到标志parametercountmax=7'b1111111;always@(posedgeclkornegedgerst)if(!rst)begincountup<=4'b1111;count1<=4'b1111;count2<=4'b1000;countdn<=4'b1000;count<=7'b0;endelseif((speed==ups)
4、
5、(speed==dns))begincount<=count+1;if(speed==ups)beginif(count1==0)co
6、unt1<=countup;if(count1!=0)count1<=count1-1;if((countup>=4'b1001)&&(count==countmax))begincountup<=countup-1;count1<=countup-1;count=7'b0;endif((countup==aver)&&(count==countmax))begincount1<=aver;countdn<=aver;count<=7'b0;endendelseif(speed==dns)begincount1=4
7、'b1111;if(count2==0)count2<=countdn;if(count2!=0)count2<=count2-1;if((countdn<=4'b1110)&&(count==countmax))begincountdn<=countdn+1;count2<=countdn+1;count<=7'b0;endif((countdn==4'b1111)&&(count==countmax))begincount2<=4'b1111;count<=7'b0;endendendelseif(speed=
8、=stop)begincountup<=4'b1111;countdn<=aver;count1<=4'b1111;count<=7'b0;endalways@(count1orcount2orrst)if(!rst)out<=4'b1001;elseif((count1==0)
9、
10、(count2==0))beginif(speed==stop)out<=out;elseif((speed==ups)
11、
12、(speed==dns))beginif(direct==1)out<={out[2:0],out[3]};el
13、seif(direct==0)out<={out[0],out[3:1]};endendendmodule