Professional Documents
Culture Documents
Capture, Compare, PWM: Spring 2019 EE3954: Microprocessors and Microcontrollers
Capture, Compare, PWM: Spring 2019 EE3954: Microprocessors and Microcontrollers
Capture, Compare, PWM: Spring 2019 EE3954: Microprocessors and Microcontrollers
Capture, Compare,
PWM
Spring 2019
EE3954: Microprocessors and Microcontrollers
Avinash Karanth
Professor, Electrical Engineering and Computer Science
Ohio University
E-mail: karanth@ohio.edu
Acknowledgment: Harsha Chenji, Jim Goble, Maarten Uijt de Haag
1
4/25/2019
Course Administration
• Homework
• Hw 5 posted - due Wed April 24
• FINAL EXAM
• Review 4/24 or 4/26, exam 5/1 Wednesday
Reference Sections
1. Microcontroller 16F18875 Datasheet (DS)
1. Chapter 30 – Capture/Compare/PWM Module
2
4/25/2019
CCP
1. Capture Mode, allows timing for the duration of an event.
This circuit gives insight into the current state of a register
which constantly changes its value. In this case, it is the
timer TMR1 register.
2. Compare Mode compares values contained in two
registers at some point. One of them is the timer TMR1
register. This circuit also allows the user to trigger an
external event when a predetermined amount of time has
expired.
3. PWM – Pulse Width Modulation can generate signals of
varying frequency and duty cycle.
CCP Modules
• The Capture/Compare/PWM module is a peripheral that
allows the user to time and control different events, and
to generate Pulse-Width Modulation (PWM) signals. In
Capture mode, the peripheral allows the timing of the
duration of an event. The Compare mode allows the user
to trigger an external event when a predetermined
amount of time has expired.
3
4/25/2019
Capture Mode
• Capture mode makes use of the 16-bit Timer1 resource. When an
event occurs on the capture source, the 16-bit CCPRxH:CCPRxL
register pair captures and stores the 16-bit value of the
TMR1H:TMR1L register pair, respectively. An event is defined as one
of the following and is configured by the CCPxMODE<3:0> bits of the
CCPxCON register:
1. Every falling edge
2. Every rising edge
3. Every 4th rising edge
4. Every 16th rising edge
• When a capture is made, the Interrupt Request Flag bit CCPxIF of the
PIR6 register is set. The interrupt flag must be cleared in software. If
another capture occurs before the value in the CCPRxH, CCPRxL
register pair is read, the old captured value is overwritten by the new
captured value.
Interrupt
flag
Event
sources
“Timetag”
4
4/25/2019
Capture – Configuration
10
10
5
4/25/2019
Capture – Sources
11
11
Capture
There are 3 timers (1/3/5) and 5 Capture modules
(1/2/3/4/5) – Need to decide which timer to connect which
CCP module
12
12
6
4/25/2019
Capture
13
13
Capture – Interrupts
Interrupt
flag
14
14
7
4/25/2019
Capture – Interrupts
15
15
Capture – Example 1
• Time tag the GPS PPS with the local clock as
generated by TIMER 1 at the rising edge using
CCP1 and TIMER1.
GPS
Receiver RB4
16
16
8
4/25/2019
Capture – Example 1
1 0 0 0 0 1 0 1
17
17
Capture – Example 1
0 0 0 0 0 0 0 1
18
18
9
4/25/2019
Capture – Example 1
0 0 0 0 0 0 0 0
PPS selection:
CCP1PPS = 0x0C;
19
19
Capture – Example 1
Initialization subroutine void CCP1_initialize() {
to be called in ‘main’:
// Set up the Capture part of CCP
CCP1CON = 0x85;
CCPTMRS0 = 0x01;
CCP1CAP = 0x00;
// Set up TIMER 1
T1CON = 0x07;
T1GCON = 0x00;
T1CLK = 0x01;
TMR1H = 0x00;
TMR1L = 0x00;
// Interrupts
PIE6bits.CCP1IE = 1;
Set relevant TRISx and
PIR6bits.CCP1IF = 0;
PPS configuration in
config.c PEIE = 1;
GIE = 1;
}
20
20
10
4/25/2019
Capture – Example 1
Interrupt service routine:
if(PIR6bits.CCP1IF) {
21
21
Interrupt
flag
Output
Pin
TIMER1 must be
connected to
fosc/4
22
11
4/25/2019
23
23
ADC – Previously
Result
Select channel
24
24
12
4/25/2019
25
25
Analog
RE2 RA1 Device #1
fosc = 4MHz
PIC16F18875
26
26
13
4/25/2019
0 0 0 0 0 0 0 1
27
27
1 0 0 0 0 0 0 1
28
28
14
4/25/2019
PPS selection:
RE2PPS= 0x09;
29
29
Timer 1– Example 1
1. TIMER 1:
1. 0.15 seconds = 150,000 microseconds
2. Prescalar of 8: 150,000 /8 = 18,750 = 0x493E
3. So, if you start TMR1 at 0x0000, then an interrupt and conversion should
be generated when it is equal to 0x493E (this value should be written to
CCPR1)
0 0 1 1 0 1 1 1
0 0 0 0 0 0 0 0
30 0 0 0 0 0 0 0 1
30
15
4/25/2019
0 0 0 0 0 0 0 1
31
31
0 0 0 0 0 0 0 0
32
32
16
4/25/2019
1 0 0 0 0 0 0 0
Is now
automatically
triggered by
the compare
unit of (CCP)
33
33
ADC – ADCLK
0 0 0 0 0 0 1 0
34
34
17
4/25/2019
35
35
if(PIR6bits.CCP1IF) {
if(RE2 == 1) RE2 = 0;
else RE2 = 1;
// Clear IF
PIR6bits.CCP1IF = 0;
36
36
18
4/25/2019
37
37
SR flip flop
38
19
4/25/2019
39
39
40
40
20
4/25/2019
41
42
42
21
4/25/2019
Capture – Configuration
43
43
CCP-related Registers
44
44
22