Sunteți pe pagina 1din 12

DC Motor Control using PWM

PWM
• PWM is a digital modulation technique widely used for coding
a digital data into a pulsating signal which usually looks like a
square wave.
• PWM uses a rectangular pulse train whose modulation results
in the average value of the pulse sequence.

• Duty cycle = (d/t *100) %


• PIC18F supports the PWM through CCP module
PWM Operation Steps
• Following steps are needed to setup the PWM module in PIC18F:
1. Set the PWM Period - The PWM period is set by writing the PR2 register.
So the value can be calculated by using this formula,
PWM period = [(PR2) + 1] x 4 x Tosc x (TMR2 prescale value)
2. Set PWM duty cycle - The PWM duty cycle is specified by writing
to the CCPRxL register and to the CCPxCON<5:4> bits.
3. Output pin configuration - set the pins used by PWM module as
output
4. Prescalar value and timer configuration
- Prescalar value is set in TMR2 register.
- After setting up the register, enable the timer2 module by writing to
T2CON.
5. PWM Module configuration
- by CCP1CON mode selection bits (bits 3-0).
PWM Register setting – Sample code
TRISC &=0xFD; // Set RC2 as output
/* CCP PWM mode */
CCP1CON &= 0xCF; // 5,4 bits zeroed (DC1B1:DC1B0 = 00)
CCP1CON |= 0x0C; // PWM mode ( CCP1M3:CCP1M0 = 1100)
/* Timer2 configuration */
PR2 = period; // configure timer2 period
T2CON = 0x02; // Set prescalar 16
TMR2ON = 1; // timer2 on

CCPR1L = dutycycle>>2; // PWM duty cycle - first 8-bits (MSb)


CCP1CON &= 0xCF; // 5,4 bits zeroed (DC1B1:DC1B0 = 00)
CCP1CON |= ((dutycycle<<4)&0x30); // PWM duty cycle - last 2-bits (LSb) in CCP1CON 5,4 bits
DC motor interfacing with PIC
• DC motors need much more current than a microcontroller
can provide, so it needs separate drivers.
• The driver should have the following capabilities:
– It should be able to handle high current, normally more than 250mA
– Filter back emf for coil safety
• A power transistor for handling high current and a diode
across the motor for solving back EMF problem can be used to
drive the motor. (The diode will prevent current in reverse
direction thus blocking back EMF)
DC motor interfacing with PIC…
• The driver circuit has the following drawbacks
– 12V is the Motor voltage, the power transistor can drive the motor with
this specifications.
– This circuit will drive the Motor in one direction only
• H bridge is used to solve the inability to rotate in both directions

• To rotate the motor, we need to close either S1 with S4 or S2


with S3. These will result in the rotation in two directions
Detailed H bridge circuit
DC motor control using PWM
DC motor control using PWM
• DC Motor is interfaced with PIC Microcontroller using L293D
Motor Driver
• Two Push Button switches are provided to control the speed
of the motor.
• Two push button switches are connected to 1st and 2nd pins
of PORTD which is used to control the duty ratio of the
generated PWM. Pressing the UP switch increases the duty
cycle, which increases the motor speed while pressing the
DOWN switch decreases the duty cycle, which decreases the
motor speed.
• The direction of rotation of motor can be controlled using the
1st and 2nd pins of PORTB.
Sample Code
PIC18F interface with DC motor
Programming steps
• Enable ADC and map its output into 0-255 range.
• Enable Global interrupt, INT0 external interrupt with a rising
edge triggered mode.
• Set PWM mode.
• Vary Duty cycle with ADC value and in an interrupt routine, we
are toggling motor direction.
• Now continuously check for an interrupt for direction and
read ADC value for speed control.
• A switch will produce interrupt which causes to change in
motor direction.

S-ar putea să vă placă și