You are on page 1of 13

1

NATIONAL UNIVERSITY OF SCIENCES & TECHNOLOGY



CONTROL SYSTEMS
PROJECT REPORT

DC MOTOR CONTROL
(USING GENERIC MICRO-CONTROLLER BOARD)

Submitted To: Mr. Naqash Afzal
Submitted By:
Arslan Ahmad 10-NUST-BE-ME-23
Maaz Hussain 10-NUST-BE-ME-45
Umer Farooq 10-NUST-BE-ME-90
Abdul Saboor 10-NUST-BE-ME-03
Section: A
Date: 10
th
Jan, 2014

2

Table of Contents

Objective -------------------------------------------------------------------------------------------------------------- 3
Project Description ------------------------------------------------------------------------------------------------- 3
Circuit ------------------------------------------------------------------------------------------------------------------ 3
Program Code -------------------------------------------------------------------------------------------------------- 6
Code Explained ------------------------------------------------------------------------------------------------------- 9
Conclusion ----------------------------------------------------------------------------------------------------------- 12











3

OBJECTIVE
To control the RPM of a DC Motor (ran by an H-Bridge) using a Generic Microcontroller
Board.

PROJECT DESCRIPTION
This project is used to control the speed of a DC Motor (ran by an H-Bridge). The Micro-
controller generates a Square Wave of 200 Hz Frequency which is used to give the Motor its
Duty Cycle. A feedback is implemented in the code which states that the motor should achieve
the speed defined in the code as soon as the encoder is attached to the motor.

CIRCUIT
This project consists of 2 circuits
1. Generic Micro-controller Board
2. H-Bridge


1. Generic Micro-controller Board
Function
The Generic Micro-controller Board acts like a computer's motherboard, and acts
as a general purpose controller.
Components
The components used in this circuit are
1. Atml 89C51 (8051 micro-controller) 1
4

2. 74LS245 (Byte Addressable Registers) 4
3. LED Bar Graph: To display output on Microcontroller Port pins 4
4. Respack (9 pins): 10k-ohm, for Pulling-up P0 Port 1
5. Respack (9-pins): 330 ohm, with LED Bar Graph 4
6. Crimp Shell (8-pins): For interfacing with external inputs/outputs 8
7. Capacitors: 33 pF (2), 10 uF (1)
8. Crystal (12 MHz): For Calculations 1
9. Push Button: For activating Reset 1
10. Resistor (8.2k-ohm): For Reset circuitry 1
11. Ziff: Bed for Micro-controller 1
12. IC Beds: For easy removal of ICs from circuit 16



5



2. H-Bridge
Function
The H-Bridge is used to run a DC motor and control its direction. For the project,
we run the motor in only one direction.
Components
We have used an H-bridge IC for this purpose commonly known by the name
L293D.


6





PROGRAM CODE
The Code burned into the Micro-controller is:
#include <reg51.h>

int encoder_counts = 0, dutycycle = 50, cycle=0;
float ppr=64;
unsigned char repetition = 0;
unsigned int reference_speed=50, speed;

sbit Enable0 = P2^7;
sbit Enable1 = P2^6;
sbit Enable2 = P2^5;
sbit Enable3 = P2^4;
sbit pulse = P0^0;

L293D Circuit
7

void Initialize();
void SpeedDisplay();

void main (void)
{
Initialize();
while(1)
{
;
}
}

void Initialize()
{
P1 = 0xFF; pulse = 1;
P2=0x00;
EA=1;EX0=1;ET0=1;ET1=1;

TMOD=0x11; //Timer 0 and Timer 1 in mode 1 (16-bit mode)
IT0=1; //edge triggered
TH0=0x3C; //t=50ms
TL0=0xB0;
TR0=1;
TH1 = 0xFF; //t=50us (t=T/100)
TL1 = 0xCD;
TR1 = 1;

}

void SpeedDisplay()
{
int i;

P2=speed%10; Enable0 = 1; for(i=0;i<100;i++); Enable0 = 0;
P2=(speed/10)%10; Enable1 = 1; for(i=0;i<100;i++); Enable1 = 0;
P2=(speed/100)%10; Enable2 = 1; for(i=0;i<100;i++); Enable2 = 0;
P2=speed/1000; Enable3 = 1; for(i=0;i<100;i++); Enable3 = 0;
}

void PwmGeneration()
{
int error,tempdc;
error = reference_speed - speed;
//tempdc = dutycycle + error;
if(error>0)tempdc = dutycycle + 1;
else tempdc = dutycycle -1;

if(tempdc>100) dutycycle = 100;
else if(tempdc<0) dutycycle = 0;
else dutycycle = tempdc;
}

void encoder() interrupt 0
{
encoder_counts++;
}

8



void rpm() interrupt 1
{
if (repetition==9)
{
speed = (encoder_counts/ppr)*60.0*2.0;
encoder_counts=0;
SpeedDisplay();
PwmGeneration();

repetition = 0;
TR0=0;
TH0=0x3C;
TL0=0xB0;
TR0=1;
}
else
{
repetition++;
TR0=0;
TH0=0x3C;
TL0=0xB0;
TR0=1;
}
}


void pwm() interrupt 3
{
if(cycle<dutycycle-1)
{
pulse = 1;
cycle++;
}
else if(cycle<99)
{
pulse = 0;
cycle++;
}
else
{
cycle = 0;
pulse = 1;
}
TR1=0;
TH1 = 0xFF;
TL1 = 0xCD;
TR1 = 1;
}







9

CODE EXPLAINED
#include <reg51.h>

int encoder_counts = 0, dutycycle = 50, cycle=0; // defining
integers
float ppr=64; // pulse per revolution... we keep it a
float so the calculations results are also in float.. if we used
integer the software will round off to integer causing inaccuracy


unsigned char repetition = 0;
unsigned int reference_speed=50, speed; //reference speed is the rpm
of motor that has to be maintained.. that is required. our program's
function is to maintain the rpm of motor at this value


sbit pulse = P0^0; //through this pin we will send a pulse to the
motor via h-bridge

void Initialize(); //we are basically stating that a function
named initialize will be used. the detail of what the function will
do is stated at end
void SpeedDisplay();

void main (void)
{
Initialize(); // we are calling the function initialize.. go
down t see what that function is doing
while(1) //infinite loop . just so that code keeps running
{
;
}
}

void Initialize()
{
pulse = 1; //setting value of pulse at 1

EA=1;EX0=1;ET0=1;ET1=1; //EA=1 states that we will be using
interrupts... but we also have to tell which of the interrupts we will
use...... EX0 tells we are using external interrupt ( basically pin
3.2 in which the motor is giving its feedback pulse) ET0 refers to
timer 0 interrupt while et1 refers to timer1 interrupt

TMOD=0x11; //Timer 0 and Timer 1 in mode 1 (16-bit mode)
IT0=1; //edge triggered.. the pin 3.2 will
detect how many edges came
TH0=0x3C; //we have set timer to run for t=50ms...
basically we have to see speed every 0.5 seconds... so we will run
this timer 10 times to get 0.5s (50ms*10) and then do calculations to
10

find out rpm
TL0=0xB0;
TR0=1; // timer 0 started
TH1 = 0xFF; //t=50us (t=T/100) // the purpose of this
timer is to generate a pulse that has to be sent to the motor..
calculatons are done such that it sends 200 hertz... why 200...
because datasheet states that at 200 motor runs the best
TL1 = 0xCD;
TR1 = 1; starting timer 1

}



void PwmGeneration() // most important part... applies control
concepts
{
int error,tempdc; // two variables defines... error and
temporarydc ( dc for dutycycle)
error = reference_speed - speed; //error is defined

if(error>0)tempdc = dutycycle + 1; // if u refer to upper line
u will see that a positive error means that the motor is running
slower than required... this means that we have to increase the
speed... hence we increase dutycycle by 1.. its initial value was 50..
adding 1 makes it 51... this means that 49 part of the time we will be
sending 0pulse while 51 part of the time we will be sending full
voltage(1 pulse) .... hence increasing overall power being given to
the motor
else tempdc = dutycycle -1; // if error negative.. we have to
do the opposite that is reduce power to the motor

if(tempdc>100) dutycycle = 100; // we dont want dutycycle to
go beyond 100 it will disturb the logic.. as such 100 or any value
above it tells us that we are sending full voltage for the whole time
period

else if(tempdc<0) dutycycle = 0; // we dont want dutycycle to go
below 0 it will disturb the logic.. as such 0 or any value below it
tells us that we are sending no voltage for the whole time period that
is motor wont be moving


else dutycycle = tempdc; //we had stored value in a temporary
variable tempdc... this is just writing the value in variable named
dutycycle which is the one used in generating pulse
}

void encoder() interrupt 0 // this is basically external interrupt..
it gets activated at every falling edge.. pin no. 3.2 it was... so
every time we detect a falling edge we increment a variable named
encountercounts
11

{
encoder_counts++;
}



void rpm() interrupt 1 // interrupt of timer 0
{
if (repetition==9) // recall that timer ran for 50ms.. but we
needed 0.5 seconds... so we are waiting that when the timer has run 10
times (from 0 to 10) go in loop
{
speed = (encoder_counts/ppr)*60.0*2.0; //we are
calculating rpm.. since we got value for 0.5 seconds and we needed it
for 1min... we multiply by 60*2 (120)

encoder_counts=0; //we reset this to zero so that pin 3.2
can start counting again from 0 for the next 0.5seconds

PwmGeneration(); // see below

repetition = 0;
TR0=0;
TH0=0x3C;
TL0=0xB0;
TR0=1;
}
else
{
repetition++;
TR0=0; // this is that if 10 times has not been
done we first increment varaiable repitition ( it basically tells how
many times the timer has run) and then start timer again
TH0=0x3C;
TL0=0xB0;
TR0=1;
}
}


void pwm() interrupt 3 //timer 1
{
if(cycle<dutycycle-1) // this all below is sending a
pulse to motor through h-bridge.... from 0 till the dutycycle value (
initially set at 50 ) pulse 1 is sent .... then from dutycle to 100
pulse 0 is sent... an example if dutycle is changed to 30... from 0 to
30 pulse1 will b sent and from 30 to 100 pulse 0 will n sent..that
means motor will be receiving power only 30% of the time
{
pulse = 1;
cycle++;
}
12

else if(cycle<99)
{
pulse = 0;
cycle++;
}
else
{
cycle = 0;
pulse = 1;
}
TR1=0;
TH1 = 0xFF;
TL1 = 0xCD;
TR1 = 1;
}



// additional question maybe why use h-bridge.... answer is that
microcontroller gives 5V and very less amperes... the motor requires
12V and high currents... so use H-bridge....
why divide pulse in 100 parts u could just divide it in 4... because
we wanted accuracy...
why havent u used digital display... because sir u stated that you
would be using tachometer to see speed...
If tachometer gives wrong reading or it isnt present show a simulation
showing that it works perfectly in software..

if feedback is removed what will happen.. the program will be getting
0 pulses and will calculate speed at 0.. it will deduce that speed
should be increased... it will keep increasing dutycycle so that motor
can speed up... hence eventually it will be sending full voltage
100percent of the time and motor will run at full speed

if sir puts some extreme load on motor... lets say ur required speed
was 200 but its working on 100... ur Ans will b that putting load
limits the max speed a motor can rotate at.. currntly the motor is
rotating at full speed poss.. that is a pulse is sent which is high
100% of the time.



CONCLUSION
The circuit is used to control the speed of motor running at 200Hz frequency. A
reference speed is set and given to the motor through the program code. On powering up the
motor, it starts to perform its operation. The generic microcontroller board can also be used for
other purposes.
13

You might also like