Taking timing further - PowerPoint PPT Presentation

About This Presentation
Title:

Taking timing further

Description:

Taking timing further Chapter Nine 9.1 9.8 Dr. Gheith Abandah * Calculations T = (PR2 + 1) (Timer 2 input clock period) = (PR2 + 1) {Tosc 4 (Timer 2 ... – PowerPoint PPT presentation

Number of Views:107
Avg rating:3.0/5.0
Slides: 56
Provided by: Dr664
Category:
Tags: further | taking | tick | timing

less

Transcript and Presenter's Notes

Title: Taking timing further


1
Taking timing further
  • Chapter Nine
  • 9.1 9.8

2
Outline
  • Timer 1
  • Timer 2
  • Capture/Compare/PWM
  • Pulse Width Modulation (PWM)
  • Digital to Analog Conversion (DAC)
  • Summary

3
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

4
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
5
PIC 16F84A Timer 0 Module
6
PIC 16F84A Timer 0 Module
7
Option Register
  • T0CS Clock source select
  • T0SE Source edge select
  • PSA Prescaler assignment bit
  • PS2PS0 Prescaler rate select

8
PIC 16F87XA Timer 1 Module
9
Timer 1 Registers
  • 16-bit register
  • TMR1L (0Eh)
  • TMR1H (0Fh)
  • Control Register
  • T1CON (10h)

10
Timer 1 control register
  • T1CKPS1T1CKPS0 Input Clock Prescale Select,
    11-18
  • T1OSCEN Oscillator Enable Control
  • T1SYNC External Clock Input Synchronization
    Control
  • TMR1CS Clock Source Select
  • TMR1ON Timer1 On

11
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.

12
Derbot odometer circuit
13
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

14
Odometer Example Page 2
  • opto_move
  • clrf tmr0 clear timers
  • clrf tmr1l
  • clrf tmr1h
  • clrf flags
  • btfss portc,0 increment T1 if ip is zero,
  • incf tmr1l as 1st rising edge isnt
  • detected
  • call leftmot_fwd start motors running
  • call rtmot_fwd

Because the counter must first have a falling
edge before it starts to count.
15
Odometer Example Page 3
  • opto_loop
  • movlw D'91' test for 1m
  • subwf tmr0,0
  • btfsc status,z
  • bcf porta,mot_en_left disable motor if
  • movlw D'91'
  • subwf tmr1l,0
  • btfsc status,z
  • bcf porta,mot_en_rt disable motor if
  • goto opto_loop

16
Generating a clock tick a repetitive
interrupt stream
17
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.

18
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.

19
PIC 16F87XA Timer 2 Module
20
Timer 2 Registers
  • 8-bit register
  • TMR2 (11h)
  • Control Register
  • T2CON (12h)
  • PR2 (92h), period register

21
Timer 2 control register
  • TOUTPS3TOUTPS0 Output Postscale Select,
    11-116
  • TMR2ON Timer 2 On
  • T2CKPS1T2CKPS0 Clock Prescale Select, 11, 14,
    116

22
The PR2 register, comparator and postscaler
23
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.

24
The capture/compare/PWM (CCP) modules
  • 2 CCP modules
  • Each CCP module contains a 16-bit register which
    can operate as a
  • 16-bit Capture register
  • 16-bit Compare register
  • Pulse width modulation Master/Slave Duty Cycle
    register

25
CCP Registers
  • 16-bit register
  • CCPR1L (15h)
  • CCPR1H (16h)
  • CCPR2L (1bh)
  • CCPR2H (1ch)
  • Control Register
  • CCP1CON (17h)
  • CCP2CON (1dh)

26
CCP x control register
  • CCPxXCCPxY PWM Least Significant bits
  • CCPxM3CCPxM0 Mode Select bits

27
CCPx Mode Select bits
28
Capture mode
29
Compare mode
30
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.
31
Pulse width modulation
Time constant large compared to on time, wide
pulse
Time constant large compared to on time, narrow
pulse.
32
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.

33
Waveforms for the 16F873A PWM generator
34
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 CCP1CONlt54gt 1

35
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.

36
PWM applied in the Derbot for motor control
37
Example waveforms
38
Derbot Motor Example Page 1
  • set up PWM
  • movlw B'00000100' switch on Timer2,
  • movwf t2con no pre or postscale
  • movlw B'00001100' enable PWM
  • movwf ccp1con
  • movwf ccp2con
  • movlw 0f9 249
  • movwf pr2
  • ...

39
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

40
Generating PWM in software
  • May use up all PWM resources or dont 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.

41
Generating PWM with timer interrupt
42
cblock assembler directive
  • cblock 20
  • var1 reserve 1 byte for var1
  • var2 reserve 1 byte for var2
  • endc

43
PWM used for digital-to-analog conversion (DAC)
44
RC low-pass filter characteristics
45
Derbot Example
fc 1/(2p100nF 20kO) 80 Hz
46
Generating a sine wave
47
Lower the PWM stream.Upper detail of analog
output
T 250 µs f 4 kHz
48
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

49
Sine wave example Page 2
  • movwf temp
  • bcf status,c adjust for CCP1CON
  • rrf temp,f
  • rrf temp,w
  • iorlw B'00001100' set some CCP1CON bits
  • movwf ccp1con
  • incf pointer,f
  • movf pointer,w
  • call delay1
  • goto sin_loop

50
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.

51
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.
52
Derbot speed measurement program
53
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

54
Speed measurement example Page 1
  • btfss portc,0 inc T1 if 0, as first
  • incf tmr1l rising edge wont be seen
  • movlw D'250' reload interrupt counter
  • movwf int_cntr
  • int_end
  • bcf pir1,tmr2if
  • retfie

55
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 time-based activities be marshaled and
    harmonized?
Write a Comment
User Comments (0)
About PowerShow.com