Beruflich Dokumente
Kultur Dokumente
HIGHLIGHTS
This section of the manual contains the following major topics:
Note: Refer to the specific device data sheet for further information on the number of
channels available in a particular device. All Output Compare channels are function-
ally identical. In this section, an x in the register name is a generic reference to an
Output Compare channel in place of a specific Output Compare channel number.
This document supersedes the following PIC24 and dsPIC Family Reference Manual sections:
1.0 INTRODUCTION
The Output Compare module has the ability to compare the value of a selected time base with
the value of one or two Compare registers (depending on the operation mode selected).
Furthermore, it has the ability to generate a single output pulse, or a train of output pulses, on a
compare match event. Like most PIC MCU peripherals, it also has the ability to generate
interrupts on compare match events.
Refer to the specific device data sheet for the number of channels available in a particular device.
All Output Compare channels are functionally identical. In this section, an x in the pin, register
or bit name denotes the specific Output Compare channel.
Each Output Compare channel can use one of two selectable time bases. The time base is
selected using the OCTSEL bit (OCxCON<3>). Please refer to the device data sheet for the
specific timers that can be used with each Output Compare channel number. The available time
bases, Timer2 and Timer3 do not support Asynchronous mode; therefore, the Output Compare
module will operate only in Synchronous mode.
OCxRS(1)
Output S Q
OCxR(1) OCx(1)
Logic R
3 Output Enable
OCM<2:0>
Mode Select OCFA or OCFB(2)
Comparator
0 1 OCTSEL 0 1
16 16
Note 1: Where x is shown, reference is made to the registers associated with the respective Output Compare
Channels 1 through 5.
2: OCFA pin controls Channels OC1-OC4. OCFB pin controls Channel OC5.
3: Each Output Compare channel can use one of two selectable time bases. Refer to the device data sheet for
the time bases associated with the module.
Note 1: The user software must disable the associated Output Compare module when writing to the Output
Compare x Control register
2: Refer to the device data sheet for specific time bases available to the Output Compare module.
3: Some older documents refer to this mode as Active-High One-Shot mode, compare event forces the OCx
pin low.
4: Some older documents refer to this mode as Active-Low One-Shot mode, compare event forces the OCx
pin high.
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as 0
-n = Value at POR 1 = Bit is set 0 = Bit is cleared x = Bit is unknown
.
Register 2-3: OCxRS: Output Compare x Secondary Register
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as 0
-n = Value at POR 1 = Bit is set 0 = Bit is cleared x = Bit is unknown
Figure 4-1: Single Compare Match Mode: Set OCx High on Compare Match Event(1,2)
TMRy 3000 3001 3002 3003 3004 3FFF 4000 0000 0001
TMRy Resets Here
PRy 4000
OCxR 3002
OCx Pin
2 TCY Cleared by User
OCxIF
Note 1: An x represents the Output Compare channel number. A y represents the time base number.
2: OCxR = Output Compare x register, OCxRS = Output Compare x Secondary register.
Timer is Reset on
Period Match
OCxR
TMRy
OCx
(OCM<2:0> = 001)
~
~
Cleared by User
Software
OCxIF
1 2 3
1. Active-Low One-Shot mode is enabled, the OCx pin is driven low immediately.
2. Timer is enabled and starts incrementing.
3. On a compare match, the OCx pin is driven high and an Output Compare interrupt flag is set.
Example 4-1: Setup for Output Compare Module in Single Compare Match Mode Output Driven High
// Initialize Output Compare Module
OC1CONbits.OCM = 0b000; // Disable Output Compare Module
OC1CONbits.OCTSEL = 0; // Select Timer 2 as output compare time base
OC1R = 100; // Load the Compare Register Value
IPC0bits.OC1IP = 0x01; // Set Output Compare 1 Interrupt Priority Level
IFS0bits.OC1IF = 0; // Clear Output Compare 1 Interrupt Flag
IEC0bits.OC1IE = 1; // Enable Output Compare 1 interrupt
OC1CONbits.OCM = 0b001; // Select the Output Compare mode
Figure 4-3: Single Compare Match Mode: Force OCx Low on Compare Match Event(1,2)
TMRy 47FE 47FF 4800 4801 4802 4BFF 4C00 0000 0001
TMRy Resets Here
PRy 4C00
OCxR 4800
OCx Pin
2 TCY Cleared by User
OCxIF
Note 1: An x represents the Output Compare channel number. A y represents the time base number.
2: OCxR = Output Compare x register, OCxRS = Output Compare x Secondary register.
Timer is Reset on
Period Match
OCxR
TMRy
OCx
(OCM<2:0> = 010)
~
~
Cleared by User
Software
OCxIF
1 2 3
1. Active-High One-Shot mode is enabled, the OCx pin is driven high immediately.
2. Timer is enabled and starts incrementing.
3. On a compare match, the OCx pin is driven low and an Output Compare interrupt is generated.
Example 4-2: Setup for Output Compare Module in Single Compare Match Mode Output Driven Low
// Initialize Output Compare Module
OC1CONbits.OCM = 0b000; // Disable Output Compare Module
OC1CONbits.OCTSEL = 0; // Select Timer 2 as output compare time base
OC1R = 100; // Load the Compare Register Value
IPC0bits.OC1IP = 0x01; // Set Output Compare 1 Interrupt Priority Level
IFS0bits.OC1IF = 0; // Clear Output Compare 1 Interrupt Flag
IEC0bits.OC1IE = 1; // Enable Output Compare 1 interrupt
OC1CONbits.OCM = 0b010; // Select the Output Compare mode
Note: The internal OCx pin output logic is set to a logic 0 on a device Reset. However,
the operational OCx pin state for the Toggle mode can be set by the user software.
Example 4-3 shows a code example for defining the desired initial OCx pin state in
the Toggle mode of operation.
Figure 4-5: Single Compare Match Mode: Toggle Output on Compare Match Event (PRy > OCxR)(1,2)
TMRy 0500 0501 0502 0600 0000 0001 0500 0501 0502
TMRy Resets Here
PRy 0600
OCxR 0500
OCx Pin
2 TCY Cleared by User
OCxIF
Note 1: An x represents the Output Compare channel number. A y represents the time base number.
2: OCxR = Output Compare x register, OCxRS = Output Compare x Secondary register.
Timer is Reset on
Period Match
OCxR
TMRy
OCx
(OCM<2:0> = 011)
OCx
(OCM<2:0> = 011)
Cleared by User
~
~
~
~
~
~
OCxIF Software
1 2 3
1. Toggle mode is enabled, the OCx pin maintains the current state.
2. Timer is enabled and starts incrementing.
3. On a compare match, the OCx pin state is toggled and an Output Compare interrupt flag is set.
Figure 4-7: Single Compare Match Mode: Toggle Output on Compare Match Event (PRy = OCxR)(1,2)
TMRy 0500 0000 0001 0500 0000 0001 0500 0000 0001
TMRy Resets Here TMRy Resets Here
PRy 0500
OCxR 0500
OCx Pin
2 TCY 2 TCY 2 TCY
OCxIF
Cleared by User Cleared by User
Note 1: An x represents the Output Compare channel number. A y represents the time base number.
2: OCxR = Output Compare x register, OCxRS = Output Compare x Secondary register.
OC1CON = 0x0001; // enable module for OC1 pin low, toggle high
OC1CONbits.OCM1 = 1; // set module to toggle mode with initial pin
// state low
OC1CON = 0x0002; // enable module for OC1 pin high, toggle low
OC1CONbits.OCM0 = 1; // set module to toggle mode with initial pin
// state high
Example 4-4 shows example code for the configuration and interrupt service of the Single
Compare Match mode toggle event.
Example 4-4: Single Compare Match Mode: Toggle Setup and Interrupt Servicing
// The following code example will set the Output Compare 1 module
// for interrupts on the toggle event and select Timer 2 as the clock
// source for the compare time-base. It is assumed that Timer 2
// and Period Register 2 are properly configured. Timer 2 will
// be enabled here.
Figure 4-8: Single Compare Match Mode: Toggle Output on Compare Match Event (PRy > OCxR)(1,2)
TMRy 0500 0000 0001 0500 0000 0001 0500 0000 0001
TMRy Resets Here TMRy Resets Here
PRy 0500
OCxR 0500
OCx Pin
2 TCY 2 TCY
OCxIF
Note 1: An x represents the Output Compare channel number. A y represents the time base number.
2: OCxR = Output Compare x register, OCxRS = Output Compare x Secondary register.
TMRy 3000 3001 3002 3003 3004 3005 3006 4000 0000
TMRy Resets Here
PRy 4000
OCxR 3000
OCxRS 3003
OCx Pin
2 TCY
OCxIF
Cleared by User
Note 1: An x represents the Output Compare channel number. A y represents the time base number.
2: OCxR = Output Compare x register, OCxRS = Output Compare x Secondary register.
OCxRS OCxRS
(7000h)
OCxR (2000h)
(4000h) OCxR
0
Time
OCx Pin
OCxIF
OCxIF = 1 OCxIF = 1
Note 1: An x represents the Output Compare channel number. A y represents the time base number.
2: OCxR = Output Compare x register, OCxRS = Output Compare x Secondary register.
Figure 4-11: Dual Compare Match Mode: Single Output Pulse Mode (OCxRS > PRy)(1,2)
TMRy 3000 3001 3002 3003 3004 3005 3006 4000 0000
TMRy Resets Here
PRy 4000
OCxR 3000
OCxRS 4100
OCx Pin
Compare Interrupt does not Occur
OCxIF
Note 1: An x represents the Output Compare channel number. A y represents the time base number.
2: OCxR = Output Compare x register, OCxRS = Output Compare x Secondary register.
Example 4-5: Single Output Pulse Mode Setup and Interrupt Servicing
// The following code example will set the Output Compare 1 module
// for interrupts on the single pulse event and select Timer 2
// as the clock source for the compare time-base. It is assumed
// that Timer 2 and Period Register 2 are properly initialized.
// Timer 2 will be enabled here.
Table 4-1: Special Cases for Dual Compare Match Mode Generating a Single Output Pulse(1,2)
SFR Logical Special Output
Operation
Relationship Conditions at OCx
PRy OCxRS and OCxR = 0, In the first iteration of the TMRy, counting from 0000h up Pulse will be
OCxRS > OCxR Initialize TMRy = 0 to PRy, the OCx pin remains low; no pulse is generated. delayed by the
After the TMRy resets to zero (on period match), the OCx value in the PRy
pin goes high due to a match with OCxR. Upon the next register
TMRy to OCxRS match, the OCx pin goes low and depending on the
remains there. The OCxIF bit will be set as a result of the setup.
second compare.
There are two alternative initial conditions to consider:
a) Initialize TMRy = 0 and set OCxR 1.
b) Initialize TMRy = PRy (PRy > 0) and set OCxR = 0
(see Figure 4-12).
PRy OCxR and OCxR 1 and TMRy counts up to OCxR and on a compare match Pulse.
OCxR OCxRS PRy 1 event (i.e., TMRy = OCxR), the OCx pin is driven to a
high state. TMRy then continues to count and eventually
resets on a period match (i.e., PRy =TMRy). The timer
then restarts from 0000h and counts up to OCxRS, and
on a compare match event (i.e., TMRy = OCxRS), the
OCx pin is driven to a low state. The OCxIF bit will be set
as a result of the second compare.
OCxRS > PRy and None Only the rising edge will be generated at the OCx pin. Rising edge/
PRy OCxR The OCxIF bit will not be set. transition to high.
OCxR = OCxRS = None The output is initialized low and remains low. The OCxIF Remains low.
PRy = 0000h bit is not set.
OCxR > PRy None Unsupported mode, the timer resets prior to the match Remains low.
condition.
Note 1: In all the cases considered herein, the TMRy register is assumed to be initialized to 0000h.
2: OCxR = Output Compare x register, OCxRS = Output Compare x Secondary register, TMRy = Timery
Count register, PRy = Timery Period register.
Timer = Period Register (PRy = 9000h) Timer = Period Register (PRy = B000h)
OCxR = 0000h
OCxR
0
Time
OCx Pin
OCxIF
OCxIF = 1 OCxIF = 1
Note 1: An x represents the Output Compare channel number. A y represents the time base number.
2: OCxR = Output Compare x register, OCxRS = Output Compare x Secondary register.
Figure 4-13: Dual Compare Match Mode: Continuous Output Pulse Mode (PRy = OCxRS)(1,2)
TMRy 3000 3001 3002 3003 0000 3000 3001 3002 3003 0000 3000
TMRy Resets Here TMRy Resets Here
PRy 3003
OCxR 3000
OCxRS 3003
OCx Pin
2 TCY 2 TCY
OCxIF
Cleared by User
Note 1: An x represents the Output Compare channel number. A y represents the time base number.
2: OCxR = Output Compare x register, OCxRS = Output Compare x Secondary register.
OCxRS OCxRS
(7000h)
OCxR (2000h)
(4000h) OCxR
0
Time
OCx Pin
OCM<2:0> = 101
TON = 1
OCxIF
Note 1: An x represents the Output Compare channel number. A y represents the time base number.
2: OCxR = Output Compare x register, OCxRS = Output Compare x Secondary register.
Figure 4-15: Dual Compare Match Mode: Continuous Output Pulse Mode (PRy < OCxRS)(1,2)
TMRy 3000 3001 3002 3003 0000 3000 3001 3002 3003 0000 3000
TMRy Resets Here TMRy Resets Here
PRy 3003
OCxR 3000
OCxRS 3003
OCx Pin
Compare Interrupt does not Occur
OCxIF
Note 1: An x represents the Output Compare channel number. A y represents the time base number.
2: OCxR = Output Compare x register, OCxRS = Output Compare x Secondary register.
Example 4-6 shows example code for configuration of the continuous output pulse event.
Table 4-2: Special Cases for Dual Compare Match Mode Generating Continuous Output Pulse Mode(1,2)
SFR Logical Special Output
Operation
Relationship Conditions at OCx
PRy OCxRS and OCxR = 0, In the first iteration of the TMRy counting from 0000h up Continuous
OCxRS > OCxR initialize TMRy = 0 to PRy, the OCx pin remains low; no pulse is generated. pulses with the
After the TMRy resets to zero (on period match), the OCx first pulse
pin goes high. Upon the next TMRy to OCxRS match, the delayed by the
OCx pin goes low. If OCxR = 0 and PRy = OCxRS, the value in the PRy
pin will remain low for one clock cycle, then be driven register, depend-
high until the next TMRy to OCxRS match. The OCxIF bit ing on setup.
will be set as a result of the second compare.
There are two alternative initial conditions to consider:
a) Initialize TMRy = 0 and set OCxR 1.
b) Initialize TMRy = PRy (PRy > 0) and set OCxR = 0
(see Figure 4-16).
PRy OCxR and OCxR 1 and TMRy counts up to OCxR, and on a compare match Continuous
OCxR OCxRS PRy 1 event (i.e., TMRy = OCxR), the OCx pin is driven to a pulses.
high state. TMRy then continues to count and eventually
resets on period match (i.e., PRy = TMRy). The timer
then restarts from 0000h and counts up to OCxRS, and
on a compare match event (i.e., TMRy = OCxR), the
OCx pin is driven to a low state. The OCxIF bit will be set
as a result of the second compare.
OCxRS > PRy and None Only one transition will be generated at the OCx pin until Rising edge/
PRy OCxR the OCxRS register contents have been changed to a transition to high.
value less than or equal to the Period register contents
(PRy). OCxIF is not set until then.
OCxR = OCxRS = None Output is initialized low and remains low. The OCxIF bit Remains low.
PRy = 0000h is not set.
OCxR > PRy None Unsupported mode, timer resets prior to match condition. Remains low.
Note 1: In all the cases considered herein, the TMRy register is assumed to be initialized to 0000h.
2: OCxR = Output Compare x register, OCxRS = Output Compare x Secondary register,
TMRy = Timery Count register, PRy = Timery Period register.
Timer
OCxR = 0000h
(9000h)
OCxRS
OCxR
0
Time
OCx Pin
OCxIF
Note 1: An x represents the Output Compare channel number. A y represents the time base number.
2: OCxR = Output Compare x register, OCxRS = Output Compare x Secondary register.
Period
Duty Cycle
As a result of the Fault condition, the respective interrupt flag, OCxIF, is asserted and an interrupt
will be generated if enabled. Upon detection of the Fault condition, the OCFLT bit (OCxCON<4>)
is asserted high (logic 1). This bit is a read-only bit and will only be cleared once the external
Fault condition has been removed, and the PWM mode is re-enabled by writing to the
appropriate mode bits, OCM<2:0> (OCxCON<2:0>).
Note: The external Fault pins, if enabled for use, will continue to control the OCx output
pins while the device is in Sleep or Idle mode.
Note 1: Based on TCY = 2/FOSC; Doze mode and PLL are disabled.
Note: A PRy value of N will produce a PWM period of N + 1 time base count cycles. For
example, a value of 7 written into the PRy register will yield a period consisting of
8 time base cycles.
FCY
log10 (F
PWM ) bits
(Timer Prescale Value)
Maximum PWM Resolution (bits) =
log10(2)
Note 1: Based on TCY = 2/FOSC; Doze mode and PLL are disabled.
TMRy 0005 0000 0001 0002 0003 0004 0005 0000 0001 0002 0003 0004 0005
PRy 0005
Note 1: An x represents the Output Compare channel number. A y represents the time base number.
2: OCxR = Output Compare x register, OCxRS = Output Compare x Secondary register.
Table 4-3: Example PWM Frequencies and Resolutions at 4 MIPS (FCY = 4 MHz)(1)
PWM Frequency 7.6 Hz 61 Hz 122 Hz 977 Hz 3.9 kHz 31.3 kHz 125 kHz
Timer Prescaler Ratio 8 1 1 1 1 1 1
Period Register Value FFFFh FFFFh 7FFFh 0FFFh 03FFh 007Fh 001Fh
Resolution (bits) 16 16 15 12 10 7 5
Note 1: Based on TCY = 2/FOSC; Doze mode and PLL are disabled.
Table 4-4: Example PWM Frequencies and Resolutions at 16 MIPS (FCY = 16 MHz)(1)
PWM Frequency 30.5 Hz 244 Hz 488 Hz 3.9 kHz 15.6 kHz 125 kHz 500 kHz
Timer Prescaler Ratio 8 1 1 1 1 1 1
Period Register Value FFFFh FFFFh 7FFFh 0FFFh 03FFh 007Fh 001Fh
Resolution (bits) 16 16 15 12 10 7 5
Note 1: Based on TCY = 2/FOSC; Doze mode and PLL are disabled.
Table 4-5: Example PWM Frequencies and Resolutions at 10 MIPs (FOSC = 40 MHz)
PWM Frequency 19 Hz 153 Hz 305 Hz 2.44 kHz 9.77 kHz 78.1 kHz 313 kHz
Timer Prescaler Ratio 8 1 1 1 1 1 1
Period Register Value 0xFFFF 0xFFFF 0x7FFF 0x0FFF 0x03FF 0x007F 0x001F
Resolution (bits) 16 16 15 12 10 7 5
Table 4-6: Example PWM Frequencies and Resolutions at 30 MIPs (FOSC = 120 MHz)
PWM Frequency 57 Hz 458 Hz 916 Hz 7.32 kHz 29.3 kHz 234 kHz 938 kHz
Timer Prescaler Ratio 8 1 1 1 1 1 1
Period Register Value 0xFFFF 0xFFFF 0x7FFF 0x0FFF 0x03FF 0x007F 0x001F
Resolution (bits) 16 16 15 12 10 7 5
OCxR = 5000h
OCxR = 0000h
0
Time
OCx Pin
Note 1: An x represents the Output Compare channel number. A y represents the time base number.
2: OCxR = Output Compare x register, OCxRS = Output Compare x Secondary register.
OCxR = 5000h
OCxR = 1000h
0
Time
OCx Pin
Note 1: An x represents the Output Compare channel number. A y represents the time base number.
2: OCxR = Output Compare x register, OCxRS = Output Compare x Secondary register.
Example 4-8: Simple PWM Mode: Pulse Setup and Interrupt Servicing
// The following code example will set the Output Compare 1 module
// for PWM mode w/o FAULT pin enabled, a 50% duty cycle and a
// PWM frequency of 52.08 kHz at Fosc = 8 MHz. Timer 2 is selected as
// the clock for the PWM time base and Timer2 interrupts
// are enabled.
Figure 4-21: PWM Output Timing (0% Duty Cycle, OCxR = 0000h)(1,2)
TMRy 0003 0000 0001 0002 0003 0000 0001 0002 0003 0000 0001 0002 0003
PRy 0003
TyIF
Note 1: An x represents the Output Compare channel number. A y represents the time base number.
2: OCxR = Output Compare x register, OCxRS = Output Compare x Secondary register.
TMRy 0003 0000 0001 0002 0003 0000 0001 0002 0003 0000 0001 0002 0003
PRy 0003
TyIF
Cleared by User Cleared by User
Cleared by User
OCxR = [OCxRS] OCxR = [OCxRS] OCxR = [OCxRS]
Duty Cycle goes to 100%
Note 1: An x represents the Output Compare channel number. A y represents the time base number.
2: OCxR = Output Compare x register, OCxRS = Output Compare x Secondary register.aa
TMRy 0003 0000 0001 0002 0003 0000 0001 0002 0003 0000 0001 0002 0003
PRy 0003
TyIF
Note 1: An x represents the Output Compare channel number. A y represents the time base number.
2: OCxR = Output Compare x register, OCxRS = Output Compare x Secondary register.
Example 5-1: Code to Modulate the PWM Duty Cycle Without CPU Intervention
// Initialize Output Compare Module in PWM mode
OC1CONbits.OCM = 0b000; // Disable Output Compare Module
OC1R=100; // Write the duty cycle for the first PWM pulse
OC1RS=200; // Write the duty cycle for the second PWM pulse
OC1CONbits.OCTSEL = 0; // Select Timer 2 as output compare time base
OC1R= 100; // Load the Compare Register Value
OC1CONbits.OCM = 0b110; // Select the Output Compare mode
// Initialize Timer2
T2CONbits.TON = 0; // Disable Timer
T2CONbits.TCS = 0; // Select internal instruction cycle clock
T2CONbits.TGATE = 0; // Disable Gated Timer mode
T2CONbits.TCKPS = 0b00; // Select 1:1 Prescaler
TMR2 = 0x00; // Clear timer register
PR2 = 500; // Load the period value
// Enable Timer
T2CONbits.TON = 1; // Start Timer
Question 1: The Output Compare pin stops functioning even when the OCSIDL bit is not
set. Why?
Answer: This is most likely to occur when the TSIDL bit (TxCON<13>) of the associated timer
source is set. Therefore, it is the timer that actually goes into Idle mode when the PWRSAV
instruction is executed.
Question 2: Can I use the Output Compare modules with the selected time base
configured for 32-bit mode?
Answer: No. The T32 bit (TxCON<3>) should be cleared when the timer is used with an Output
Compare module.
Note: Please visit the Microchip web site (www.microchip.com) for additional application
notes and code examples for the dsPIC33/PIC24 family devices.
Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the
intended manner and under normal conditions.
There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our
knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchips Data
Sheets. Most likely, the person doing so is engaged in theft of intellectual property.
Microchip is willing to work with the customer who is concerned about the integrity of their code.
Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not
mean that we are guaranteeing the product as unbreakable.
Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our
products. Attempts to break Microchips code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts
allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.
QUALITY MANAGEMENT SYSTEM Microchip received ISO/TS-16949:2009 certification for its worldwide
headquarters, design and wafer fabrication facilities in Chandler and
CERTIFIED BY DNV Tempe, Arizona; Gresham, Oregon and design centers in California
and India. The Companys quality system processes and procedures
== ISO/TS 16949 ==
are for its PIC MCUs and dsPIC DSCs, KEELOQ code hopping
devices, Serial EEPROMs, microperipherals, nonvolatile memory and
analog products. In addition, Microchips quality system for the design
and manufacture of development systems is ISO 9001:2000 certified.