Chapter 9: Taking timing further

advertisement
Taking timing further
Chapter Nine
9.1 – 9.8
Dr. Gheith Abandah
1
Outline
•
•
•
•
•
•
Timer 1
Timer 2
Capture/Compare/PWM
Pulse Width Modulation (PWM)
Digital to Analog Conversion (DAC)
Summary
Dr. Gheith Abandah
2
Timing Issues
• Maintaining continuous counting functions
• Recording (‘capturing’) in timer hardware the
time an event occurs
• Triggering events at particular times
• Generating repetitive time-based events
• Measuring frequency, e.g., motor speed
Dr. Gheith Abandah
3
The PIC 16 Series
Device
Pins
Features
16F84A
18
1 8-bit timer
1 5-bit port
1 8-bit port
16F873A
16F876A
28
3 parallel ports,
3 counter/timers,
2 capture/compare/PWM,
2 serial, 5 10-bit ADC, 2 comparators
16F874A
16F877A
40
5 parallel ports,
3 counter/timers,
2 capture/compare/PWM,
2 serial, 8 10-bit ADC, 2 comparators
Dr. Gheith Abandah
4
PIC 16F84A Timer 0 Module
Dr. Gheith Abandah
5
PIC 16F84A Timer 0 Module
Dr. Gheith Abandah
6
Option Register
•
•
•
•
T0CS: Clock source select
T0SE: Source edge select
PSA: Prescaler assignment bit
PS2:PS0: Prescaler rate select
Dr. Gheith Abandah
7
PIC 16F87XA Timer 1 Module
Dr. Gheith Abandah
8
Timer 1
Registers
16-bit register:
•TMR1L (0Eh)
•TMR1H (0Fh)
Control Register:
•T1CON (10h)
Dr. Gheith Abandah
9
Timer 1 control register
• T1CKPS1:T1CKPS0: Input Clock Prescale Select,
1:1-1:8
• T1OSCEN: Oscillator Enable Control
• T1SYNC’: External Clock Input Synchronization
Control
• TMR1CS: Clock Source Select
• TMR1ON: Timer1 On
Dr. Gheith Abandah
10
Derbot odometer
• Timers 0 and 1 are used to count pulses
generated by the optical sensors mounted on
the shaft encoders.
• The program drives the Derbot forward for
1m. It then completes a 180◦ turn on the spot
and runs forward for 1m again. The program
loops continuously in this manner.
Dr. Gheith Abandah
11
Derbot odometer circuit
Dr. Gheith Abandah
12
Odometer Example – Page 1
;Initialization
movlw B'01000100' ;set port A for right
movwf adcon1
;analog/digital mix
movlw B'11101000' ;T0: external input,
movwf option_reg ;low to high transition,
;no prescale
movlw B'00000011' ;T1: no prescale,
movwf t1con
;oscillator disabled,
;external sync input
Dr. Gheith Abandah
13
Odometer Example – Page 2
opto_move
clrf tmr0
;clear timers
clrf tmr1l
Because the counter must
first have a falling edge
clrf tmr1h
before it starts to count.
clrf flags
btfss portc,0 ;increment T1 if ip is zero,
incf tmr1l
;as 1st rising edge isn’t
;detected
call leftmot_fwd ;start motors running
call rtmot_fwd
Dr. Gheith Abandah
14
Odometer Example – Page 3
opto_loop
movlw D'91'
subwf tmr0,0
btfsc status,z
bcf porta,mot_en_left
movlw D'91'
subwf tmr1l,0
btfsc status,z
bcf porta,mot_en_rt
…
goto opto_loop
;test for 1m
;disable motor if =
;disable motor if =
Dr. Gheith Abandah
15
Generating a ‘clock tick’ – a
repetitive interrupt stream
Dr. Gheith Abandah
16
Example: ‘clock tick’ generation
• Assuming an oscillator frequency of 4MHz,
what is the slowest ‘clock tick’ rate that can
be obtained from Timer 0 and Timer 1?
• T0: slowest interrupt rate with prescaler ÷256.
• The input frequency to T0 is 1 MHz/256, or
3.906 kHz.
• The 8-bit timer divides this frequency by 256
to produce the clock tick frequency, which will
be 3.906 kHz/256, or 15.26 Hz.
Dr. Gheith Abandah
17
Example: ‘clock tick’ generation
• T1: slowest interrupt rate with prescaler ÷8.
• The input frequency to T1 is 1 MHz/8, or 125
kHz.
• The 16-bit timer divides this frequency by 216
to produce the clock tick frequency, which will
be 125 kHz/ 216, or 1.91 Hz.
Dr. Gheith Abandah
18
PIC 16F87XA Timer 2 Module
Dr. Gheith Abandah
19
Timer 2
Registers
8-bit register:
•TMR2 (11h)
Control Register:
•T2CON (12h)
•PR2 (92h), period
register
Dr. Gheith Abandah
20
Timer 2 control register
• TOUTPS3:TOUTPS0: Output Postscale Select,
1:1-1:16
• TMR2ON: Timer 2 On
• T2CKPS1:T2CKPS0: Clock Prescale Select, 1:1,
1:4, 1:16
Dr. Gheith Abandah
21
The PR2 register, comparator and
postscaler
Dr. Gheith Abandah
22
Example: Timer 2 interrupt rate
• Assuming an oscillator frequency of 4MHz, what is
the slowest ‘clock tick’ rate that can be obtained
from Timer 2?
• Slowest interrupt rate with prescaler ÷16 and
postscaler ÷16 .
• The input frequency is 1 MHz/16, or 62.5 kHz.
• If PR2 is preset to 255, the frequency is divided by
256 to produce the reset frequency, which will be
62.5 kHz/256, or 244.14 Hz.
• With postscaler of 16, then the interrupt frequency
will be 15.26 Hz.
Dr. Gheith Abandah
23
The capture/compare/PWM (CCP)
modules
• 2 CCP modules
• Each CCP module contains a 16-bit register
which can operate as a:
1. 16-bit Capture register
2. 16-bit Compare register
3. Pulse width modulation Master/Slave Duty Cycle
register
Dr. Gheith Abandah
24
CCP Registers
16-bit register:
•CCPR1L (15h)
•CCPR1H (16h)
•CCPR2L (1bh)
•CCPR2H (1ch)
Control Register:
•CCP1CON (17h)
•CCP2CON (1dh)
Dr. Gheith Abandah
25
CCP x control register
• CCPxX:CCPxY: PWM Least Significant bits
• CCPxM3:CCPxM0: Mode Select bits
Dr. Gheith Abandah
26
CCPx Mode Select bits
Dr. Gheith Abandah
27
Capture mode
Dr. Gheith Abandah
28
Compare mode
Dr. Gheith Abandah
29
Pulse width modulation
The current rises from
10 to 90% of its final
value in time 2.2L/R
Time constant small compared
to ‘on’ time.
Dr. Gheith Abandah
30
Pulse width modulation
Time constant large
compared to ‘on’ time,
narrow pulse.
Time constant large compared
to ‘on’ time, wide pulse
Dr. Gheith Abandah
31
PWM mode
Note 1: The 8-bit
timer is concatenated
with 2-bit internal Q
clock, or 2 bits of the
prescaler, to create
10-bit time base.
Dr. Gheith Abandah
32
Waveforms for the 16F873A PWM
generator
Dr. Gheith Abandah
33
Calculations
T = (PR2 + 1) × (Timer 2 input clock period)
= (PR2 + 1) × {Tosc × 4 × (Timer 2 prescale value)}
ton = (pulse width register) × (PWM timer input clock period),
= (pulse width register) × {Tosc × (Timer 2 prescale value)}
pulse width register = CCPR1L | CCP1CON<5:4> + 1
Dr. Gheith Abandah
34
Example
PR2 is loaded with 249D. The clock oscillator
frequency is 4 MHz. Neither pre- nor
postscale. Find the PWM period.
T = (PR2 + 1) × {Tosc × 4 × (Timer 2 prescale value)}
= 250 × (250 ns × 4 × 1)
= 250 μs
i.e. PWM frequency = 4.00 kHz.
Dr. Gheith Abandah
35
PWM applied in the Derbot for
motor control
Dr. Gheith Abandah
36
Example waveforms
Dr. Gheith Abandah
37
Derbot Motor Example – Page 1
;set up
movlw
movwf
movlw
movwf
movwf
movlw
movwf
...
PWM
B'00000100'
t2con
B'00001100'
ccp1con
ccp2con
0f9
pr2
;switch on Timer2,
;no pre or postscale
;enable PWM
;249
Dr. Gheith Abandah
38
Derbot Motor Example – Page 2
leftmot_fwd
;run left motor forward
bsf
porta,mot_en_left
movlw D'176'
movwf CCPR2L
return
leftmot_rev
;run left motor backward
bsf
porta,mot_en_left
movlw D'80'
movwf CCPR2L
return
Dr. Gheith Abandah
39
Generating PWM in software
• May use up all PWM resources or don’t have
them in a low-cost microcontroller.
• PWM outputs can be generated based on
software delay loops only.
• PWM outputs can also be generated based on
timer interrupts.
Dr. Gheith Abandah
40
Generating PWM with timer
interrupt
Dr. Gheith Abandah
41
cblock assembler directive
cblock 20
var1
;reserve 1 byte for var1
var2
;reserve 1 byte for var2
…
endc
Dr. Gheith Abandah
42
PWM used for digital-to-analog
conversion (DAC)
Dr. Gheith Abandah
43
RC low-pass filter characteristics
Dr. Gheith Abandah
44
Derbot Example
fc = 1/(2π*100nF* 20kΩ)
= 80 Hz
Dr. Gheith Abandah
45
Generating a sine wave
Dr. Gheith Abandah
46
Lower: the PWM stream.
Upper: detail of analog output
T = 250 μs
f = 4 kHz
Dr. Gheith Abandah
47
Sine wave example – Page 1
clrf pointer
sin_loop
movf pointer,w
call sin_table ;get most significant byte
movwf ccpr1l
;move it to the PWM output
incf pointer,f ;increment the pointer
movf pointer,w
call sin_table ;get the MS byte
andlw B'11000000' ;we only use ms 2 bits
Dr. Gheith Abandah
48
Sine wave example – Page 2
movwf
bcf
rrf
rrf
iorlw
movwf
incf
movf
temp
status,c
;adjust for CCP1CON
temp,f
temp,w
B'00001100' ;set some CCP1CON bits
ccp1con
pointer,f
pointer,w
call
goto
delay1
sin_loop
…
Dr. Gheith Abandah
49
Weaknesses of this method
• The output analog voltage is directly dependent
on the logic levels of the PWM stream. These in
turn are dependent on the accuracy of the power
supply voltage.
• The low-pass filter cannot generate fast-changing
signals..
• Running the PWM faster decreases the
resolution.
• There will always be some residual ripple on the
analog output.
Dr. Gheith Abandah
50
Frequency measurement
Both a counter and a timer are needed, the timer to
measure the reference period of time and the counter
to count the number of events within that time.
Dr. Gheith Abandah
51
Derbot speed measurement
program
Dr. Gheith Abandah
52
Speed measurement example –
Page 1
Timer2_Int
decfsz int_cntr
goto
int_end
;here if making a measurement
movf
tmr0,w
;save counter values
movwf tmr0_temp
movf
tmr1l,w
movwf tmr1_temp
clrf
tmr0
;clear counters
clrf
tmr1l
Dr. Gheith Abandah
53
Speed measurement example –
Page 1
btfss portc,0 ;inc T1 if = 0, as first
incf tmr1l
;rising edge won’t be seen
movlw D'250' ;reload interrupt counter
movwf int_cntr
int_end
bcf
pir1,tmr2if
retfie
Dr. Gheith Abandah
54
Summary
• Timing is an essential element of embedded system design –
both in its own right and to enable other embedded
activities, like serial communication and pulse width
modulation.
• A range of timers is available, with clever add-on facilities
which extend their capability to capture, compare, create
repetitive interrupts or generate PWM pulse streams.
• In applications of any complexity, a microcontroller is likely
to have several timers running simultaneously, for quite
different and possibly conflicting applications. The question
remains open at this stage: how can these different timebased activities be marshaled and harmonized?
Dr. Gheith Abandah
55
Download