Winter 2009
Chapter 15: Capture/ Compare/ PWM ModulePIC16F917 Data Sheet
Source: Microchip
Md. Suruz Miahhttp://www.site.uottawa.ca/˜ smiah069
School of Information Technology and Engineering (SITE)University of Ottawa
March 16, 2009
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 1 / 16
Winter 2009
Outline
1 IntroductionTimer ResourceCCP ModuleCCP Module - Control Register Format
2 Pulse Width Modulation (PWM) ModePWM BasicsPWM in PIC16F917
3 Example Program - PWM
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 2 / 16
Winter 2009
Introduction
Outline
1 IntroductionTimer ResourceCCP ModuleCCP Module - Control Register Format
2 Pulse Width Modulation (PWM) Mode
3 Example Program - PWM
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 3 / 16
Winter 2009
Introduction
Introduction
A Capture/ Compare/ PWM (CCP) module is softwareprogrammable to operate in one of three modes:
Capture modeCompare modePulse Width Modulation(PWM) mode
Two modules in PIC16Fxxx family are1. CCP1 module2. CCP2 module
Each CCP module contains a 16-bit register which can operate asa
a) 16-bit capture registerb) 16-bit compare registerc) PWM Master/Slave Duty Cycle register
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 4 / 16
Winter 2009
Introduction Timer Resource
Timer Resource
For the CCP module to function, timer resources must be used inconjunction with the CCP module
Desired CCP mode of operation determines which timerresources are required
CCP mode Timer resource
Capture Timer 1Compare Timer 1PWM Timer 2
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 5 / 16
Winter 2009
Introduction CCP Module
CCP Module
CCP1 module
CCP2 module
CCP1 module
Capture/Compare/PWM Register1 (CCPR1) is comprised of two8-bit registers: CCPR1L (low byte) and CCPR1H (high byte)
CCP1CON register controls the operation of CCP1
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 6 / 16
Winter 2009
Introduction CCP Module
CCP Module
CCP1 module
CCP2 module
CCP2 module
Capture/Compare/PWM Register2 (CCPR2) is comprised of two8-bit registers: CCPR2L (low byte) and CCPR2H (high byte)
CCP2CON register controls the operation of CCP2
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 6 / 16
Winter 2009
Introduction CCP Module - Control Register Format
CCP1CON-CCP2CON1 Register format
bit 0
CCPxX CCPxY CCPxM3 CCPxM2 CCPxM1 CCPxM0
LSBs of PWM duty cycle Mode select (11xx for PWM mode)Unimplemented (read as ’0’)
bit 7
1CCP2CON used for PIC16F914/917 onlyM.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 7 / 16
Winter 2009
Pulse Width Modulation (PWM) Mode
Outline
1 Introduction
2 Pulse Width Modulation (PWM) ModePWM BasicsPWM in PIC16F917
3 Example Program - PWM
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 8 / 16
Winter 2009
Pulse Width Modulation (PWM) Mode PWM Basics
PWM Mode
PWM Basics
A PWM output is a signal that has a time-base (period) and a timethat the output stays high (duty cycle)
The period is the duration after which the PWM rising edgerepeats itself
The resolution of the PWM output is the granularity with which theduty cycle can be varied
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 9 / 16
Winter 2009
Pulse Width Modulation (PWM) Mode PWM in PIC16F917
PWM in PIC16F917
PWM period
Duty cycle
TIMR2 = CCPRxHTIMR2 = PR2TIMR2 = PR2
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 10 / 16
Winter 2009
Pulse Width Modulation (PWM) Mode PWM in PIC16F917
PWM in PIC16F917
CCPx pin produces up to a 10-bit resolution PWM output
TRISC< 5 > bit must be cleared to make theRC5/T1CKI/CCP1/SEG10 pin an output
PWM period calculation
PWM period = [PR2 + 1]× 4× Tosc × (TMR2 presclale value) ,
where PR2 is the 8-bit period register and Tosc is the oscillatortime periodWhen TMR2 is equal to PR2, the following three events occur onthe next increment cycle:
TMR2 is clearedThe RC5/T1CKI/CCPx/SEG10 pin is set (exception: if PWM dutycycle = 0%, the RC5/T1CKI/CCP1/SEG10 pin will not be set)The PWM duty cycle is latched from CCPR1L into CCPR1H
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 11 / 16
Winter 2009
Pulse Width Modulation (PWM) Mode PWM in PIC16F917
PWM in PIC16F917
PWM duty cycle
PWM duty cycle is specified by writing to the CCPRxL registerand to the CCPxCON< 5 : 4 > bits
CCPRxL contains the eight MSbs and the CCPxCON< 5 : 4 >
contains the two LSbs
The 10-bit PWM value is represented byCCPRxL:CCPxCON< 5 : 4 >
PWM duty cycle, D, is calculated by:
D = (CCPRxL : CCPxCON < 5 : 4 >)× Tosc
×(TMR2 presclale value)
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 12 / 16
Winter 2009
Pulse Width Modulation (PWM) Mode PWM in PIC16F917
PWM in PIC16F917
Steps for PWM operation
1 Set the PWM period by writing to the PR2 register
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 13 / 16
Winter 2009
Pulse Width Modulation (PWM) Mode PWM in PIC16F917
PWM in PIC16F917
Steps for PWM operation
1 Set the PWM period by writing to the PR2 register2 Set the PWM duty cycle by writing to the CCPRxL register and
CCPxCON< 5 : 4 > bits
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 13 / 16
Winter 2009
Pulse Width Modulation (PWM) Mode PWM in PIC16F917
PWM in PIC16F917
Steps for PWM operation
1 Set the PWM period by writing to the PR2 register2 Set the PWM duty cycle by writing to the CCPRxL register and
CCPxCON< 5 : 4 > bits3 Make the RC5/T1CKI/CCPx/SEG10 pin an output by clearing the
TRISC< 5 > bit
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 13 / 16
Winter 2009
Pulse Width Modulation (PWM) Mode PWM in PIC16F917
PWM in PIC16F917
Steps for PWM operation
1 Set the PWM period by writing to the PR2 register2 Set the PWM duty cycle by writing to the CCPRxL register and
CCPxCON< 5 : 4 > bits3 Make the RC5/T1CKI/CCPx/SEG10 pin an output by clearing the
TRISC< 5 > bit4 Set the TMR2 prescale value and enable Timer2 by writing to
T2CON
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 13 / 16
Winter 2009
Pulse Width Modulation (PWM) Mode PWM in PIC16F917
PWM in PIC16F917
Steps for PWM operation
1 Set the PWM period by writing to the PR2 register2 Set the PWM duty cycle by writing to the CCPRxL register and
CCPxCON< 5 : 4 > bits3 Make the RC5/T1CKI/CCPx/SEG10 pin an output by clearing the
TRISC< 5 > bit4 Set the TMR2 prescale value and enable Timer2 by writing to
T2CON5 Configure the CCPx module for PWM operation
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 13 / 16
Winter 2009
Pulse Width Modulation (PWM) Mode PWM in PIC16F917
PWM in PIC16F917
Steps for PWM operation
1 Set the PWM period by writing to the PR2 register2 Set the PWM duty cycle by writing to the CCPRxL register and
CCPxCON< 5 : 4 > bits3 Make the RC5/T1CKI/CCPx/SEG10 pin an output by clearing the
TRISC< 5 > bit4 Set the TMR2 prescale value and enable Timer2 by writing to
T2CON5 Configure the CCPx module for PWM operation
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 13 / 16
Winter 2009
Example Program - PWM
Outline
1 Introduction
2 Pulse Width Modulation (PWM) Mode
3 Example Program - PWM
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 14 / 16
Winter 2009
Example Program - PWM
Example Program - PWM
Write a program that generates a PWM signal of 31.2 KHz and turnson a DC motor to rotate at a constant speed of 316 rpm.Setup:
1 Hardware connections2 Initialize bank
Hardware connections
#define N2 PORTD,2 ; PWM with CCP2 module#define P1 PORTD,7 ; Variable to turn on the motor
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 15 / 16
Winter 2009
Example Program - PWM
Example Program - PWM
Write a program that generates a PWM signal of 31.2 KHz and turnson a DC motor to rotate at a constant speed of 316 rpm.Setup:
1 Hardware connections2 Initialize bank3 Configure I/O pins
Initialize bank
clrf STATUS ; bank 0bsf STATUS,RP0 ; we’ll set up the bank 1 Special Function Registersfirst
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 15 / 16
Winter 2009
Example Program - PWM
Example Program - PWM
Write a program that generates a PWM signal of 31.2 KHz and turnson a DC motor to rotate at a constant speed of 316 rpm.Setup:
1 Hardware connections2 Initialize bank3 Configure I/O pins4 Set internal oscillator frequency
Configure I/O pins
bcf TRISD,7 ; RD7 is an outputbcf TRISD,2 ; RD2 is an output
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 15 / 16
Winter 2009
Example Program - PWM
Example Program - PWM
Write a program that generates a PWM signal of 31.2 KHz and turnson a DC motor to rotate at a constant speed of 316 rpm.Setup:
1 Hardware connections2 Initialize bank3 Configure I/O pins4 Set internal oscillator frequency5 Configure Capture Compare PWM Module
Set internal oscillator frequency
movlw b’01110000’ ; 8Mhzmovwf OSCCONmovlw 0x3F ; 31.2 kHz PWMmovwf PR2bcf STATUS,RP0 ; go back to bank 0
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 15 / 16
Winter 2009
Example Program - PWM
Example Program - PWM
Write a program that generates a PWM signal of 31.2 KHz and turnson a DC motor to rotate at a constant speed of 316 rpm.Setup:
1 Hardware connections2 Initialize bank3 Configure I/O pins4 Set internal oscillator frequency5 Configure Capture Compare PWM Module
Configure CCP module
clrf CCPR2Lmovlw b’00001100’ ; Select pwm modemovwf CCP2CONbsf T2CON,TMR2ON ; turn on Timer 2clrf PORTD ; Turn off Motor
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 15 / 16
Winter 2009
Example Program - PWM
Example Program - PWM
Write a program that generates a PWM signal of 31.2 KHz and turnson a DC motor to rotate at a constant speed of 316 rpm.Setup:
1 Hardware connections2 Initialize bank3 Configure I/O pins4 Set internal oscillator frequency5 Configure Capture Compare PWM Module
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 15 / 16
Winter 2009
Example Program - PWM
Example Program - PWM
Main program
;;;;;;;;;;;;;; Set duty cycle for 316 RPM ;;;;;;;;;;;;;movlw 0x3F ; Turn on PWM, bit # 5,4 = 1,1iorwf CCP2CON,fmovlw 0x13 ; 8 bit of CCPR2Lmovwf CCPR2Lbsf P1 ; Turn on Motorend ; directive ’end of program’
M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 16 / 16