Taking timing further Chapter Nine 9.1 – 9.8 Dr. Gheith Abandah1.

Slides:



Advertisements
Similar presentations
Working with time: interrupts, counters and timers Chapter Six Dr. Gheith Abandah1.
Advertisements

Electronics Design Lab TUTORIAL PIC Microcontrollers Francesco Tenore 2/10/2006.
م/محمد علي عطية حسين. Pin Diagram DescriptionPin Name Oscillator crystal input/external clock source input. OSC1/CLKIN Oscillator crystal output.OSC2/CLKOUT.
The 8051 Microcontroller and Embedded Systems
More fun with Timer/Counters
ECT 357 Ch 16 Output Control. Today’s Quote: The measure of a man is not how great his faith is bt how great his love is. The measure of a man is not.
Data acquisition and manipulation
Introduction of Holtek HT-46 series MCU
The human and physical interfaces Chapter Eight 8.1 – 8.9 Dr. Gheith Abandah1.
Starting with serial Chapter Ten 10.1, 10.2,
The PIC uCs PIC Microcontroller and Embedded Systems Muhammad Ali Mazidi, Rolin McKinlay and Danny Causey Eng. Husam Alzaq The Islamic Uni. Of Gaza 9-1.
5-1 Timer/Counters In many microprocessor systems we need to:  count logic pulses  measure the frequency of signals  measure the period of pulses 
Parallel ports, power supply, and the clock oscillator Chapter Three Dr. Gheith Abandah1.
1.  8051 Timers “count up,” incrementing the Timer’s respective “count register” each time there is a triggering clock pulse. 2  When the “count register”
Lecture 9 Timer Operations and Programming. 2  Introduction  Summary of timers  Timer programming sequence  Summary of timer SFRs  Timer 0: 8-bit.
Engineering 1040: Mechanisms & Electric Circuits Fall 2011 Introduction to Embedded Systems.
Today’s Lecture List the Timers of PIC18 and their associated registers Describe the various modes of the PIC18 timers Program the PIC18 timers in Assembly.
Minimum Systems and the PIC 16F84A Chapter Two Dr. Gheith Abandah1.
V 0.21 Timers A timer on a  C is simply a counter The input clock frequency to a timer can be prescaled so that it is some fraction of the system clock.
1 Chapter 4 Timer Operation (I. Scott MacKenzie).
7/23 Timers in Coldfire Processor Computer Science & Engineering Department Arizona State University Tempe, AZ Dr. Yann-Hang Lee (480)
Chapter 13 Linear-Digital ICs. Copyright ©2009 by Pearson Education, Inc. Upper Saddle River, New Jersey All rights reserved. Electronic Devices.
ARM Timers.
Building Assembler Programs Chapter Five Dr. Gheith Abandah1.
System Clocks.
16F877A. Timer 0 The Timer0 module timer/counter has the following features: –8-bit timer/counter –Readable and writable –8-bit software programmable.
Timers ELEC 330 Digital Systems Engineering Dr. Ron Hayne
Chapter 4 TIMER OPERATION
CoE3DJ4 Digital Systems Design Chapter 4: Timer operation.
Timers The timers of the PIC16C7X microcontroller can be briefly described in only one sentence. There are three completely independent timers/counters.
PIC16F877 ISR Points to note Interrupt Triggers –Individual interrupt flag bits are set, regardless of the status of their corresponding mask bit, PEIE.
Time Measurement Capture Mode of the Capture/Compare/PWM module is used for time measurement. TMR1 or TMR3 16-bit value transferred to 16-bit capture.
CCP MODULES  The CCP module (Capture/Compare/PWM) is a peripheral which allows the user to time and control different events.  Capture Mode provides.
1 68HC11 Timer Chapter HC11 Timer Subsystem Several timing functions: Basic timing Basic timing Real time interrupts Real time interrupts Output.
EEE237 Introduction to Microprocessors Week x. SFRs.
Architecture and instruction set. Microcontroller Core Features:  Operating speed: DC - 20 MHz clock input DC ns instruction cycle Up to 8K x.
1 68HC11 Timer HC11 or HC12: Chapter HC11 Timer Subsystem  Several timing functions: Basic timing Basic timing Real time interrupts Real time.
Interrupts  An interrupt is any service request that causes the CPU to stop its current execution stream and to execute an instruction stream that services.
Working with Time: Interrupts, Counters and Timers
Microcontrollers session. What is Microcontroller? Microcontroller is composed of microprocessor,ram, flash memory,EEPROM and some modules like timers,
Special Features. Device Configuration bits Revision Device Configuration bits Revision On-chip Power-on Reset (POR) Revision On-chip Power-on Reset (POR)
One more PIC18F252 example and revision for exam B222L Branislav Vuksanovic, UoP, ECE.
TIMERS.
Electronic Devices and Circuit Theory
Timer modules in PIC 16F877.  The PIC 16F877 basically has three independent timer modules,  denoted by the symbols, TIMER-0, TIMER1,andTIMER2. . These.
Chapter 9 PIC18 Timer Programming in Assembly
Why are Timer Functions Important?
Microprocessor Systems Design I
Timers and Event Counters
Microprocessor Systems Design I
Microprocessor Systems Design I
Timer and Interrupts.
Chapter 13 Linear-Digital ICs
8051 Timers Timers are the peripherals of the 8051 Microcontroller.
Microprocessor Systems Design I
Microprocessors Timers
Project Title EVM IN PIC Under the Guidance of Submitted by.
RX 8-Bit Timer (TMR) 4/20/2011 Rev. 1.00
Introduction to Micro Controllers & Embedded System Design Timer Operation Department of Electrical & Computer Engineering Missouri University of Science.
Pugazhendhi.M 11/12/2018 TIMERS 11/12/2018 CCET CCET.
ECE 354 Computer Systems Lab II
Timer.
PIC18 Timer Programming “Explain the assembly language programming for the timer and counter module”
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
Timer Source: under
EECE.3170 Microprocessor Systems Design I
ADC and DAC Data Converter
Timers Chapter 2.
Presentation transcript:

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