Sri Lanka Institute of Information Technology
Faculty of Engineering
Department of Electrical and Computer
Engineering
EC248: Introduction to Controls and Robotics
Lab 04
Name : De Silva O.R.M.
Index Number : EN 14536458
Group : Group 03
Submission Date : 07th September 2015
INTRODUTCTION TO CONTROLS AND ROBOTICS EC248
SERVO AND STEPPER MOTOR CONTROL
Discussion
Part 1 Stepper Motor Control
Following code can be used to turn the stepper motor in clockwise direction.
void main() {
TRISC=0;
while(1){
PORTC = 0X01;
delay_us(900);
PORTC = 0X02;
delay_us(900);
PORTC = 0X04;
delay_us(900);
PORTC = 0X08;
delay_us(900);
}
In order to reverse the above stated direction of the stepper motor, the signal pattern of the bits
should be given in the opposite direction as below.
void main() {
TRISC=0;
while(1){
PORTC = 0X08;
delay_us(900);
PORTC = 0X04;
delay_us(900);
PORTC = 0X02;
delay_us(900);
PORTC = 0X01;
delay_us(900);
}
1
INTRODUTCTION TO CONTROLS AND ROBOTICS EC248
Part 2 Servo Motor Control
Servo motor is used to turn the motor in a desired angle using a PWM pulse.
90 0 +90 0 90
The above rotational pattern could be implemented to a servo motor using a PIC microcontroller
from the below code.
int i;
void main() {
TRISC=0;
while(1){
for(i=0; i<100; i++){
PORTC.RC0 = 1;
delay_us(750); //-90 Rotation
PORTC.RC0 = 0;
delay_us(19500);
}
for(i=0; i<100; i++){
PORTC.RC0 = 1;
delay_us(1500); //0 Rotaton
PORTC.RC0 = 0;
delay_us(18500);
}
for(i=0; i<100; i++){
PORTC.RC0 = 1;
delay_us(2500); //+90 Rotaton
PORTC.RC0 = 0;
delay_us(17500);
}
for(i=0; i<100; i++){
PORTC.RC0 = 1;
delay_us(1500); //0 Rotaton
PORTC.RC0 = 0;
delay_us(18500);
}
for(i=0; i<100; i++){
PORTC.RC0 = 1;
delay_us(750); //-90 Rotation
PORTC.RC0 = 0;
delay_us(19500);
}
}
2
INTRODUTCTION TO CONTROLS AND ROBOTICS EC248
The period of the pulse train is 20 ms.
= 20
1
=
1
=
20 103
= 50
As seen in the above code, the pulse width (time high) for the servo motor to turn at different
angles can be given as follows.
Angle Pulse Width (Time High)
-90o 750s
0o 1500s
+90o 2500s
`
+90o
2500s
-90o
750s
1500s