7.
PWM Speed & Direction Control Of DC Motor
OBJECTIVE :
The objective of this experiment is to design a speed control system of DC Motor by
using PIC16F877A microcontroller.
REQUIREMENT : MPLAB-IDE , PIC SIM Lab .
THEORY :
With the help of PWM it is possible to control the average power delivered to
a load and by thus we can easily regulate the speed of the DC Motor. When the
width of pulse is high, the motor will rotate with full speed. And with low pulse
width, speed of motor gets reduced. In both cases full current reaches the motor and
only the average power changes, thus we get better torque in each case.
PWM Mode -
In Pulse Width Mode, the CCPx pin produces up to 10-bit resolution
PWM output -
Since the. CCP1 pin is multiplexed with the PORTC data latch, the TRISC<2>
bit must be cleared to make the CCP1 pin as output.
Setup for PWM operation -
1. Set the PWM period by writing to the PR2 register.
2. Set the PWM duty cycle by writing to the CCP1L register and CCP1CON
<5:4> bits.
3. Make the CCP1 pin an output by clearing the TRISC<2> bit.
4. Set the TMR2 prescale value and enable Timer2 by writing to T2CON
register.
5. Configure the CCP1 module for PWM operation.
CALCULATION :
Let us generate a square wave having 10kHz frequency with 50% duty cycle and
assume 4MHz clock frequency.
Here : PWM Period = 1/10k
Tosc = 1/4M
PWM duty cycle = 50%
Let us take, TMR2 prescale valve= 1:1
We know , PWM Period = [(PR2)+1] * 4 * Tosc * TMR2 prescale value
1/10k = [(PR2)+1] * 4 * 1/4M * 1:1
(PR2)+1 = 100
PR2 = 99 d
Also ,
PWM Duty cycle = (CCPR1L:CCP1CON<5:4>) *Tosc * TMR2 prescale value
50 = (CCPR1L:CCP1CON<5:4>) * 1/4M * 1:1
50*4 = (CCPR1L:CCP1CON<5:4>)
(CCPR1L:CCP1CON<5:4>) = 200 d
(CCPR1L:CCP1CON<5:4>) = 0011001000 hex
PROGRAM :
; Oscillator : 4 MHz
;Program That Generates PWM
;PR2 = 99 (So The Frequency Would Be 10kHz)
#INCLUDE <P16F877A.INC>
__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _RC_OSC
& _WRT_OFF & _LVP_ON & _CPD_OFF
ORG 0X00
GOTO MAIN
MAIN:
BCF STATUS,RP0
CLRF PORTC ;CLEAR CONTENT OF PORT C
BSF STATUS,RP0 ;CHANGE TO BANK 1
CLRF TRISC
MOVLW 0X63 ; period = 0.0001 seconds 99d=63h
MOVWF PR2
BCFSTATUS,RP0
MOVLW B'00110010';50%
;MOVLW B'01010000';80%
MOVWF CCPR1L
BCF CCP1CON,5
BCF CCP1CON,4
BSFSTATUS,RP0
BCF TRISC,2 ; pin 2 of port c is an output
BCFSTATUS,RP0
MOVLW B'00000100'
MOVWF T2CON ; prescale =1 , timer2 is on
BSF CCP1CON,3 ;configuring ccp1 module for pwm operation
BSF CCP1CON,2
END
Figure 1.1 : PICSIM LAB Virtual Board
Figure 1.2 : OUTPUT OF PWM FOR 50% DUTY CYCLE
Figure 1.3 : OUTPUT OF PWM FOR 80% DUTY CYCLE
FLOWCHART :
CONCLUSION :
DC motors have speed control capabilities, which means that speed, torque and
even direction of rotation can be changed at anytime to meet new condition. By
varying the PWM signal from microcontroller to the motor driver, motor speed can
be controlled back to desired value easily.