0% found this document useful (0 votes)
14 views16 pages

Unit 3 PWM DCMotor

pwm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views16 pages

Unit 3 PWM DCMotor

pwm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Go, change the

RV College of world
Engineering

Course Code:CS344AI
IOT and Embedded Computing

Programming LPC 2148 PWM,DC Motor Speed Control


RV College of
Engineering Go, change the world

What is PWM..

Pulse rate / Pulse Period


Period of one cycle- Ton + Toff time

Pulse Width
Ton - (On time)

Varying Duty Cycle

By Programming Pule Period and Pulse


width, hence achieving different duty cycles (
ratio of ON time to ON+OFF time.
RV College of
Engineering Go, change the world

LPC 2148 PWM Unit...


RV College of
Engineering Go, change the world

PWM Block Diagram ...


RV College of
Engineering Go, change the world

Defining Pule Period & Pulse Width


Pulse period and width can be any number of timer counts
and all PWM outputs will occur at the same repetition rate.
Meaning of MR0:
One Match Register is required to define the number of PCLK
cycles required to make one T, Pulse period (Ton + Toff), MR0
is used to define this value.
(Pulse period and width can be any number of timer counts and all PWM outputs will occur at
the same repetition rate.)
Meaning Of MR1-MR6 :
To define, different pulse periods for six different PWM
channels, PWM1 TO PWM6, six Match Registers are provided,
MR1 to MR6. The number, which is loaded into these
registers, define number of PCLK cycles will constitute Ton
time.
RV College of
Engineering Go, change the world

PWM Registers..
PWMTCR - PWM Timer Control Register

PWMPCR – PWM control register


used to selectively enable / disable PWM channels PWM1 – PWM6. Also this register is
used to configure the PWM channels as
a) single edge
b) double edge.
Bits: D15 D14 D13……D9.. D6 D5 …..D2 D1 D0
( Bits D9 to D14 are used to enable the channels PWM1 to PWM6)

PWMLER – PWM Latch Enable Register,


Bits: D7 D6 D5 D4 D3 D2 D1 D0
MR6 MR5 MR4 MR3 MR2 MR1 MR0
‘1’ – Writing 1 to this bit allows the last value written to the Match register to become
effective, when the timer is next by a PWM Match event.

.
RV College of
Engineering Go, change the world
RV College of
Engineering Go, change the world

Generate the PWM waveform of


25% duty cycle at the PWM channel 3
#include <LPC214x.h>
int main()
{
PWM_Init();
while(1)
{
PWMMR3 = 500; // value which decides pulse ON time, ¼ of 2000
PWMLER = 0X08;
}
}
RV College of
Engineering Go, change the world

void PWM_Init(void)
{
//P0.1 pin has second alternate function as PWM3 channel, so using PINSEL0 register
// Select P0.1 as PWM output , bits D2 & D3 are for P0.1
PINSEL0 |= 0x00000008;
//load the value to MR0 to fix the pulse rate
PWMMR0 = 2000; // any other value, I could have taken
//Configure PWM channel 3 as single edge type and enable the channel
//bit D3 to select single edge(make it 0), bit D11 is for enabling PWM3 channel(make it 1)
PWMPCR = 0x00000800;
// enable PWM unit of LPC2148 and start the timer
PWMTCR = 0x00000009; // bit D3 = 1 (enable PWM), bit D0=1 (start the timer)
}
RV College of
Engineering Go, change the world

Calculation of duty cycle in the above program:


if PCLK is 15MHz, then T= 0.067µsec,
Pulse rate/Period, T = ON+OFF = 2000 * T
Puse width, Ton = 500 * T
Duty cycle = Ton/T = 500*T/2000*T = ¼= 0.25 x 100 = 25 %
RV College of
Engineering Go, change the world

DC Motor Speed Control

Effective DC Voltage Applied to DC Motor


= (Ton/T) x Supply voltage (say 12V)

If Ton = T, Full Power i.e 12V


If Ton = T/2, 50 % DC (1/2 Power)
RV College of
Engineering Go, change the world

H Bridge arrangement
To Drive DC Motor
RV College of
Engineering Go, change the world

Interface DC Motor to LPC 2148 and write an Embedded C program to


generate PWM wave to control speed of DC motor.
RV College of
Engineering Go, change the world

#include <lpc214x.h>
void runDCMotor(int direction, int dutycycle) ;

int main()
{
IO0DIR |= 1U << 28; //set P0.28 as output pin
PINSEL0 |= 2 << 18; //select P0.9 as PWM6 (option 2)
runDCMotor(2,10); // run at 10% duty cycle
while(1); // do other jobs
}
RV College of
Engineering Go, change the world

void runDCMotor(int direction, int dutycycle)


{
if (direction == 1)
IO0SET = 1 << 28; //set to 1, to choose anti-clockwise direction
else
IO0CLR = 1 << 28; //set to 0, to choose clockwise direction
PWMMR0 = 1000; // set PULSE rate to value suitable for DC Motor operation
PWMMR6 = (1000U*dutycycle)/100; // set PULSE period
PWMPCR = (1 << 14); // enable PWM6 channel
PWMTCR = 0x00000009; // bit D3 = 1 (enable PWM), bit D0=1 (start the timer)
PWMLER = 0X70; // load the new values to PWMMR0 and PWMMR6 registers
}
RV College of
Engineering Go, change the world

//Control the duty cycle by analog input fed from potentiometer.


//Duty cycle - 0 to 100 controlled by PWM, fed from Potentiometer connected to ADC
#include <lpc214x.h>
void runDCMotor(int direction,int dutycycle);
unsigned int adc(int no,int ch); // refer UNIT 4 Notes, for this function implementation
int main()
{
int dig_val;
IO0DIR |= 0x00FF0000 | 1U << 30; // to set P0.16 to P0.23 as o/ps
do{
dig_val = adc(1,2) / 10;
if(dig_val > 100) dig_val =100;
runDCMotor(1,dig_val); // run at 10% duty cycle
}
while(1);
}

You might also like