Você está na página 1de 7

156 Part 4

Listing 4.7 Outline of Rev Counter Program


MOTREVS
Specify MCU 16F917
Include LCD function file

Initialize display digits to zero


Setup LCD
Setup timer as external pulse counter

Main loop
Display 3 digits on LCD
Wait for input switch on
Reset counter and start motor
Wait for input switch off
Stop motor
Convert timer count to 3 digit BCD

The timer is set up for external input using setup_timer_1(T1_EXTERNAL), and the
resulting count is read using get_timer1(). The binary number obtained from the timer
is divided by 2 and converted to BCD by a process of successive subtraction, which is
simple if not elegant. The calculated digits are then displayed as in previous examples,
using the function lcd_symbol() to output the display digits and the include file
LCD.INC for the display encoding.

4.4 PICDEM Stepper Motor Control


● Stepper motor operation
● Stepper motor test program
● Speed and direction control

The main advantage of the stepper motor is that it provides position control without the
feedback required by a DC motor. It has stator windings distributed around a cylindrical
rotor, which has permanent or induced magnetic poles. The windings operate in groups to
move the rotor by a fraction of a revolution at a time (Figure 4.8).

www.n ewn es p res s .c o m

Ch04-H8960.indd 156 6/10/2008 5:20:31 PM


C Mechatronics Applications 157

N S N SN

Figure 4.9: Bipolar Permanent Magnet Stepper Motor with Two Winding sets

SW2 RD7 P1
Drive 1
Brown
Direction CCP1 PWM1
Motor
RD6 P2
control Drive 2 Orange
SW3 PIC logic Rotor
16F917 RD5 P3 linked for
Faster full bridge Red
CCP2 PWM3 drive Drive 3
SW3 operation
RD4 P4
Winding
Slower Yellow
Drive 4

Figure 4.10: Stepper Motor Test System Connections

Construction
The small stepper motor on the mechatronics board is an inexpensive permanent magnet
(PM) type, giving 7.5 degrees per step, 48 steps per revolution. It can also be moved in
half steps by suitable operation of the windings or even smaller steps (microstepping) by
suitable modulation of the winding current. The motor has two bipolar windings, which
means the current is reversed to change the polarity of the stator pole. The coils energize
two rings of poles, creating alternating north and south poles, which interact with the
permanent rotor poles (Figure 4.9).

Representative windings are shown Figure 4.10; in the actual motor, coils are distributed
around the whole circumference, multiplying the torque produced. Their terminals are

w w w.ne w nespress.com

Ch04-H8960.indd 157 6/10/2008 5:20:31 PM


158 Part 4

connected to the four driver outputs on the board, which are normally connected for full-
bridge operation. This allows the current to be reversed in the stator windings, reversing
the polarity of the stator poles. The stator coils are brought out to four color-coded wires,
which are connected to the driver terminals.
In more expensive motors, a smaller step (typically 1.8º) can be obtained with four sets of
windings. These motors usually have six wires, with a common connection for each pair
of windings.

Stepper Motor Test


The stepper motor is connected to the driver outputs, in clockwise order. The six driver
input links must be closed to enable full-bridge operation, since the bipolar motor
requires winding current in both directions. P1, P2, P3, and P4 inputs are connected to
RD4, RD5, RD6, and RD7, respectively. When run, the program generates the required
switching sequence on the coils to energize them in the right order. SW2/RA1 changes
the direction, and SW3/RA3 and SW4/RA4 allow the step speed to be varied.

Source code STEPTEST.C is shown in Listing 4.8. Only the control inputs P1, P2, P3,
and P4 need to be connected to outputs RD7–RD4 at this stage. Note that the stepper
motor terminal connections are not in numerical color order. As can be seen, no special
program setup is needed. The program simply switches on the drivers in the order 1,4,2,3
by outputting a suitable hex code to Port D. The delay is set so that the steps can be
counted visually. It is helpful to attach an indicator flag to the motor shaft, so that the
stepping can be seen more easily. The number of full steps per rev can then be
confirmed (48).

Program STEPSPEED, Listings 4.9 and 4.10, is a development of the basic program to
test the motor response to a range of step rates. The input tactile switches change the
speed by modifying the delay time parameter, which is set to 16 ms by default. This gives
speed of

16 ms/step  16  48  0.768 sec/rev  0.768  60  46 rpm

Direction Control
The stepper motor program can now be further developed to include direction control, as
shown in STEPDIR.C (Listing 4.11). The program has been restructured to incorporate

www.n ewn es p res s .c o m

Ch04-H8960.indd 158 6/10/2008 5:20:32 PM


Listing 4.8 Stepper Motor Test Program
// STEPTEST.C
// Test program for PICDEM Mechatronics Board stepper motor,
// basic full step mode. Connect RD7-P1, RD6-P2, RD5-P3, RD4-P4
// plus all 6 jumpers for full bridge mode
// Motor moves 48 steps per rev (7.5 deg/step)
///////////////////////////////////////////////////
#include "16F917.h"
#use delay(clock=8000000)
void main()
{
while(1) //Loop always
{
output_D(0x80); //Switch on Drive 1
delay_ms(200);
output_D(0x10); //Switch on Drive 4
delay_ms(200);
output_D(0x40); //Switch on Drive 2
delay_ms(200);
output_D(0x20); //Switch on Drive 3
delay_ms(200);
}
}

Listing 4.9 Outline of Stepper Motor Speed Control Program


STEPSPEED
Specify MCU 16F917
Set default step delay time
Main loop
If Direction switch pulsed, Call Forward
If Direction switch pulsed, Call Reverse
Forward
Call Speed
Output one forward cycle (4 steps) to motor
Reverse
Call Speed
Output one reverse cycle (4 steps) to motor
Speed
If Up button on, halve step delay
If Down button on, double step delay

w w w.new nespress.com

Ch04-H8960.indd 159 6/10/2008 5:20:32 PM


160 Part 4

Listing 4.10 Stepper Motor Speed Control Program


///////////////////////////////////////////////////////////////////////
// STEPSPEED.CMPB 22-4-07
// Program for PICDEM Mechatronics Board stepper motor, full step mode
// Connect RD7-P1, RD6-P2, RD5-P3, RD4-P4 plus all 6 jumpers for full
// bridge mode plus SW3-RA3 and SW4-RA4. Motor speed SW3 up SW4 down
///////////////////////////////////////////////////////////////////////

#include "16F917.h"
#use delay(clock=8000000)

void main()
{
int8 time=16; // Variable step delay

while(1) //Loop always


{

//CHECK SWITCHES

if(!input(PIN_A3)) //Poll SW3


{ delay_ms(10); //Debounce
if(time!=1)time=time/2; //Not if min
}
while(!input(PIN_A3)){}; //Wait switch

if(!input(PIN_A4)) //Poll SW3


{ delay_ms(10); //Debounce
if(time!=128)time=time*2; //Not if max
}
while(!input(PIN_A4)){}; //Wait switch

//4 STEPS CLOCKWISE

output_D(0x20); delay_ms(time); //Step 1


output_D(0x40); delay_ms(time); //Step 2
output_D(0x10); delay_ms(time); //Step 3
output_D(0x80); delay_ms(time); //Step 4
}
}

a procedure for modifying speed. In the main loop, the reversing button is tested; by
default the motor runs forward and is reversed each time the button is pressed. Before each
sequence of four steps, the speed buttons are polled and the delay modified if requested.
The structure makes it easier to write the program with the right logical sequence. A flaw

www.n ewn es p res s .c o m

Ch04-H8960.indd 160 6/10/2008 5:20:32 PM


C Mechatronics Applications 161

Listing 4.11 Stepper Motor Speed and Direction Control


///////////////////////////////////////////////////////////////////////
// STEPDIR.C PICDEM Mechatronics Board stepper motor speed and dirc.
// Connect RD7-P1, RD6-P2, RD5-P3, RD4-P4 plus all 6 jumpers(full bridge)
// SW2-RA2, SW3-RA3, SW4-RA4. Motor speed SW3 up SW4 down, motor dirc SW2
///////////////////////////////////////////////////////////////////////

#include "16F917.h" //MCU select


#use delay(clock=8000000) //Internal clock
int8 time=16; //Default speed

//PROCEDURES////////////////////////////////////////////////////

void speed() //Halve or double speed //////////


{
if(!input(PIN_A3)) //Poll SW3
{ delay_ms(10); //Debounce
if(time!=1)time=time/2; //Not if min
}
while(!input(PIN_A3)){}; //Wait switch

if(!input(PIN_A4)) //Poll SW3


{ delay_ms(10); //Debounce
if(time!=128)time=time*2; //Not if max
}
while(!input(PIN_A4)){}; //Wait switch
}

void forward() //4 steps clockwise /////////////


{
speed();
output_D(0x20); delay_ms(time); //Step 1
output_D(0x40); delay_ms(time); //Step 2
output_D(0x10); delay_ms(time); //Step 3
output_D(0x80); delay_ms(time); //Step 4
}

void reverse() //4 steps counter-clockwise /////


{
speed();
output_D(0x80); delay_ms(time); //Step 4
output_D(0x10); delay_ms(time); //Step 3
output_D(0x40); delay_ms(time); //Step 2
output_D(0x20); delay_ms(time); //Step 1
}

w w w.new nespress.com

Ch04-H8960.indd 161 6/10/2008 5:20:32 PM


162 Part 4

void main() //Main loop///////////////////////////////////////////


{
while(1) //Loop always
{
while(input(PIN_A2)) { forward(); } //Run forward
delay_ms(10); //Debounce
while(!input(PIN_A2)){}; //Wait until released

while(input(PIN_A2)) { reverse(); } //Run reverse


delay_ms(10); //Debounce
while(!input(PIN_A2)){}; //Wait until released
}
}

in the algorithm is that the program checks the buttons only after four steps, so the direction
and speed do not change immediately if the motor is running at low speed. This type of
problem can be solved using interrupts.

4.5 PICDEM Analog Sensors


● Light switch application
● Temperature display application

The mechatronics board is fitted with a light and temperature sensor, each of which
produces an analog output in the range of 0–5 V. In common with many sensors now
available, a signal conditioning amplifier is built in, so that no additional components are
needed to interface with an MCU.

Light Sensor
The light sensor can be tested using the analog comparator inputs of the 16F917, which
allow two input voltages to be compared. An output bit in a status register is set if the
positive input (C) is at a higher voltage than the negative input (C) or a reference
voltage. A range of setup options are defined in the header file.
The block diagram in Figure 4.11 shows the hardware configuration for this test. The
connector pin LIGHT, the light sensor output, is connected to RA0 (comparator input C)
and POT1 to RA3 (comparator input C), with LED D7 is assigned to RD7 to display the

www.n ewn es p res s .c o m

Ch04-H8960.indd 162 6/10/2008 5:20:32 PM

Você também pode gostar