Lecture 12 Timers and CCP
Lecture 12 Timers and CCP
Lecture 12
Timers and CCP
(Capture/Compare/PWM)
CSE 3442/5442
Embedded Systems 1
Based heavily on slides by Dr. Gergely Záruba and Dr. Roger Walker
PIC18 Timer Peripherals
2
PIC Timers
• PIC18 family microcontrollers have 2 to 5 timers on-board
• Timers can be used to generate time delays or to count
(outside) events happening “in the background”
• Some timers can also be used to control the timing of other
peripherals (some automatically like ADC)
• Every timer needs a clock that will make it to count
• Timers have the option to use at most ¼ of the main clock’s
frequency Fosc or use a separate external signal for clocking
– Timer: uses internal clock source (Fosc /4)
• “Wait this amount of fixed time” or
• “Let me know when X sec/ms/us/etc. have elapsed”
– Counter: fed pulses through one of the PIC’s pins
• “Count how many events/pulses occur on a pin” 3
Timer
main()
{
Setup Timer
Start Timer
//delay X ms/sec/etc.
while(timerNotDone);
continue…
}
4
Timer
main()
{
Setup Timer/Ints
Start Timer
continue…
}
interrupt timer()
{
//X time has elapsed
//perform ADC
//output
//etc.
} 5
Counter
1
2
3
• IR Sensor 4
…
• Encoder 150
• Button
Func()
• Water Flow Sensor {
…
} 6
Timer Length (Width or Mode)
and Preload
• 8-bit timers: Can count from 0 – 255
• 16-bit timers: Can count from 0 – 65,535
• 32-bit timers: Can count from 0 – 4,294,967,295
• Can start counting at 0 or any preload within range
– 0 1 2 … 254 255 0 1 2…
OV Reload Preload
Source: http://roberthall.net/PIC18F4550_Timers
8
Prescaler
Fin Divide by
1:2, 1:4, … , 1:256
9
Ex: F/4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
1 2 3 4
10
How to Calculate Example
(Want 1.2 sec Delay)
• d = 1.2sec (time period)
• Fosc = 10MHz Fin = Fosc / 4 = 2.5MHz
• 16-bit Timer: 0 – 65,535
14
Four PIC18F452 Timers
RAM
FileReg
SFRs
16
17
Timer0
• Timer0 can be used as an 8-bit or as a 16-bit timer
• Thus, two SFRs are used to contain the count:
or
18
Timer0 Control Register
T0CON
• Note that timer
interrupt enable/flag
bits are in registers
related to interrupts
(e.g., INTCON)
• When the timer
overflows, TMR0IF
is set.
• 16- vs. 8-bit timer
• Prescalers are
useful for large time
delays
19
Timer0 8-bit Programming
1. Select 8-bit mode and prescaler
2. Load TMR0L with preload value (ignore TMR0H)
3. Start timer (TMR0ON = 1)
4. Monitor TMR0IF (or set interrupt on it)
5. When TMR0IF is set, stop the timer, reset the flag (and if needed go to step 2)
0 – 255
Bypass
Prescaler Prescaler?
Flag Bit 20
Timer0 16-bit Programming
1. Select 16-bit mode and prescaler
2. Load TMR0H and then TMR0L with preload values (load HIGH first!!)
– Ex: 18,661 dec = 0x48E5 TMR0H = 0x48 and TMR0L = 0xE5
3. Start timer (TMR0ON = 1)
4. Monitor TMR0IF (or set interrupt on it)
5. When TMR0IF is set, stop the timer, reset the flag (and if needed go to step 2)
Bypass
Prescaler
Prescaler?
21
Timer1
TMR1H TMR1L
• Timer1 is 16-bit only
• T1CON is the control register
• TMR1IF is the interrupt flag in the PIR1 register
• Prescaler does not support divisions above 1:8
• Timer1 has 2 external clock sources and 1 regular internal
– Clock fed into T1CK1 pin (RC0)
– Crystal (typically 32-kHz) connected between the T1CKI and T1OSI
PINS (RC0&RC1) – for saving power during sleep mode. Timer1 is
not shut down allowing use a clock that can be used for waking up
22
Timer1 Control Register
T1CON
• 16-bit mode only
• Smaller prescaler range
• Timer1 can be used as
1. timer
2. synchronous counter
(T1SYNC)
3. asynchronous counter
23
Timer1 Block Diagram
Used with
CCP option
External
Clock Timer 1 crystal, e.g.,
Signal In 32KHz for sleep mode
24
Timer2
TMR2
25
Timer2 Control Register
T2CON
26
Timer2 Block Diagram
PR2:
FLAG = 1
TMR2: 27
Timer3
TMR3H TMR3L
• Timer3 is 16-bit only
• T3CON is the control register
• TMR3IF is the interrupt flag in the PIR2 register
• Can work with CCP peripheral (later)
• Timer3 has 2 external clock sources and 1 regular internal
– Same external source(s) as timer1
• Can be used as timer, ascynchronous, or synchronous counter
28
Timer3 Block Diagram
29
Timer3 Control Register
T3CON
30
Timer0 Interrupt Example
1. In T0CON…
1. Select 16-bit mode
2. Select internal or external clock source
3. Allow prescaler option if desired
4. Select desired prescaler
2. Load TMR0H and TMR0L with preloads (load HIGH first!!)
– Ex: 18,661 dec = 0x48E5 TMR0H = 0x48 and TMR0L = 0xE5
3. In INTCON…
1. Enable the TMR0IE interrupt bit
2. Enable the PEIE peripheral interrupt bit
3. Enable the GIE global interrupt bit
4. Start timer (T0CONbits.TMR0ON = 1)
5. Monitor TMR0IF (if only polling)
6. When overflow occurs (1.2s has passed) TMR0IF is set to 1
7. In the ISR…
1. Identify the interrupt source
2. Stop the timer (disable Timer0) 31
3. Reset the flag (if needed go to step 2 of writing preload values)
Using PIC18 Timers for CCP
(Capture, Compare, and PWM)
• Timer0 is usually just for generic timing
• Timers 1 and 3 can be used for capture and
compare features
– T3CON is used to chose the timer for CCP
• Timer2 is used for PWM
– Note: These rules do not always apply, have to
check the specific PIC18 datasheet
32
Using PIC18 Timers for CCP
(Capture, Compare, and PWM)
33
CCP
• Compare (input)
– Count outside events (incoming to the PIC’s pins)
– When X have occurred do something
• Capture (input)
– Measure an unknown signal’s frequency (period) or
PWM Duty Cycle
• PWM (output)
– Send a precise signal out of the PIC
34
PWM Basics
(Pulse Width Modulation)
• Digital signals have two distinct levels: high and low
• These levels are usually represented by a voltage
– e.g., in PIC low is 0V and high is VCC (5V)
• A temporal digital signal changes with time from low
to high and back
• Thus we can describe temporal digital signals with a
series of values representing the time for which
they stay in one state
• Periodic temporal digital signals have a distinct
frequency
– the inverse of the time between two consecutive rising
edges 35
PWM Basics (cont’d)
• Such periodic signals can still have varying times they spend
in high vs. low state
• PWM Duty Cycle is the portion of the pulse that stays HIGH
relative to the entire period
t1
DC[%] = 100 ∗
t 1 + t2
36
PWM Basics (cont’d)
Source: http://www.hho4free.com/pulse_width_modulator__pwm.html
37
PWM Basics (cont’d)
PWM Analog
C Source: http://www.electronics-tutorials.ws/
38
PIC18’s CCP Modules
• PIC18s have 0 – 5 CCP modules on-board (CCPx) with 3 modes
• Capture
– can use an external input to copy timer values into a 16-bit register
– provides the capability of measuring the period of a pulse
• Compare
– enables the counter value of timers to be compared to a 16-bit register
– if equal, then perform an action
• PWM
– can be used as a quasi-analog output (timed digital output with duty
cycle setting)
• These are great for driving motors, reading encoders, IR comm.
• For DC motor control some of the CCPs have been enhanced
and are called ECCP
• The PIC18F452 has 2 CCP Modules: CCP1 & CCP2
– can be used at the same time but only 1 mode per CCP at a time 39
Timers and CCP Association
These rules do not always apply – have to check the specific PIC18 datasheet 40
T3CON
41
CCPx Pins
42
CCP Module Basics
43
CCP 1 & 2 Module Control
CCPxCON
prescalers
44
Relevant Registers
45
Compare Mode
PIC18F452
Set HIGH or LOW
Toggle
ADC GO
Int Flag
TMR++ YES?
If == CCPH:L
47
Compare Mode Programming
50
Capture Mode Programming for
Frequency Measurement
PIC18F452
A B A CCPH:L CCPH:L
B
51
Capture Mode Programming for
Measuring PWM Duty Cycle
1. Initialize CCPxCON for capture
2. Make CCPx pin an input pin (TRISB/TRISC)
3. Pick timer source (T3CON)
4. On rising edge, Timer is started & mode set to falling edge
5. On falling edge the CCPRxH:CCPRxL should be saved, CCP
should be set to rising edge
6. On rising edge CCPRxH:CCPRxL is saved
– Now we have measurements for t1 and t2
7. DC can be calculated while new measurement is prepared
52
Capture Mode Programming for
Measuring PWM Duty Cycle
PIC18F452
1 full period
(time)
53
Capture Mode Programming for
Measuring PWM Duty Cycle
PIC18F452
𝐵𝐵 −𝐴𝐴
DC = 1 full period
𝐶𝐶 −𝐴𝐴 (time)
54
PWM Mode
(Generate Precise Output)
55
PWM Mode
PR2:
0-255 FLAG = 1
TMR2: 57
PWM
Specify Two Things
Unit of time
(ms, us, etc.)
Unit of time
(ms, us, etc.)
58
PWM Mode
Desired Period and Frequency
• Tpwm = desired PWM period (time, secs/cycle)
• Fpwm = desired PWM freq. (rate, cycles/sec)
• Tpwm = 1 / Fpwm
• PR2: 0 – 255 (from TMR2)
• Tosc = 1 / Fosc
• Slowest Rate
– Max Tpwm = 4*16*(255+1) * Tosc = 16,384 Tosc
– Min Fpwm = 1 / Tpwm = Fosc / 16,384
60
PWM Mode Ex.
Desired Period and Frequency
61
PWM Mode Ex.
Desired Period and Frequency
62
PWM Period and Duty Cycle
CCPRxL PWM
Fosc CCPxCON Duty Cycle
63
PWM Period and Duty Cycle
CCPRxL PWM
Fosc CCPxCON Duty Cycle
64
PWM Period and Duty Cycle
CCPRxL PWM
Fosc CCPxCON Duty Cycle
65
PWM Period and Duty Cycle
CCPRxL PWM
Fosc CCPxCON Duty Cycle
66
PWM Period and Duty Cycle
CCPRxL PWM
Fosc CCPxCON Duty Cycle
67
PWM Period and Duty Cycle
CCPRxL PWM
Fosc CCPxCON Duty Cycle
68
PWM Period and Duty Cycle
CCPRxL PWM
Fosc CCPxCON Duty Cycle
69
PWM Period and Duty Cycle
CCPRxL PWM
Fosc CCPxCON Duty Cycle
70
PWM Mode
Desired Duty Cycle
• PIC18F452 has “10-bit” duty cycle resolution
– Remember, DC is just a percentage of the period
Ex:
– DC[%] = 75% = .75
– TPWM = .4ms
– TDC = (.75)(.4ms) = .3ms
Bit 9 Bit 8 Bit 7 Bit 6 Bit 5 Bit 4 Bit 3 Bit 2 Bit 1 Bit 0
CCPxCON CCPxCON
----------------------------------CCPRxL-------------------------------
DCxB1 DCxB0
71
PWM Example
Calculate
these values
73
PWM Mode - Programming
1. Set PWM period by setting PR2 and T2CON (prescaler)
2. Set PWM duty cycle by calculating and writing top 8 bits
to CCPRxL and the remainder 2 to CCPxCON<5:4> bits
3. Set the CCPx as output (TRIS)
4. Clear TMR2
5. Set CCPx to PWM mode
6. Start Timer 2
7. CCPx output pin will constantly keep outputting your signal
at the set period and DC until you turn it off/disable it
74
DC Motor Drive Half bridge
75
DC Motor Drive Full H Bridge
76
Rotary Encoders
• Rotary encoders are rotational sensors (one component of servos), they
can provide precise readings (PWM) of shafts turning (flow valves, etc.)
• Internally they can be mechanical, magnetic (induction) based or optical
• Optical encoders are usually of high precision, contain encoder wheels
• Encoders can be absolute or incremental
• They can be read using timers but will tie up microcontroller; there are
special purpose circuitry to read them, which have parallel or serial
interfaces to microcontrollers
77
CCP Questions?
78