Вы находитесь на странице: 1из 27

Chapter 15: Capture/ Compare/ PWM Module

PIC16F917 Data Sheet


Source: Microchip

Md. Suruz Miah


http://www.site.uottawa.ca/˜ smiah069

School of Information Technology and Engineering (SITE)


University of Ottawa

March 16, 2009

Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 1 / 16
Outline

1 Introduction
Timer Resource
CCP Module
CCP Module - Control Register Format

2 Pulse Width Modulation (PWM) Mode


PWM Basics
PWM in PIC16F917

3 Example Program - PWM

Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 2 / 16
Introduction

Outline

1 Introduction
Timer Resource
CCP Module
CCP Module - Control Register Format

2 Pulse Width Modulation (PWM) Mode

3 Example Program - PWM

Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 3 / 16
Introduction

Introduction

A Capture/ Compare/ PWM (CCP) module is software


programmable to operate in one of three modes:
Capture mode
Compare mode
Pulse Width Modulation(PWM) mode
Two modules in PIC16Fxxx family are
1. CCP1 module
2. CCP2 module
Each CCP module contains a 16-bit register which can operate as
a
a) 16-bit capture register
b) 16-bit compare register
c) PWM Master/Slave Duty Cycle register

Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 4 / 16
Introduction Timer Resource

Timer Resource

For the CCP module to function, timer resources must be used in


conjunction with the CCP module
Desired CCP mode of operation determines which timer
resources are required

CCP mode Timer resource


Capture Timer 1
Compare Timer 1
PWM Timer 2

Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 5 / 16
Introduction CCP Module

CCP Module

CCP1 module
CCP2 module

CCP1 module
Capture/Compare/PWM Register1 (CCPR1) is comprised of two
8-bit registers: CCPR1L (low byte) and CCPR1H (high byte)
CCP1CON register controls the operation of CCP1

Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 6 / 16
Introduction CCP Module

CCP Module

CCP1 module
CCP2 module

CCP2 module
Capture/Compare/PWM Register2 (CCPR2) is comprised of two
8-bit registers: CCPR2L (low byte) and CCPR2H (high byte)
CCP2CON register controls the operation of CCP2

Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 6 / 16
Introduction CCP Module - Control Register Format

CCP1CON-CCP2CON1 Register format

bit 7 bit 0

CCPxX CCPxY CCPxM3 CCPxM2 CCPxM1 CCPxM0

Unimplemented (read as ’0’) LSBs of PWM duty cycle Mode select (11xx for PWM mode)

1
CCP2CON used for PIC16F914/917 only Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 7 / 16
Pulse Width Modulation (PWM) Mode

Outline

1 Introduction

2 Pulse Width Modulation (PWM) Mode


PWM Basics
PWM in PIC16F917

3 Example Program - PWM

Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 8 / 16
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 time
that the output stays high (duty cycle)
The period is the duration after which the PWM rising edge
repeats itself
The resolution of the PWM output is the granularity with which the
duty cycle can be varied

Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 9 / 16
Pulse Width Modulation (PWM) Mode PWM in PIC16F917

PWM in PIC16F917

PWM period

Duty cycle

TIMR2 = PR2
TIMR2 = PR2 TIMR2 = CCPRxH

Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 10 / 16
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 the
RC5/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 oscillator
time period
When TMR2 is equal to PR2, the following three events occur on
the next increment cycle:
TMR2 is cleared
The RC5/T1CKI/CCPx/SEG10 pin is set (exception: if PWM duty
cycle = 0%, the RC5/T1CKI/CCP1/SEG10 pin will not be set)
The PWM duty cycle is latched from CCPR1L into CCPR1H
Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 11 / 16
Pulse Width Modulation (PWM) Mode PWM in PIC16F917

PWM in PIC16F917

PWM duty cycle


PWM duty cycle is specified by writing to the CCPRxL register
and 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 by
CCPRxL:CCPxCON< 5 : 4 >
PWM duty cycle, D, is calculated by:

D = (CCPRxL : CCPxCON < 5 : 4 >) × Tosc

×(TMR2 presclale value)

Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 12 / 16
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

Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 13 / 16
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
2 Set the PWM duty cycle by writing to the CCPRxL register and
CCPxCON< 5 : 4 > bits

Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 13 / 16
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
2 Set the PWM duty cycle by writing to the CCPRxL register and
CCPxCON< 5 : 4 > bits
3 Make the RC5/T1CKI/CCPx/SEG10 pin an output by clearing the
TRISC< 5 > bit

Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 13 / 16
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
2 Set the PWM duty cycle by writing to the CCPRxL register and
CCPxCON< 5 : 4 > bits
3 Make the RC5/T1CKI/CCPx/SEG10 pin an output by clearing the
TRISC< 5 > bit
4 Set the TMR2 prescale value and enable Timer2 by writing to
T2CON

Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 13 / 16
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
2 Set the PWM duty cycle by writing to the CCPRxL register and
CCPxCON< 5 : 4 > bits
3 Make the RC5/T1CKI/CCPx/SEG10 pin an output by clearing the
TRISC< 5 > bit
4 Set the TMR2 prescale value and enable Timer2 by writing to
T2CON
5 Configure the CCPx module for PWM operation

Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 13 / 16
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
2 Set the PWM duty cycle by writing to the CCPRxL register and
CCPxCON< 5 : 4 > bits
3 Make the RC5/T1CKI/CCPx/SEG10 pin an output by clearing the
TRISC< 5 > bit
4 Set the TMR2 prescale value and enable Timer2 by writing to
T2CON
5 Configure the CCPx module for PWM operation

Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 13 / 16
Example Program - PWM

Outline

1 Introduction

2 Pulse Width Modulation (PWM) Mode

3 Example Program - PWM

Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 14 / 16
Example Program - PWM

Example Program - PWM


Write a program that generates a PWM signal of 31.2 KHz and turns
on a DC motor to rotate at a constant speed of 316 rpm.
Setup:
1 Hardware connections
2 Initialize bank

Hardware connections
#define N2 PORTD,2 ; PWM with CCP2 module
#define P1 PORTD,7 ; Variable to turn on the motor

Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 15 / 16
Example Program - PWM

Example Program - PWM


Write a program that generates a PWM signal of 31.2 KHz and turns
on a DC motor to rotate at a constant speed of 316 rpm.
Setup:
1 Hardware connections
2 Initialize bank
3 Configure I/O pins

Initialize bank
clrf STATUS ; bank 0
bsf STATUS,RP0 ; we’ll set up the bank 1 Special Function Registers
first

Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 15 / 16
Example Program - PWM

Example Program - PWM


Write a program that generates a PWM signal of 31.2 KHz and turns
on a DC motor to rotate at a constant speed of 316 rpm.
Setup:
1 Hardware connections
2 Initialize bank
3 Configure I/O pins
4 Set internal oscillator frequency

Configure I/O pins


bcf TRISD,7 ; RD7 is an output
bcf TRISD,2 ; RD2 is an output

Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 15 / 16
Example Program - PWM

Example Program - PWM


Write a program that generates a PWM signal of 31.2 KHz and turns
on a DC motor to rotate at a constant speed of 316 rpm.
Setup:
1 Hardware connections
2 Initialize bank
3 Configure I/O pins
4 Set internal oscillator frequency
5 Configure Capture Compare PWM Module

Set internal oscillator frequency


movlw b’01110000’ ; 8Mhz
movwf OSCCON
movlw 0x3F ; 31.2 kHz PWM
movwf PR2
bcf STATUS,RP0 ; go back to bank 0
Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 15 / 16
Example Program - PWM

Example Program - PWM


Write a program that generates a PWM signal of 31.2 KHz and turns
on a DC motor to rotate at a constant speed of 316 rpm.
Setup:
1 Hardware connections
2 Initialize bank
3 Configure I/O pins
4 Set internal oscillator frequency
5 Configure Capture Compare PWM Module

Configure CCP module


clrf CCPR2L
movlw b’00001100’ ; Select pwm mode
movwf CCP2CON
bsf T2CON,TMR2ON ; turn on Timer 2
clrf PORTD ; Turn off Motor
Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 15 / 16
Example Program - PWM

Example Program - PWM


Write a program that generates a PWM signal of 31.2 KHz and turns
on a DC motor to rotate at a constant speed of 316 rpm.
Setup:
1 Hardware connections
2 Initialize bank
3 Configure I/O pins
4 Set internal oscillator frequency
5 Configure Capture Compare PWM Module

Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 15 / 16
Example Program - PWM

Example Program - PWM

Main program
;;;;;;;;;;;;;; Set duty cycle for 316 RPM ;;;;;;;;;;;;;
movlw 0x3F ; Turn on PWM, bit # 5,4 = 1,1
iorwf CCP2CON,f
movlw 0x13 ; 8 bit of CCPR2L
movwf CCPR2L
bsf P1 ; Turn on Motor
end ; directive ’end of program’

Winter 2009

M.S. Miah (University of Ottawa) ELG4154: Integrated Control System March 16, 2009 16 / 16

Вам также может понравиться