Taking timing further Chapter Nine 9.1 – 9.8 Dr. Gheith Abandah1
Outline Timer 1 Timer 2 Capture/Compare/PWM Pulse Width Modulation (PWM) Digital to Analog Conversion (DAC) Summary Dr. Gheith Abandah2
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 Abandah3
The PIC 16 Series DevicePinsFeatures 16F84A181 8-bit timer 1 5-bit port 1 8-bit port 16F873A 16F876A 283 parallel ports, 3 counter/timers, 2 capture/compare/PWM, 2 serial, 5 10-bit ADC, 2 comparators 16F874A 16F877A 405 parallel ports, 3 counter/timers, 2 capture/compare/PWM, 2 serial, 8 10-bit ADC, 2 comparators Dr. Gheith Abandah4
PIC 16F84A Timer 0 Module Dr. Gheith Abandah5
PIC 16F84A Timer 0 Module Dr. Gheith Abandah6
Option Register T0CS: Clock source select T0SE: Source edge select PSA: Prescaler assignment bit PS2:PS0: Prescaler rate select Dr. Gheith Abandah7
PIC 16F87XA Timer 1 Module Dr. Gheith Abandah8
Timer 1 Registers 16-bit register: TMR1L (0Eh) TMR1H (0Fh) Control Register: T1CON (10h) Dr. Gheith Abandah9
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 Abandah10
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 Abandah11
Derbot odometer circuit Dr. Gheith Abandah12
Odometer Example – Page 1 ;Initialization movlw B' ';set port A for right movwf adcon1;analog/digital mix movlw B' ';T0: external input, movwf option_reg;low to high transition, ;no prescale movlw B' ' ;T1: no prescale, movwf t1con ;oscillator disabled, ;external sync input Dr. Gheith Abandah13
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 1 st rising edge isn’t ;detected call leftmot_fwd ;start motors running call rtmot_fwd Dr. Gheith Abandah14 Because the counter must first have a falling edge before it starts to count.
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 Dr. Gheith Abandah15
Generating a ‘clock tick’ – a repetitive interrupt stream Dr. Gheith Abandah16
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 kHz. The 8-bit timer divides this frequency by 256 to produce the clock tick frequency, which will be kHz/256, or Hz. Dr. Gheith Abandah17
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 2 16 to produce the clock tick frequency, which will be 125 kHz/ 2 16, or 1.91 Hz. Dr. Gheith Abandah18
PIC 16F87XA Timer 2 Module Dr. Gheith Abandah19
Timer 2 Registers 8-bit register: TMR2 (11h) Control Register: T2CON (12h) PR2 (92h), period register Dr. Gheith Abandah20
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 Abandah21
The PR2 register, comparator and postscaler Dr. Gheith Abandah22
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 Hz. With postscaler of 16, then the interrupt frequency will be Hz. Dr. Gheith Abandah23
The capture/compare/PWM (CCP) modules Dr. Gheith Abandah24 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
CCP Registers 16-bit register: CCPR1L (15h) CCPR1H (16h) CCPR2L (1bh) CCPR2H (1ch) Control Register: CCP1CON (17h) CCP2CON (1dh) Dr. Gheith Abandah25
CCP x control register Dr. Gheith Abandah26 CCPxX:CCPxY: PWM Least Significant bits CCPxM3:CCPxM0: Mode Select bits
CCPx Mode Select bits Dr. Gheith Abandah27
Capture mode Dr. Gheith Abandah28
Compare mode Dr. Gheith Abandah29
Pulse width modulation Dr. Gheith Abandah30 Time constant small compared to ‘on’ time. The current rises from 10 to 90% of its final value in time 2.2L/R
Pulse width modulation Dr. Gheith Abandah31 Time constant large compared to ‘on’ time, wide pulse Time constant large compared to ‘on’ time, narrow pulse.
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 Abandah32
Waveforms for the 16F873A PWM generator Dr. Gheith Abandah33
Calculations T = (PR2 + 1) × (Timer 2 input clock period) = (PR2 + 1) × {T osc × 4 × (Timer 2 prescale value)} t on = (pulse width register) × (PWM timer input clock period), = (pulse width register) × {T osc × (Timer 2 prescale value)} pulse width register = CCPR1L | CCP1CON + 1 Dr. Gheith Abandah34
Example PR2 is loaded with 249D. The clock oscillator frequency is 4 MHz. Neither pre- nor postscale. Find the PWM period. T = (PR2 + 1) × {T osc × 4 × (Timer 2 prescale value)} = 250 × (250 ns × 4 × 1) = 250 μs i.e. PWM frequency = 4.00 kHz. Dr. Gheith Abandah35
PWM applied in the Derbot for motor control Dr. Gheith Abandah36
Example waveforms Dr. Gheith Abandah37
Derbot Motor Example – Page 1 ;set up PWM movlw B' ' ;switch on Timer2, movwf t2con;no pre or postscale movlw B' ' ;enable PWM movwf ccp1con movwf ccp2con movlw 0f9;249 movwf pr2... Dr. Gheith Abandah38
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 Abandah39
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 Abandah40
Generating PWM with timer interrupt Dr. Gheith Abandah41
cblock assembler directive cblock 20 var1;reserve 1 byte for var1 var2;reserve 1 byte for var2 … endc Dr. Gheith Abandah42
PWM used for digital-to-analog conversion (DAC) Dr. Gheith Abandah43
RC low-pass filter characteristics Dr. Gheith Abandah44
Derbot Example Dr. Gheith Abandah45 f c = 1/(2π*100nF* 20kΩ) = 80 Hz
Generating a sine wave Dr. Gheith Abandah46
Lower: the PWM stream. Upper: detail of analog output Dr. Gheith Abandah47 T = 250 μs f = 4 kHz
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' ' ;we only use ms 2 bits Dr. Gheith Abandah48
Sine wave example – Page 2 movwf temp bcf status,c ;adjust for CCP1CON rrf temp,f rrf temp,w iorlw B' ' ;set some CCP1CON bits movwf ccp1con incf pointer,f movf pointer,w … call delay1 goto sin_loop Dr. Gheith Abandah49
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 Abandah50
Frequency measurement Dr. Gheith Abandah51 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.
Derbot speed measurement program Dr. Gheith Abandah52
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 Abandah53
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 Abandah54
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? Dr. Gheith Abandah55