Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Motors
And
Controlling it through Parallel Port
BHASKAR GUPTA
I Year student. B.Tech
Indian Institute of Information Technology, Allahabad.
India.
E-mail: bhaskar4006@yahoo.com, bgupta_b03@iiita.ac.in
Homepage: http://collections.sharewith.us
1
Content
1. Theory 3
2. Stepper Motor
i. Definition
ii. Characteristics
iii. Working 4
b. Parallel Port
3. The Circuit 11
4. Programming 14
14
15
2
c. I/O Port Access in Turbo C, Borland C/C++
19
19
AIM :- To understand and run a stepper motor through PC’s parallel port Interface.
THEORY
1. STEPPER MOTOR
2. PARALLEL PORT
1. STEPPER MOTOR
Definition
A stepper motor is basically an electromechanical device which converts electrical
pulses into discrete mechanical movements. The shaft or spindle of a stepper motor
rotates in discrete step increments when electrical command pulses are applied to it in the
proper sequence. The motors rotation has several direct relationships to these applied
input pulses. The sequence of the applied pulses is directly related to the direction of
motor shafts rotation. The speed of the motor shafts rotation is directly related to the
3
frequency of the input pulses and the length of rotation is directly related to the number
of input pulses applied.
Characteristics:
• Holding Torque - Steppers have very good low speed and holding torque.
Steppers are usually rated in terms of their holding force (oz/in) and can even hold a
position (to a lesser degree) without power applied, using magnetic 'detent' torque.
• Open loop positioning - Perhaps the most valuable and interesting feature of a
stepper is the ability to position the shaft in fine predictable increments, without need to
query the motor as to its position. Steppers can run 'open-loop' without the need for any
kind of encoder to determine the shaft position. Closed loop systems- systems that feed
back position information, are known as servo systems. Compared to servos, steppers are
very easy to control; the position of the shaft is guaranteed as long as the torque of the
motor is sufficient for the load, under all its operating conditions.
• Load Independent - The rotation speed of a stepper is independent of load,
provided it has sufficient torque to overcome slipping. The higher rpm a stepper motor is
driven, the more torque it needs, so all steppers eventually poop out at some rpm and start
slipping. Slipping is usually a disaster for steppers, because the position of the shaft
becomes unknown. For this reason, software usually keeps the stepping rate within a
maximum top rate. In applications where a known RPM is needed under a varying load,
steppers can be very handy.
Working:
The stepper motor uses the theory of operation for magnets to make the motor shaft
turn a precise distance when a pulse of electricity is provided. You learned previously
that like poles of a magnet repel and unlike poles attract. Figure 1 shows a typical cross-
sectional view of the rotor and stator of a stepper motor. From this diagram you can see
that the stator (stationary winding) has four poles, and the rotor has six poles (three
complete magnets). The rotor will require 12 pulses of electricity to move the 12 steps to
make one complete revolution. Another way to say this is that the rotor will move
precisely 30° for each pulse of electricity that the motor receives. The number of degrees
the rotor will turn when a pulse of electricity is delivered to the motor can be calculated
by dividing the number of degrees in one revolution of the shaft (360°) by the number of
poles (north and south) in the rotor. In this stepper motor 360° is divided by 12 to get 30°.
4
Figure 1. Diagram that shows the position of the six-pole rotor and four-pole
stator of a typical stepper motor
When no power is applied to the motor, the residual magnetism in the rotor magnets
will cause the rotor to detent or align one set of its magnetic poles with the magnetic
poles of one of the stator magnets. This means that the rotor will have 12 possible detent
positions. When the rotor is in a detent position, it will have enough magnetic force to
keep the shaft from moving to the next position. This is what makes the rotor feel like it
is clicking from one position to the next as you rotate the rotor by hand with no power
applied.
When power is applied, it is directed to only one of the stator pairs of windings,
which will cause that winding pair to become a magnet. One of the coils for the pair will
become the north pole, and the other will become the south pole. When this occurs, the
stator coil that is the north pole will attract the closest rotor tooth that has the opposite
polarity, and the stator coil that is the south pole will attract the closest rotor tooth that
has the opposite polarity. When current is flowing through these poles, the rotor will now
have a much stronger attraction to the stator winding, and the increased torque is called
holding torque.
By changing the current flow to the next stator winding, the magnetic field will be
changed 90°. The rotor will only move 30° before its magnetic fields will again align
with the change in the stator field. The magnetic field in the stator is continually changed
as the rotor moves through the 12 steps to move a total of 360°. Figure 2 shows the
position of the rotor changing as the current supplied to the stator changes.
5
north, (d) Current is applied to the left and right windings so the right winding is
north.
In Fig. 2a you can see that when current is applied to the top and bottom stator
windings, they will become a magnet with the top part of the winding being the north
pole, and the bottom part of the winding being the south pole. You should notice that this
will cause the rotor to move a small amount so that one of its south poles is aligned with
the north stator pole (at the top), and the opposite end of the rotor pole, which is the north
pole, will align with the south pole of the stator (at the bottom). A line is placed on the
south-pole piece that is located at the 12 o'clock position in Fig. 2a so that you can follow
its movement as current is moved from one stator winding to the next. In Fig. 2b current
has been turned off to the top and bottom windings, and current is now applied to the
stator windings shown at the right and left sides of the motor. When this occurs, the stator
winding at the 3 o'clock position will have the polarity for the south pole of the stator
magnet, and the winding at the 9 o'clock position will have the north-pole polarity. In this
condition, the next rotor pole that will be able to align with the stator magnets is the next
pole in the clockwise position to the previous pole. This means that the rotor will only
need to rotate 30° in the clockwise position for this set of poles to align itself so that it
attracts the stator poles.
In Fig. 2c you can see that the top and bottom stator windings are again energized, but
this time the top winding is the south pole of the magnetic field and the bottom winding
is the north pole. This change in magnetic field will cause the rotor to again move 30° in
the clockwise position until its poles will align with the top and bottom stator poles. You
should notice that the original rotor pole that was at the 12 o'clock position when the
motor first started has now moved three steps in the clockwise position.
In Fig. 2d you can see that the two side stator windings are again energized, but this
time the winding at the 3 o'clock position is the north pole. This change in polarity will
cause the rotor to move another 30° in the clockwise direction. You should notice that the
rotor has moved four steps of 30° each, which means the rotor has moved a total of 120°
from its original position. This can be verified by the position of the rotor pole that has
the line on it, which is now pointing at the stator winding that is located in the 3 o'clock
position.
Stepping Modes
6
For the following discussions please refer to
the figure 9.
In Wave Drive only one winding is energized
at any given time. The stator is energized
according to the sequence A -> B -> A ->
B and the rotor steps from position 8 -> 2
->4 -> 6. For unipolar and bipolar wound
motors with the same winding parameters this
excitation mode would result in the same
mechanical position. The disadvantage of this
drive mode is that in the unipolar wound
motor you are only using 25% and in the
bipolar motor only 50% of the total motor
winding at any given time. This means that
you are not getting the maximum torque
output from the motor.
In Full Step Drive you are energizing two
phases at any given time. The stator is
energized according to the sequence and the rotor steps from
position 1 ->3 ->5-> 7 . Full step mode results in the same angular movement as 1 phase
on drive but the mechanical position is offset by one half of a full step. The torque output
of the unipolar wound motor is lower than the bipolar motor (for motors with the same
winding parameters) since the unipolar motor uses only 50% of the
Figure 9 Unipolar and bipolar available winding while the bipolar motor uses the entire winding.
wound stepper motors. Half Step Drive combines both wave and full step (1&2 phases
on) drive modes. Every second step only one phase is energized
and during the other steps one phase on each stator. The stator is energized
according to the sequence
and the rotor steps from position 1 ->2 -> 3-> 4 ->5 -> 6
-> 7 -> 8. This results in angular movements that are half of those in 1- or 2-phases-on
drive modes. Half stepping can reduce a phenomena referred to as resonance which can
be experienced in 1- or 2- phases-on drive modes.
The excitation sequences for the above drive modes are summarized in Table 1.
7
Shortcut for finding the proper wiring sequence
For 5 wires – 1 is common to be plugged at positive supply and rest four to the
pulses. For 6 wires – 2 are common to be plugged at positive supply and rest four to the
pulses.
Connect the center tap(s) to the power source (or current-Limiting resistor.) Connect
the remaining 4 wires in any pattern. If it doesn't work, you only need try these 2
swaps...
1 2 3 4 - (arbitrary first wiring order)
1 2 4 3 - switch end pair
1 4 2 3 - switch middle pair
You're finished when the motor turns smoothly in either direction. If the motor turns
in the opposite direction from desired, reverse the wires so that ABCD would become
DCBA.
Now that we are done with the circuitry for the wiring
sequence, and theory of stepper motor, we will build up a circuit
through the use of TTL device to get a supply voltage of 12V.this
is because, our stepper motor used is a 12V one and the parallel
port gives us only 5V. But before that lets discuss how to intract
with parallel port.
What is a port?
8
A port contains a set of signal lines that the CPU sends or receives data with other
components. We use ports to communicate via modem, printer, keyboard, mouse etc. In
signaling, open signals are "1" and close signals are "0" so it is like binary system. A
parallel port sends 8 bits and receives 5 bits at a time. The serial port RS-232 sends only
1 bit at a time but it is multidirectional so it can send 1 bit and receive 1 bit at a time...
9
• C0: This pin is reversed. It sends a command to read D0-D7 on the port. When the
computer starts it is high in the connector. Which is called nStrobe
• C1: This pin is reversed. It sends a command to the printer to feed the next line. It
is high in the connector after the machine starts. Which is called Auto LF
• C2: This pin is for reset the printer and clear the buffer. Which is called nInit,
nInitialize
• C3: This pin is reversed. Sends a high(1) for opening data inputs. It is low after
the machine starts. Which is called nSelectIn
• C4: Opens the cut operation for the printer. Not visible in the connector...
• C5: Sets the direction control in multidirectional ports. Not visible in the
connector...
• C6: Not used and also Not visible in the connector...
• C7: Mostly not used but it is used as a C5 in some ports. Not visible in the
connector...
10
+Paper End S5 12 Input
+Select In S4 13 Input
-Auto Feed ¬C1 14 Output
-Error S3 15 Input
-Initialize C2 16 Output
-Select ¬C3 17 Output
18-
Ground - Ground
25
CIRCUIT
Electricity - Lets get some Zzzzzttt zzzzttt...
Note- Here the circuit is based on running a stepper motor of 12V (dc),
1.8 DEG/Step, .16A/Phase
Here, we will build up a circuit through the use of TTL device to get a supply voltage
of 12V; this is because, our stepper motor used is a 12V one and the parallel port gives us
only 5V.
Here I have used IC ULN2803, which is an 8-bit, TTL-input npn Darlington driver
with a maximum rating of 50V, 500mA. You can also use high current gain transistor
like CMJE 3055T and place diodes to block the back current flow.
As Parallel port is very delicate port attached with mother board, any back current or
reverse emf can blow your entire parallel port along with motherboard off. To prevent
this you need to isolate it through optocouplers. Any optocouplers will serve the purpose
and safeguard the computer. Here I have used IC 4N35.
11
Figure 10. 4N35 IC configuration
And…
C MJE 3055T
What if you want to make your own Darlington pair. Well ofcource you can.
Here I show the circuit using very high current gain robust npn transistor Mje 3055T.
Mmm…I tried to make it on paint, but my mice drawing being very poor I will
explain it…
• Connect the optocpupler input from parallel port as described above.
• Now, you don’t need additional 5 V supply as you can take 12 V from the
other circuit by making these simple changes:
o Connect the emitter of optocoupler to base of transistor.
o Connect the collector of optocoupler to the collector of the transistor.
What this will do is act as a switch. As soon as the photo-diode is
activated it infrared light, which falls on the base of optocoupler, making it
saturate. It will hence pass current from collector to emitter. This will
trigger base of 3055T transistor to go into saturation.
• Ground the emitter of 3055T transistor to ground of 12V supply.
• Put diodes IN4007 in reverse bias direction from negative and positive supply
of 12 V with the collector. That is, positive end in the negative supply and the
other to collector. And the negative side of diode to positive supply of 12V
and other end to collector just to make sure that any back current doesn’t flow.
• If you want, put a good capacitor (which can sustain) between negative and
positive supply of 12V as a filter in the circuit. I have used 100 Micro F, 63V
electrolytic capacitor.
13
• Now all set and done in securing and enhancing the circuit… Now connect the
common cord of stepper motor to positive supply and rest four into the
collector of the transistors.
Before Coding
Mmmm… A problem that plagues Windows NT/2000 and Windows XP
Unlike Windows 95 & 98, Windows NT/2000/XP will cause an exception (Privileged
Instruction) if an attempt is made to access a port that you are not privileged to talk too.
Actually it's not Windows NT that does this, but any 386 or higher processor running in
protected mode.
Accessing I/O Ports in protected mode is governed by two events, The I/O privilege
level (IOPL) in the EFLAGS register and the I/O permission bit map of a Task State
Segment (TSS).
Under Windows NT, there are only two I/O privilege levels used, level 0 & level 3.
Usermode programs will run in privilege level 3, while device drivers and the kernel will
run in privilege level 0, commonly referred to as ring 0. This allows the trusted operating
system and drivers running in kernel mode to access the ports, while preventing less
trusted usermode processes from touching the I/O ports and causing conflicts. All
usermode programs should talk to a device driver which arbitrates access.
The I/O permission bitmap can be used to allow programs not privileged enough (I.e.
usermode programs) the ability to access the I/O ports. When an I/O instruction is
executed, the processor will first check if the task is privileged enough to access the
ports. Should this be the case, the I/O instruction will be executed. However if the task is
not allowed to do I/O, the processor will then check the I/O permission bitmap.
The I/O permission bitmap, as the name suggests uses a single bit to represent each
I/O address. If the bit corresponding to a port is set, then the instruction will generate an
exception however if the bit is clear then the I/O operation will proceed. This gives a
means to allow certain processes to access certain ports. There is one I/O permission
bitmap per task.
The Program
15
I made the coding in Microsoft Excel VB editor as in excel, it complies automatically
and you can make dynamic changes. It doesn’t create the exe file, but excel worksheet act
as a form.
In Excel go to tool->Macros->VB editor.
In new window, click insert Module.
In module type:
Public Declare Function Inp Lib "inpout32.dll" Alias "Inp32" _
(ByVal PortAddress As Integer) _
As Integer
In editor,
Edit buttons as follows
Loop
Private Sub myTimer = Sheet1.Cells(4, 3)
CommandButton1_Click() Out 888, 6
countit = 0 Do While myTimer > 0
myTimer = 0 myTimer = myTimer - 1
countit = Sheet1.Cells(4, 1) Loop
MsgBox "Before Starting...Please Out 888, 12
be Patience as it may take few myTimer = Sheet1.Cells(4, 3)
minutes" Do While myTimer > 0
Do While countit > 0 myTimer = myTimer - 1
myTimer = Sheet1.Cells(4, 3) Loop
Out 888, 9
Out 888, 3 myTimer = Sheet1.Cells(4, 3)
Do While myTimer > 0 Do While myTimer > 0
myTimer = myTimer - 1 myTimer = myTimer - 1
16
Loop countit = countit - 1
countit = countit - 1 Loop
Loop Out 888, 0
Out 888, 0 End Sub
MsgBox "Done"
17
myTimer = Sheet1.Cells(4, 3) Do While myTimer > 0
Out 888, 12 myTimer = myTimer - 1
Do While myTimer > 0 Loop
myTimer = myTimer - 1 Out 888, 1
Loop myTimer = Sheet1.Cells(4, 3)
Out 888, 8 Do While myTimer > 0
myTimer = Sheet1.Cells(4, 3) myTimer = myTimer - 1
Do While myTimer > 0 Loop
myTimer = myTimer - 1 countit = countit - 1
Loop Loop
Out 888, 9 Out 888, 0
myTimer = Sheet1.Cells(4, 3) End Sub
Do While myTimer > 0
myTimer = myTimer - 1 Private Sub
Loop CommandButton6_Click()
countit = 0
countit = countit - 1 myTimer = 0
Loop countit = Sheet1.Cells(4, 1)
Out 888, 0 MsgBox "Before Starting...Please
End Sub be Patience as it may take few
minutes"
Private Sub Do While countit > 0
CommandButton5_Click() myTimer = Sheet1.Cells(4, 3)
countit = 0
myTimer = 0 Out 888, 9
countit = Sheet1.Cells(4, 1) Do While myTimer > 0
MsgBox "Before Starting...Please myTimer = myTimer - 1
be Patience as it may take few Loop
minutes" myTimer = Sheet1.Cells(4, 3)
Do While countit > 0 Out 888, 12
myTimer = Sheet1.Cells(4, 3) Do While myTimer > 0
myTimer = myTimer - 1
Out 888, 8 Loop
Do While myTimer > 0 Out 888, 6
myTimer = myTimer - 1 myTimer = Sheet1.Cells(4, 3)
Loop Do While myTimer > 0
myTimer = Sheet1.Cells(4, 3) myTimer = myTimer - 1
Out 888, 4 Loop
Do While myTimer > 0 Out 888, 3
myTimer = myTimer - 1 myTimer = Sheet1.Cells(4, 3)
Loop Do While myTimer > 0
Out 888, 2 myTimer = myTimer - 1
myTimer = Sheet1.Cells(4, 3) Loop
18
countit = countit - 1 myTimer = Sheet1.Cells(4, 3)
Loop Out 888, 2
Out 888, 0 Do While myTimer > 0
MsgBox "Done" myTimer = myTimer - 1
End Sub Loop
Out 888, 3
Private Sub myTimer = Sheet1.Cells(4, 3)
CommandButton7_Click() Do While myTimer > 0
countit = 0 myTimer = myTimer - 1
myTimer = 0 Loop
countit = Sheet1.Cells(4, 1) Out 888, 1
MsgBox "Before Starting...Please myTimer = Sheet1.Cells(4, 3)
be Patience as it may take few Do While myTimer > 0
minutes" myTimer = myTimer - 1
Do While countit > 0 Loop
myTimer = Sheet1.Cells(4, 3)
countit = countit - 1
Out 888, 9 Loop
Do While myTimer > 0 Out 888, 0
myTimer = myTimer - 1 End Sub
Loop
myTimer = Sheet1.Cells(4, 3)
Out 888, 8 Also I/O Port Access in Turbo C,
Do While myTimer > 0 Borland C/C++
myTimer = myTimer - 1
Loop Turbo C and Borland C/C++ provide
Out 888, 12
access to the I/O ports on the 80x86
CPU via the predefined functions
myTimer = Sheet1.Cells(4, 3)
inportb / inport and
Do While myTimer > 0 outportb / outport.
myTimer = myTimer - 1
Loop int inportb(int portid); /* returns a
Out 888, 4 byte read from the I/O port portid
myTimer = Sheet1.Cells(4, 3) */
Do While myTimer > 0
myTimer = myTimer - 1 int inport(int portid); /* returns a
Loop word read from the I/O port portid
myTimer = Sheet1.Cells(4, */
3)
void outportb(int portid, unsigned
Out 888, 6 char value);
Do While myTimer > 0 /* writes the byte
myTimer = myTimer - 1 value to the I/O port portid */
Loop
19
void outport(int portid, int value);
/* writes the word static int peek_char=-1;
value to the I/O port portid */ // global data declaring port
#include <stdio.h> values
#include <dos.h> #define DATAPORT 0x378
#define CONTROLPORT 0x37a
#define Data 0x378
#define Status 0x379 //functions declarations
#define Control 0x37a
void init_kb();
unsigned char Bits; void close_kb();
void move_right();
outportb(Data,Bits); /* output data void move_left();
*/ void move_forward();
void move_backward();
Bits = inportb(Status); /* input data void stop();
*/
enum mot {still,forward,backward};
I got a program for same to run in
UNIX. I have not checked it, neither I static enum mot motVal=still;
had made. But it has been tested and int kbhit();
run. I got it from, Mr. Rachit Rastogi int readch();
doing final year M.Tech in IS from
IIITA. Heres the code…
int main(void)
{
#include<unistd.h>
int x,temp,id,uid;
#include<sys/io.h>
#include<stdio.h>
// uid=getuid();
#include <termios.h>
#include <curses.h>
#include <term.h>
init_kb(); //initialize keyboard
20
printf("permission to the port }
granted\n");
outb(0,DATAPORT);
// function definitions
printf("motor intialized\n ");
void stop()
printf("entering the motor control {
area\n"); motVal=still;
outb(0,DATAPORT);
while(1) }
{ void move_right()
{
if(motVal==forward)
key=kbhit(); outb(9,DATAPORT);
if(key=='q')
break; if(motVal==backward)
outb(10,DATAPORT);
if(key=='6') if(motVal==still)
move_right(); outb(8,DATAPORT);
if(key=='4')
move_left(); //outb(stepData[pos+
+],DATAPORT);
if(key=='8') //pos=pos%4;
move_forward();
}
if(key=='2')
move_backward(); void move_left()
{
if(key=='5')
stop(); if(motVal==forward)
outb(5,DATAPORT);
if(motVal==backward)
outb(6,DATAPORT);
21
if(motVal==still)
outb(4,DATAPORT); int kbhit()
{
char ch;
//outb(stepData[pos--],DATAPOR int nread;
T);
//pos=(pos+4)%4; if(peek_char !=-1)
} return 1;
tcgetattr(0,&initial_settings); return 0;
new_settings=initial_settings; }
new_settings.c_lflag &=
~ICANON;
new_settings.c_lflag &= ~ECHO;
new_settings.c_lflag &= ~ISIG; int readch()
new_settings.c_cc[VMIN] = 1; {
new_settings.c_cc[VTIME] = 0; char ch;
tcsetattr(0,TCSANOW,&new_sett
ings); if(peek_char !=-1)
} {
ch=peek_char;
peek_char=-1;
void close_kb() return ch;
{ }
tcsetattr(0,TCSANOW,&initial_s
ettings); read(0,&ch,1);
} return ch;
22
}
23