0% found this document useful (0 votes)
29 views78 pages

Lecture 12 Timers and CCP

Intro to micro electronics lecture slides

Uploaded by

serget1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views78 pages

Lecture 12 Timers and CCP

Intro to micro electronics lecture slides

Uploaded by

serget1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 78

The University of Texas at Arlington

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

• Software specified time delay or “background” time elapsed

main()
{
Setup Timer
Start Timer

//delay X ms/sec/etc.
while(timerNotDone);

continue…
}

4
Timer

• Software specified time delay or “background” time elapsed

main()
{
Setup Timer/Ints
Start Timer

continue…
}

interrupt timer()
{
//X time has elapsed
//perform ADC
//output
//etc.
} 5
Counter

• Count external/outside events and pulses

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

• Ex. 8-bit Overflow:


OV

– 0  1  2  …  254  255  0  1  2…
OV  Reload Preload

– 200  201  202  …  254  255  0  200  201… 7


Timer Overflow

Source: http://roberthall.net/PIC18F4550_Timers

8
Prescaler

• Sometimes the frequency is too fast


• A prescaler divides the clock source to
obtain a smaller frequency (less frequent)
– 1, 2, 4, 8, 16, 32, 64, 128, 256…
Fosc/4 Ftimer

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

• X = d * Fin = 1.2s * 2.5Mhz


𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐
• X = d * Fin = 1.2sec * 2,500,000
𝑠𝑠𝑠𝑠𝑠𝑠
• X = d * Fin = 1.2s * 2.5Mhz = 3,000,000 cycles
– 3,000,000 cycles (ticks) occur in 1.2s time span
11
How to Calculate Example
(Want 1.2 sec Delay)
• X = d * Fin = 1.2s * 2.5Mhz = 3,000,000
– 3,000,000 cycles (pulses) occur in a 1.2s time span
• Use prescaler to bring down X to fit into the
16-bit Timer register (0 – 65,535)
– 3,000,000 / 4 = 750,000 (> 65,535)
– 3,000,000 / 16 = 187,500 (> 65,535)
– 3,000,000 / 32 = 93,750 (> 65,535)
– 3,000,000 / 64 = 46,875 (< 65,535)
Use Prescaler 1:64
12
How to Calculate Example
(Want 1.2 sec Delay)
• X = d * Fin = 1.2s * 2.5Mhz = 3,000,000
– 3,000,000 cycles/ticks occur in a 1.2s time span
• Using Prescaler 1:64 to find Preload value
– Now 46,875 ticks/cycles will occur in 1.2s span
• Preload = 65,535 – 3,000,000/64
= 65,535 – 46,875
= 18,661
Instead of counting 0  65,535
Now count from 18,661  65,535 13
How to Calculate Example
(Want 1.2 sec Delay)
• So if we want a 1.2 second delay when
using a 10MHz oscillator…
1. We select a 16-bit Timer
2. Select the prescaler 1:64
3. Load the timer register with 18,661 (dec)
4. Turn on the Timer
5. When the Timer overflows, we know that
exactly 1.2s has passed

14
Four PIC18F452 Timers

• Timer0: 8 or 16-bit timer/counter


– T0CON, TMR0H:TMR0L
– Prescalers: 1:2, 1:4, … ,1:128, 1:256
• Timer1: 16-bit timer/counter
– T1CON, TMR1H:TMR1L
– Prescalers: 1:1, 1:2, 1:4, 1:8
• Timer2: 8-bit timer
– T2CON, TMR2L
– Prescalers: 1:1, 1:4, 1:16 and Postscalers: 1:1 … 1:16
• Timer3: 16-bit timer/counter
– T3CON, TMR3H:TMR3L
– Prescalers: 1:1, 1:2, 1:4, 1:8
15
SFRs are Used to “Control”
the Timer Peripherals

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

• T0CON is the control register


• TMR0IF is the interrupt flag in the INTCON register
• The clock source for Timer0 may be internal or external

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)

Port RA4 or Int. Clock Source

0 – 255

Timer vs. Counter

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)

Port RA4 or Int. Clock Source Flag Bit


0 – 65,536

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

• Timer2 is an 8-bit only


• T2CON is the control register
• TMR2IF is the interrupt flag in the PIR1 register
• Timer2 has a period register PR2; Timer2 can be set to
count only to PR2 and set TMR2IF then
• Clock source is only Fosc/4 (Timer2 cannot be a counter)
• Has both a prescaler and a postscaler

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)

• If t1 + t2 remain constant  frequency remains constant

• 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)

• There are various sensors that provide their output as PWM


signals, where the DC corresponds to the reading
• There are various actuators that work well with a PWM input

• An appropriate RC filter (Integrator)


can make an analog signal out
of a PWM digital signal
R

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

• Each CCP module has 3 associated registers


– CCPxCON controlling the modes
– CCPxL and CCPxH as a 16-bit
compare/capture/PWM duty cycle register

• Each CCP module has a pin associated with


it (input or output)

43
CCP 1 & 2 Module Control
CCPxCON

prescalers

44
Relevant Registers

45
Compare Mode

~ IF(CCP == TMR1/3) THEN…


• The CCPRxH:CCPRxL is loaded by the user
• If Timer1 TMR1H:TMR1L (or Timer3 – T3CON)
count becomes equal to the above set value then
the Compare module can:
1. Drive the CCPx pin high (CCPx config’d as out)
2. Drive the CCPx pin low (CCPx config’d as out)
3. Toggle the CCPx pin (CCPx config’d as out)
4. Trigger a CCPxIF interrupt and clear the timer
5. CCP2 can be used to kick off the A/D converter
46
Compare Mode

PIC18F452
Set HIGH or LOW
Toggle
ADC GO

Int Flag

TMR++ YES?

If == CCPH:L

47
Compare Mode Programming

0. Set up CCP interrupt if needed


1. Initialize CCPxCON for compare
2. Pick timer source (T3CON)
3. Initialize the CCPRxH:CCPRxL 16-bit value
4. Make sure CCPx pin is output if used
– setting appropriate TRISbits
5. Initialize Timer1 (or Timer3)
6. Start Timer1 (or Timer3)
7. Poll CCPxIF flag or make sure interrupt is handled
48
Capture Mode

• The CCPx pin is set as input (set TRISbits)


• When an external event triggers the CCPx pin, then
the TMR1H:TMR1L (or Timer3) values will be loaded
into CCPRxH:CCPRxL
• Four options for CCPx pin triggering:
– Every falling edge
– Every rising edge
– Every 4th rising edge
– Every 16th rising edge
• Typical applications are measuring frequency or
pulsewidth 49
Capture Mode Programming for
Frequency Measurement
1. Initialize CCPxCON for capture
2. Make CCPx pin an input pin (TRISB/TRISC)
3. Pick timer source (T3CON)
4. On first rising edge, Timer1/3 is loaded into CCPRxH:CCPRxL
– remember values
5. On next rising edge, Timer1/3 is loaded into CCPRxH:CCPRxL
– subtract previous values from current values
6. You have now the period of the signal captured by timer ticks.
Some basic math will give you frequency

50
Capture Mode Programming for
Frequency Measurement
PIC18F452

TMR++ A: TMR B: TMR

A B A CCPH:L CCPH:L
B

CCPx 1 full period


(time)

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

TMR++ A: TMR B: TMR C:TMR

A B C A CCPH:L CCPH:L CCPH:L


B
C
HIGH time
CCPx

1 full period
(time)
53
Capture Mode Programming for
Measuring PWM Duty Cycle
PIC18F452

TMR++ A: TMR B: TMR C:TMR

A B C A CCPH:L CCPH:L CCPH:L


B
C
HIGH time
CCPx

𝐵𝐵 −𝐴𝐴
DC = 1 full period
𝐶𝐶 −𝐴𝐴 (time)
54
PWM Mode
(Generate Precise Output)

55
PWM Mode

• PWM output can be created without tedious


programming of the compare mode or timers
• ECCP’s PWM mode enables generating temporal
digital signals of varying frequencies and varying DC
– recall: width of the pulse indicates some measured quantity
• Recall, that the PWM Duty Cycle is the portion of the
pulse at HIGH relative to the entire period
– DC[%] = 100*t1/(t1+t2)

• For PWM, Timer2 is used


• Recall, that Timer 2 has a period register PR2
56
Timer2 Block Diagram

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

• Tpwm = 4*N*(PR2+1) / Fosc or….


• Tpwm = 4*N*(PR2+1) * Tosc
– where N is the prescaler of TMR2 (1, 4, 16) 59
PWM Mode
Desired Period and Frequency
• Fastest Rate
– Min Tpwm = 4*1*(0+1) * Tosc = 4 Tosc
– Max Fpwm = 1 / Tpwm = Fosc / 4

• 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

Fosc Timer2 PWM


4 PR2 Period

CCPRxL PWM
Fosc CCPxCON Duty Cycle

63
PWM Period and Duty Cycle

Fosc Timer2 PWM


4 PR2 Period

CCPRxL PWM
Fosc CCPxCON Duty Cycle

64
PWM Period and Duty Cycle

Fosc Timer2 PWM


4 PR2 Period

CCPRxL PWM
Fosc CCPxCON Duty Cycle

65
PWM Period and Duty Cycle

Fosc Timer2 PWM


4 PR2 Period

CCPRxL PWM
Fosc CCPxCON Duty Cycle

66
PWM Period and Duty Cycle

Fosc Timer2 PWM


4 PR2 Period

CCPRxL PWM
Fosc CCPxCON Duty Cycle

67
PWM Period and Duty Cycle

Fosc Timer2 PWM


4 PR2 Period

CCPRxL PWM
Fosc CCPxCON Duty Cycle

68
PWM Period and Duty Cycle

Fosc Timer2 PWM


4 PR2 Period

CCPRxL PWM
Fosc CCPxCON Duty Cycle

69
PWM Period and Duty Cycle

Fosc Timer2 PWM


4 PR2 Period

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

• Knowns • Unknowns to Calculate


– FOSC – PR2 register value
– FPWM • To set TPWM (PWM period)
– DC(%) – CCPR1L:CCP1CON<5:4>
register value
• To set TDC (Duty Cycle period)
• So we also know
– TOSC
– TPWM
– TDC
72
PWM Example

• FOSC = 10 MHz  Note


PR2 (8-bit): 0-255
• FPWM = 2.5 kHz CCPR… (10-bit): 0-1023
• DC(%) = 75%

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?

• Lab 7 will be detailed and given out soon


– Take-home lab/project
– Explained in lab and class
• Remaining Lectures…
– Hardware Connections (ICSP, .hex details, etc.)
– Communication (MSSP, SPI, USART, I2C, etc.)

78

You might also like