CPLD stepper motor AN488 - Page 1
Forum section: electronics / general

author: sirbuctin
author_rank: Newbie
timestamp: July 03, 2019, 10:38:00 pm
content: Hello,I am trying to drive 3 stepper motors with 3 L298 H bridge,I am a beginner, never coded or used a cpld, and i found the AN488 note witch drives a steeper motor, but i need to drive 3.I've installed quartus software, and opened the project from the application note.I see in the first part the following codeCode://TOP LEVEL MODULEmodule stepmot(reset,dir,mode,move,phaseout); //module stepmot is the top level module.input reset,mode,dir,move; //reset when asserted brings the shaft to reference position.output [3:0] phaseout; //mode specifies step operation or continuous operation.wire osc,clk; //dir is for clockwise or anti clockwise motion.wire oscena; //move shifts the shaft by half step in step mode.assign oscena=1'b1;/* Instantiation of ufmclk module. */ufmclk ufmclk1(.oscena (oscena),.osc (osc));/* Instantiation of clk divider module.*/divider d1(.osc (osc),.clk (clk));/* Instantiation of controller module.*/stepper s1(.clk (clk),.reset (reset),.dir (dir),.mode (mode),.move (move),.phaseout (phaseout));endmoduleI think i need to modify and add the variables for the other 2 stepper here, and the code should become this.. is it ok ?Code://TOP LEVEL MODULEmodule stepmot(resetX,dirX,modeX,moveX,phaseoutX,resetY,dirY,modeY,moveY,phaseoutY,resetZ,dirZ,modeZ,moveZ,phaseoutZ);input resetX,modeX,dirX,moveX,resetY,modeY,dirY,moveY,resetZ,modeZ,dirZ,moveZ;output [3:0] phaseoutX;output [3:0] phaseoutY;output [3:0] phaseoutZ;wire osc,clk; //dir is for clockwise or anti clockwise motion.wire oscena; //move shifts the shaft by half step in step mode.assign oscena=1'b1;/* Instantiation of ufmclk module. */ufmclk ufmclk1(.oscena (oscena),.osc (osc));/* Instantiation of clk divider module.*/divider d1(.osc (osc),.clk (clk));/* Instantiation of controller module.*/stepper s1(.clk (clk),.reset (resetX),.dir (dirX),.mode (modeX),.move (moveX),.phaseout (phaseoutX));stepper s2(.clk (clk),.reset (resetY),.dir (dirY),.mode (modeY),.move (moveY),.phaseout (phaseoutY));stepper s3(.clk (clk),.reset (resetZ),.dir (dirZ),.mode (modeZ),.move (moveZ),.phaseout (phaseoutZ));endmoduleThe next part i think is a function, and it is called with those parameters for s1, s2, or s3, and its not needed to make copies multiple of it.. is it ok?Code:/* Module stepper is the motor controller module */module stepper (clk,reset,dir,mode,move,phaseout);input clk,reset,dir,mode;input move;output [3:0] phaseout;reg [3:0] phaseout,position;always@(posedge clk or negedge reset) beginif(reset==0) begin // motor resets to 0111 at reset=0phaseout=4'b0111;end else begin/* Mode=0 for continous motion */if (mode==0) begin//dir=0 for anticlockwise motioncase({phaseout}) //dir=1 for clockwise motion4'b0111 : if (dir==0)phaseout=4'b0110;elsephaseout=4'b0011;4'b0011 : if(dir==0)phaseout=4'b0111;elsephaseout=4'b1011;4'b1011 : if (dir==0)phaseout=4'b0011;elsephaseout=4'b1001;4'b1001 : if (dir==0)phaseout=4'b1011;elsephaseout=4'b1101;4'b1101 : if (dir==0)phaseout=4'b1001;elsephaseout=4'b1100;4'b1100 : if (dir==0)phaseout=4'b1101;elsephaseout=4'b1110;4'b1110 : if (dir==0)phaseout=4'b1100;elsephaseout=4'b0110;4'b0110: if (dir==0)phaseout=4'b1110;elsephaseout=4'b0111;default: $display("inavlid input");endcase/* Mode=1 for step motion */end else beginif(move==1'b0) begincase ({position})4'b0111 : if (dir==0)phaseout=4'b0110;elsephaseout=4'b0011;4'b0011 : if(dir==0)phaseout=4'b0111;elsephaseout=4'b1011;4'b1011 : if (dir==0)phaseout=4'b0011;elsephaseout=4'b1001;4'b1001 : if (dir==0)phaseout=4'b1011;elsephaseout=4'b1101;4'b1101 : if (dir==0)phaseout=4'b1001;elsephaseout=4'b1100;4'b1100 : if (dir==0)phaseout=4'b1101;elsephaseout=4'b1110;4'b1110 : if (dir==0)phaseout=4'b1100;elsephaseout=4'b0110;4'b0110 : if (dir==0)phaseout=4'b1110;elsephaseout=4'b0111;default: $display("inavlid input");endcaseendendendend.. but what is with the 'position' variable in reg [3:0] phaseout,position; i dont see 'position' being a variable in the function, is it a variable? doesn't need to be different for each motor?next is thisCode:/* Motor moves half step on the negetive edge of move*/always@(negedge move or negedge reset) beginif (reset==0)position=4'b0000;else if(reset==1)position=phaseout;endendmodulei think i should make two copies for this for the othere two steppers and become this :Code:/* Motor moves half step on the negetive edge of move*/always@(negedge move or negedge reset) beginif (reset==0)position=4'b0000;else if(reset==1)position=phaseoutX;endendmodulealways@(negedge move or negedge reset) beginif (reset==0)position=4'b0000;else if(reset==1)position=phaseoutY;endendmodulealways@(negedge move or negedge reset) beginif (reset==0)position=4'b0000;else if(reset==1)position=phaseoutZ;endendmodule.. but again, what is with the 'position' variable...the rest of the code seems to be the clock dividers and i think no modifications needed :Code:Module ufmclk is the modulewhich gets the clock from the oscillator******************************************************************************************/`timescale 1 ps / 1 ps//synopsys translate_onmodule ufmclk_altufm_osc_7p3(osc,oscena) /* synthesis synthesis_clearbox=1 */;output osc;input oscena;wire wire_maxii_ufm_block1_osc;maxii_ufm maxii_ufm_block1(.arclk(1'b0),.ardin(1'b0),.arshft(1'b0),.bgpbusy(),.busy(),.drclk(1'b0),.drdout(),.drshft(1'b0),.osc(wire_maxii_ufm_block1_osc),.oscena(oscena)`ifdef FORMAL_VERIFICATION`else// synopsys translate_off`endif,.drdin(1'b0),.erase(1'b0),.program(1'b0)`ifdef FORMAL_VERIFICATION`else// synopsys translate_on`endif// synopsys translate_off,.ctrl_bgpbusy(),.devclrn(),.devpor(),.sbdin(),.sbdout()// synopsys translate_on);defparammaxii_ufm_block1.address_width = 9,maxii_ufm_block1.osc_sim_setting = 180000,maxii_ufm_block1.lpm_type = "maxii_ufm";assignosc = wire_maxii_ufm_block1_osc;endmodule //ufmclk_altufm_osc_7p3//VALID FILE// synopsys translate_off`timescale 1 ps / 1 ps// synopsys translate_onmodule ufmclk (oscena,osc)/* synthesis synthesis_clearbox = 1 */;inputoscena;outputosc;wire sub_wire0;wire osc = sub_wire0;ufmclk_altufm_osc_7p3ufmclk_altufm_osc_7p3_component (.oscena (oscena),.osc (sub_wire0));endmodule/*************************************************************************************************** Module divider divides the clock from ufmclk to produce* a clock suitable to drive the motor controller*************************************************************************************************/module divider (osc, clk);input osc;output clk;reg clk;reg [16:0] count;initial count=17'b00000000000000000;always @( posedge osc)begincount = count + 1;clk = count;endendmodule/***********************************END OF PROGRAM.**********************************************/are my assumptions wrong ?what else should be modified in the code to drive 3 motors?

author: NorthGuy
author_rank: Super Contributor
timestamp: July 04, 2019, 02:36:39 am
content: You instantiate module for every stepper. You don't change anything within the module.Strange example. The same "case" code duplicated twice.

author: sirbuctin
author_rank: Newbie
timestamp: July 04, 2019, 04:54:36 am
content: oh, the 'always@(negedge move or negedge reset)' is part within the 'stepper' module, thought the stepper module ended and this is some separate code..Thank you,