PDA

View Full Version : Motor Stepper Example

Melanie
- 14th October 2003, 16:42
Whilst this was written some time ago, the subject crops up every now and again, so may be useful to some folks. Written for a 4-wire Stepper Motor...

Melanie

Squibcakes
- 28th October 2003, 00:55
Fantastic Mel.. I shall give this a go. You wouldn't happen to have a schematic of the circuit at all?

Melanie
- 28th October 2003, 07:18
I did this about a year ago on request from another piclist member. In the code you will see where the each individual windings are connected. The actual arrangement will depend on your stepper... naturally you will have to use a driver circuit suitable for your stepper's demand. If you want a test and a light-show, connect LED's (with series Resistors) to represent each winding.

DougB
- 6th July 2005, 18:41
Mel,

I see in your program you go to half step.
How difficult would it be to increase it 1/256th step?

I am trying to configure o stepper drive system
that would allow this. I am not sure about the coding tho.

DougB

Melanie
- 6th July 2005, 19:56

Do you really mean 1/256th of a single step... or do you mean there are 256 steps in a revolution and you mean 1/256th of a Revolution?

Most (though not all) stepper motors have four coils. We'll call them A, B, C and D. A step is when we rotate the shaft from one set of coils to the next... Let's also say that coil A is our starting sequence (it doesn't have to be, but for our example it is)...

A to B = A is energised, then de-energise A and energise B = 1st step
B to C = B is energised, then de-energise B and energise C = 2nd step
C to D = C is energised, then de-energise C and energise D = 3rd step
D to A = D is energised, then de-energise D and energise A = 4th step
the cycle now repeats from the top.

This is the conventional way a Stepper Motor rotates. Now the number of steps you have per revolution depends on your actual stepper motor. There's dozens and dozens of variants. Steppers are usually specified in a step angle (eg 1.8 Degrees per Step, or 7.5 Degrees per step), usually a figure divisible evenly into 360 (one rotation). So the 1.8 Degree stepper needs 200 steps for a revolution.

You can drive most Steppers in half-steps... like so...
A only is energised = 1st step
A to AB = B is additionally energised (A+B energised) = 1.5 step
B only is energised = 2nd step
B to BC = C is additionally energised (B+C energised) = 2.5th step
C only is energised = 3rd step
C to CD = D is additionally energised (C+D energised) = 3.5th step
D only is energised = 4th step
D to DA = A is additionally energised (D+A energised) = 4.5th step
the cycle repeats from the top

With this 'half-step' arrangement you now double the steps of your stepper making it twice as accurate. So if you had a Stepper that needed 200 steps per revolution (our 1.8 Degree Stepper above), you now can give it 400 half-steps to achieve the same result (which means we've an achieved an accuracy of 0.9 Degrees with our 1.8 Degree Stepper)

Naturally you can reverse the sequence from any point to drive backwards.

Now, can we take that 1.8 Degree step and achieve an accuracy of 1/256th of our 1.8 Degrees? - in other words 0.007 Degree micro-steps?... the answer is No. I've not seen a Stepper with such a complex coil phasing arrangement that it allows 256 micro-steps per individual step. But point me to a Datasheet if you know of one. Trust me, if it exists, we can drive it.

Luciano
- 6th July 2005, 22:08
0.007degrees per microstep

http://www.indoor.flyer.co.uk/stepperdriver.htm
http://www.allmotion.com/EZHR17ENDescription.htm

Luciano

Melanie
- 6th July 2005, 22:14
Very clever... using a standard four coil stepper too... must find out more how they've achieved this.

Luciano
- 6th July 2005, 23:10
A tutorial.

By Douglas W. Jones
THE UNIVERSITY OF IOWA
Department of Computer Science

Control of Stepping Motors

http://www.cs.uiowa.edu/~jones/step/index.html

Chapter 5:

Microstepping of Stepping Motors

http://www.cs.uiowa.edu/~jones/step/micro.html

* * *

Luciano

hansknec
- 8th July 2005, 18:23
You are trying to compare apples to oranges. Melanies code (or anybodys code) will only be able to get you to half step mode. That's the digital limit. Anybody who talks about microstepping is in reality using a stepper motor as a magnetic voice coil between steps. By varying the amount of current to one energized coil vs the other energized coil you can get the N-S poles of the rotor to line up anywhere between the two energized poles of the stator. This is analog circuitry which is often programmed using an 8-bit DAC and thats where the 256 number fits into the equation as a step size.

magand
- 12th July 2005, 15:23
I agree with hansknec.
I apologize for my english but..... is the stepper driver that give to the motor the right sequence for microsteps.
Trickle is to reproduce a sine wave using a current sensor on the motor phase and an AtoD converter (high number of bit = high number of microsteps).
Usually these devices are inside dedicated drivers for microsteps.
See ALLEGRO A3972 for 1/16 steps.
regards
andrea

mslaney
- 14th July 2005, 13:08
Get it?
On and off.... :-)

I'm having a problem...
In it's simplest form, would the following be a brute force method of driving a bi-polar motor?

(PIC 16F876)
(defines, includes, and OSC set....)
(tris a, b, and c set to 0)

speed var word
speed = 100 'just an arbitrary number - will eventually ramp up to max

Spin:
porta = %00000001
pause speed
porta = %00000100
pause speed
porta = %00000010
pause speed
porta = %00001000
pause speed
goto Spin

When testing this with LEDs, this seems to work fine - the LEDs flash separately. However, when I attach the motor, nothing moves and the LED's flash in pairs.

Pinout:
porta.0 -> IRL510 -> LED/CoilA
porta.1 -> IRL510 -> LED/CoilB
porta.2 -> IRL510 -> LED/CoilC
porta.3 -> IRL510 -> LED/CoilD

If it matters, it's a work project that is behind schedule. A quick answer would be truly appreciated. I might even send you a check for \$5 USD!!! :-)
Is there a single IC like the UCN5804 but for bi-polar motors instead of unipolar?

mslaney
- 17th July 2005, 22:24
I guess what I 'm asking is....Do I need an H bridge or is it a function of the program?

hansknec
- 18th July 2005, 02:23
If you are driving a bipolar motor you will need a dual H-bridge circuit. (2 coils each need dual driving polarity). The code presented earlier in the thread appears to me to be for driving a unipolar motor. Since you appear to be familiar with the allegro driver chips you should see that they would only require two outputs from a PIC to drive the motor. One is a direction bit and the other is toggled the amount of steps you want. I prefer using unipolar motors myself because they are easier to build a drive circuit using only 4 FETs.

John

DougB
- 20th July 2005, 21:20
I am using a microstepper from allmotion that microsteps at 1/256th step per step input

I plan to use a 16F676 to provide step & direction signals to the motor controller

I have a -5v to +5v input siggnal to the pic a/d

How do I configure the pic to apply the hightest resolution on the input
and calculate the steps & direction for the output.

I am stumped right now.

0v means the motor doesn't move.

the input signal is derived from an electrolytic tilt sensor condtioning board

hansknec
- 21st July 2005, 00:43
Well first of all you need to get that - 5V off the PIC before you burn it up. I'm surprised you haven't seen smoke yet. The voltage references need to stay between the range of 0 to Vcc. You are going to have to get yourself an op-amp and offset that -5V up to a zero volt potential and then divide your signal down. The resolution is what it is. You will get 10 bit resolution as the signal at the input pin varies between vref- and vref+ potential. If you want higher resolution or the ability to use your -5V to +5V signal directly then you will need a separate ADC. Funny... I don't see PIC16F676 as a device offered by Microchip.

mslaney
- 21st July 2005, 15:23
Thanks for the help.
I decided to get an L297 and L298 from ST. It'll drive a bipolar with a pulse from the PIC. However, there is WAY too much wiring to interface and use the L298. It works but it takes too much time.
Is there a single chip solution for a bi-polar motor?

I think DougB meant PIC16F876.

mister_e
- 22nd July 2005, 03:31
Funny... I don't see PIC16F676 as a device offered by Microchip.
Funny...
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1335&dDocName=en010213
and worldwide available from Digikey and others ;)

Luciano
- 22nd July 2005, 08:22
Bipolar Stepper Motor Driver with Translator

* * *
L6228 (1.4 Arms)
http://www.st.com/stonline/books/pdf/docs/9454.pdf

* * *
L6208 (2.8 Arms)
http://www.st.com/stonline/books/pdf/docs/7514.pdf

* * *
APPLICATION NOTE AN1451
L6208 FULLY INTEGRATED TWO PHASE STEPPER MOTOR DRIVER
http://www.st.com/stonline/books/pdf/docs/8607.pdf

Modern motion control applications need more flexibility that can be addressed only with specialized IC products. The L6208 is a fully integrated stepper motor driver IC specifically developed to drive a wide range of two phase (bipolar) stepper motors. This IC is a one-chip cost effective solution that includes several unique circuit design features. These features, including a decoding logic that can generate three different stepping sequences, allow the device to be used in many applications including microstepping. The principal aim of this development project was to produce an easy to use, fully protected power IC. In addition several key functions such as protection circuit and PWM current control drastically reduce external components count to meet requirements for many different applications.

* * *
APPLICATION NOTE AN1495
MICROSTEPPING STEPPER MOTOR DRIVE USING PEAK DETECTING CURRENT CONTROL
http://www.st.com/stonline/books/pdf/docs/8700.pdf

* * *
One project with L6208
http://www.otocoup.com/CarteL6208_e.htm#Specif

* * *

Regards,

Luciano

DougB
- 22nd July 2005, 14:18
Funny...
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1335&dDocName=en010213
and worldwide available from Digikey and others ;)

There is a 16F676 PIC, I have one on my desk.

mslaney
- 23rd July 2005, 14:03
Hey Mister E!!!
Thanks for the tips on the chips!

mslaney
- 23rd July 2005, 14:05
And you too Luciano. The drivers look like the right thing. I may continue hacking printer motors for my project.

I truly hate the L297/298 arrangement!

mslaney
- 23rd July 2005, 22:30
How do I cycle through an array in reverse order?

Ok, I ditched the bi-polar and I'm using a pic and mosfets to drive a uni motor. No translator.
I can spin the motor both ways with the following code but there has to be a more elegant way....I'm thinking I could cut the code in half.

Here's a clip of the program:

'Array vars
stepccw VAR BYTE(4) 'coils
stepcw var byte(4)
stepcw[0] = %10100000
stepcw[1] = %01100000
stepcw[2] = %01010000
stepcw[3] = %10010000

stepccw[0] = %10010000
stepccw[1] = %01010000
stepccw[2] = %01100000
stepccw[3] = %10100000

CW:
steps = 0
for delaytime = 1 to 27
steps = steps + 1
portb = stepccw[steps //4]
pause 10
next

pause 1000
steps = 0
CCW:
for delaytime = 27 to 1 step -1
steps = steps + 1
portb = stepcw[steps //4]
pause 10
next

pause 1000
goto cw

hansknec
- 24th July 2005, 15:55
It looks like this code could be valuable to quite a few users, so here you go. I originally developed this system about 15 years ago using basic stamps, but over the years it has had modifications as the hardware has changed and moved on to PICS. I work at a National Laboratory and wanted to save the Govt some money, so instead of buying commercial stepper systems I developed my own. The system began as a single stepper that needed to be moved remotely via RS-232. It eventually grew to a requirement to drive about 10 separate (UNIPOLAR) motors on a laser table to operate attenuators and X-Y linear stages. The theory behind the system is each stepper has its own PIC. All PICS are sitting on a single RS-232 bus in parallel. Each PIC "listens" for its name to be called and reponds only when called. This avoids any chance of a communication collision. The code will work for any number of PICS placed on the single RS-232 bus until you have loaded down the impedance of the bus so far that you cannot sustain communications. I have never needed more than 10 steppers on a single bus, but if you work out the math you could calculate the maximum. This code doesn't use the USART pin, so I have a 20K resistor on each PIC for the SerIn pin.
Hardware: Each stepper motor is on an apparatus that has a limit switch for the "home" position. If a power failure occurs or if requested to reset via the RS-232, it will drive the stage home and reset its counter to zero position.
I have found that the system is so reliable that I do not need to have additional encoders to sense the position. If you ensure that your motor is not overloaded and drive it with the proper current, it will never slip poles and the software count of current position will be quite accurate.

Software: The code follows. This same software is loaded into each PIC on the system, but the name and travel limit settings are changed for each . I have another version of the code that allows one to input a new name and travel limit via the RS-232 into NVRAM, but I felt it was overly complicated for sharing.

Command protocol: A terminal program (terraterm or equivalent) would send the name of the stepper followed by a space and then the new postion command or a special control code.
Example 1. Send the stepper to reset position:
ATTENB 65535<CR>

Example 2. Move the stepper to position 415:
ATTENB 415<CR>

Example 3. Ask the stepper its current position:
ATTENB 65534<CR>
The PIC would then respond with "AttenB @ 415"

Heres the PIC code:

'************************************************* ***************
'* Name : StepNetwork.pbp Target=PIC16F84 OR F628 *
'* Author : John C. Hansknecht *
'* Notice : Copyright (c) 2004 *
'* Date : 10/14/04 *
'* Version : 1.0 *
'* Notes :
' Universal code for all stepper controllers in a serial network.
' Only the name and travel limit
' are changed for the specific device.
'************************************************* ***************
'_______ Declarations __________________________________________________ ___
PAUSE 500
include "modedefs.bas"
'@ device INTRC_OSC_NOCLKOUT 'default to internal oscillator
'@ device WDT_ON 'watchdog timer on
'@ device PWRT_ON 'power on timer on
'@ device MCLR_OFF ' master clear option off
'@ device BOD_OFF ' brown out detect
'@ device LVP_OFF ' low voltage programming off
'@ device PROTECT_OFF ' program code protect off
Steps var word
I var word
newval var word
oldval var word
resetbit var byte
b0 var byte
pins var portb
endstop var portb.4
define osc 4
'_______ Initializations __________________________________________________
TRISB = %10110000
pins = 0 ' Initialize output all output transistor OFF.
oldval = 0
input portb.5 'make serout a high impedence so all others can talk
'________Main program___________________________________________ ___________
pause 500
reset: resetbit = 0
if endstop = 0 then del1 'take motor CCW to limit switch on pwrup.
oldval = 0

Cmd:resetbit = 1
serin 7,N2400,["ATTENB"],#newval ' Get orders from terminal.
if newval = 65534 then report 'just report current position
if newval = 65535 then reset 'send motor to limit switch
if newval > 600 then Cmd ' My own software motion limit
if newval < oldval then delstep
if newval = oldval then Cmd
' else we will drop into addstep (inferred goto)
steps = newval - oldval
for I = 1 to Steps ' Number of steps.
for b0 = 0 to 7 'full step mode used here.
LOOKUP b0,[9,1,3,2,6,4,12,8],PINS 'forward drive sequence
pause 1
next b0
NEXT I
oldval = newval 'make oldval reflect the current position.
goto Cmd

delstep:
steps = oldval - newval
FOR I = 1 TO steps
del1: for b0 = 0 to 7 'enters sub here if resetting motor
LOOKUP b0,[12,4,6,2,3,1,9,8],PINS
pause 1
next b0
if resetbit = 0 then reset 'exit this sub if resetting motor
NEXT I
oldval = newval
goto Cmd

report: serout 5, N2400, ["ATTENB @ ",#oldval,13]
input portb.5 'make serout pin an input so others can talk
goto Cmd

'___ End of program __________________________________________________ ______

Hope you find it useful. You should realize of course that this serial network is not limited to stepper motors. I also have the ability to remotely send values to DAC's, position Servos, Control and readback digital I/O and remotely read ADC's and via the same network using very similar code that has been modified for the application. I call the system PSUB. (polarized source universal bus)
John

pflex1
- 30th August 2005, 06:45
Melanie,

Are you interesed in developing PICBasic PRO source code on a consulting basis for new product development project?

Thanks

Tom

Melanie
- 30th August 2005, 07:06
Ohhh... a proposition! I just knew the ad in the back of the dodgy personals would pay off eventually! If you really want to wreck your Bank Managers day Tom, message me off list.

DougB
- 19th October 2005, 19:40
Hi all,

I am still working on the stepper tilt project,
I am looking to do the following:

1) recieve an ascii string (similar to nmea) on a port pin serially from
a signal conditioning board, the string loks loke this in hyperterminal:

\$ 0.00,16112.95,21.96,N6374

2) I need to parse this string and keep only the 16112.95 value or whatever value it happens to be.

3) I need to take this value and compare it to a stored value and if it is less
it sends the appropriate count to a port pin as pulses to a stepper driver

4) I also need it to determine the sign of the number and output a high for + and a low for -.

Currently I am using a 16F627A, There are a few more things needed but this is the main parts.

Help!!!!

sean-h
- 19th October 2005, 20:47
Hi Doug

Am in the middle of this myself but have NOT gone down to the level of energising each coil as per example.

I opted for a dedicated controller board to run the motors so the controller box could be ran using popular software for CNC such as Kcam and Mach2 etc via the parallel port, but also so people could use the processor board for controlling the motor circuit direct.

By using this method, all your Pic has to do, is supply 2 pins to control each motor.
One for direction High/Low and the other for Pulses using PWM from the Pic or simple toggle high and low with a pauseus in between each step.

On the CNC machine I have built, the motors along with the thread on the shaft work out at 533 pulses per 1 MM in eight step mode and decrease as you step up.

If interested, please check out http://www.cncdudez.com and under the development link you will see the protocol I have gone with to control the motors via USB or Serial using a 18F4550 chip.

The commands shown at the moment are for Jog mode, but am working on taking raw G-code and feeding it straight into the Pic, which is going well.

The huge difference between using a Pic to control the pulses and using a PC Parallel port is incredible. There is too much going on in windows that disturbs the pulses and the motors run so noisey.
Using a Pic lets you hit incredible speeds and hardly any noise of the motors.

Regards

Sean.

hansknec
- 14th December 2006, 15:31
I must first correct myself from an earlier comment. One can digitally microstep using a pic using fast comparator inputs triggering interrupts and such. There is an application note about this at the microchip website, but the code is assembly and rather complex.

Now that I know better, I would also steer away from the simple code driving 4 FET's, because the resulting torque you get from your motor is poor. (unless you don't care about torque).

There is a new DMOS Microstepping driver with translator chip available from Alegro MicroSystems that can perform up to 1/16th microstepping precision and drive up to 2.5Amps/phase right from the chip.

The chip is the A3979.
http://www.allegromicro.com/datafile/3979.pdf

The chips are only about \$5 ea in single quantity.

Now you can write your pic code to simply count steps and give a direction bit. With the code space you save, you have more room available for cool things like trapazoidal acceleration/deceleration curves and speed control.

Enjoy

Archangel
- 14th December 2006, 17:25
Ohhh... a proposition! I just knew the ad in the back of the dodgy personals would pay off eventually! If you really want to wreck your Bank Managers day Tom, message me off list.:) ;) :)

apollo21
- 16th May 2008, 15:52
Whilst this was written some time ago, the subject crops up every now and again, so may be useful to some folks. Written for a 4-wire Stepper Motor...

Melanie
I live in Japan.
I do not understand English.
I write sentences with a translation machine.
I arranged the circuit and the operation theory of Maicrostepping.
It comparatively puts out the angle ratio by using a power element, detection resistance, and an analog comparator referring to the table.
It ..program.. doesn't actually have the storage power though I hope for the assembly language because of operation speed.

http://2.suk2.tok2.com/user/nonnno-protok2/img/2008-02-16-0.JPG
ttp://2.suk2.tok2.com/user/nonnno-protok2/img/2008-02-11-0.JPG
ttp://2.suk2.tok2.com/user/nonnno-protok2/img/2008-02-03-1.JPG

About the MICRO STEPPING STEPPER drive circuit.
The image and the circuit chart that I displayed here are ,so to speak, informal signboards and an accurate appearance is not drawn.
I can continuously write Sa and details of the circuit.
I can show the table of the voltage ratio of two aspects and the table of the decrease rate of both aspects.
No one showed the interest when this matter was shown in Japan, and it was not possible to develop into Maicrostepping.
You can able to make an effort to someone's serious approach of this method, and the completion of the program and promise to open it to the public in the place of the lever?
If it is so, it wants to announce everything I learnt, to catch everyone's opinion, to put, and to make a better device.
I believe that this function can be composed of comparatively cheap PIC and AVR, etc.
Even if I do not use special IC such as "Sunken" "ST" "NS", I can enjoy this function.
In addition, I :. ｢Pawa?Switch｣Separates and it is possible to correspond also to the motor drive of "Super-Big".
It believes if it is possible to change to the one, a damage repair, and a bigger power element easily.
Kyoji
There are a lot of "I" because "Translation machine" demands "Subject".

There are "Switching type" and "Pawardoropp type" in the fixed current drive of "stepper", and "Switching type" doesn't generate extra heat, and be able to use the electric power effectively.
To characterize, to detect the current, and to convert it into the voltage, the main current route is finished by a low voltage.

"Unipolar" Maicrostepping is written in the starts most.
Is this an easy circuit and Pawa of 70% of "Bipolar"?Can be demonstrated and it makes it to "Bipolar?" in an easy change.
The feature of "Unipolar" is a current in the energizing period in which momentary increases by sink "Inductance of the rolling line" at the drive as for the current in a positive direction.
This current is put for the interception period and changing momentary ..current sucked up.. decreases from ground in the opposite direction.
I think about the constant current of 3A as an example and am seeing.
It should be a voltage of "1.0V from 0.5" because it suppresses the electric power loss though the current of these positive and negative is converted into the voltage of the positive and negative by the detection resistance.
In addition, it is rectified, it converts into the signal in a positive direction, and it transmits to the following circuits by the current of 10 considerable mA.
A positive direction obtains the absolute value current comparatively easily by "Level shift" and the direction of negative using "Level shift" and "Break out" in this circuit.
An integrated current can be controlled by easily adding the mean value of these both aspects to a smooth comparator.
In addition, driving power smooth at all circumference angle can be generated by tempering with the decrease rate according to driving angle.

apollo21
- 7th June 2008, 06:40
Whilst this was written some time ago, the subject crops up every now and again, so may be useful to some folks. Written for a 4-wire Stepper Motor...

Melanie
I might be doing an impolite way to come.
Please forgive me if it is so.

Does this code have the ability to output the frequency pulse of
which place?
Do its deflecting have the ability to enable Maicrostepping?

I am looking for the PWM code of PIC16F648A to enable Maicrostepping.

Please show if there is something that becomes some references.

apollo21
- 22nd December 2008, 14:22
hi.

The following image is a connection of two aspect Yuniporrasteppingmortarmaicrostepping to
LED.
It tries to build the fixed current circuit into following.
It was named Raundorimittoscear.

mataston
- 12th June 2009, 01:23
i came accros this thread by accident but looks like it maybe what i need.
i have a bipolar stepper motor, plus a driver chip. will this code work on a pic16f84a ?? also i need an imput pin for step pulses and a direction pin, trying to see in the code if this exists

luxornet
- 25th March 2010, 07:30
I tried to figure a schematic for Melanie code.
Both motors seem to work in the same way.
The second motor has a ULN2003 driver (ULN 2803). My question is if I choose the right schematic for driver configuration.
Thank you

aratti
- 25th March 2010, 11:03
My question is if I choose the right schematic for driver configuration.

First of all never connect directly to the pic port any motor ( max current you can supply is 25 milliAmps) but use a current driver as the ULN2803.

ULN2803 is an array of 8 NPN darlington transitors, that you can connect directly to the port pin without any other passive components (no resistors needed) so with one ULN2803 you can drive two unipolar stepper motor with 350 milliAmps per coil.

From the schematic the motor appears to be bipolar, and if they have only 4 wires there is no way to connect them as unipolar.

Google for unipolar stepper motor to understand the difference.

Al.

Nozapo
- 15th January 2011, 13:12
Whilst this was written some time ago, the subject crops up every now and again, so may be useful to some folks. Written for a 4-wire Stepper Motor...

Melanie

Hello Mel, Everyone,

I`d like to know how to get the code into the chip, (might sound silly), but it doesn`t seem to want to compile in any of the software.

I`ll more than likely be using a 6 wire stepper motor, but shouldn`t matter, as there`s still only 4 to drive the coils, the other two are power wires.

Have tried a few things, like using PortC outputs on pin 5,6,7,8 instead, that`s just by changing the portB to portC, no go either.

Other items would be,
9&10 for LED output, 11 for step, 12 for Direction, 13 for Enable, pins 1&2 to pull hi / low, via jumpers or 2 switch DIL component. (1on/2on, 1off/2on, 2on/1off, 1off/2off)

Any assistance at all would be appreciated immensley.

Thank you.

ScaleRobotics
- 15th January 2011, 14:08
Hello Mel, Everyone,

I`d like to know how to get the code into the chip, (might sound silly), but it doesn`t seem to want to compile in any of the software.

What version of PicBasic Pro do you have? Melanie's example from post#1 seems to compile fine over here. I tried the 16F877.

You might also be interested in this microstepping example http://www.picbasic.co.uk/forum/content.php?r=225-Microstepper-controller

and these: http://www.picbasic.co.uk/forum/content.php?r=39

Acetronics2
- 15th January 2011, 16:36
Hello Mel, Everyone,

Other items would be,
9&10 for LED output, 11 for step, 12 for Direction, 13 for Enable, pins 1&2 to pull hi / low, via jumpers or 2 switch DIL component. (1on/2on, 1off/2on, 2on/1off, 1off/2off) MS1, MS2 ???

Thank you.

May I understand you use a dedicated driver circuit, Allegro 3967 i.e. ??? perhaps some existing satellite Module ???

Alain

apollo21
- 16th January 2011, 02:36
[QUOTE=apollo21;68104]hi.
Greetings
Since I posted the previous post, that many more experienced.
Allegro One solution would be achieved at IC.
Another, however, set a vector of phase 2 D / A converter in the conduct,
Sorted by comparing the current signal value and the output switching element is a way to drive.
The current regulation requires the ability to fit the criteria.
Vector miracle two-phase partitioning error occurs from an angle and draw a circle, the intermediate value of 0.7 instead of 0.57 has been announced to be correct.
D / A converters to simplify the PWM can be used.
1bit PDM is called conversion ways, by number, because repetition is undefined, and is not suitable for such use.
MPU is a 2ch analog 2ch and needs of the PWM output.
If you seek to maintain the accuracy of the angular speed 8bit pulse repetition frequency should not be limited to around 6bit.
Thus repetition frequency 6bit seeking 8bit accuracy,
If you use a decentralized approach four times, without sacrificing the angle, fast response can be obtained.
(Many more bit of resolution can be given)
I did a mathematical analysis of Open Office Calc functions.
http://den-nekonoko.blog.so-net.ne.jp/_images/blog/_615/den-nekonoko/WS000539.JPG
Can stay if you need more, "6bit_pwm.ods" We attached.
Yours sincerely

cncmachineguy
- 16th January 2011, 03:15
Hello all, after lots of posts with some other members here, and lots of digging on my own, I came to the conclusion for anything over 500mA drive capacity, it really is less expensive to go with a dedicated chip solution. This is when you figure in board space, manufacturing cost, ...

Just my 2 cents FWIW

Nozapo
- 16th January 2011, 16:42
What version of PicBasic Pro do you have? Melanie's example from post#1 seems to compile fine over here. I tried the 16F877.

You might also be interested in this microstepping example http://www.picbasic.co.uk/forum/content.php?r=225-Microstepper-controller

and these: http://www.picbasic.co.uk/forum/content.php?r=39

Hi Scalerobotics,
Thanks for the other couple of links, I`ll have a look.
One of the latest versions, but see the attachment one screen capture of BasicPro & one from Picaxe compiler and their respective messages.

Note, I don`t have any headers etc. just copy pasted the code as is, oh, and with different port#`s, which hasn`t made any difference to the error messages.
In any case, the 16F877 isn`t any good to me, my requirements/specs are for a 14 pin Pic.
I`d have to reconfigure the code to suit again, so there`s Picaxe14M/2, 16F684, 16C505, 16F505, 16F676,etc, or if I`m not fussy, I can get really universal and put one of those AVR chips in if they`re easier to configure, that way both camps can enjoy (a balance in the force, perhaps ha,ha).

Thanks again:)

Nozapo
- 16th January 2011, 17:11
May I understand you use a dedicated driver circuit, Allegro 3967 i.e. ??? perhaps some existing satellite Module ???

Alain

Hello Alain,
Not quite sure of your response, so as I understand it, I`ll respond...
The Allegro won`t fit either,( but having a look at it, you`ve given me another idea, on which I must think on).
Just for the following unit I have constructed, 3 separate ICs, not "networked".
looking at a few alternatives.
5087

Thank you

mackrackit
- 16th January 2011, 17:15
If you are not using Pic Basic Pro the examples from this forum will not work. Nor will they work for an AVR chip or picaxe.

thenew
- 3rd March 2011, 09:11
please help me..we have a project using stepper motor..we are making simple linear robot..when the circuit is on it will automatically open the gripper then after a seconds it will close the grip then after a seconds the slider stepper will turn forward then after a seconds again the gripper will open again then close and back to its original position..

please give me sample code or correct my code because when i tried it the gripper will only vibrate:

clear
ti var byte
c var byte
z var byte
ti=100
trisb=0

gripper open(cw the gripper stepper):
for z=1 to 2
portb=128
pause ti
portb=64
pause ti
portb=32
pause ti
portb=16
pause ti
next z
goto delay

gripper open1(cw the gripper stepper):
for z=1 to 2
portb=128
pause ti
portb=64
pause ti
portb=32
pause ti
portb=16
pause ti
next z
goto delay3

close(reverse gripper stepper):
for z=1 to 2
portb=16
pause ti
portb=32
pause ti
portb=64
pause ti
portb=128
pause ti
next z
goto delay1

close1(reverse gripper stepper):
for z=1 to 3
portb=16
pause ti
portb=32
pause ti
portb=64
pause ti
portb=128
pause ti
next z
goto delay4

delay:
portb=0
pause 2000
goto close

delay1:
portb=0
pause 2000
goto loop
delay2:
portb=0
pause 2000
goto open1

delay3:
portb=0
pause 2000
goto close1

delay4:
portb=0
pause 2000
goto loop1
delay5:
portb=0
pause 2000
goto open

loop(cw the slider):
for c=0 to 11
portb=8
pause ti
portb=4
pause ti
portb=2
pause ti
portb=1
pause ti
next c
pause 2000
goto delay2

loop1(ccw the slider):
for c=0 to 11
portb=1
pause ti
portb=2
pause ti
portb=4
pause ti
portb=8
pause ti
next c
goto delay5
end

luxornet
- 4th March 2011, 08:28
If a stepper motor vibrate this is happen because the power supply is too low or the rotation speed (given by the code) is too high.

thenew
- 4th March 2011, 11:22
what is the correct speed or delay am i using in order to run it well?

it necessary to define the external oscillator in the program?like:

define osc 4

cncmachineguy
- 4th March 2011, 12:53
We really need to know more about the hardware. Are you using a uni-polor or bi-polor motor? How are you driving the motor? direct from the PIC (REALLY small motor) or with some kind of amplifier/driver? I don't think you are pulsing too fast, if you have a 200 step motor, you are asking it to move at 5 RPM (i think,)

Can you make the motor move by replacing the pic with 4 switches? this will simulate the pic to test the hardware. Although this still will not tell you about current needs.

thenew
- 4th March 2011, 15:24
i am using unipolar stepper motor...i connect the pic to uln2803...then connect to the stepper motor...

in your own experience..what time delay usually used in order the stepper to operate properly?

cncmachineguy
- 4th March 2011, 16:26
delay time all depends on how fast you need it to move.

Please do some simple troubleshooting for us. Replace the motor with LED's. then you will be able to see if all 4 lines are stepping. Vibrating can also come from a missed step or 2. Also could you please apply 5V to the input of the ULN, 1 at a time and see if the motor moves?

This problem really needs to be isolated to hardware, software, or both. At first glance, it seems like your program should work. I don't know if you will get your desired function, but the motor should move.

aratti
- 4th March 2011, 16:43
Check out all the labels in your code! You have names that you never call. Example:

goto close1 your goto will never goto label close1, since you have named it close1(reverse gripper stepper):.

Either move the : to the correct place and rem the rest of the label or place the correct name under the goto instruction.

Close1: ' (reverse gripper stepper)

GOTO Close1

or

close1(reverse gripper stepper):

GOTO close1(reverse gripper stepper)

Timing is not that critical. If too long your motor will run slowly if time delay is too short your motor will not run at all.

Cheers

Al.

thenew
- 4th March 2011, 17:51
thank you for your help..i will try your suggestions...the label close1(reverse gripper stepper) , i only edit it before i post the program so that to clarify the operation of my project..actually is only close1:

mark_s
- 4th March 2011, 18:16
If it still does not work after you clean up your code. You may just have one of your coils reversed on your stepper motor. This will cause the motor hum and not rotate. A schematic of your project would help.
good luck

thenew
- 5th March 2011, 11:20
what's wrong when i on my circuit..the led i used as indicator for the sequence will only standby on the first step..and when i touch the positive side of the supply it start to run..i construct my circuit in the breadboard as of now for trial...

aratti
- 6th March 2011, 17:57
..and when i touch the positive side of the supply it start to run

Did you pulled up with a 10K the MCLR (reset pin) of your microcontroller?

Why don't you post your circuit?

Al.

thenew
- 7th March 2011, 10:55
i didnt use the MCLR in my mcu...

what is the right thing to do?

mackrackit
- 7th March 2011, 11:06
You might be able to turn MCLR off but we do not know the chip you are using.
Post your configs, complete code, and schematic so the folks here will be able to help you.

thenew
- 7th March 2011, 15:10
the schematic above is the one i used to...

i am using pic16f84 and uln2803...

here is the code:

define osc 4
ti var byte
c var byte
z var byte
ti=40
trisb=0
open:
for z=0 to 25
portb=128
pause ti
portb=64
pause ti
portb=32
pause ti
portb=16
pause ti
next z
goto delay

open1:
for z=0 to 25
portb=128
pause ti
portb=64
pause ti
portb=32
pause ti
portb=16
pause ti
next z
goto delay3

close:
for z=0 to 25
portb=16
pause ti
portb=32
pause ti
portb=64
pause ti
portb=128
pause ti
next z
goto delay1

close1:
for z=0 to 25
portb=16
pause ti
portb=32
pause ti
portb=64
pause ti
portb=128
pause ti
next z
goto delay4

delay:
portb=0
pause 2000
goto close

delay1:
portb=0
pause 2000
goto loop
delay2:
portb=0
pause 2000
goto open1

delay3:
portb=0
pause 2000
goto close1

delay4:
portb=0
pause 2000
goto loop1
delay5:
portb=0
pause 2000
goto open

loop:
for c=0 to 275
portb=12
pause ti
portb=6
pause ti
portb=3
pause ti
portb=9
pause ti
next c
pause 2000
goto delay2

loop1:
for c=0 to 275
portb=9
pause ti
portb=3
pause ti
portb=6
pause ti
portb=12
pause ti
next c
goto delay5
end

aratti
- 7th March 2011, 16:30
From the schematic you have posted, you have left the MCLR pin floating, and this is the reason of the problem you reported. Connect the MCLR pin to Vdd (+5V) using a 10K resistor. If you need to reset your chip then short the MCLR pin momentarely to ground for a second and than remove the short.

Did you connect ULN2803 to ground ?

Al.

thenew
- 8th March 2011, 01:12
yes i connect the uln2803 to ground...

should i put switch to MCLR? or just connect to 10K and +5v?

it is ok to connect the 4.7k resistor directly to the supply and MCLR without switch?

mackrackit
- 8th March 2011, 02:01
Look at the example in the manual.

thenew
- 8th March 2011, 12:30
my previous problem solved..:)

do you have an ides how to program using sensor as input? That when it is high something would activate then when it is low still activate.

Shermona7
- 23rd February 2013, 13:59
hey!!! im using a simple bipolar stepper motor with L297 and L298N and want a degree resolution of 1 degree, both in CCW and CW directions. but im clueless about the coding.

HenrikOlsson
- 23rd February 2013, 15:14
The resolution depends on the motor and the type of stepping sequence you do.
Most step-motors from have 200 "natural" steps per revolution - or a step angle of 1.8° per step.
The L297 can do either full- or half-step depending on the state of pin 9. So if your motor has a step angle of 1.8° you get either that or 0.9°
If you need better than that you need to change the motor to one with a more steps/rev (motors with 400 steps/rev are available but not very common) or change the driver to one capable of microstepping.

The L297 is pretty easy to drive. You set the CW/CCW pin high or low depending on which direction you want the motor to move and then give it a pulse on CLOCK pin for each step. Want to move one rev, send a total of 400 pulses (if you're halfstepping with a 1.8°/step motor).

/Henrik.

Shermona7
- 24th February 2013, 04:48
ok!!so the stepper i intend to use is
http://www.geeetech.com/wiki/index.php/Stepper_Motor_5V_4-Phase_5-Wire_%26_ULN2003_Driver_Board_for_Arduino
so basically to get a resolution if 1 degree i need to use 0.9 is it? and half and full steps isnt an issue?

would you be able to provide me with the required coding? as im VERY new to C language as well!!!! and thanku soo much for ur reply :)

HenrikOlsson
- 24th February 2013, 08:14
Hi,
First off all, that motor is an unipolar motor with a single common connection for both windings, it can not be driven by a L298.
The documentation for that motor isn't really that great. It looks like there's a gearbox on the motor but I can't figure out if the stated step angle of 5.625° includes the gearbox, if there is one. If 5.625° includes the gearbox then you won't be able to get 1° resolution. Fullstep would give you 5.625°, halfstep 2.8125°.

Finally this is a forum for PBP, most of us don't do C so if you're looking for that you're in the wrong place basically.

astanapane
- 28th February 2013, 12:30
Hi all,

long time no see, because i'm o the business trip and really didnt have a time to meet you.

I have a question.

First of all i havent open a new thread because it is already one here.

Now let me explain you what i would like to do.

I have the following parts in my lab and would like to do some experiments. Simple not very difficult in the begining.

1) Big Easy Driver (https://www.sparkfun.com/products/10735)
2) Stepper Motor with Cable (https://www.sparkfun.com/products/9238)

Then to control the easy driver i have a pic16f88. I hope that is enough or do i need a bigger microcontroller with 4 different ports?

I understand that i need to connect from the controller to the Big Easy Driver 3 pins.

The GND, the STEP, and the DIR.

My question is: The step has to be a PWM signal? The direction pin is only high or low?

It is would be nice if anyone has a similar questions and will discuss several options.

i just want to give to the stepper motor a simple directions and basic step rotation....not microstep. I know that i can do with the Big easy driver, but i would like to start from the very basics.

thanks a lot for any help in advance.

regards,

aratti
- 28th February 2013, 20:45
Hi astanapane, pic16f88 is more then you need to make your motor turn CW and CCW. Just connect two digital outputs plus the ground to your big easy driver and you are done. Have the pin connected to step toggling with a pause in between shorter is the delay faster the motor will run. Pull high direction pin when you want invert the rotation.

That is all.

Cheers

Al.

astanapane
- 1st March 2013, 04:49
Thanks a lot for the reply. i will give it a little bit more time for searching what i need To do.
Actually i would like to see how i can give a single Step at a time by using pwm.

Ioannis
- 1st March 2013, 07:39
Hi astanapane.

One pulse on STEP input moves motor one step only.

Ioannis

astanapane
- 1st March 2013, 13:52
Hi astanapane.

One pulse on STEP input moves motor one step only.

Ioannis

Hi Ioanni

I will do some tests on the Code and once i have some Good news i will let you Know.

At the moment I'm on the 3d design of the model. Actually i design a Base for the stepper Motor.

May i ask you something that might sound stupid?

On the Motor i have it is recognised as a 1.8 degree angle.

They do say that is a 200 step per revolution.

If i turn the axis by hand i feel the steps but as i measured is not more that 50 steps.

Am i right or what i try to count doesnt have to do anything corresponding to its operation and the 200 steps.

Ioannis
- 1st March 2013, 14:07
The actual steps have nothing to do with what your are feeling turning it by hand.

If you follow the step pulses then you will confirm the 1.8 degrees/step. With microstepping even less, but this is complicated for now.

Ioannis

astanapane
- 1st March 2013, 14:38
thanks for the clarification.

i will do my tests and will upload my code.

HenrikOlsson
- 1st March 2013, 16:12
The 50 detents you feel when rotating the shaft by hand comes from the 50 magnetic poles of a standard 1.8° 2-phase motor. Had it been a 0.9° 2-phase motor you would have felt 100 detents.
Moving the rotor between two "natural resting positions" (the detents) takes 4 electrical steps (the 90° phase shifted signal you use to drive the motor). 4*50=200 steps.

/Henrik.

astanapane
- 1st March 2013, 21:35
That is really interesting.

I learn from you guys everyday. Thanks a lot.

New i only need to understand how To give a step and a direction at the same time

HenrikOlsson
- 1st March 2013, 22:09
Basically you don't.
You set the direction signal high or low depending on the desired direction, then you start sending out the step-pulses. You want to make sure the direction signal is in its correct state before the step-pulse goes out. If it's not the motor might move a step in the wrong direction.

astanapane
- 2nd March 2013, 04:01
This sounds much easier than i thought.
Thanks:-D

astanapane
- 2nd March 2013, 07:01
This morning i dissasembled the stepper Motor.

I found out that There are 8 magnets with 6 teeth magnets each.

That in the middle you can find the axis shaft with a gear which the 50 teeth slighty touch the outer magnets teeth.

That is interesting.

HenrikOlsson
- 2nd March 2013, 08:56
Hi,
Actually, the magnets are in the rotor. The 50 teeths on the "gear" are alternating N/S poles of the magnets.
The stator contains the windings which, together with the iron teeths forms elctromagnets. By running current thru the windings the magnetic field developed eitther retracts or repells the magnet in the rotor. By changning the direction of current flow thru the winding the polarity of the magnetic field changes, kind of draging and/or pushing the rotor in either direction.

When the motor runs there will be a rotating magnetic field in the stator and the rotor follows this field. When the torque reaches a point where the rotor "lags" the rotating magnetic field too much it will "desynchronize", if the speed is high it will just plain stall. The good thing is that you can never destroy a step-motor by simply overloading it.

The rotor does not touch the stator at all, there's a VERY fins airgap between them. You should never open a step-motor in a dirty environment since, the magnets in the rotor are very strong and will "suck in" small metallic chips from your workbench which will end up in the fine airgap and either lock the motor up completely or make run "strange".

Depending on the type of magnetic material in the rotor it's also advised against removing the rotor from the stator as it can then loose some of its magnetism. Same reason why you have what's called a keeper-plate on "ordinary" magnets. With that said I've personally had several motors appart to drill holes thru the shaft and they have been working afterwards. I don't have anything to measure the actual torque but I couldn't "feel" any difference.

/Henrik.

astanapane
- 2nd March 2013, 17:41
Henrik,

It is very kind of you for giving all these detailed info.

I completely understand now the principles of the stepper motor.

What i have done today is to design a Base as a jig and future, in order To Work easier.

My next design will be a circle with 200 pins that represents the steps. then from the motor's axis i will put a vertical pin that will follow the 200 pins on the outer circle.

At the moment the following Mode is completed.

astanapane
- 3rd March 2013, 07:29
sorry for uploading pictures of the design, but would like before starting with the simple code, to give you a feeling of the jig and fixture model.

So what you see is a model that has 200 steps inside the circle, and 16 points outside (8 sharp and 8 with a hole)

Those can represent basic angles on a 360.

When i have them ready then i can start to right one or two lines of program to play with.

After that i will improve the code with your help :)

thanks a lot.

astanapane
- 3rd March 2013, 18:12
Dear All,

I dont Know if the following Code will MAKE the Motor To rotate.

I would like to have your opinion.

'************************************************* ***************
'* Name : UNTITLED.BAS *
'* Author : [Leonardo Bilalis] *
'* Notice : Copyright (c) 2013 *
'* 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

HenrikOlsson
- 3rd March 2013, 18:39
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.

Main:
If But = 0 THEN
GOSUB StepIt
ENDIF
Goto Main

END

StepIt:
High Motor_Step
Pause 1
Low Motor_Step
Pause 1
RETURN

/Henrik.

astanapane
- 3rd March 2013, 18:58
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 ?

HenrikOlsson
- 3rd March 2013, 19:29
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:

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.

astanapane
- 3rd March 2013, 19:49
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.

astanapane
- 4th March 2013, 06:37
today i will try to build the models but will be ready for testing tomorrow. So please give me some time. thanks a lot.

astanapane
- 4th March 2013, 19:51
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.

HenrikOlsson
- 4th March 2013, 20:42
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

astanapane
- 4th March 2013, 21:02
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.

astanapane
- 5th March 2013, 05:07
"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.

Acetronics2
- 5th March 2013, 07:31
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

astanapane
- 5th March 2013, 08:38
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?

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?

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?

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

Ioannis
- 5th March 2013, 08:52
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

astanapane
- 5th March 2013, 09:31
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.

HenrikOlsson
- 5th March 2013, 11:16
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.

astanapane
- 5th March 2013, 11:34
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.

astanapane
- 6th March 2013, 09:38
i think i will change the microcontroller to PIC16f628A because i have spare.

I only have at the moment one PIC16f88 and 3 PIC16F688.

'************************************************* ************************************
'* Name : STEPPER MOTOR.BAS *
'* Author : GOT a HELP FROM HENRIK, Acetronics and Ioannis at (MELABS forum) *
'* Notice : Copyright (c) 2013 *
'* 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.

Ioannis
- 6th March 2013, 11:39
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

astanapane
- 6th March 2013, 12:21
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.

astanapane
- 6th March 2013, 18:23
I did it.

'************************************************* ***************
'* Name : UNTITLED.BAS *
'* Author : [Leonardo Bilalis] *
'* Notice : Copyright (c) 2013 *
'* 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.

Acetronics2
- 6th March 2013, 20:09
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 ...

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

HenrikOlsson
- 6th March 2013, 20:57
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.

astanapane
- 6th March 2013, 21:15
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.

astanapane
- 7th March 2013, 12:42
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.

Acetronics2
- 7th March 2013, 12:59
Hi, Astanapane

but Some years ago, I wrote THAT to play with a stepper motor ...

/*
* Project name:
Demo for MkE Stepper card
(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);
}
//************************************************** ****************************
{
if (Acc == 0)Acc = 1 ; // Floor value = 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

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);*/

//************************************************** ****************************
//************************************************** ****************************
// {
// 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
}

//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
//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

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;
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
//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

astanapane
- 7th March 2013, 15:14
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.

HenrikOlsson
- 7th March 2013, 15:49
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.

astanapane
- 8th March 2013, 07:33
All thanks a lot.

I made it. I did microsteps. It was easier than i thought.

I will the Code in a bit.

astanapane
- 8th March 2013, 11:25

'************************************************* ************************************
'* Name : STEPPER MOTOR.BAS *
'* Author : [Leonardo Bilalis] GOT a HELP FROM HENRIK, Acetronics and Ioannis (MELABS forum) *
'* Notice : Copyright (c) 2013 *
'* 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.

astanapane
- 8th March 2013, 22:16
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.

Demon
- 9th March 2013, 21:06
Congratulations Leonardo. :)

Robert

astanapane
- 10th March 2013, 05:38
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.

'************************************************* ************************************
'* Name : STEPPER MOTOR.BAS *
'* Author : [Leonardo Bilalis] GOT a HELP FROM HENRIK, Acetronics and Ioannis *
'* Notice : Copyright (c) 2013 *
'* 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

astanapane
- 10th March 2013, 10:50
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?

'************************************************* ***************************************
'* Name : STEPPER MOTOR.BAS *
'* Author : [Leonardo Bilalis] GOT a HELP FROM HENRIK, Acetronics and Ioannis *
'* Notice : Copyright (c) 2013 *
'* 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.

astanapane
- 12th March 2013, 10:15
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.

'************************************************* ***************************************
'* Name : STEPPER MOTOR.BAS *
'* Author : [Leonardo Bilalis] GOT a HELP FROM HENRIK, Acetronics and Ioannis *
'* Notice : Copyright (c) 2013 *
'* 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

astanapane
- 1st April 2013, 09:02
the jig and fixture model is ready.

Ioannis
- 1st April 2013, 10:27
Nice job. Very detailed 3d Printing indeed!

Ioannis

astanapane
- 1st April 2013, 11:32
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

astanapane
- 6th April 2013, 06:31
a small video for you guys

Demon
- 6th April 2013, 06:41
Wow that sucker moves so fast! Impressive.

Robert

Ioannis
- 6th April 2013, 10:11
I was impressed too by the speed of the motor. Wow!

Ioannis

astanapane
- 6th April 2013, 20:59
Yep you can find that SPECIFIC MOTOR at sparkfun.

It is not expensive and not with HIGH torque but it is Good for many applications.

astanapane
- 18th April 2013, 11:13
normally i dont open a new thread, i really dont like it, so i will continue the next request from you guys here.

If any moderator would like to open a new thread, it is free to do it. My request is related to motors again, but this time to a brushells motors.

I have the following motor (http://www.rapidrcmodels.com/ekmps/shops/rapid/images/ntm-prop-drive-series-28-30a-750kv-140w-389-p.jpg)

and Detrum driver 25 A (http://site.hobbypartz.com/image/Dynam-ESC-combo/60P-DY-1037-MotorESCCombo-02.jpg)

i would like to control very simply the driver with a PIC and a potentiometer (http://upload.wikimedia.org/wikipedia/commons/thumb/b/b5/Potentiometer.jpg/200px-Potentiometer.jpg)

so they way to rotate the potentiometer to speed up the motor via PWM from PIC.

Is that possible?

have a look first of my new design and what i would like to do.

rmteo
- 18th April 2013, 13:33
...i would like to control very simply the driver with a PIC and a potentiometer (http://upload.wikimedia.org/wikipedia/commons/thumb/b/b5/Potentiometer.jpg/200px-Potentiometer.jpg)

so they way to rotate the potentiometer to speed up the motor via PWM from PIC.

You could do it with a PIC but a \$5 servo tester like this (http://www.hobbyking.com/hobbyking/store/__18309__Hobbyking_LED_Servo_Tester.html) would work just as well.

http://www.hobbyking.com/hobbyking/store/catalog/18309.jpg

Ioannis
- 18th April 2013, 13:38
The motors are Brushless type and need a special High current driver (at least 30 Amps).

When a specific driver is selected we can see how to control it.

Ioannis

astanapane
- 18th April 2013, 13:49
Hi rmteo,

thanks a lot for your reply, just wanted to use a PIC and a potentiometer, because i would like to understand the programming as well behind it.

Right now i have a reciever and transmitter to do it.

But i would like to use my own pic PWM with potentiometer to send pulses to controller.

astanapane
- 18th April 2013, 13:51
The motors are Brushless type and need a special High current driver (at least 30 Amps).

When a specific driver is selected we can see how to control it.

Ioannis

For my case i have selected the Dentrum 25A controller for the specific motor. IT works fine.

But i havent checked the input for the controller yet. And dont want to mess around a lot with it. The cost is not too high...about 20 euros.

Ioannis
- 18th April 2013, 13:59
I could not find any datasheet about the Dentrum, but I suppose it is a standard servo input controller.

If this is the case then it just needs pulses every 20ms and from 1 to 2ms wide.

Please confirm the input of the driver.

Ioannis

rmteo
- 18th April 2013, 15:27
....But i would like to use my own pic PWM with potentiometer to send pulses to controller.
Well then, as mentioned "it just needs pulses every 20ms and from 1 to 2ms wide." Here is a hint, you do not want to use the PIC's PWM module as it will give only about 5.5 bits of resolution.

Ioannis
- 18th April 2013, 17:40
This is a little very simple code to produce the required servo pulse by reading an analog port (say a pot).

It then creates the pulse after some maths and loops back again.

again:
if value>250 then value=250 'compress value over 250
pulse=4*value+1000 'upscale from 1000 to 2000
HIGH output_pin
pauseus pulse 'create the servo pulse
low output_pin
pauseus (20000-pulse)' pause for the rest of the 20ms period
goto again

Ioannis

Ioannis
- 18th April 2013, 22:20
... The cost is not too high...about 20 euros.

Or less: http://www.hobbypartz.com/60p-dye-1002-25a-esc.html

astanapane
- 19th April 2013, 07:16
This is a little very simple code to produce the required servo pulse by reading an analog port (say a pot).

It then creates the pulse after some maths and loops back again.

again:
if value>250 then value=250 'compress value over 250
pulse=4*value+1000 'upscale from 1000 to 2000
HIGH output_pin
pauseus pulse 'create the servo pulse
low output_pin
pauseus (20000-pulse)' pause for the rest of the 20ms period
goto again

Ioannis

Right Now i need first to do the schematic and will wait for the printed parts tomorrow morning.

Then I can do the tests.

Thanks a lot Ioannis.

astanapane
- 26th April 2013, 06:27
Hello All, Havent updated you because i changed the designed and wait for the port.
The first test i did with a new motor at 19000 rpm was Very Good.

But still with the joystick transmitter and receiver.

astanapane
- 12th October 2013, 20:41
Long time since Last update. Havent done a BIG test yet but i will upload the video before the end of November.

astanapane
- 9th February 2014, 18:21
it is being long time but i have promised you those videos.

Galder02
- 3rd January 2015, 17:58
Hi there astanapane!, i just read all threads and i have to congratulate you for your progress in programation, as i am a big fanatic to electrnonics and robotics. I have a request to anybody willing to help, i dont have a stepper driver as LEonardo has (big easy) but i made one with a 16f84a, i programmed it to receive a dir and step pulse from another pic, and to give the combinations to som mosfets and to the coils of the bipolar motor. At first it worked well with some weird movements sometimes, (i guess it wass because of the supply or some electrical noises), but as i have been studying, im interested in programming a pic to work as a microstepper driver. Until now, i know that some pwm are required to make the required signals to operate the coils, but i dont have it very clear.

Of course, if somebody can help me out with this in picbasic pro i would be very grateful!. So teh basic idea is:

-Inputs:
*Full Step
*Half Step
*switches to select the microstepping config (4, 8, 16, 32)
*Step
*DIR
*Enable
-Outputs
*the pwm signals to the power transistors or the Hbridges to drive the Bi Polar Motor
*anything else needed
I think that just one pic would control one motor, because it would be too much to ask for the three motors.
I guess it is to much to ask, but i want to learn futher about programming in picbasic pro and maybe it can be done :).

And sorry for my bad english, im from Mexico!

Best Regards and happy holidays.!