You are on page 1of 32

PIC Controlled ~ Stepper Motor

Printer Project
Lakehead University
Electrical Engineering
Technologist Diploma Project
Group Members: Doug Carlson, Ben Zaporzan
Tech Supervisor: Bruce Misner
April 29
PIC - Stepper Motor Prnter Engneerng Dpoma Pro|ect
Carson/Zaporzan
verview:
The purpose of utilizing this Printer Trainer was to give us a device that we could develop
and test various codes using the PC 1F8 microcontroller. The choice was made to
develop around the 1F8 because of its available features and the use of the features when
utilizing microcontrollers.
The flash memory of the 1F8 device was desirable because of its ability to be rapidly
reprogrammed and put bac in circuit to be tested using the docing station connected via a
serial connection to the programming computer.
The printer base itself had been previously used for projects such as this, all testing various
code and mechanical adaptations to the generic style of the printer body. The parts that were
used in this experiment can be recognized as older technology and therefore are readily
available as unwanted materials to most people. The plastic base of the printed body had
been designed to house stepper motors to control the dot matrix printer head, so this housing
was a preferable design when being considered to house this diploma project. sing the old
printer body to its full potential and to our benefit meant that little physical/mechanical
alterations had to be made to the printer body.
Electrical fabrication in way of PCB's design and construction, along with the soldering of
circuit components, and wiring the completed unit was all that was needed to complete the
project. The generic PC development board was used for its flexible, multi-purpose
characteristics which suited our project requirements.
2 | P a g e
PIC - Stepper Motor Prnter Engneerng Dpoma Pro|ect
Carson/Zaporzan
Tale of Contents
Overview 2
Table of Contents 3
HorJwore Secrion
PC Trainer Functions 3
PCB Manufacturing Process
Development Board
Stepper Motor Drive Circuit 8
Switch Circuitry 8
LCD Contrast Adjustment 9
So[rwore onJ Proqromminq Secrion
Programming the PC 11
MPLAB Software 11
Controlling the Stepper Motor 12
Communication nterface 13
CoJe Lisrinq onJ Explonorion
Code Listing and Explanation 14-23
Scnemorics
Hardware Layout 2
Development Board 28
Stepper Motor Drive Circuit 29
Switch Circuitry 30
LCD Display Data Sheet 31
LPT Connections for PC Trainer 32
Re[erences
PC Microcontroller Trainer Document
Bruce Misner, August 1
st
, 2002
Laehead niversity Electrical Engineering
3 | P a g e
PIC - Stepper Motor Prnter Engneerng Dpoma Pro|ect
Carson/Zaporzan
HADWAE SECTIN
4 | P a g e
PIC - Stepper Motor Prnter Engneerng Dpoma Pro|ect
Carson/Zaporzan
PIC Trainer unctions:
The printer trainer started as an old dot matrix printer that had previously been scavenged
for parts for various projects. The components that were used in this project were the plastic
base, metal frame, printer trac, printer head, stepper motor and drive components.
A photo-interrupter was mounted with an L-bracet using one the existing screw holes on
the frame of the printer. t was mounted in such a fashion as to be within full view of the
slotted wheel which was attached to the stepper motor controlling the printer head drive.
sing the results from previous experiments to our advantage, we used a 20O current
limiting resistor for the LED on the interrupter and a 1O resistor on the emitter of the photo
transistor. This set up of resistors produced a nice 3V output pulse as the photo-interrupters
LED was interrupted by the slotted drive wheel. The PC could count with the use of a
TMR0 interrupt.
The stepper motor in this printer was 4V 1.3A and came standard with this Hewlett Pacard
model of printer. ith the higher AC power demands a separate motor voltage supply was
needed. 12V, 3V, and Ground binding posts were installed into the front base of the
printer and wired to the stepper motor drive circuit with a terminal strips and voltage and
ground bus points.
Small bracets had been constructed to fasten the printer head limit switches. These were
used to limit the travel of the printer head on the trac. hen the printer head ran to its full
left or right travel on the trac it would hit the limit switch which was a default high switch
once the switch was depressed a zero signal was sent bac to the 1F8.
The digital position pushbutton was mounted on a PCB which was then fixed to the front
panel of the printer. This digital input board gives the user manual control of the print head
in either desired direction. The push button itself also increments or decrements the count
on the LCD which will be discussed in the upcoming paragraph.
Other PCBs designed and built to wor with this setup were an eight bit LED board and an
analog output board. The LED board was not used in our project, instead 8x1 non-graphical
LCD output display was utilized. This unit will be described in more detail and its use can
be seen in the following code and its explanation.
5 | P a g e
PIC - Stepper Motor Prnter Engneerng Dpoma Pro|ect
Carson/Zaporzan
PCB Manufacturing Process:
The following steps are the procedure that we followed in the construction of a Development
board PCB:
1. Selected a sheet of photo resist board from the available samples. eeping in
mind that the board could be either single or double sided, depending on
design and circuit requirements.
2. Leaving the plastic covering in place, cut the sheet of photo resist board to the
desired size leaving a liberal border around the outside for later placement in
the etching tan.
3. Carefully position the pre-made negatives on the board and taped them into place.
The process of printing a negative of the circuit design was not included in
this write up as the negative for our circuit was pre-developed.
4. Turned on the etching tan and set its timer to the maximum position as to heat up
the tan.
3. Exposed the board with the attached negatives to V light for approximately four
and a half minutes.
. Peeled off the protective plastic covering and removed the taped on negatives.
. Poured premade R210 Developer into a tray and let the board sit in the developer
solution for approximately three to five minutes with the application of heat.
The time for this process was found to be extremely variable.
8. sing the toothbrush provided, brushed the exposed surface of the board while
still in the developer to see if the five minutes was sufficient. The board is
removed from solution once the etchings are visible and a copper colour is
uniformly distributed on the board.
9. Rinsed the board off in warm water to stop the developing process.
10. Anchored the board into the etching tan with the help of a wire.
11. Placed the board in the now heated etching tan for approximately fifteen minutes
to be etched in a persulphate solution.
12. Removed the board from the etching tan after the elapsed fifteen and rinsed
it with acetone. The purpose of this being to remove any excess photo
resist around the tracings.
13.Rinsed the board thoroughly under running water to remove any remaining
developer, persulphate solution, or acetone.
14. Dried the board and drilled the holes to the appropriate sizes for their
corresponding circuit elements. Our PCB was now complete and all that
was left was to mount the circuit elements.
| P a g e
PIC - Stepper Motor Prnter Engneerng Dpoma Pro|ect
Carson/Zaporzan
Development Board:
The PC Development board can be seen as the most important circuit element in the
project. ts previous predecessor was the PC DEM2 board which had its faults. Since that
time, the new PC Development board was designed and constructed by Bruce Misner, our
supervising lab technician.
The board itself consists of several elements they will be explained in detail in the
following paragraphs. The central circuit element is a 40 pin socet to place the
programmed PC chip into a ZF socet has also been placed into it. Anybody who has
previously used, or will use the Development board in the future can see that that ZF socet
maes it very easy to add/remove the chip from the board during the development and
testing phases of construction.
The board was designed to be powered through a DC power supply. This voltage was then
passed through a voltage regulator on the board which reduces the voltage to 3V
DC
required
for the controller. The current rating on the wall adapter is 300mA which was more than
sufficient to run the board and peripherals. As mentioned previously, we equipped the
printed tray with a 3V
DC
terminal to receive power from an external supply. This was what
was primarily used throughout the testing phases of our circuit.
The next major element is the oscillator. The original developers of the PC DEM2 board
Microchip used a 10MHz oscillator that was carried through for use in the development
board. t has a 14-pin footprint but only the corner 4 pins are utilized for the circuit.
The remaining portion of the board consists of the /O pins that are connected to various
small terminal strips throughout the board and are ready for connection. The development
board schematic is included in the schematics section.
7 | P a g e
PIC - Stepper Motor Prnter Engneerng Dpoma Pro|ect
Carson/Zaporzan
The Stepper Motor Drive Circuit:
The stepper motor drive circuit used that was used in out project is one of a very simple
design. The circuit is made up of three basic parts:
- drive transistor TP31
- freewheeling diode and
- base resistor.
sing this circuit, only 4 bits are required for the controller one for each coil. The
programming must pulse each coil in the proper order to initiate the stepper motor in the
desired direction. The required code to move the stepper motor in the right direction is
attached and explained in further detail in the upcoming paragraphs.
The stepper circuit drives the base of the drive transistor directly using only one bit on the
controller port. These ports source around 200mA through the transistor beta of
approximately 10 which produces a maximum collector current of around 2A. The motor
itself requires less current then the 2A which can be used, but another external source is
connected to the terminal post at the front of the printer tray to provide the required voltage.
Switch Circuitry:
As mentioned previously in the overview and general operation of our project there are 4
switches overall. There are two limit switched located on the limiting ends of the printer
trac and there are two push button switches which are used manually alter the location of
the printer head. These switches also increment or decrement the count through logic in the
microcontroller.
The limit switches are connected in the same manner with the purpose of limiting the travel
of the printer head on the printer trac these switches are default high. One end of the
switch is connected to the 3V
DC
supply terminal via the 3V
DC
bus. The other end of the
switch is connected to a 10O resistor which is then taen to ground.
The input signal to the PC for controlling the printer head is taen in between the switch
and the resistor. hen the switch is closed a logic low input is sent to the input of the PC
controller. This action limits the travel of the printer head in that particular direction. This
logic low is accomplished by dropping the 3V
DC
across the 10O resistor. The push buttons
are located at the front of the trainer and are used for manual positioning of the print head.
These switches are self explanatory in their operation.
| P a g e
PIC - Stepper Motor Prnter Engneerng Dpoma Pro|ect
Carson/Zaporzan
LCD Contrast Adjustment:
On the front panel of the printer, we placed a 10O potentiometer which has joint
connections through the PCB used for the manual input push buttons. One end is to ground,
one end is to 3V
DC
and the other end is to the contrast input of the LCD display. The wiper voltage is placed
across this input and adjusts the contrast on the LCD screen as desired by the user. The
LCD display is explained in more detail below and the data sheet is provided in the attached
pages.
9 | P a g e
PIC - Stepper Motor Prnter Engneerng Dpoma Pro|ect
Carson/Zaporzan
STWAE AND PGAMMING SECTIN
10 | P a g e
PIC - Stepper Motor Prnter Engneerng Dpoma Pro|ect
Carson/Zaporzan
Programming the PIC Microcontroller:
The Microchip Company has a large selection of microcontrollers from which we had to
choose from based on our application. sing the nowledge from our Tech Project
Supervisor, Bruce Misner, the 1F8 was selected to be purchased by the school. t was
chosen because of its feature set which benefited us and the wide range of tass the
previously mentioned Development board was capable of doing.
The main benefit of the 1F8 for our application was the flash program memory which
was used for it easy reprogramming ability. There are many features offered by the 1F8
but the only detail will be covered on the ones that were ey to the success of our project.
The chip requires that you flash the memory with the assembled machine language using a
programmer of your choice. The programmer that was available to us at the time was the
PCstart Plus Programmer which wored flawlessly. The programming was done in the C
programming language and was compiled and assembled into the microcontroller's machine
language. This offered the benefit of readability and portability.
A tip presented to us by our instructor when programming the microcontroller is to disable
as many features which offer no use in our application. This removes any unwanted
behaviour which may be a result of these features and could be later enabled if it offers any
benefit after development. ou must however eep in mind the consequences of turning off
these features.
MPLAB Software:
The MPLAB Software was used to compile, assemble and flash the microcontroller through
the PCstart Plus programmer. t offered several handy features such as manipulating the
microcontroller's configuration without requiring it to be written in the code. This enabled
us to write much simpler and readable code, but left several disabled features unexplained.
11 | P a g e
PIC - Stepper Motor Prnter Engneerng Dpoma Pro|ect
Carson/Zaporzan
Controlling the Stepper Motor:
The two things you must consider when trying to operate the stepper motor if the order to
pulse the stepper motor coils, and the pulse duration. n the attached schematic the coils
have been labelled 1-4 and through prior experimentation by Mr. Misner the pulse sequences
are located in the table below.
Coil

Step 1 Step 2 Step 3 Step 4


Coil
1
O O OFF OFF
Coil
2
OFF OFF O O
Coil
3
O OFF OFF O
Coil
4
OFF O O OFF
Table 1 Coil Step Pulse Sequences
To our benefit the pulse sequence was predetermined and the /O ports were already
assigned on the Development board. The next step was to assign the bits in the code that
would follow through with the stepping procedure. As explained by Mr. Misner in our
project tutorials, the best procedure for this was to decrease the pulse length until the stepper
motor wouldn't move anymore, then made it a little longer. This new delay value is the
delay required when used with the 10MHz cloc mentioned previously.
The portion of the code that is responsible for moving the stepper motor will be explained
within its segment.
12 | P a g e
PIC - Stepper Motor Prnter Engneerng Dpoma Pro|ect
Carson/Zaporzan
Communication Interface:
A similar project had been previously completed by another group of students who used the
LPT1 printer interface to communicate with the PC Development board therefore for ease
of use we continued with this theme.
The Development board came in handy in this portion as we connected the 8 data bits from
the LPT1 to PORTD located on the board. Pin 1 of LPT1 bit was connected to PORTB, bit
0 as it was done this way in the similar project.
The whole process goes through as follows:
1. The PC is first turned on and displays a string of text on the LCD display as
specified by the user in the code. Once it has started it will search for the first
limit switch and reference this in the code as position 0. Position 0 is located
by moving
2. the printer head all the way until it contacts the switching sending a logic low
output to the PC.
3. Once it has reached position 0 and cannot go any further due to the limit
switch, the printer head is sent bac in the opposite direction in search of the
other limit switch, or position 1. The printer head will travel the full length
of the printer trac to do so and hence it will count on the way. Counting can
be accomplished in several ways, by either counting the pulses through the
photo-interrupter or by taing a count directly from the stepper control
portion of the code.
4. As the code is incremented as it moves across the trac, the output is
displayed on LCD display screen. Once the second limit switch it found the
count is stopped. The code then divides the number of the count by two and
moves the printer head to this location on the trac the middle of the trac.
3. From the middle of the trac the user can use the manual method of moving
the printer head using both the left and right digital pushbuttons on the front
of the printer tray. hen the printer head is moved using the buttons the
count is incremented or decremented appropriately using the high to low
variation in signal supplied from the pushbutton. hen the printer head
moves left as viewed from the front the count is decremented, when the
printer head moves right, the count in incremented.
13 | P a g e
PIC - Stepper Motor Prnter Engneerng Dpoma Pro|ect
Carson/Zaporzan
CDE LISTING AND EXPLANATIN
14 | P a g e
PIC - Stepper Motor Prnter Engneerng Dpoma Pro|ect
Carson/Zaporzan
Code Listing and Explanation:
/
This file has been commented to provide full explanation of the following syntax
along with an explanation of what each code segment does
/
/
The include syntax is used to tell the preprocessor to include the header file
specified in
This file can reside in the C library root directory or can reside in the same directory
as the compiled file
if double quotes are used Ex.
include pic.h would require that pic.h reside in the same directory has stepper.c
pic.h includes definitions for configuration bits/bytes which are defined from
locations in its memory. variables such as RE2,RE1,RE0 are defined within pic.h and
possibly pic.c
/
include pic.h
/
define is also another preprocessor syntax used to replace one eyword with another
before compilation time. This generally used to mae code loo more readable
From the code, it can be seen that Ports E2 to E0 are being defined as the terms
lcdrs, lcdrw, lcden and lcddata before compilation, each of these terms will be
replaced with its assignment
/
define lcdrs RE2
define lcdrw RE1
define lcden RE0
define lcddata PORTD
/
The ports controlling the coils operation are also given distinguishable names for
simplicity
/
define coila RC4
define coilb RC3
define coilc RC
define coild RC
15 | P a g e
PIC - Stepper Motor Prnter Engneerng Dpoma Pro|ect
Carson/Zaporzan
/
Variables declared outside of the main function or outside of any function are
initialized within the global scope. This means the variables can be modified within
any function.
/
int noleft,noright,stepno,totalstep,tempcount
/
TRSA,TRSB,TRSC,TRSD,TRSE - are each made up of 8 bits, with each bit
signifying an input O or output OFF
PORTC,PORTD,PORTE - each variable represents 0- rows for each port. nitially
they are all zero
TCO - configuration bits to enable or disable interrupts
ADCO1 - configuration bits to control PORTA and PORTEs functionality as
either analog or digital inputs
ADCO0 - configuration bits used to turn off Analog to Digital converter
OPTO - configuration bits used to ensure timer is assigned to PORTA ot
Required
At startup, noleft, noright and stepno are also given values
/
void initvoid
TRSA 0xff //PT
TRSB 0xff //PT
TRSC 0 //OTPT
TRSD 0 //OTPT
TRSE 0 //OTPT
PORTC 0 //port c value 0
PORTD 0 //port d value 0
PORTE 0 //port e value 0
TCO 0x0 //no interrupts
ADCO1 0x0 //port a and e are digital inputs
ADCO0 0x0 //no A/D
OPTO 0xa0 //TMR0 external input
noleft 1
noright 1
stepno 0

1 | P a g e
PIC - Stepper Motor Prnter Engneerng Dpoma Pro|ect
Carson/Zaporzan
/
This function was made by Bruce for a 10Mhz external cloc to produce a one
millisecond delay
/
void delaymsvoid
int i
for i1i180i

/
The LCD display uses a HD4480 controller to store and display characters on the
screen.
the first 3 bits that are connected to the controller are named and explained along
with the data byte:
lcdrs -- RE2: Determines whether the following data is instruction-based Off or
Data-based input or output On
lcdrw -- RE1: Determines whether the given data is to be read On or written
Off to the output
lcden -- RE0: The LCD controller is enabled whenever this value is set to the O
state
lcddata -- PORTD: Contains the last remaining 8-bits which are used to read or
write information to the display
lcdputcmd is used to set instructions on the LCD controller. This is apparent due to
lcdrs being low along with lcdrw being low for instruction-mode and write-mode.
hen the command is stored on lcddata and the lcd is enabled, the instruction is
initialized. A delay time is required to ensure the completion of the command.
/
void lcdputcmdint cmd
int i
lcddata cmd //put command on portd
lcdrw 0 //write operation
lcdrs 0 //command mode
lcden 1 //enable lcd
delayms
lcden 0
for i0i3i //3ms delay
delayms

17 | P a g e
PIC - Stepper Motor Prnter Engneerng Dpoma Pro|ect
Carson/Zaporzan
/
One of the simplest commands employing the lcdputcmd is lcdclr.
hile in instruction mode, different bit patterns employ different instructions
the bit pattern with only the last bit O will instruct the display to clear its contents
/
void lcdclrvoid
lcdputcmd0x01 //clear lcd display

/
lcdputdata is different from the lcdputcmd by setting lcdrs to a high setting which
instructs the controller to go into data-mode. lcdrw remains in a low state to write
the contents when lcden is set high. A delay is also employed to ensure that the
instruction completes.
/
void lcdputdataint dat
lcddata dat //put data on portd
lcdrw 0 //write operation
lcdrs 1 //data mode
lcden 1 //enable lcd
delayms
lcden 0 //control lines bac to 0
lcdrs 0

/
lcdinit is used at the startup of the HD4480 controller to configure it for the LCD
display used in our project. Several instruction codes were indexed and will be
explained as follows:
lcdputcmd0x30: 0 0 1 DL F 0 0
DL 1, 0, F 0
DL -- 1 8-bit nterface
0 1-bit nterface
-- 1 1/1 Duty 2 Line
0 1/8 or 1/11 Duty 1 Line
F -- 1 3x10 Dots
0 3x Dots
1 | P a g e
PIC - Stepper Motor Prnter Engneerng Dpoma Pro|ect
Carson/Zaporzan
lcdputcmd0x0e: 0 0 0 0 1 D C B
D 1, C 1, B 0
D -- 1 Display On
0 Display Off
C -- 1 Cursor On
0 Cursor Off
B -- 1 Blin of Cursor Position On
0 Blin of Cursor Position Off
lcdputcmd0x0: 0 0 0 0 0 1 /D S
/D 1, S 0
/D-- 1 ncrement on data input
0 Decrement on data input
S -- 0/1 Enable/Disable Shift of display
/
void lcdinitvoid
lcdputcmd0x30 //function set 8x1 display
lcdputcmd0x0e //entry mode
lcdputcmd0x0 //display mode

/
There are four coils connected to pins RC4 through RC which must be pulsed in
sequence to mae the motor move in
a particular direction. Conditional statements determine its sequencing by checing
the function parameter, with a 0 used to symbolize the motor moving to the right
and a 1 used to symbolize the motor moving to the left. Proper timing must be
made between coil pulses to allow proper coil induction to occur. The function is
called once to mae the motor step left or right once.
/
void bigstepint dir

int i
//
// move a full step to the right
//
if dir 1
coila 1
coilb 0
coilc 1
coild 0
19 | P a g e
PIC - Stepper Motor Prnter Engneerng Dpoma Pro|ect
Carson/Zaporzan
// energize coils for 2ms
for i0i2i
delayms
coila 1
coilb 0
coilc 0
coild 1
for i0i2i
delayms
coila 0
coilb 1
coilc 0
coild 1
for i0i2i
delayms
coila 0
coilb 1
coilc 1
coild 0
for i0i2i
delayms

// move one full step to the left


if dir 0
coila 0
coilb 1
coilc 1
coild 0
for i0i2i
delayms
coila 0
coilb 1
coilc 0
coild 1
for i0i2i
delayms
20 | P a g e
PIC - Stepper Motor Prnter Engneerng Dpoma Pro|ect
Carson/Zaporzan
coila 1
coilb 0
coilc 0
coild 1
for i0i2i
delayms
coila 1
coilb 0
coilc 1
coild 0
for i0i2i
delayms

// turn off coils


coila 0
coilb 0
coilc 0
coild 0

/
dispcount provides interesting functionality. Since tempcount has been declared in
the global scope, the tempcount value can be used within the function. To place data
onto the LCD display from the tempcount, the number has to be expanded out into
powers of 10. The data type is integer-based when stored in memory, therefore
division will result in the remainder being dropped. As a result, dividing by that
particular power of ten will give the integer value for that power
and any higher power Ex. 1023 / 102 10
As a woring example for the full understanding of this code, ill present an example:
tempcount 2423
temp 2423 / 1000
2
data is appended to display 2
temp1 tempcount - temp1000/100
2423 - 21000/100
2423 - 2000/100
423/100 OTE: the remainder is dropped
4
data is appended to display 24
21 | P a g e
PIC - Stepper Motor Prnter Engneerng Dpoma Pro|ect
Carson/Zaporzan
temp2 tempcount - temp 1000-temp1100/10
2423 - 2000 - 400/10
23/10
2
data is appended to display 242
temp3 tempcount - temp 1000-temp1100-temp210
2423 - 2000 - 400 - 20
3
data is appended to display 2423
/
void dispcountvoid
int temp,temp1,temp2,temp3
temp tempcount/1000
lcdputdata0x30temp
temp1 tempcount - temp 1000/100
lcdputdata0x30temp1
temp2 tempcount - temp 1000-temp1100/10
lcdputdata0x30temp2
temp3 tempcount - temp 1000-temp1100-temp210
lcdputdata0x30temp3

/
The main function is where the program begins execution of the code:
-The integers i,j and maxpos are initialized at the start
-character arrays line1 ane line2 are initialized and declared with preset text strings
-nstead of using delayms, a for loop is used with a 10ms delay time
-lcdinit is called to pre-configure the lcd controller
-a for' loop then iterates through the first character array, and the ascii values are
appended to display
-a for' loop is then declared and the resulting delayms is called 499 times approx
0.3 secs
- lcdputcmd0x18: 0 0 0 1 S/C R/L 0 0
S/C 1, R/L 0
S/C -- 1 Shift Display
0 Cursor Move
R/L -- 1 Shift to the Left
0 Shift to the Right

-the lcdputcmd is declared to instruct the display to shift the currently displayed
data
22 | P a g e
PIC - Stepper Motor Prnter Engneerng Dpoma Pro|ect
Carson/Zaporzan
-lcdclr is called to clear the screen
- RB is an input to the sides of the rail to detect whether or not the side has been
touched. t is used in the while' loop to move the motor all the way to the right.
How this wors is the RB input is always set high until it is set low due to the side
switches being hit. The side switches being switched on causes the output on RB to
become a low value.hen RB is low, the while' loop will stop since the condition
is not being met.

-The second while loop will increment the tempcount and step the motor
until the switch on the opposite side is set low RB
-the value stored in tempcount is then stored in the maxpos variable
-this value is then displayed on the lcd display with dispcount
-half of the max position value is then stored in tempcount
-a while loop is employed to step the motor to the halfway position on the rail
-The LCD is cleared and the second character array is appended to the LCD display
with a for loop
- The operation from here on is stuc in an infinite loop since the while loops
condition is always true.The loop continues to iterate over the two if conditional
statements and will perform the expressions provided given circumstances. The first
if statement will move the motor to the left if it has not touched the side of the rail
and has the left button pushed. The count will also be decremented and the resulting
count is displayed. The same is apparent for the other if statment, except the count is
incremented and the motor moves to the right side.

/
main
int i,j,maxpos
char line1 stepper control
char line2 push but
init
for j0j4j //delay for lcd to power up
fori0i1900i
lcdinit //initialize the LCD display
for i0i13i //display intro message on display
lcdputdataline1i
for i1i300i //half second delay
delayms
for j0j1j //scroll message
lcdputcmd0x18
23 | P a g e
PIC - Stepper Motor Prnter Engneerng Dpoma Pro|ect
Carson/Zaporzan
for i1i300i
delayms

lcdclr //clear display


whileRB //move all the way to the right
bigstep1 //move right one step

tempcount 0 //zero counter


whileRB //move all the way to the left
bigstep0 //move left one step
tempcount //increment count

maxpos tempcount //maxpos of steps for whole distance


lcdclr //clear lcd
dispcount //display maxpos on lcd

for i0itempcount/2i //move to middle of trac
bigstep1

tempcount tempcount/2
lcdclr
for i0i8i //display mess. to push button
lcdputdataline2i
while1 //main loop
if RB3 RB //if button pushed and not at end
bigstep0 //move left
tempcount-- //update count
lcdclr
dispcount //display current position

if RB4 RB
bigstep1 //move right
tempcount
lcdclr
dispcount

24 | P a g e
PIC - Stepper Motor Prnter Engneerng Dpoma Pro|ect
Carson/Zaporzan
/
Difference between nterrupt-Based and on-nterrupt-Based Programming

Code Differences

1For an interrupt-based setup, the while loop contained on the end is
replaced with nothing:

while1
2A new interrupt function is made available:
static void interrupt
isrvoid
if RBF
if RB noleft 1
if RB noright 1
if RB noleft 0
if RB noright 0
if RB3
if RB
bigstep0
tempcount--
lcdclr
dispcount
if RB4
if RB
bigstep1
tempcount
lcdclr
dispcount
RBF 0
3TCO is enabled at initialization within the main function:
TCO 0x88
The interrupt-based programming wors by calling the
static void interrupt function defined as isr. This function is called
whenever an interrupt is called regardless of whether or not it
resides in the main function. TCO contains configuration bits for enabling
and disabling interrupts. The fourth and eighth bits enable global
interrupts and enable the interrupts on portB
/
25 | P a g e
PIC - Stepper Motor Prnter Engneerng Dpoma Pro|ect
Carson/Zaporzan
SCHEMATICS
2 | P a g e
5
5
4
4
3
3
2
2
1
1
D D
C C
B B
A A
Limit Switch 1 Limit Switch 2
PIC Dev. Board
Stepper Driver
Postion Switches
Stepper Motor with
slotted wheel and
photo interupter
Print head
Carriage
Belt Drive
Lakehead University Electrical Engineering
LPT Connection Motor Power
POT
Analog Input
Wiring Terminals
Wiring Terminals
5 1
PIC Printer Trainer
A
1 1 Friday, August 02, 2002
Title
Size Document Number Rev
Date: Sheet of
5
5
4
4
3
3
2
2
1
1
D D
C C
B B
A A
AC Source can be any transformer secondary 9V to 30V
All Port headers are direct connections to the
Microcontroller. They can source up to 200 mA.
1 1
Bruce's PIC Development Board
A
1 1 Tuesday, July 30, 2002
Lakehead University Electrical Engineering
Title
Size Document Number Rev
Date: Sheet of
0
0
U1
PIC16F877
1
13
11
32
2
3
4
5
6
7
33
34
35
36
37
38
39
40
15
16
17
18
23
24
25
26
19
20
21
22
27
28
29
30
8
9
10
14
MCLR/VPP
OSC1/CLK
VDD
VDD
RA0
RA1
RA2
RA3
RA4/T0CLK
RA5/SS
RB0/INT
RB1
RB2
RB3
RB4
RB5
RB6
RB7
RC0/T1OSI/T1CLK
RC1/T1OSO
RC2/CCP1
RC3/SCK/SCL
RC4/SDI/SDA
RC5/SDO
RC6
RC7
RD0/PSP0
RD1/PSP1
RD2/PSP2
RD3/PSP3
RD4/PSP4
RD5/PSP5
RD6/PSP6
RD7/PSP7
RE0/RD
RE1/WR
RE2/CS
OSC2/CLKOUT
J1
Port B Header
1
2
3
4
5
6
7
8
J2
Port D Header
1
2
3
4
5
6
7
8
J3
Port C Header
1
2
3
4
5
6
7
8
J4
Port A Header
1
2
3
4
5
6
J5
Port E Header
1
2
3
- +
D1
BRIDGE
1
4
3
2
C1
220u
U2
LM7805
1 3
IN OUT
V1
115Vac
Y1
10Mhz
8
OUT
5
5
4
4
3
3
2
2
1
1
D D
C C
B B
A A
PORT C Bit 0
PORT C Bit 1
PORT C Bit 2
PORT C Bit 3
Lakehead University Electrical Engineering
2 1
Stepper Motor Drive Circuit
A
1 1 Thursday, August 01, 2002
Title
Size Document Number Rev
Date: Sheet of
0


0


R1
220
4Vdc

L2
Stepper Coil B
Q2
TIP31
D1
1N4005
R2
220
4Vdc

L3
Stepper Coil C
D2
1N4005
Q3
TIP31
R3
220
4Vdc

L4
Stepper Coil D
D3
1N4005
Q4
TIP31
R4
220
4Vdc

D4
1N4005
Q1
TIP31 L1
Stepper Coil A
5
5
4
4
3
3
2
2
1
1
D D
C C
B B
A A
Port A Bit 3
Port B Bit 4 Port B Bit 5 Port B Bit 6 Port B Bit 7
Positiong switches and potentiometer
located on Input board at the front of
the Trainer
Limit switches wired on the terminal
strips and switches located at each end
of the carriage
5Vdc supply comes from the PIC
Development Board
Analog Input Circuitry
Switch Circuitry
Lakehead University Electrical Engineering
3 1
Miscellaneous Circuitry
A
1 1 Thursday, August 01, 2002
Title
Size Document Number Rev
Date: Sheet of
0
0
R1
10K
R2
10K R3
10K
R4
10K
SW1
Positioning Switch 1
SW2
Positioning Switch 2
SW3
Limit Switch 1
SW4
Limit Switch 2
DC Source
5Vdc
DC Source
5Vdc
R5
POT
5
5
4
4
3
3
2
2
1
1
D D
C C
B B
A A
Lakehead University Electrical Engineering
To 8 Bit Port
on PIC
on PIC
To 8 Bit Port
PIC Dev. 5Vdc
PIC Dev. Gnd
Analog Output
Analog Gnd common with logic ground
6 1
LED and Analog Output
A
1 1 Tuesday, August 06, 2002
Title
Size Document Number Rev
Date: Sheet of

0
R1
270
D1
LED
R6
270
D6
LED
R2
270
D2
LED
R7
270
D7
LED
R3
270
D3
LED
R8
270
D8
LED
R4
270
D4
LED
U3
AD558
1
2
3
4
5
6
7
8
10
9
16
15
14
11
12
13
DB0
DB1
DB2
DB3
DB4
DB5
DB6
DB7
CS
CE
VOUT
SENSE
SELECT
Vdd
GND
GND
R5
270
D5
LED
C1
10u
5
5
4
4
3
3
2
2
1
1
D D
C C
B B
A A
Printer Side
Pins 3 - 17 Odd numbers are
pins 2 - 9 on the DB25
connector on the PC
Port B Bits 0 - 7
Ground PIC Development Board
Port B Bit 0 (INT)
Lakehead University Electrical Engineering
Pin 18 lpt
Pin 15 lpt
Pin 18 is the ground shared
between computer and PIC
Pin 16 is the /INIT line on the
printer side and is an output
connected to Port B Bit 0 of the
PIC. Triggers an interupt on low to
high transition.
PIC Side
4 1
LPT Connections for PIC Trainer
A
1 1 Thursday, August 01, 2002
Title
Size Document Number Rev
Date: Sheet of
J1
Printer Cable Header
2
4
6
8
10
12
14
16
18
20
22
24
26
1
3
5
7
9
11
13
15
17
19
21
23
25

You might also like