Sunteți pe pagina 1din 7

EXPERIMENT 6: Advanced I/O Programming

Objectives:
To familiarize students with DC Motor control and Stepper Motor Interfacing.
To utilize MikroC and MPLAB for Input Output Interfacing and motor
control.

Introduction to DC Motor

DC (Direct Current) brush motor is the most common and easy-to-control actuator which is
usually used in many types of machines and automation systems. The main advantage of DC
motor is that it can be operated by DC current, normally is from batteries. In many
applications such as cars, ships, remote controlled racing cars or mobile robots where AC
power supply is not available, DC operated components are preferred. In normal application,
a DC brush motor is equipped with a set of gear to reduce the output speed of the motor and
increase the torque at the same time.

Controlling DC Motor

Most of the DC motors can be controlled easily by providing the necessary voltage to it. To
change the rotating direction of DC motor, simply reverse the polarity of the DC input. This
changeover process can be achieved via a simple changeover switch (relay) or by using a
suitable motor driver. In this training kit, motor driver L293D is employed to control the DC
motor as the output pins of the microcontroller cannot directly drive a DC motor.

Another advantage of DC motors is speed control of motor can be easily achieved by


providing variable voltage to it. There are many methods to offer more precise control and
maximum efficiency in controlling the speed. PWM (pulse width modulation) is among the
popular alternative in DC motor speed control. Figure below shows the connection between
DC motor and PIC.

Figure 6.1 Interface between Motor Driver (L293D) and microcontroller.

EEEB371 E41
Figure 6.2 Interfaces between Motor Driver (L293D) and DC Motor.

From Figure 6.1, the two inputs (IN1 and IN2) to the motor driver are connected to PORTB,
pin RB4 and RB5. The enable pin of the motor driver is connected to PWM pin (RC2). As
long as the motor driver is enabled via pin RC2, the two inputs will produce the required
outputs at pin MO1 and MO2. If IN1 is supplied with high logic, MO1 will produce high
logic as the same applies to IN2 and MO2. Using these two inputs, we can easily control the
direction of the DC motor. To rotate the DC motor clockwise, supply IN1 with low logic and
IN2 with high logic. To rotate the DC motor anti-clockwise, supply IN1 with high logic and
IN2 with low logic. By manipulating the input logic, we are actually changing the polarity of
the DC input to the DC Motor and hence we are able to control the rotation of the DC Motor.

Hardware Configuration

For hardware configuration, put mini jumper on JP20 and JP21 to select DC motor and mini
jumper on JP10 to select PWM.

EEEB371 E42
Introduction to Stepper Motor

A stepper motor is a brushless, synchronous electric motor that converts electrical pulses into
mechanical movement. Every revolution of the stepper motor is divided into a discrete
number of steps, and the motor must be sent a separate pulse for each step. The stepper motor
can only take one step at a time and each step is the same size. Since each pulse causes the
motor to rotate a precise angle, the motors position can be controlled without any feedback
mechanism. As the electrical pulses increase in frequency, the step movement changes into
continuous rotation, with the speed of rotation directly proportional to the frequency of the
pulses. Step motors are used every day in both industrial and commercial applications because
of their low cost, high reliability, high torque at low speeds and a simple, rugged construction
that operates in almost any environment.

Unipolar Stepper Motor

The unipolar stepper motor has five or six wires and four coils (actually two coils divided by
center connections on each coil). The center connections of the coils are tied together and
used as the power connection. They are called unipolar steppers because power always comes
in on this one pole.

Figure 6.3 Unipolar Stepper Motor Windings

EEEB371 E43
Stepper Motor Interfacing with Microcontroller

Stepper motors can be used in various areas of microcontroller projects such as making
robots, robotic arm, automatic door lock system etc. Here, Full Step Interfacing Technique
using L293D to control stepper motor will be described.

In the full step sequence, two coils are energized at the same time and motor shaft rotates. The
order in which coils has to be energized is given in the table below.

Figure 6.4 Full Step Sequence

In PTK40A, we use a motor driver L293D to drive the stepper motor. Figure 6.5 shows the
schematic diagram to control stepper motor.

Figure 6.5 Circuit connections for stepper motor.

EEEB371 E44
The four inputs (A, B, C, D) to control the stepper motor are connected to pin RB4, RB5,
RB6 and RB7 respectively. The motor driver enable pin is connected to pin RC2 and must be
enabled at all times to control the stepper motor. By manipulating the inputs as illustrated in
Figure 6.4, we will be able to control the step angle of the stepper motor.

Stepper Motor Step Angle

Step angle of the stepper motor is defined as the angle traversed by the motor in one step. To
calculate step angle, simply divide 360 by number of steps a motor takes to complete one
revolution.

As in figure 6.4, Stepper Motor rotating in full mode sequence takes 24 steps to complete a
revolution, So, step angle can be calculated as

Step Angle = 360 / 24 = 15. One full sequence as in Figure 6.4 produces an angle of 30,
so by repeating the sequence 12 times, we can achieve a full 360 rotation.

So in this way we can calculate step angle for any stepper motor. Usually step angle is given
in the spec sheet of the stepper motor you are using. Knowing stepper motors step angle
helps you calibrate the rotation of motor also to helps you move the motor to correct angular
position.

Hardware Configuration

For hardware configuration, set mini jumper to stepper motor at JP20 and JP21. Then put
mini jumper JP24 to unipolar and JP23 to select bipolar. Another mini jumper used on
JP10 to select PWM.

EEEB371 E45
Procedure

Part A: DC Motor Control

1. Write the source code in MikroC Compiler, build it and download the Hex file into the
microcontroller and run the program. Print out the source file and write down your
observation.

voidmain()
{
ADCON1=0x0F; //ConfigureA/Dfordigitalinputs
CMCON=0x07; //Configurecomparatorsfordigitalinput
TRISB=0x00; //ConfigurePORTBasoutput
TRISC=0x00; //ConfigurePORTCasoutput
PORTC.F2=1; //Enablethemotordriver

while(1)//Endlessloop
{
PORTB.F4=1; //Rotatemotor
PORTB.F5=0; //anticlockwise
Delay_ms(5000); //for5seconds

PORTB.F4=0; //Stop
PORTB.F5=0; //rotatingmotor
Delay_ms(2000); //for2seconds

PORTB.F4=0; //Rotatemotor
PORTB.F5=1; //clockwise
Delay_ms(5000); //for5seconds

PORTB.F4=0; //Stop
PORTB.F5=0; //rotatingmotor
Delay_ms(2000); //for2seconds
}//Endofwhile(1)

}//Endofvoidmain()

2. Modify the program in MikroC to fulfill the following requirement:


- when SW1 is pressed, the DC motor will rotate clockwise.
- when SW2 is pressed, the DC motor will rotate anti-clockwise and
- when SW3 is pressed, the DC motor will stop.

Write down your observation and print out the source file.

3. Write the source code in procedure 1 in assembly language using MPLAB. Build it and
download the Hex file into the microcontroller and run the program. Print out the source
file.

EEEB371 E46
PART B: Stepper Motor Control

4. Write the source code below in MikroC so that the stepper rotates a full 360 degree
rotation. Print out the source file and write down your observation.

voidmain()
{inti=0;
ADCON1=0x0F; //ConfigureA/Dfordigitalinputs
CMCON=0x07; //Configurecomparatorsfordigitalinput
TRISB=0x00; //ConfigurePORTBasoutput
TRISC=0x00; //ConfigurePORTCasoutput
PORTC.F2=1; //Enablethemotordriver

while(i<12)//Loopfor12timestoachieve360rotation
{
PORTB.F4=1;
PORTB.F5=0;
PORTB.F6=0;
PORTB.F7=1;
Delay_ms(100);
PORTB.F4=1;
PORTB.F5=1;
PORTB.F6=0;
PORTB.F7=0;
Delay_ms(100);
PORTB.F4=0;
PORTB.F5=1;
PORTB.F6=1;
PORTB.F7=0;
Delay_ms(100);
PORTB.F4=0;
PORTB.F5=0;
PORTB.F6=1;
PORTB.F7=1;
Delay_ms(100);
i++;
}//Endofwhile(1)

}//Endofvoidmain()

5. Modify the program in MikroC to achieve an angle of 60, 120 and 180. Write down
your observation and print out the source file.

6. Write the source code in procedure 4 in assembly language using MPLAB. Build it and
download the Hex file into the microcontroller and run the program. Print out the source
file.

7. Turn off the PTK40A power supply, rearrange the USB cable back into the Training Kit.
Exit from MikroC, MPLAB and PICKit 2 programmer. Shutdown your PC and rearrange
your workstation before you leave.

EEEB371 E47

S-ar putea să vă placă și