murter 发表于 2011-8-13 06:09:56

CPLD 控制步进电机加减速

module bjj(
        Reset,
        Clock_8MHz,
        in,in1,in2,
        Pulse,
        cc,cs,led
);
input        Reset,in,in1,in2;
input        Clock_8MHz;
output        Pulse,cc,cs;
output led;
reg        Acceleration;//加速度
reg        Speed;//速率,速度
reg        Sum;//总数

wire        F_65536Hz;
reg        WIRE_1;
wire        F_1Hz;
reg        WIRE_4;
reg        WIRE_5;
reg        WIRE_6;
reg cc;
reg m0,m2;
wire led;
assign led=WIRE_6;
always @(posedge Clock_8MHz )
begin
if(in1==0&&m0==1)begin
m0=1'b0;
cc<=~cc;
Acceleration<=20000;
Speed<=50;
Sum<=2000000;
end
if(in1==1&&m0==0)m0=1'b1;
end
/////////////////////////////////////////

reg CNT;

//全局时钟经过100分频后得到65536Hz时钟信号
always @(posedge Clock_8MHz or negedge in)
begin
//if(Reset == 1'b0)
      if(in == 1'b0)
    CNT <= 23'b0;
else
begin
    if(CNT < 23'b11111111111111111111111)   //实际中的分频值
      CNT <= CNT + 23'b1;
    else
      CNT <= 23'b0;
end
end
/*以下是实际系统的值*/
assign F_65536Hz = CNT;
assign F_1Hz   = CNT;
///////////////////////////////////////////////////


always @(posedge F_65536Hz or negedge in)
begin
    if(in == 1'b0)
    WIRE_1<=16'b0;
else begin if(WIRE_1 < 16'b1111111111111111)
    WIRE_1 <= WIRE_1 + 16'b1;
else
    WIRE_1 <= 16'b0;
    end
end
/////////////////////////////////////////////////


always @(Reset,WIRE_1,WIRE_4,in)
begin
//if(Reset == 1'b0)
      if(in == 1'b0)
    WIRE_4 <= 16'b0;
else
begin
    if(WIRE_1 == 1'b1)
      WIRE_4 <= 1'b1;
    else
      WIRE_4 <= 1'b0;

    if(WIRE_1 == 2'b10)
      WIRE_4 <= 1'b1;
    else
      WIRE_4 <= 1'b0;

    if(WIRE_1 == 3'b100)
      WIRE_4 <= 1'b1;
    else
      WIRE_4 <= 1'b0;

    if(WIRE_1 == 4'b1000)
      WIRE_4 <= 1'b1;
    else
      WIRE_4 <= 1'b0;

    if(WIRE_1 == 5'b10000)
      WIRE_4 <= 1'b1;
    else
      WIRE_4 <= 1'b0;

    if(WIRE_1 == 6'b100000)
      WIRE_4 <= 1'b1;
    else
      WIRE_4 <= 1'b0;

    if(WIRE_1 == 7'b1000000)
   WIRE_4 <= 1'b1;
    else
   WIRE_4 <= 1'b0;

    if(WIRE_1 == 8'b10000000)
   WIRE_4 <= 1'b1;
    else
      WIRE_4 <= 1'b0;

    if(WIRE_1 == 9'b100000000)
      WIRE_4 <= 1'b1;
    else
   WIRE_4 <= 1'b0;

    if(WIRE_1 == 10'b1000000000)
      WIRE_4 <= 1'b1;
    else
   WIRE_4 <= 1'b0;

    if(WIRE_1 == 11'b10000000000)
      WIRE_4 <= 1'b1;
    else
      WIRE_4 <= 1'b0;

    if(WIRE_1 == 12'b100000000000)
      WIRE_4 <= 1'b1;
    else
      WIRE_4 <= 1'b0;

    if(WIRE_1 == 13'b1000000000000)
      WIRE_4 <= 1'b1;
    else
      WIRE_4 <= 1'b0;

    if(WIRE_1 == 14'b10000000000000)
      WIRE_4 <= 1'b1;
    else
      WIRE_4 <= 1'b0;

    if(WIRE_1 == 15'b100000000000000)
      WIRE_4 <= 1'b1;
    else
      WIRE_4 <= 1'b0;

    if(WIRE_1 == 16'b1000000000000000)
      WIRE_4 <= 1'b1;
    else
      WIRE_4 <= 1'b0;

end

end
/////////////////////////////////////////////

initial
begin
WIRE_5 <= Speed;
end

always @(posedge F_1Hz or negedge in)
begin
    if(in == 1'b0)
//if(Reset == 1'b0)
    WIRE_5 <= 16'b0;
else
begin
    if(Acceleration == 1'b0)//加速度为正
    begin
      if((WIRE_5 + Acceleration) < 16'b1111111111111111)
      //加速,且速度不超过65535
      WIRE_5 <= WIRE_5 + Acceleration;
      else
      //速度超过65535,直接固定在65535
      WIRE_5 <= 16'b1111111111111111;
    end
      
    else if(Acceleration == 1'b1) //加速度为负
    begin
      if((WIRE_5 - Acceleration) > 16'b0)
      //减速,且速度不小于0
      WIRE_5 <= WIRE_5 - Acceleration;
      else
      //减速,且速度小于0,直接固定在0,即停止
      WIRE_5 <= 16'b0;
    end
end
end
///////////////////////////////////////////////////////

reg pulse_1;
reg cnt;

always @(Reset,F_65536Hz,WIRE_4,WIRE_5,pulse_1,cnt,in)
begin
//if(Reset == 1'b0)
      if(in == 1'b0)
    WIRE_6 <= 1'b0;
else
begin
    for(cnt = 4'b0;cnt < 4'b1111;cnt = cnt + 4'b1)
    begin
      pulse_1 <= WIRE_5 && WIRE_4 && F_65536Hz;
      if(cnt == 4'b1111) cnt <= 4'b0;
    end
    WIRE_6 <= pulse_1 || pulse_1 ||
               pulse_1 || pulse_1 ||
               pulse_1 || pulse_1 ||
               pulse_1 || pulse_1 ||
               pulse_1 || pulse_1 ||
               pulse_1 || pulse_1 ||
               pulse_1 || pulse_1 ||
               pulse_1 || pulse_1;
end
end
//////////////////////////////////////////////////////////////

reg step_counter;
reg out_control;


always @(posedge WIRE_6 or negedge in)
begin
//if(Reset == 1'b0)
    if(in == 1'b0)
begin
    step_counter <= 24'b0;
    out_control<= 1'b0;
end
else
begin
    if(step_counter == (Sum - 24'b1))
      out_control<= 1'b0;
    else if(step_counter == 24'b111111111111111111111111)
      step_counter <= 24'b0;
    else
    begin
      step_counter <= step_counter + 24'b1;
      out_control<= 1'b1;
    end      
end
end


assign Pulse = WIRE_6 && out_control;
endmodule
只有加速、高速、没有减速的控制过程大家帮忙看一下

my_love 发表于 2011-8-13 08:41:24

没看懂控制步进电机加减速 只要改变脉冲改变的延时就行了

没这么复杂吧

fourir 发表于 2011-8-15 14:59:55

我也觉得有点复杂,而且initial语句在QUTARTUS中是无法综合的,是仿真用的
加减速,只要控制输入的脉冲频率就可以了

flyaudio 发表于 2011-8-23 20:34:47

我也觉得只要改变脉冲频率就ok了。LZ最好能描述一下程序的大致功能,特别是几个输入端口。

flyaudio 发表于 2011-9-8 15:47:28

真没看懂。

jym20030037 发表于 2018-6-6 22:13:24

哎还是的认真看看,应该是有用到的
页: [1]
查看完整版本: CPLD 控制步进电机加减速