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 E4-1
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 E4-2
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 motor s 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 E4-3
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 E4-4
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 motor s 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 E4-5
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. //Put students ID No and Names here! void main() { ADCON1 = 0x0F; // Configure A/D for digital inputs CMCON = 0x07; // Configure comparators for digital input TRISB = 0x00; // Configure PORTB as output TRISC = 0x00; // Configure PORTC as output PORTC.F2 = 1; //Enable the motor driver while(1) // Endless loop { PORTB.F4 = 1; Delay_ms(5000); Delay_ms(2000); PORTB.F5 = 1; Delay_ms(5000); Delay_ms(2000); }//End of while(1) //Rotate motor //anti-clockwise //for 5 seconds //Stop //rotating motor //for 2 seconds //Rotate motor //clockwise // for 5 seconds // Stop // rotating motor // for 2 seconds }//End of void main() 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 explain the changes which you have made to the program. 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 ASM source file with proper comments. EEEB371 E4-6
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. //Put students ID No and Names here! void main() { int i = 0; ADCON1 = 0x0F; // Configure A/D for digital inputs CMCON = 0x07; // Configure comparators for digital input TRISB = 0x00; // Configure PORTB as output TRISC = 0x00; // Configure PORTC as output PORTC.F2 = 1; //Enable the motor driver while(i<12) // Loop for 12 times to achieve 360 rotation { PORTB.F4 = 1; 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.F5 = 1; PORTB.F6 = 1; PORTB.F7 = 0; Delay_ms(100); PORTB.F6 = 1; PORTB.F7 = 1; Delay_ms(100); i++; }//End of while(1) }//End of void main() 5. Modify the program in MikroC to achieve an angle of 60, 120 and 180. Write down your observation and explain the changes which you have made to the program. 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 LST file. Don t forget to include student s ID No and Names in the ASM file. EEEB371 E4-7
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 E4-8