0% found this document useful (0 votes)
65 views4 pages

Sri Lanka Institute of Information Technology: Faculty of Engineering Department of Electrical and Computer Engineering

This document contains code to control a stepper motor and servo motor using a PIC microcontroller. The stepper motor code uses different bit patterns to turn the motor clockwise and counterclockwise. The servo motor code uses pulse-width modulation to rotate the servo through four positions: -90°, 0°, +90°, and back to 0° by varying the pulse width from 750μs to 2500μs. The period of the PWM signal is 20ms, giving a frequency of 50Hz.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views4 pages

Sri Lanka Institute of Information Technology: Faculty of Engineering Department of Electrical and Computer Engineering

This document contains code to control a stepper motor and servo motor using a PIC microcontroller. The stepper motor code uses different bit patterns to turn the motor clockwise and counterclockwise. The servo motor code uses pulse-width modulation to rotate the servo through four positions: -90°, 0°, +90°, and back to 0° by varying the pulse width from 750μs to 2500μs. The period of the PWM signal is 20ms, giving a frequency of 50Hz.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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

You might also like