-
Re: Motor Stepper Example
Dear All,
I dont Know if the following Code will MAKE the Motor To rotate.
I would like to have your opinion.
Code:
'****************************************************************
'* Name : UNTITLED.BAS *
'* Author : [Leonardo Bilalis] *
'* Notice : Copyright (c) 2013 *
'* : All Rights Reserved *
'* Date : 3/3/2013 *
'* Version : 1.0 *
'* Notes : Stepper Motor *
'* : *
'****************************************************************
define osc 4
Include "MODEDEFS.BAS"
ANSEL = 0
cmcon = 7
TRISB = %1111011
but var PORTB.2
Motor_Step VAR PORTB.5 ; ASIGN THE PIC16F88 PORTB.5 TO STEP PIN ON THE BIG EASY DRIVER
DIRECTION VAR PORTB.4 ; ASIGN THE PIC16F88 PORT.4 TO DIR PIN ON THE BIG EASY DRIVER
BEGIN:
LOW PORTB.4 ; WE CAN START BY MAKING LOW THE PORTB.4
LOW PORTB.5 ; WE CAN START BY MAKING LOW THE PORTB.5
PAUSE 1000 ; THEN WE GIVE 1 SECOND DELAY
if but = 0 then ;WE HAVE CONNECTED A PUSH BUTTON AT PORTB.2
goto rotation ;IF WE PRESS IT THEN PROGRAM GOES TO ROTATION LABEL
else ;IF WE DO NOT PRESS IT THE PROGRAM RETURN TO BEGIN
goto begin
endif
;THE FOLLOWING CODE SUPPOSE TO ROTATE THE SHAFT 500 STEPS
;AS FROM THE CALCULATION THE SECOND HAS 1000 MILI SECONDS
;AND WE ONLY GIVE 2 MILI SECONDS DELAY, SO WE GET 500 STEPS.
;IN CASE WE WOULD LIKE TO HAVE ONE REVOLUTION, THE WE NEED
;TO GIVE EXACTLY 200 STEPS FOR THIS SPECIFIC MOTOR.
;IN THIS CASE WE NEED WE NEED 5 MILI SECONDS FOR A PAUSE OF HIGH AND LOW TOGETHER
ROTATION:
HIGH MOTOR_STEP ; HERE WE MAKE HIGH THE PORT.B WHICH IS CONNECTED TO step PIN ON THE BED
PAUSE 1 ; DELAY ONE ms
low MOTOR_STEP ; HERE WE MAKE low THE PORT.B WHICH IS CONNECTED TO step PIN ON THE BED
PAUSE 1 ; DELAY ONE ms
GOTO BEGIN
END
-
Re: Motor Stepper Example
Hi,
If you want to move the motor 500 steps you need to send 500 pulses. The frequency of the pulsetrain, ie the delay between pulses, controls the speed of the motor.
The way you currently have it will move ONE step when the button is pressed, then it will start over, wait a second, move ONE step and so on - as long as the button is pressed.
Finally, you should try to avoid using GOTO. As the program grows it tends to get really hard to follow when there's a lots of GOTOs all over. Instead make your program into subroutines and use GOSUB/RETURN.
Code:
Main:
If But = 0 THEN
GOSUB StepIt
ENDIF
Goto Main
END
StepIt:
High Motor_Step
Pause 1
Low Motor_Step
Pause 1
RETURN
/Henrik.
-
Re: Motor Stepper Example
Hi Henrik,
thanks a lot for the advice.
in order to send for example 200 pulses for one revolution, do i need to give for i = 200 to do the revolution label so many times as the i ?
-
Re: Motor Stepper Example
Yes, exactly!
By the way, you seem to have your TRIS settings backwards, a set bit makes the pin an input, a cleared bit makes it an output.
I haven't actually tested the following so please see it as an overall idea and not verified/working code:
Code:
DEFINE OSC 4
StepCount VAR WORD ' Distance to move
Delay VAR WORD ' Time, in us, between each step-pulse
i VAR WORD ' General purpose index/counter variable
But VAR PortB.2
Dir_Pin VAR PortB.4
Step_Pin VAR PortB.5
CW CON 0
CCW CON 1
CMCON = 7
ANSEL = 0
TRISB = %11001111 ' PortB4-5 outputs, rest are inputs.
Main:
If But = 0 THEN
StepCount = 200 ' Distance to move motor
Delay = 2000 ' 2000us between each step
Dir_Pin = CCW
Gosub MoveMotor
ENDIF
Goto Main
END
MoveMotor:
For i = 0 to (StepCount - 1)
Gosub StepIt
PauseUs Delay
NEXT
RETURN
StepIt:
Step_Pin = 1 ' Generate a 10us wide pulse on the step pin.
PauseUs 10
Step_Pin = 0
RETURN
/Henrik.
-
Re: Motor Stepper Example
ops my bad, you are right, TRISB must be otherway round.
1 is inpupt and 0 is output.
Apart from than i will compile the code and try to run it and test it tomorrow.
will keep you updated. Let me print first the jig and fixture models so i can test the steps easier.
thanks a lot.
-
Re: Motor Stepper Example
today i will try to build the models but will be ready for testing tomorrow. So please give me some time. thanks a lot.
-
3 Attachment(s)
Re: Motor Stepper Example
I have promised you to show you the jig and fixture Mode for the stepper Motor with 1.8* degree and 200 steps per revolution. I have ONLY built 2 parts from the assembly.
For your reference checking the design on the Page 2.
Please find attached pictures. The other 2 parts which is the Base and the columns will be ready tomorrow.
-
Re: Motor Stepper Example
Wow! I don't know much about 3d-printing but those pieces looks very profesional, nice work!
What's the end application, is it only for experimenting/learning about step-motors or are these pieces going to be used for some specific application?
Again, nice work indeed!
/Henrik
-
4 Attachment(s)
Re: Motor Stepper Example
hi Henrik,
thanks a lot. the models are from a Fortus 400mc system designed in a 3D CAD.
The specific model is only a jig and fixture part. I designed it for helping me to learn how to move step by step the motor.
But the end application will be based on the following.
a small gear is attached on the small gear and will drive the big gear.
-
1 Attachment(s)
Re: Motor Stepper Example
"a small gear is attached on the small gear and will drive the big gear."
sorry i used my smart phone to upload the previous.....i meant:
a small gear is attached on the motor and will drive the big gear.
The following picture is let's say version 2. little changes are made.
So now i have only the columns to print and i will start testing the code.
-
Re: Motor Stepper Example
Code:
DEFINE OSC 4 ' not compulsory because default value ...
StepCount VAR BYTE ' Distance to move ' No need a word as Stepcount is < 255 !
Delay VAR WORD ' Time, in 2ms steps, between each step-pulse
i VAR WORD ' General purpose index/counter variable
But VAR PortB.2
Dir_Pin VAR PortB.4
Step_Pin VAR PortB.5
CW CON 0
CCW CON 1
CMCON = 7
ANSEL = 0
PORTB = 0 'Reset PortB to be sure
TRISB = %11001111 ' PortB4-5 outputs, rest are inputs.
Main:
If But = 0 THEN
StepCount = 200 ' Distance to move motor
Delay = 2 ' 2ms between each step
Dir_Pin = CCW
Gosub MoveMotor
ENDIF
Goto Main
END
MoveMotor:
For i = 0 to (StepCount - 1)
PULSOUT Step_Pin, 1 'Gosub StepIt
PauseUs Delay
NEXT
RETURN
Here are some ideas ...
Alain
-
Re: Motor Stepper Example
Hi Acetronics,
first of all, thanks a lot for the help as well.
On the code you have reset the PORTB but you make it all 0, why do we reset the port?
Code:
PORTB = 0 'Reset PortB to be sure
And then at the bottom you used the PULSEOUT command. What is the different between the following codes?
Code:
MoveMotor:
For i = 0 to (StepCount - 1)
Gosub StepIt
PauseUs Delay
NEXT
RETURN
StepIt:
Step_Pin = 1 ' Generate a 10us wide pulse on the step pin.
PauseUs 10
Step_Pin = 0
RETURN
and this one?
Code:
MoveMotor:
For i = 0 to (StepCount - 1)
PULSOUT Step_Pin, 1 'Gosub StepIt
PauseUs Delay
NEXT
RETURN
StepIt:
Step_Pin = 1 ' Generate a 10us wide pulse on the step pin.
PauseUs 10
Step_Pin = 0
RETURN
-
Re: Motor Stepper Example
Pulsout really toggles the output. So if it was at '1' it makes it '0' and then '1' again.
Thats why Alain made sure port B was reset in the first place.
Besides the exact timing of the Subroutine version of the program, they should perform the same.
Many ways to do the same thing.
Ioannis
-
4 Attachment(s)
Re: Motor Stepper Example
nice, now i need to collect all my components and start playing with the software.
It might take a bit longer because i do not have mounting screw terminals for the big easy driver.
For your reference i have completed the model so everything is ready apart from the electronic connections.
-
Re: Motor Stepper Example
Alain,
Thanks for editing my code. One small note though.
You changed the delay from 2000 to 2 but I think you missed that I was using PauseUS. If you want 2ms between steps then it should be Delay=2000. Also, using Pulsout takes more program space than manually toggling the pin. But, as have been said, many ways to do the same thing.
Astanapane,
One last note, when you get to running the motor, make sure you start running it slow. If the speed of the pulses are two fast the motor won't be able to follow and will stall. No harm done but it might look like something is wrong. If you want to run at high speed you need to provide some acceleration/deceleration ramp instead of simply starting at the desired velocity.
Keep up the nice work!
/Henrik.
-
Re: Motor Stepper Example
Henrik,
you can call me Leonardo.
As from the time and the speed then i need to give longer time for a revolution. That will make the motor to run slower.
Regarding the acceleration and deceleration i would like to do that YES. But i will do simple staff in the beggining.
Let me get a soldering iron and some small components i need because here in Dubai there is nothing to find.
I ordered all the components from US but didnt have in mind that i would need something to connect them. (all my electronic staff and my lab is back home in Hellas)
thanks all of you for your help and time these days.
-
Re: Motor Stepper Example
i think i will change the microcontroller to PIC16f628A because i have spare.
I only have at the moment one PIC16f88 and 3 PIC16F688.
Code:
'*************************************************************************************
'* Name : STEPPER MOTOR.BAS *
'* Author : GOT a HELP FROM HENRIK, Acetronics and Ioannis at (MELABS forum) *
'* Notice : Copyright (c) 2013 *
'* : All Rights Reserved *
'* Date : 3/3/2013 *
'* Version : 1.0 *
'* Notes : Stepper Motor *
'* : *
'*************************************************************************************
'we are going to use the PIC16F628A for controling BIG EASY DRIVER
define osc 4
Include "MODEDEFS.BAS"
StepCount var word ; distance to move
delay var word ; time in ms between each step pulse
i var word
but var PORTB.1 ; ASIGN THE PIC16F628A PORTB.1 AS A PUSH BUTTON
Motor_Step VAR PORTB.2 ; ASIGN THE PIC16F628A PORTB.2 TO STEP PIN ON THE BIG EASY DRIVER
DIRECTION VAR PORTB.3 ; ASIGN THE PIC16F628A PORT.3 TO DIR PIN ON THE BIG EASY DRIVER
CW con 0 ;HERE WE GIVE A CONSTANT DIRECTION
CCW con 1 ;HERE WE GIVE A CONSTANT DIRECTION
ANSEL = 0
cmcon = 7
PORTB = 0 ;Reset PORT B (thanks to Acetronics from MELabs
TRISB = %11110011 ;Make Portb.2 and Portb.3 O/P and rest I/P
BEGIN:
LOW Motor_Step ; WE CAN START BY MAKING LOW THE PORTB.2
LOW DIRECTION ; WE CAN START BY MAKING LOW THE PORTB.3
PAUSE 1000 ; THEN WE GIVE 1 SECOND DELAY
if but = 0 then ; WE HAVE CONNECTED A PUSH BUTTON AT PORTB.1
StepCount = 200 ; the number of the motor steps
delay = 100 ; 100us between each step
direction = CW ; THE DIRECTION IS COUNTERWISE
gosub Rotation
endif
goto begin
end
;THE FOLLOWING CODE SUPPOSE TO ROTATE THE SHAFT with a pulse of 200
ROTATION:
for i = 0 to (StepCount - 1)
PULSEOUT Motor_Step, 1 ;gosub StepIt
pauseUs delay
next
return
;StepIt:
; motor_step = 1 ;generate a 10us wide pulse on the step pin
; pauseus 10
; motor_step = 0
; RETURN
;thanks to Henrik at Melabs forum.
Do i need to add the code marked in RED? I think i need to say some how to the motor_step to be 1 or 0
I havent compiled the code yet because i'm at work but hopefully tonight i will do the first tests. So i might need your help.
-
Re: Motor Stepper Example
The red lines are for the version of the code using subroutine. You can delete them.
I recommend using the port toggle instead of the Pulsout command though.
Ioannis
-
Re: Motor Stepper Example
thanks Ioanni.
I'm building up the test board now. Once i go home after work i will try to make the first test with the code. :)
lets see.
-
Re: Motor Stepper Example
I did it.
Code:
'****************************************************************
'* Name : UNTITLED.BAS *
'* Author : [Leonardo Bilalis] *
'* Notice : Copyright (c) 2013 *
'* : All Rights Reserved *
'* Date : 3/3/2013 *
'* Version : 1.0 *
'* Notes : Stepper Motor *
'* : *
'****************************************************************
@ DEVICE pic16F628A, intOSC_osc_noclkout, WDT_OFF, PWRT_OFF, BOD_OFF, MCLR_ON
define osc 4
Include "MODEDEFS.BAS"
StepM var WORD ; distance to move
delay var word ; time in ms between each step pulse
i var word
cmcon = 7
PORTB = 0
TRISB = %11110011
but var PORTB.1 ; ASIGN THE PIC16F88 PORTB.2 AS A PUSH BUTTON
Motor_Step VAR PORTB.2 ; ASIGN THE PIC16F88 PORTB.5 TO STEP PIN ON THE BIG EASY DRIVER
DIRECTION VAR PORTB.3 ; ASIGN THE PIC16F88 PORT.4 TO DIR PIN ON THE BIG EASY DRIVER
CW con 0 ;HERE WE GIVE A CONSTANT DIRECTION
CCW con 1 ;HERE WE GIVE A CONSTANT DIRECTION
BEGIN:
LOW MOTOR_STEP ; WE CAN START BY MAKING LOW THE PORTB.4
LOW DIRECTION ; WE CAN START BY MAKING LOW THE PORTB.5
pAUSE 100 ; THEN WE GIVE 1 SECOND DELAY
if but = 0 then ; WE HAVE CONNECTED A PUSH BUTTON AT PORTB.2
stepM = 3200 ; the number of the motor steps
DELAY = 1 ; 1us between each step
direction = CW ; THE DIRECTION IS COUNTERWISE
gosub Rotation
endif
goto begin
end
;THE FOLLOWING CODE SUPPOSE TO ROTATE THE SHAFT 1 STEP AT A TIME
ROTATION:
for i = 0 to (stepM - 1)
gosub StepIt
pauseUS delay
next
return
StepIt:
motor_step = 1 ;generate a 1us wide pulse on the step pin
pauseUS 1
motor_step = 0
RETURN
;thanks to Henrik at Melabs forum.
there are some problems though.
i cannot sent one step pulse. i think i need to understand completely what is doing exactly through the Big easy driver.
-
Re: Motor Stepper Example
Hi, astanapane ...
the problem is Pauseus 1 gives a ... 24µs pulse @ 4Mhz !!! ( see manual p.185 ) - I found 23µs @ scope !
so, you might need to use ... assembler and absolutely avoid the Stepit sub ...!
so, try this piece of code ...
Code:
ROTATION:
for i = 0 to (stepM - 1)
@ bsf PORTB,2 ; This gives ON pulse for 1µs ...
@ bcf PORTB,2
pauseUS delay
next
return
Alain
-
Re: Motor Stepper Example
When you set Delay = 1 it tries to delay 1us between each pulse. As Alain wrote the shortest dealy you'll get with the PauseUs command (which is used by the code) is 24uS. So, the step pulse will be 24uS when using a 4MHz clock. Then there will be another 24us delay before the next pulse, add to that perhaps another 50us for the actual code executed between steps for a total of 100us. That equals a step frequency of 10kHz and there's no way the motor will be able to follow that without accelerating it up to speed.
Leave the StepIt routine alone for now and change the Delay value to 2000 as I had in the original code I posted. That should step the motor at ~500Hz which it should be able to follow without accelerating.
/Henrik.
-
Re: Motor Stepper Example
it didnt work it is my fault
actually i'm tired really for today. Here is midnight. I will try tomorrow morning.
thanks a lot for your time and interest. i really apreciated
I will keep going testing the software.
-
Re: Motor Stepper Example
this morning i was playing with the code but i could only do some movements. Not microstepping though.
I actually moved the shaft and pointer 16 times per revolution in an angle of 22.5 degrees with 200 steps per loop and then backwards to 3200 steps for one revolution.
It does not looks like so many steps but is what from the code i had to do.
First of all i used the MS1 MS2 and MS3 from the big easy driver to control the mictosteps.
I couldnt really make it work but the point is that i turned the motor with half microsteps at 400 steps per revolution so 0.9 degrees each step.
here is a small video that i could add any comments because i was at work.
I promise you on my next viideo will explain the code and the model.
thanks a lot.
My next goal is to try and control microsteps. If anyone has the easy driver and any stepper motor, would be nice to share ideas.
Not that i can do a lot but i can test and play with that. Now i have set up everything and it is easier for me.
http://www.youtube.com/watch?v=U-eS-...ature=youtu.be
-
Re: Motor Stepper Example
Hi, Astanapane
I do not know if it can really Help you ...
but Some years ago, I wrote THAT to play with a stepper motor ...
Code:
/*
* Project name:
Demo for MkE Stepper card
* Copyright:
(c) Acetronics
* Revision History:
0.0
* Status: Operationnal
* Description:
Demonstrate Stepper Commands.
Maximum frequency limited by the 40µs dead time to ~ 2.5 rpm / s @ 400 steps/rev ...
max clock Frequ is limited to 1100 x MS0 x MS1 Hz ...
Add 2 volts to supply for 3967 internal drop ...
* Test configuration:
MCU: Pic 16F877A
Dev.Board: EasyPic5
Oscillator: 8 Mhz
Ext. Modules: Stepper card on PortC
LCD 2x16 on PortB - Legacy Wiring
SW: MkC Pro 4.15
* NOTES:
Command Connexions:
PortA.0 : Accel rate - Analog
PortA.1 : Decel rate - Analog
PortA.2 : Unlock Motor
Porta.3 : DO NOT USE
Porta.4 : Emergency Stop @ Maximum rate
Porta.5 : Stop @ PortA.1 deceleration rate
Porta.6 : Increase Speed
PortA.7 : Decrease Speed
Pullups on PortA.2 to PortA.7
PortD.5 : Invert Rotation
Pullup on PortD.5
BUTTONS : GND when Pushed
Display connexions:
PortB : STD 2x16 LCD Connexions
Stepper card Connexions:
PortC.0 : Enable
PortC.1 : Step
PortC.2 : Reset
PortC.3 : Sleep
PortC.4 : MS1
PortC.5 : MS2
PORTC.6 : Dir
PortC.7 : Free
// Display :
First line displays Motor Status
Second line displays 1) Speed in % , 2) Acceleration Rate , 3) Deceleration Rate.
ENJOY ...
*/
# include "built_in.h"
// Port Aliases
# define Accel rate PortA.B0
# define Decel rate PortA.B1
# define Run PortA.B2
// # define Free PortA.B3 // A/D reserved
# define Emergency_B PortA.B4
# define Stop PortA.B5
# define Increase PortB.B6
# define Decrease PortB.B7
# define Enable PortC.B0
# define Step PortC.B1
# define Reset PortC.B2
# define Sleeep PortC.B3
# define MS1 PortC.B4
# define MS2 PortC.B5
# define Dir PORTC.B6
# define Alive PortC.B7
// # define Enable PortD.B0
// # define Step PortD.B1
# define Locked PortD.B2
// # define Sleeep PortD.B3
// # define MS1 PortD.B4
# define Reverse PortD.B5
# define Accen PortD.B6
# define Decen PortD.B7
// Variables
# define LoSpeedLimit 500 // (6µs) // Deadband 40µs !!!
# define HiSpeedLimit 20000
# define Debounce 20
// Lcd pinout settings *********************************************************
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D4 at RB0_bit;
// Pin direction ***************************************************************
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D7_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB0_bit;
// Types ***********************************************************************
bit oldstate, lock, Emergency, Rev ;
unsigned int Acc, Dec, Speedo, OldSpeed ,Speed , Speeds;
char text[7];
// Messages ********************************************************************
const char txt0[] = " "; // Blank Line
const char txt1[] = "mikroElektronika";
const char txt2[] = "EasyPIC5";
const char txt3[] = "Stepper Demo";
const char txt4[] = "Dir :"; // First Row
const char txt5[] = "Forward";
const char txt6[] = "Reverse";
const char txt7[] = "Speed :"; // Second Row
const char txt8[] = "Acc :";
const char txt9[] = "Dec :";
const char txt10[] = "Emerg. Brake lock"; // Third Row
const char txt11[] = "Locked";
const char txt12[] = "Decelerating ";
const char txt13[] = "Accelerating";
const char txt14[] = "Stopped";
const char txt15[] = "Waiting Start";
const char txt16[] = "Steady running";
const char txt17[] = "Auto Decelerate";
const char txt18[] = "Auto Accelerate";
const char txt19[] = "Invert Rotation";
//******************************************************************************
//******************************************************************************
void interrupt()
{
if ( PIR1.B0 == 1) // Timer Period ended
{
Step = ! Step; // Toggle Output
Speedo = 65550 - Speed ; // 15 units for interrupt
TMR1H = Hi(Speedo);
TMR1L = Lo(Speedo);
PIR1.B0 = 0; // Reset Flag
}
}
//******************************************************************************
void Text_To_LCD (unsigned Row, unsigned Col, const unsigned char *m) // Write ROM Messages to LCD
{
unsigned i = 0;
Lcd_Out(Row, Col,"");
while(m[i] != 0)
{
Lcd_Chr_Cp(m[i]);
i++;
}
}
//******************************************************************************
void Showstatus() // Show Motor Status
{
Text_to_LCD ( 1,1,txt0 );
Text_to_LCD ( 1,1,txt14 );
delay_ms(500);
}
//******************************************************************************
void Showspeed() // Show actual parameters
{
Lcd_Cmd(_LCD_RETURN_HOME );
if ( Emergency == 1 )
{
Text_to_LCD(1,1,txt0);
Text_To_LCD(1,1,txt10);
}
{
if (speed == HiSpeedLimit)
{
Speeds = 0;
}
else
{
Speeds = LoSpeedLimit*100/Speed;
}
WordtoStr(Speeds, text);
Lcd_Out(2,1,text);
WordtoStr(Acc, text);
Lcd_Out(2,6,text);
WordtoStr(Dec, text);
Lcd_Out(2,11,text);
}
delay_ms(500);
}
//******************************************************************************
void Read_Accel() // Read Acceleration rate
{
Acc = ADC_Read(0);
if (Acc == 0)Acc = 1 ; // Floor value = 1
Dec = ADC_Read(1);
if (Dec == 0)Dec = 1 ; // Floor value = 1
}
//******************************************************************************
void Decell() // Deceleration routine
{
if( Speed <= (HiSpeedLimit - Dec)) // Care for Underspeed
{
Speed += Dec ;
delay_ms(10);
Showspeed();
}
else // Motor will stop ...
{
T1CON.B0 = 0 ; // Stop TMR1 if motor Stopped
Step = 0; // Low Step Output
Speed = HiSpeedLimit;
ShowSpeed();
Showstatus();
}
}
//******************************************************************************
void Accell() // Acceleration routine
{
if( Acc < (Speed - LoSpeedLimit))
{
Speed -= Acc ;
}
else
{
Speed = LoSpeedLimit ; // Respect Speed Limit
}
delay_ms(10);
ShowSpeed();
}
//******************************************************************************
//******************************************************************************
void main()
{
// Registers Setup: ************************************************************
/*ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;*/
CMCON = 7; // Comparators OFF
CVRCON = 0b00000000; // Vref OFF
ADCON0 = 0b1100001; // Adc Config 10 Bits ch 0,1,2 Analog.
ADCON1 = 0b1100100;
T1CON = 0b00010000; // Config Timer 1 : prescaler 1/2, Osc off, - off
CCP1CON = 0; // "CCPM" Config ( Disable all )
CCP2CON = 0;
// Interrupts *****************************************************************
INTCON = 0b11000000; // GIE and PIE Enabled
PIE1 = 0b00000001; // CCP1 int Enabled
PIR1 = 0;
PIE2 = 0;
PIR2 = 0;
// Config Ports ***************************************************************
PORTA = 0b11111111; // Input Control + pots
PORTB = 0b11000000; // LCD Display
PORTC = 0b00000000; // Stepper module control
PORTD = 0b00100000; // Led status display
PORTE = 0b00000000;
TRISA = 0b11111111;
TRISB = 0b11000000;
TRISC = 0b00000000;
TRISD = 0b01100000;
TRISE = 0b00000000; // PortD Digital I/O
// Variables Setup *************************************************************
OldState = 0;
Lock = 0; // Start in Locked Status
Emergency = 0;
Speed = HiSpeedLimit; // Reset to min Speed
OldSpeed = HiSpeedLimit;
Rev = 0; // Forward
T1CON.B0 = 0; // TMR1 Stopped
TMR1H = 0; // Reset TMR1
TMR1L = 0;
delay_ms(500); // LCD Power up delay
// LCD Initialization **********************************************************
LCD_Init();
Lcd_Cmd(_LCD_CLEAR); // Clear Lcd display:
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor OFF
Text_to_LCD ( 1,1,txt11 ); // Show Starting Status
// Output Setup ***************************************************************
Enable = 0; // PortC.B0
Step = 0; // PortC.B1
Reset = 1; // PortC.B2
Sleeep = 1; // PortC.B3
MS1 = 0; // PortC.B4
MS2 = 0; // PortC.B5
Dir = 0; // PORTC.B6
while(1)
{
/*Alive = 1 ; // Show it's alive
delay_ms(300);
Alive = 0;
delay_ms(300);*/
// Read_Inputs();
//******************************************************************************
//******************************************************************************
//void Read_Inputs() // Check Commands
// {
// Lock check***********************************************************
if ( Lock == 1 ) // if Unlocked
{
// 1) Check Emergency Stop *******************************************
if (Button(&PORTA, 4, Debounce, 0)) // Check Emergency Stop demand - Stops motor
{
oldstate = 1; // Update flag
Emergency = 1;
Dec = 1023; // Full Brake ( 64 Steps @ 10 ms)
while ( Speed < HiSpeedLimit )
{
Decell();
}
T1CON.B0 = 0; // TMR1 Stopped
Step = 0; // Low Step Signal
Lock = 0; // Lock Motor
Locked = 0; // Show locked
Text_to_LCD ( 1,1,txt0 );
Text_to_LCD ( 1,1,txt10 ); // Lcd_Out(1,1,"Emerg. Brake Lock");
Text_to_LCD ( 2,1,txt0 );
while (oldstate && Button(&PORTA, 4, Debounce, 0)){} // Loop until button released
oldstate = 0; // Button released - Update flag - Restart authorized
goto bailout;
}
// 2) Check for Direction when stopped ********************************
if ( Speed == HiSpeedLimit )
{
while (Button(&PORTD, 5, Debounce, 0)) // Check for Reverse Demand
{
oldstate = 1;
Text_to_LCD ( 1,1,txt0 );
Text_to_LCD ( 1,1,txt19 ); // Show " Invert Rotation
delay_ms(500);
}
if (oldstate && Button(&PORTD, 5, Debounce, 1)) // Reverse Button Released
{
T1CON.B0 = 0; // TMR1 Stopped to be sure
Step = 0; // Low Step Output
Alive = 1;
Rev = ~ Rev; // invert rotation
Dir = ~ Dir;
Text_to_LCD ( 1,1,txt0 );
if ( Dir == 0 )
{
Text_to_LCD ( 1,1,txt5 ); // Lcd_Out(1,1,"Forward");
}
else
{
Text_to_LCD ( 1,1,txt6 ); // Lcd_Out(1,1,"Reverse");
}
}
delay_ms(500);
oldstate = 0;
Alive = 0;
}
// Now we can do something else
// 2) Check for Increase Speed IF Unlocked*****************************
if (Speed >= LoSpeedLimit)
{
PortD.B0 = 1; // show accel / Decel enabled
while (Button(&PORTB, 6, Debounce, 0)) // Check Increase demand - Increases motor until released
{
oldstate = 1; // Update flag
if ( Speed == HiSpeedLimit) // If motor stopped
{
T1CON.B0 = 1; // Restart TMR1 @ Slow Speed;
PortD.B6 = 1; // Show Accel Led
}
Read_Accel(); // Read Accel Value
//Acc = Acc << 6; // Scale to 16 Bits
if( Speed > LoSpeedLimit )
{
Text_to_LCD ( 1,1,txt0 );
Text_to_LCD ( 1,1,txt13 ); // Lcd_Out(1,1,"Accelerating");
Accell();
}
else
{
Text_to_LCD ( 1,1,txt16 ); // Lcd_Out(1,1,"Steady running");
}
delay_ms(200);
}
if (oldstate && Button(&PORTB, 6, Debounce, 1)) // Accel Released
{
oldstate = 0; // Update flag
PortD.B6 = 0; // Clear Accel Led
Text_to_LCD ( 1,1,txt0 );
Text_to_LCD ( 1,1,txt16 ); // Lcd_Out(1,1,"Steady running");
delay_ms(200);
}
}
// Decrease Speed Only if motor running !!!
if ( Speed < HiSpeedLimit )
{
// 3) Check for Slow Stop demand *******************************
if (Button(&PORTA, 5, Debounce, 0)) // Check Stop demand - Stops motor slowly
{
oldstate = 1; // Update flag
Read_Accel(); // Read Brake Value
//Dec = Dec << 6; // Scale to 16 Bits
while( Speed < HiSpeedLimit )
{
Text_to_LCD ( 1,1,txt0 );
Text_to_LCD ( 1,1,txt17 ); // Lcd_Out(2,1,"Auto Decelerate");
Decell();
delay_ms(200);
}
T1CON.B0 = 0; // TMR1 Stopped
Step = 0; // Low Step Output
Text_to_LCD ( 1,1,txt0 );
Text_to_LCD ( 1,1,txt14 ); // Lcd_Out(1,1,"Stopped ");
// Lock = 0; // Lock Motor once stopped - not compulsory
}
if (oldstate && Button(&PORTA, 5, Debounce, 1)) // Stop Released
{
oldstate = 0; // Update flag
}
// Check for Direction *******************************************
while (Button(&PORTD, 5, Debounce, 0)) // Check for Reverse Demand
{
Text_to_LCD ( 1,1,txt0 );
Text_to_LCD ( 1,1,txt19 ); // Show " Invert Rotation
delay_ms(500);
oldstate = 1;
}
if (oldstate && Button(&PORTD, 5, Debounce, 1)) // Reverse Button Released
{
OldState = 0;
OldSpeed = Speed; // memorize current Speed
Read_Accel();
while( Speed < HiSpeedLimit ) // Decelerate to stop
{
Text_to_LCD ( 1,1,txt0 );
Text_to_LCD ( 1,1,txt17 ); // Lcd_Out(1,1,"Auto Decelerate");
Decell();
delay_ms(200);
}
T1CON.B0 = 0; // TMR1 Stopped
Step = 0; // Low Step Output
delay_ms(500);
Rev = ~ Rev; // invert rotation
Dir = ~ Dir;
Text_to_LCD ( 1,1,txt0 );
if ( Dir == 0 )
{
Text_to_LCD ( 1,1,txt5 ); // Lcd_Out(1,1,"Forward");
}
else
{
Text_to_LCD ( 1,1,txt6 ); // Lcd_Out(1,1,"Reverse");
}
delay_ms(500);
// Locked = 1; // Show Motor Unlocked
T1CON.B0 = 1;
Read_Accel();
while( Speed > OldSpeed ) // Accelerate to OldSpeed
{
Text_to_LCD ( 1,1,txt0 );
Text_to_LCD ( 1,1,txt18 ); // Lcd_Out(1,1,"Auto Accelerate");
Accell();
delay_ms(200);
}
Text_to_LCD ( 1,1,txt0 );
Text_to_LCD ( 1,1,txt16 ); // Lcd_Out(1,1,"Steady running");
}
// 4) Check for Decrease Speed *********************************
while (Button(&PORTB, 7, Debounce, 0)) // Check Slow demand - Slows motor until released
{
oldstate = 1; // Update flag
Read_Accel(); // Read Brake Value
//Dec = Dec << 6; // Scale to 16 Bits
if( Speed < HiSpeedLimit ) // is Motor Running ???
{
Text_to_LCD ( 1,1,txt0 );
Text_to_LCD ( 1,1,txt12 ); // Lcd_Out(1,1,"Decelerating");
Decell();
delay_ms(200);
PortD.B7 = 1;
}
else // NO Motor Stopped
{
T1CON.B0 = 0; // TMR1 Stopped
Step = 0; // Low Step Output
Text_to_LCD ( 1,1,txt0 );
Text_to_LCD ( 1,1,txt14 ); // Lcd_Out(1,1,"Stopped");
}
}
if (oldstate && Button(&PORTB, 7, Debounce, 1)) // Stop Released
{
oldstate = 0; // Update flag
if ( Speed != HiSpeedLimit) // IF motor Stopped
{
Text_to_LCD ( 1,1,txt0 );
Text_to_LCD ( 1,1,txt16 ); // Lcd_Out(1,1,"Steady running");
}
delay_ms(200);
PortD.B7 = 0;
}
}
}
bailout:
// If Motor Locked ... Check for Start !!! ****************************
if (Lock == 0) // if locked
{
if (Button(&PORTA, 2, Debounce, 0)) // Check for Start Demand
{
Oldstate = 1;
}
if (oldstate && Button(&PORTA, 2, Debounce, 1)) // Start Button Released - Motor unlocked
{
T1CON.B0 = 0; // TMR1 Stopped
Speed = HiSpeedLimit; // Slow Speed
Emergency = 0; // Disengage Emergency
Text_to_LCD ( 1,1,txt0 );
Text_to_LCD ( 1,1,txt15 ); // Lcd_Out(1,1,"Waiting Start");
Lock = 1; // Unlock Motor
Locked = 1; // Show Motor Unlocked
delay_ms(500); // Min Delay to read
oldstate = 0; // Update flag
}
}
} // Process inputs
// }
}
I do agree it's not PicBasicPro :D ( MikroCPro ... )
But may show you how to use the functions of your card ...
Alain
-
Re: Motor Stepper Example
Hehehe it very kind of you shearing the code.
For many hours this morning i was playing with the BIG EASY DRIVER and only succeed to disable the power To the motor when not in use.
I will checking the code but for me it is easier to control the easy driver.
Keep you updated.
Thanks a lot.
-
Re: Motor Stepper Example
Hi,
Since you need 3200 pulses to move one revolution the driver is already set to its highest resolution, ie. 16 microsteps per fullstep, 200*16=3200. If you look at the schematic for the Big Easy Driver you'll see that MS1, MS2 and MS3 are all tied high thru 20k resistors and if you look at table 1 in the A4983 datasheet you'll see that that indeed means 16 microsteps. If you want to run at fullstep (200 steps/rev) then connect MS1-MS3 to GND.
/Henrik.
-
Re: Motor Stepper Example
All thanks a lot.
I made it. I did microsteps. It was easier than i thought.
I will the Code in a bit.
-
Re: Motor Stepper Example
http://www.youtube.com/watch?v=UaaGpVkjQAc
Code:
'************************************************* ************************************
'* Name : STEPPER MOTOR.BAS *
'* Author : [Leonardo Bilalis] GOT a HELP FROM HENRIK, Acetronics and Ioannis (MELABS forum) *
'* Notice : Copyright (c) 2013 *
'* : All Rights Reserved *
'* Date : 3/3/2013 *
'* Version : 1.0 *
'* Notes : Stepper Motor *
'* : *
'************************************************* ************************************
'we are going to use the PIC16F628a for controling BIG EASY DRIVER
@ DEVICE pic16F628A, intOSC_osc_noclkout, WDT_OFF, PWRT_OFF, BOD_OFF, MCLR_ON
define osc 4
Include "MODEDEFS.BAS"
StepM var WORD ; distance to move
delay var word ; time in ms between each step pulse
i var word
j var word
cmcon = 7
PORTB = 0
PORTA = 0
TRISB = %11110011
TRISA = %00100011
but var PORTB.1 ; ASIGN THE PIC16F88 PORTB.2 AS A PUSH BUTTON
Motor_Step VAR PORTB.2 ; ASIGN THE PIC16F88 PORTB.5 TO STEP PIN ON THE BIG EASY DRIVER
DIRECTION VAR PORTB.3 ; ASIGN THE PIC16F88 PORT.4 TO DIR PIN ON THE BIG EASY DRIVER
MS1 VAR PORTA.2 ;big easy driver microstep contro
MS2 VAR PORTA.3 ;big easy driver microstep contro
MS3 VAR PORTA.4 ;big easy driver microstep contro
ENABLE_BED VAR PORTA.6 ;use this in order to cut current to the stepper motor
CW con 0 ;HERE WE GIVE A CONSTANT DIRECTION
CCW con 1 ;HERE WE GIVE A CONSTANT DIRECTION
BEGIN:
LOW MOTOR_STEP ; WE CAN START BY MAKING LOW THE PORTB.4
LOW DIRECTION ; WE CAN START BY MAKING LOW THE PORTB.5
pAUSE 100 ; THEN WE GIVE 1 SECOND DELAY
HIGH MS1 ; MS1
HIGH MS2 ; MS2
HIGH MS3 ; MS3
PAUSE 100
IF BUT = 1 THEN
pause 100
ENABLE_BED = 1
ENDIF
if but = 0 then ; WE HAVE CONNECTED A PUSH BUTTON AT PORTB.2
PAUSE 100
for j = 0 to 16
ENABLE_BED = 0
stepM = 1 ; the number of the motor steps
DELAY = 200 ; 100us between each step
direction = CW ; THE DIRECTION IS COUNTERWISE
gosub Rotation
PAUSE 200
NEXT
pause 100
stepM = 200 ; the number of the motor steps
DELAY = 200 ; 100us between each step
direction = CW ; THE DIRECTION IS COUNTERWISE
gosub Rotation
PAUSE 200
stepM = 216 ; the number of the motor steps
DELAY = 200 ; 100us between each step
direction = cCW ; THE DIRECTION IS COUNTERWISE
gosub Rotation
PAUSE 200
stepM = 3200 ; the number of the motor steps
DELAY = 50 ; 100us between each step
direction = CCW ; THE DIRECTION IS COUNTERWISE
PAUSE 2000
gosub Rotation
ENDIF
goto BEGIN
;THE FOLLOWING CODE SUPPOSE TO ROTATE THE SHAFT 1 STEP AT A TIME
ROTATION:
for i = 0 to (stepM - 1)
;@ BSF PORTB,2
;@ BCF PORTB,2
gosub StepIt
pauseUS delay
next
return
;ROTATION2:
;for i = 0 to stepM
;@ BSF PORTB,2
;@ BCF PORTB,2
gosub StepIt
; pauseUS delay
;next
; return
StepIt:
motor_step = 1 ;generate a 10us wide pulse on the step pin
pauseUS 10
motor_step = 0
RETURN
;thanks to Henrik at Melabs forum.
-
Re: Motor Stepper Example
on the code above i havent changed the comments to pic16f628a.
as the first time the code was written for the pic16f88 and the comments are only for this one.
in any case i will upload an clean code tomorrow . SORRY ABOUT THAT.
-
Re: Motor Stepper Example
Congratulations Leonardo. :)
Robert
-
Re: Motor Stepper Example
Quote:
Originally Posted by
Demon
Congratulations Leonardo. :)
Robert
Hi Robert,
thanks a lot, i didnt do much, Henrik Acetronics and Ioannis helped me with the code a lot. Now it is easier to understand some things. But as i dont have much knowledge with programming language i'm moving slow and i know that i make you some times to lough with my stupid questions. Hehehehe. I hope with this small jig and fixtire model i will have some more practice.
At the end i compiled the following code which is working just fine. Please have a look at the code and the video.
https://www.youtube.com/watch?v=u2_ZFLXod0g&feature=youtube_gdata_player
Code:
'************************************************* ************************************
'* Name : STEPPER MOTOR.BAS *
'* Author : [Leonardo Bilalis] GOT a HELP FROM HENRIK, Acetronics and Ioannis *
'* Notice : Copyright (c) 2013 *
'* : All Rights Reserved *
'* Date : 3/3/2013 *
'* Version : 1.0 *
'* Notes : Stepper Motor *
'* : *
'************************************************* ************************************
'we are going to use the PIC16F628A for controling BIG EASY DRIVER
'************************************************* ************************************
@ DEVICE pic16F628A, intOSC_osc_noclkout, WDT_OFF, PWRT_OFF, BOD_OFF, MCLR_ON
define osc 4
Include "MODEDEFS.BAS"
StepM var WORD ; distance to move
delay var word ; time in ms between each step pulse
i var word
j var word
cmcon = 7
PORTB = 0
PORTA = 0
TRISB = %11110011 ;we are setting the ports to inputs or outputs
TRISA = %00100001 ;we are setting the ports to inputs or outputs
led var porta.1 ; I just put a power led for the PIC16f628 at porta.1
but var PORTB.1 ; ASIGN THE PIC16F628A PORTB.1 AS A PUSH BUTTON
Motor_Step VAR PORTB.2 ; ASIGN THE PIC16F628A PORTB.2 TO STEP PIN ON THE BIG EASY DRIVER
DIRECTION VAR PORTB.3 ; ASIGN THE PIC16F628A PORT.3 TO DIR PIN ON THE BIG EASY DRIVER
MS1 VAR PORTA.2 ;big easy driver microstep control
MS2 VAR PORTA.3 ;big easy driver microstep control
MS3 VAR PORTA.4 ;big easy driver microstep control
ENABLE_BED VAR PORTA.6 ;use this in order to cut current to the stepper motor, it is very important when motor is not doing anything
CW con 0 ;HERE WE GIVE A CONSTANT DIRECTION
CCW con 1 ;HERE WE GIVE A CONSTANT DIRECTION
for j = 0 to 2 ; this is just a loop for flashing the LED
pauseus 100
low led ; power led for the pic in porta.1
pause 500
high led
pause 1
next
BEGIN:
LOW MOTOR_STEP ; WE CAN START BY MAKING LOW THE PORTB.2
LOW DIRECTION ; WE CAN START BY MAKING LOW THE PORTB.3
pAUSE 100 ; THEN WE GIVE 1 SECOND DELAY
HIGH MS1 ; MS1, as from the datasheet when all are high 16 microsteps can be applied
HIGH MS2 ; MS2, as from the datasheet when all are high 16 microsteps can be applied
HIGH MS3 ; MS3, as from the datasheet when all are high 16 microsteps can be applied
PAUSE 100
IF BUT = 1 THEN ;there is a button at PORTB.1, so if we do not push it
pause 100
ENABLE_BED = 1 ; the enable port on the big easy driver will remain high.
;as from the datasheed of the BED when high the output to the motor is dissabled.
ENDIF
if but = 0 then ; WE HAVE CONNECTED A PUSH BUTTON AT PORTB.2
PAUSE 100
for j = 0 to 15 ;loop
ENABLE_BED = 0 ; will activate the PORTA.6 on the pic16f628A which is connected to enable pin on the Big Easy Driver
pause 500 ; every step takes 500us
stepM = 1 ; the number of the motor steps
DELAY = 500 ; 500us between each step
direction = cCW ; THE DIRECTION IS COUNTERWISE
gosub Rotation ; check ROTATION routin
next j
PAUSE 200
for j = 0 to 199 ;loop of 200
pause 1
stepM = 1 ; the number of the motor steps
DELAY = 300 ; 300us between each step
direction = CW ; THE DIRECTION IS COUNTERWISE
gosub Rotation
PAUSE 5
next j
pause 200
for j = 0 to 199 ;loop of 200
pause 1
stepM = 1 ; the number of the motor steps
DELAY = 300 ; 300us between each step
direction = cCW ; THE DIRECTION IS COUNTERWISE
gosub Rotation
PAUSE 5
next j
pause 200
stepM = 800 ; the number of the motor steps
DELAY = 100 ; 100us between each step
direction = CW ; THE DIRECTION IS COUNTERWISE
gosub Rotation
PAUSE 2000
stepM = 800 ; the number of the motor steps
DELAY = 100 ; 100us between each step
direction = cCW ; THE DIRECTION IS COUNTERWISE
gosub Rotation
PAUSE 200
for j = 0 to 1599
pause 1
stepM = 1 ; the number of the motor steps
DELAY = 4000 ; 100us between each step
direction = CW ; THE DIRECTION IS COUNTERWISE
gosub Rotation
next j
PAUSE 2000
stepM = 1600 ; the number of the motor steps
DELAY = 50 ; 500us between each step
direction = cCW ; THE DIRECTION IS COUNTERWISE
gosub Rotation
PAUSE 2000
stepM = 3200 ; the number of the motor steps
DELAY = 50 ; 100us between each step
direction = CCW ; THE DIRECTION IS COUNTERWISE
gosub Rotation
PAUSE 200
for j = 1 to 15
pause 1
stepm = 1
delay = 300
direction = cw
gosub rotation
next j
pause 200
ENDIF
goto BEGIN
;THE FOLLOWING CODE SUPPOSE TO ROTATE THE SHAFT 1 STEP AT A TIME
ROTATION:
for i = 0 to (stepM - 1)
;@ BSF PORTB,2
;@ BCF PORTB,2
gosub StepIt
pauseUS delay
next
return
;ROTATION2:
;for i = 0 to stepM
;@ BSF PORTB,2
;@ BCF PORTB,2
gosub StepIt
; pauseUS delay
;next
; return
StepIt:
motor_step = 1 ;generate a 10us wide pulse on the step pin
pauseUS 10
motor_step = 0
RETURN
;thanks to Henri
I hope the video and the code will help you to understand why i designed and build this jig and fixture model
-
Re: Motor Stepper Example
here is an addition code which controllig the enable and sleep modes on the Big Easy Driver.
Now everything is safe when the motor is not working.
Now i need to play with the code for making some linear accelerations. Any help of how can i start that?
Code:
'****************************************************************************************
'* Name : STEPPER MOTOR.BAS *
'* Author : [Leonardo Bilalis] GOT a HELP FROM HENRIK, Acetronics and Ioannis *
'* Notice : Copyright (c) 2013 *
'* : All Rights Reserved *
'* Date : 3/3/2013 *
'* Version : 1.0 *
'* Notes : Stepper Motor *
'* : *
'****************************************************************************************
'we are going to use the PIC16F628A for controling BIG EASY DRIVER
'********************************* PIC16F628A *******************************************
' ______________________________ *
' / _____________________________/| *
' | PIN 1 : RA2/AN2/VREF | | *
' | PIN 2 : RA3/AN3/CMP1 | | *
' | PIN 3 : RA4/TOCKI/CMP2 | | *
' 9 8 7 6 5 4 3 2 1 | PIN 4 : RA5/MCLR/VPP | | *
' ------------------------------------ | PIN 5 : VSS | | *
' | R R R R V R R R R *| | PIN 6 : RB0/INT | | *
' | B B B B S A A A A | | PIN 7 : RB1/RX/DT | | *
' | 3 2 1 0 S 5 4 3 2 | | PIN 8 : RB2/TX/CK | | *
' | | | PIN 9 : RB3/CCP1 | | *
' | PIC16F628A | |______________________________|/ *
' | | /_____________________________ /| *
' | R R R R V R R R R | | PIN 10 : RB4/PGM | | *
' | B B B B D A A A A | | PIN 11 : RB5 | | *
' | 4 5 6 7 D 7 6 0 1 | | PIN 12 : RB6/T10SO/T1CKI/PGC| | *
' ------------------------------------ | PIN 13 : RB7/T1OSI/PGC | | *
' 10 11 12 13 14 15 16 17 18 | PIN 14 : VDD | | *
' | PIN 15 : RA6/OSC2/CLOCKOUT | | *
' | PIN 16 : RA7/OSC1/CLOCKIN | | *
' | PIN 17 : RA0/AN0 | | *
' | PIN 18 : RA1/AN1 | | *
' |______________________________|/ *
'****************************************************************************************
@ DEVICE pic16F628A, intOSC_osc_noclkout, WDT_OFF, PWRT_OFF, BOD_OFF, MCLR_ON
define osc 4
Include "MODEDEFS.BAS"
StepM var WORD ; distance to move
delay var word ; time in ms between each step pulse
i var word
j var word
cmcon = 7
PORTB = 0
PORTA = 0
TRISB = %11100011 ;we are setting the ports to inputs or outputs
TRISA = %00100001 ;we are setting the ports to inputs or outputs
led var porta.1 ; I just put a power led for the PIC16f628 at PORTA.1
but var PORTB.1 ; ASIGN THE PIC16F628A PORTB.1 AS A PUSH BUTTON
Motor_Step VAR PORTB.2 ; ASIGN THE PIC16F628A PORTB.2 TO STEP PIN ON THE BIG EASY DRIVER
DIRECTION VAR PORTB.3 ; ASIGN THE PIC16F628A PORT.3 TO DIR PIN ON THE BIG EASY DRIVER
MS1 VAR PORTA.2 ;big easy driver microstep control
MS2 VAR PORTA.3 ;big easy driver microstep control
MS3 VAR PORTA.4 ;big easy driver microstep control
ENABLE_BED VAR PORTA.6 ;use this in order to cut current to the stepper motor, it is very important when motor is not doing anything
SLEEP_THE_BED VAR PORTB.4 ;WE DO MAKE THE BED GO LOW POWER MODE
CW con 1 ;HERE WE GIVE A CONSTANT DIRECTION
CCW con 0 ;HERE WE GIVE A CONSTANT DIRECTION
ACTIVATED con 0 ;THIS IS AN ACTIVATION SIGNAL FROM PORTA.6 TO ENABLE PIN ON BED (IS RELATED TO ENABLE_BED VAR PORTA.6)
DEACTIVATED con 1 ;THIS IS A DEACTIVATION SIGNAL FROM PORTA.6 TO ENABLE PIN ON BED (IS RELATED TO ENABLE_BED VAR PORTA.6)
SLEEP_BED CON 0 ;BIG EASY DRIVER IS GOING TO SLEEP
WAKEUP CON 1 ;BIG EASY DRIVER IS AWAKE
for j = 0 to 2 ; this is just a loop for flashing the LED
pauseus 100
low led ; power led for the pic in porta.1
pause 500
high led
pause 1
next
BEGIN:
LOW MOTOR_STEP ; WE CAN START BY MAKING LOW THE PORTB.2
LOW DIRECTION ; WE CAN START BY MAKING LOW THE PORTB.3
pAUSE 100 ; THEN WE GIVE 1 SECOND DELAY
HIGH MS1 ; MS1, as from the datasheet when all are high 16 microsteps can be applied
HIGH MS2 ; MS2, as from the datasheet when all are high 16 microsteps can be applied
HIGH MS3 ; MS3, as from the datasheet when all are high 16 microsteps can be applied
PAUSE 100
IF BUT = 1 THEN ;there is a button at PORTB.1, so if we do not push it
pause 100
ENABLE_BED = DEActivated ; the enable port on the big easy driver will remain high.
;as from the datasheed of the BED when high the output to the motor is dissabled.
SLEEP_THE_BED = SLEEP_BED
ENDIF
if but = 0 then ; WE HAVE CONNECTED A PUSH BUTTON AT PORTB.2
PAUSE 100
for j = 0 to 15 ;loop
ENABLE_BED = Activated ; will activate the PORTA.6 on the pic16f628A which is connected to enable pin on the Big Easy Driver
SLEEP_THE_BED = WAKEUP
pause 500 ; every step takes 500us
stepM = 1 ; the number of the motor steps
DELAY = 500 ; 500us between each step
direction = CW ; THE DIRECTION IS COUNTERWISE
gosub Rotation ; check ROTATION routin
next j
PAUSE 200
for j = 0 to 199 ;loop of 200
pause 1
stepM = 1 ; the number of the motor steps
DELAY = 300 ; 300us between each step
direction = CCW ; THE DIRECTION IS COUNTERWISE
gosub Rotation
PAUSE 5
next j
pause 200
for j = 0 to 199 ;loop of 200
pause 1
stepM = 1 ; the number of the motor steps
DELAY = 300 ; 300us between each step
direction = CW ; THE DIRECTION IS COUNTERWISE
gosub Rotation
PAUSE 5
next j
pause 200
stepM = 800 ; the number of the motor steps
DELAY = 100 ; 100us between each step
direction = CCW ; THE DIRECTION IS COUNTERWISE
gosub Rotation
PAUSE 2000
stepM = 800 ; the number of the motor steps
DELAY = 100 ; 100us between each step
direction = CW ; THE DIRECTION IS COUNTERWISE
gosub Rotation
PAUSE 200
for j = 0 to 1599
pause 1
stepM = 1 ; the number of the motor steps
DELAY = 5000 ; 100us between each step
direction = CCW ; THE DIRECTION IS COUNTERWISE
gosub Rotation
next j
PAUSE 2000
stepM = 1600 ; the number of the motor steps
DELAY = 50 ; 500us between each step
direction = CW ; THE DIRECTION IS COUNTERWISE
gosub Rotation
PAUSE 2000
stepM = 3200 ; the number of the motor steps
DELAY = 50 ; 100us between each step
direction = CW ; THE DIRECTION IS COUNTERWISE
gosub Rotation
PAUSE 200
for j = 1 to 15
pause 1
stepm = 1
delay = 300
direction = Ccw
gosub rotation
next j
pause 200
ENDIF
goto BEGIN
;THE FOLLOWING CODE SUPPOSE TO ROTATE THE SHAFT 1 STEP AT A TIME
ROTATION:
for i = 0 to (stepM - 1)
;@ BSF PORTB,2
;@ BCF PORTB,2
gosub StepIt
pauseUS delay
next
return
;ROTATION2:
;for i = 0 to stepM
;@ BSF PORTB,2
;@ BCF PORTB,2
gosub StepIt
; pauseUS delay
;next
; return
StepIt:
motor_step = 1 ;generate a 10us wide pulse on the step pin
pauseUS 10
motor_step = 0
RETURN
;thanks to Henrik at Melabs forum.
-
Re: Motor Stepper Example
On the following code you can make the motor to run fast in the beggining and slow at the end.
I did have high all the MS on the Big Easy driver so lets thing with 3200 microsteps per rev.
So what i'm trying to do is to give form low speed to high from 0 degrees home possition to 45 degrees and then from 45 to end of travel high speed to low.
Then back the same way to home possition.
Is there any other way to make it?
apart from that i would like to design a very small stupid cnc machine that i can give very basic shapes like square and cyrcle.
Then i will write a data base with all the letters and i will try to call them each time i need them from a file or lookup.
I know that i'm going to far now, i really dont know yet the basic of the PICBASIC. hehehhe.
Code:
'****************************************************************************************
'* Name : STEPPER MOTOR.BAS *
'* Author : [Leonardo Bilalis] GOT a HELP FROM HENRIK, Acetronics and Ioannis *
'* Notice : Copyright (c) 2013 *
'* : All Rights Reserved *
'* Date : 3/3/2013 *
'* Version : 1.0 *
'* Notes : Stepper Motor *
'* : *
'****************************************************************************************
'we are going to use the PIC16F628A for controling BIG EASY DRIVER
'********************************* PIC16F628A *******************************************
' ______________________________ *
' / _____________________________/| *
' | PIN 1 : RA2/AN2/VREF | | *
' | PIN 2 : RA3/AN3/CMP1 | | *
' | PIN 3 : RA4/TOCKI/CMP2 | | *
' 9 8 7 6 5 4 3 2 1 | PIN 4 : RA5/MCLR/VPP | | *
' ------------------------------------ | PIN 5 : VSS | | *
' | R R R R V R R R R *| | PIN 6 : RB0/INT | | *
' | B B B B S A A A A | | PIN 7 : RB1/RX/DT | | *
' | 3 2 1 0 S 5 4 3 2 | | PIN 8 : RB2/TX/CK | | *
' | | | PIN 9 : RB3/CCP1 | | *
' | PIC16F628A | |______________________________|/ *
' | | /_____________________________ /| *
' | R R R R V R R R R | | PIN 10 : RB4/PGM | | *
' | B B B B D A A A A | | PIN 11 : RB5 | | *
' | 4 5 6 7 D 7 6 0 1 | | PIN 12 : RB6/T10SO/T1CKI/PGC| | *
' ------------------------------------ | PIN 13 : RB7/T1OSI/PGC | | *
' 10 11 12 13 14 15 16 17 18 | PIN 14 : VDD | | *
' | PIN 15 : RA6/OSC2/CLOCKOUT | | *
' | PIN 16 : RA7/OSC1/CLOCKIN | | *
' | PIN 17 : RA0/AN0 | | *
' | PIN 18 : RA1/AN1 | | *
' |______________________________|/ *
'****************************************************************************************
@ DEVICE pic16F628A, intOSC_osc_noclkout, WDT_OFF, PWRT_OFF, BOD_OFF, MCLR_ON
define osc 4
Include "MODEDEFS.BAS"
StepM var WORD ; distance to move
delay var word ; time in ms between each step pulse
i var word
j var word
a var word
q var word
w var word
cmcon = 7
PORTB = 0
PORTA = 0
TRISB = %11100011 ;we are setting the ports to inputs or outputs
TRISA = %00100001 ;we are setting the ports to inputs or outputs
led var porta.1 ; I just put a power led for the PIC16f628 at PORTA.1
but var PORTB.1 ; ASIGN THE PIC16F628A PORTB.1 AS A PUSH BUTTON
Motor_Step VAR PORTB.2 ; ASIGN THE PIC16F628A PORTB.2 TO STEP PIN ON THE BIG EASY DRIVER
DIRECTION VAR PORTB.3 ; ASIGN THE PIC16F628A PORT.3 TO DIR PIN ON THE BIG EASY DRIVER
MS1 VAR PORTA.2 ;big easy driver microstep control
MS2 VAR PORTA.3 ;big easy driver microstep control
MS3 VAR PORTA.4 ;big easy driver microstep control
ENABLE_BED VAR PORTA.6 ;use this in order to cut current to the stepper motor, it is very important when motor is not doing anything
SLEEP_THE_BED VAR PORTB.4 ;WE DO MAKE THE BED GO LOW POWER MODE
CW con 1 ;HERE WE GIVE A CONSTANT DIRECTION
CCW con 0 ;HERE WE GIVE A CONSTANT DIRECTION
ACTIVATED con 0 ;THIS IS AN ACTIVATION SIGNAL FROM PORTA.6 TO ENABLE PIN ON BED (IS RELATED TO ENABLE_BED VAR PORTA.6)
DEACTIVATED con 1 ;THIS IS A DEACTIVATION SIGNAL FROM PORTA.6 TO ENABLE PIN ON BED (IS RELATED TO ENABLE_BED VAR PORTA.6)
SLEEP_BED CON 0 ;BIG EASY DRIVER IS GOING TO SLEEP
WAKEUP CON 1 ;BIG EASY DRIVER IS AWAKE
for j = 0 to 2 ; this is just a loop for flashing the LED
pauseus 100
low led ; power led for the pic in porta.1
pause 500
high led
pause 1
next
BEGIN:
LOW MOTOR_STEP ; WE CAN START BY MAKING LOW THE PORTB.2
LOW DIRECTION ; WE CAN START BY MAKING LOW THE PORTB.3
pAUSE 100 ; THEN WE GIVE 1 SECOND DELAY
HIGH MS1 ; MS1, as from the datasheet when all are high 16 microsteps can be applied
HIGH MS2 ; MS2, as from the datasheet when all are high 16 microsteps can be applied
HIGH MS3 ; MS3, as from the datasheet when all are high 16 microsteps can be applied
PAUSE 100
IF BUT = 1 THEN ;there is a button at PORTB.1, so if we do not push it
pause 100
ENABLE_BED = DEActivated ; the enable port on the big easy driver will remain high.
;as from the datasheed of the BED when high the output to the motor is dissabled.
SLEEP_THE_BED = SLEEP_BED ;WILL DEACTIVATE THE PORTB.4 ON THE PIC16F628A WHICH IS CONNECTED TO SLEEP PIN ON THE BIG EASY DRIVER
ENDIF
if but = 0 then ; WE HAVE CONNECTED A PUSH BUTTON AT PORTB.2
PAUSE 100
ENABLE_BED = Activated ; will activate the PORTA.6 on the pic16f628A which is connected to enable pin on the Big Easy Driver
SLEEP_THE_BED = WAKEUP ; WILL ACTIVATE THE PORTB.4 ON THE PIC16F628A WHICH IS CONNECTED TO SLEEP PIN ON THE BIG EASY DRIVER
pause 500
; every step takes 500us
stepM = 16 ; the number of the motor steps
DELAY = 300 ; 500us between each step
direction = CW ; THE DIRECTION IS COUNTERWISE
gosub Rotation ; check ROTATION routin
PAUSE 200
a = 0
for j = 0 to 400
pause 1
stepM = 1 ; the number of the motor steps
if a < 1200 THEN ; here we secure the a not to be negative value
DELAY = 1200 - a ; we put delay and substract a which in our case is a = a+3
ENDIF
a = a+3 ; we give a variable a for controlling the delay
direction = CCW ; THE DIRECTION IS COUNTERWISE
gosub Rotation
next j
a=0
for q = 0 to 400
pause 1
stepM = 1 ; the number of the motor steps
if a < 1200 then
a = a+3
endif
DELAY = a ; between each step
direction = cCW ; THE DIRECTION IS COUNTERWISE
gosub Rotation
next q
a=0
for j = 0 to 400
pause 1
stepM = 1 ; the number of the motor steps
if a < 1200 THEN ; here we secure the a not to be negative value
DELAY = 1200 - a ; we put delay and substract a which in our case is a = a+3
ENDIF
a = a+3 ; we give a variable a for controlling the delay
direction = CW ; THE DIRECTION IS COUNTERWISE
gosub Rotation
next j
a=0
for q = 0 to 400
pause 1
stepM = 1 ; the number of the motor steps
if a < 1200 then
a = a+3
endif
DELAY = a ; between each step
direction = CW ; THE DIRECTION IS COUNTERWISE
gosub Rotation
next q
a=0
ENDIF
goto BEGIN
;THE FOLLOWING CODE SUPPOSE TO ROTATE THE SHAFT 1 STEP AT A TIME
ROTATION:
for i = 0 to (stepM - 1)
gosub StepIt
pauseUS delay
next
return
StepIt:
motor_step = 1 ;generate a 10us wide pulse on the step pin
pauseUS 10
motor_step = 0
RETURN
-
6 Attachment(s)
Re: Motor Stepper Example
the jig and fixture model is ready.
-
Re: Motor Stepper Example
Nice job. Very detailed 3d Printing indeed!
Ioannis
-
Re: Motor Stepper Example
Quote:
Originally Posted by
Ioannis
Nice job. Very detailed 3d Printing indeed!
Ioannis
The Stratasys Fortus 400mc is one of the Best for 3D print. For design i used Catia V6. Programming the stepper motor used PICBASIC 2.50B
And got all the help from you guys. :D
-
Re: Motor Stepper Example
-
Re: Motor Stepper Example
Wow that sucker moves so fast! Impressive.
Robert
-
Re: Motor Stepper Example
I was impressed too by the speed of the motor. Wow!
Ioannis