Capture, Compare, PWM: Spring 2019 EE3954: Microprocessors and Microcontrollers

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

4/25/2019

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

TRIVIA: How many


lines of code in a
modern top-end car?
a) 100K
b) 1 Million
c) 10 Million
d) 100 Million

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.

• The PIC16F18875 has 5 CCP modules, which makes


register names big fun.

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.

CCP – Capture Module


Function:
Capture the time from TIMER 1 (so-called ”time-tag”) when
an external event happens

Interrupt
flag
Event
sources
“Timetag”

4
4/25/2019

Timer1 Mode Resource


• Timer1 must be running in Timer mode or Synchronized
Counter mode for the CCP module to use the capture
feature.

• In Asynchronous Counter mode, the capture operation may


not work.

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

GPS receiver typically outputs a


so-called Pulse-Per-Second, PIC16F18875
synchronized to GPS time
(related to UTC); do not confuse
this PPS with the Peripheral Pin
Select

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:

// Global variable with GPS timetag


uint16_t gps_timetag;

void interrupt my_isr(void) {

if(PIR6bits.CCP1IF) {

// Get GPS timetag from CCP registers by concatenating both


gps_timetag = ((uint16_t )CCPR1H << 8) | (uint16_t)CCPR1L;

// Clear the interrupt flag


PIR6bits.CCP1IF = 0; // clear flag
}
}

21

21

CCP – Compare Module


Function:
When there is a match between TMR1H:TMR1L and
CCPRXH:CCPRxL then an external event happens

Interrupt
flag

Output
Pin
TIMER1 must be
connected to
fosc/4

Triggers and Analog-to-Digital conversion


22

22

11
4/25/2019

CCP – Compare Module – Modes

23

23

ADC – Previously

Configuration and start


(GO) of conversion

Result

Set bit conversion time


and voltage references

Select channel

24

24

12
4/25/2019

CCP – Auto-conversion Trigger

Automatically start a ADC conversion, can


use ADC interrupt when complete
conversion

25

25

CCP – Compare - Example


1. Use the CCP1 module to make an analog-to-digital
conversion every 0.15 seconds using TMR1.
2. The analog device provides an analog voltage between 0
and 5V
3. Store the most significant 8 bits result in global variable
adc_data;
4. Toggle LED every 0.15 seconds as well.

Analog
RE2 RA1 Device #1

fosc = 4MHz
PIC16F18875

26

26

13
4/25/2019

CCP – Compare - Example

0 0 0 0 0 0 0 1

27

27

CCP – Compare - Example

1 0 0 0 0 0 0 1

28

28

14
4/25/2019

CCP – Compare - Example

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

ADC – Input Selection

0 0 0 0 0 0 0 1

31

31

ADC – Reference Selection

0 0 0 0 0 0 0 0

32

32

16
4/25/2019

ADC – Configuration Register

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

Will take 1 microsecond to convert 1 bit

34

34

17
4/25/2019

CCP – Compare - Example


Initialization subroutine
to be called in ‘main’:
void CCP1_initialize() {

// Set up the Capture part of CCP


CCP1CON = 0x81;
// ADC setup
CCPTMRS0 = 0x01;
ADPCH = 0x01;
ADREF = 0x00;
// Set up the register that TMR1 is compared to
ADCLK = 0x02;
CCPR1H = 0x49;
ADCON0 = 0x80;
CCPR1L = 0x3E;
ADACT = 0x0B
// Set up TIMER 1
T1CON = 0x37; // with prescalar of 1:8 // Interrupts
T1GCON = 0x00; PIE6bits.CCP1IE = 1;
T1CLK = 0x01; PIR6bits.CCP1IF = 0;
TMR1H = 0x00;
TMR1L = 0x00;
PEIE = 1;
GIE = 1;
Set relevant TRISx and PPS
}
configuration in config.c

35

35

CCP – Compare - Example


Interrupt service routine:

void interrupt my_isr(void) {

if(PIR6bits.CCP1IF) {

if(RE2 == 1) RE2 = 0;
else RE2 = 1;

// Clear IF
PIR6bits.CCP1IF = 0;

// Wait for ADC to complete conversion


while(ADGO) continue;
adc_result = ADRESH;

// TIMER1 is reset already


}
}

36

36

18
4/25/2019

CCP – PWM Module


Function:
Generate a Pulse-Width Modulated (PWM) signal

Note: duty cycle is (pulse width/pulse period)*100%

37

37

CCP – PWM Module

Duty cycle value

SR flip flop

Period value S R Qnext Action


0 0 Q hold
0 1 0 reset
1 0 1 set
38 1 1 X not allowed

38

19
4/25/2019

CCP – PWM Module

39

39

CCP – PWM Module

40

40

20
4/25/2019

CCP – PWM – Module


1. For example:
1. fosc = 4MHz => 4Tosc = 1 microsecond
2. required PWM frequency: 20kHz => T = 50 microseconds
3. duty cycle: 20%

[(PR2) + 1]*prescalar = 50e-6/1.0e-6 =>


(PR2) + 1 = 50 => PR2 = 49

log(200)/log(2) = 7.6 bits

20% = 0.2 = (CCPR)/200 =>


CCPRxH:CCPRxL = 40
100% = 1.0 = (CCPR)/200 =>
41
CCPRxH:CCPRxL = 200

41

CCP – PWM Module

42

42

21
4/25/2019

Capture – Configuration

43

43

CCP-related Registers

44

44

22

You might also like