Escolar Documentos
Profissional Documentos
Cultura Documentos
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.
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).
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
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
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.
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
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
w w w.new nespress.com
#include "16F917.h"
#use delay(clock=8000000)
void main()
{
int8 time=16; // Variable step delay
//CHECK SWITCHES
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
//PROCEDURES////////////////////////////////////////////////////
w w w.new nespress.com
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.
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