ENG3640 Microcomputer Interfacing Week #6 Timing Generation and Measurement.

Slides:



Advertisements
Similar presentations
Interrupts, Low Power Modes and Timer A (Chapters 6 & 8)
Advertisements

More fun with Timer/Counters
4-1 Timers Timers can be used for  timing  event counting  pulse width measurement  pulse generation  frequency multiplication There are 8 Timers.
Microcontroller Programming II MP6-1
10-1 EE 319K Introduction to Microcontrollers Lecture 10: Interrupts, Output Compare Periodic Interrupts Read Book Sections 9.1, 9.2, 9.4, 9.6.1, 9.6.2,
EET 2261 Unit 10 Enhanced Capture Timer  Read Almy, Chapter 20.  Homework #10 and Lab #10 due next week.  Quiz next week.
68HC11 Polling and Interrupts
Timers Chapter 10 9S12DP256. Timers The 9S12DP256 Programmable Timer Output Compares Pulse Train Using Interrupts Input Capture Measuring the Period of.
8-Bit Timer/Counter 0 Counter/Timer 0 and 2 (TCNT0, TCNT2) are nearly identical. Differences: -TCNT0 can run off an external 32Khz clock (Tosc) or the.
Timers and Interrupts Shivendu Bhushan Summer Camp ‘13.
Timers and Interrupts Shivendu Bhushan Sonu Agarwal.
T IMERS - 2. O UTPUT U NIT Each capture/compare block contains an output unit. The output unit is used to generate output signals such as PWM signals.
16-Bit Timer/Counter 1 and 3 Counter/Timer 1,3 (TCNT1, TCNT3) are identical in function. Three separate comparison registers exist. Thus, three separate.
1 Timing System Timing System Applications. 2 Timing System components Counting mechanisms Input capture mechanisms Output capture mechanisms.
Ch. 9 Interrupt Programming and Real-Time Sysstems From Valvano’s Introduction to Embedded Systems.
ELN5622 Embedded Systems Class 5 Spring, 2003 Aaron Itskovich
8254 Programmable Interval Timer
1 Chapter 4 Timer Operation (I. Scott MacKenzie).
ECE 371- Unit 11 Timers and Counters (“Stop Watches and Alarm Clocks”)
7/23 Timers in Coldfire Processor Computer Science & Engineering Department Arizona State University Tempe, AZ Dr. Yann-Hang Lee (480)
HCS12 Technical Training Module 8 –Enhanced Timer, Slide 1 MOTOROLA and the Stylized M Logo are registered in the US Patent & Trademark Office. All other.
ARM Timers.
System Clocks.
Timers ELEC 330 Digital Systems Engineering Dr. Ron Hayne
Unit 10.2 Timer Examples. Example – Music Generation Channel 6 – Set up as a timer Output to Generate Square Waves Channel 4 – Set up as a timer Output.
Revised: Aug 1, ECE 263 Embedded System Design Lessons 23, 24 - Exceptions - Resets and Interrupts.
ECE 447: Lecture 8 Timer System (1). ECE 447: 68HC11 Timer System 1.Generating delays - imposing a specific delay between two points in the program by.
Chapter 11: Timer Subsystem Esteban Rodriguez-Marek Eastern Washington University Department of Engineering & Design.
ELE22MIC Lecture 15 Applications of Parallel Input Output (I/O)
George W. Woodruff School of Mechanical Engineering, Georgia Tech ME4447/6405 ME 4447/6405 Microprocessor Control of Manufacturing Systems and Introduction.
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.
Timers and Interrupts Anurag Dwivedi. Let Us Revise.
George W. Woodruff School of Mechanical Engineering, Georgia Tech ME4447/6405 ME 4447/6405 Microprocessor Control of Manufacturing Systems and Introduction.
CS-280 Dr. Mark L. Hornick 1 Atmel Timer/Counter System Most microcontrollers include some type of timer system Facilitates real-time monitoring and control.
8254 Timer.
1 68HC11 Timer HC11 or HC12: Chapter HC11 Timer Subsystem  Several timing functions: Basic timing Basic timing Real time interrupts Real time.
© 2009, Renesas Technology America, Inc., All Rights Reserved 1 Course Introduction  Purpose This course provides an introduction to the peripheral functions.
Lecture 4 General-Purpose Input/Output NCHUEE 720A Lab Prof. Jichiang Tsai.
#1 of 10 Tutorial Introduction PURPOSE -To explain how to configure and use the Timer Interface Module in common applications OBJECTIVES: -Identify the.
1 68HC11 Timer. 2 68HC11 Timer Subsystem Several timing functions: Basic timing Basic timing Real time interrupts Real time interrupts Output compare.
Timers Presented by: Griffin Reid Rohit Vardhan Freddie Wilson Date: October 25, 2005.
One more PIC18F252 example and revision for exam B222L Branislav Vuksanovic, UoP, ECE.
ELE22MIC Lecture 17 Writing 68HC11 software 68HC11 Main Timer System –Output Compare –What is different about TOC1?
EET 2261 Unit 13 Enhanced Capture Timer
Why are Timer Functions Important?
Timers and Event Counters
ECE 3430 – Intro to Microcomputer Systems
ELE22MIC Lecture 18 Writing 68HC11 software 68HC11 Main Timer System
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
Homework Reading Machine Projects Labs
Timer and Interrupts.
ELE22MIC Lecture 18 Writing 68HC11 software 68HC11 Main Timer System
AVR Addressing Modes Subject: Microcontoller & Interfacing
RX 8-Bit Timer (TMR) 4/20/2011 Rev. 1.00
Timer/Counter Modified from Dr. Lam Phung’s Slides.
8-bit Timer/Counter0 with PWM
ECE 3430 – Intro to Microcomputer Systems
An Introduction to Microprocessor Architecture using intel 8085 as a classic processor
Introduction to Microprocessors and Microcontrollers
ECE 3430 – Intro to Microcomputer Systems
Lecture 12 Multi-Function Timer Pulse Unit 2 (MTU2a)
8253 – PROGRAMMABLE INTERVAL TIMER (PIT). What is a Timer? Timer is a specialized type of device that is used to measure timing intervals. Timers can.
ECE 3430 – Intro to Microcomputer Systems
MCO556 Timer System Exercise.
Timer/Counter Timer/Counter 0 Timer/Counter 1 Timer/Counter 2 8 bit
Presentation transcript:

ENG3640 Microcomputer Interfacing Week #6 Timing Generation and Measurement

ENG3640 Fall Topics  Timer Module  Input Capture  Output Compare  Pulse Accumulator  Pulse Width Modulation

ENG3640 Fall Resources  Huang, Chapter 8, Sections  8.2 Why are Timer Functions Important?  8.3 Standard Timer Module  8.4 Timer Counter Register  8.5 Input Capture Function  8.6 Output Compare  8.8 Pulse Accumulator  8.10 Pulse Width Modulation

4 68HC812A4 Block Diagram CPU12 1-KB SRAM 4-KB EEPROM Port T Timer Module ENG3640 Fall 2012

5 Introduction  Instruction timing (software) is indeterminate (on most processors) to achieve desired level of precision  The timer module is used to handle interfacing with time-precise events  pulse measurement  accelerometer  pulse counting  shaft rotation  pulse modulation  limb actuator

ENG3640 Fall Timer Block Diagram Very Complicated! So we need to divide and conquer to understand the functionality of each sub- module.

ENG3640 Fall Register Listing 25 registers bit HC12

ENG3640 Fall Interrupt Vectors

ENG3640 Fall Basic Workings  The module clock, which is the system clock, at 8Mhz, increments a 16 bit counter called TCNT.  Eight channels use this as a reference.  16 bit register  16 bit comparator  Eight associated input/output pins for reading signals or creating waveforms.  Registers include channel and timer control registers

ENG3640 Fall (I) Output Compare Module  The OC pin is an output of the MCU, hence can be used to control an external device.  The output compare module can be used to 1. Create square waves 2. Generate pulses 3. Implement time delays 4. Execute periodic interrupts 5. Pulse Width Modulation

ENG3640 Fall Basic Components of Output Compare  An output compare event occurs when either the 16-bit TCNT matches the 16-bit OC register or software writes a 1 to the FOC bit. 1. The OMn, OLn bits specify what effect the output compare event will have on the output pin. 2. The module is armed when the mask bit is The flag is set when the event occurs TCNT OC reg 015 OLnOMnflagmask OCn FOC

ENG3640 Fall Output Compare Actions  Each channel can be programmed to affect the output pin to toggle, go high, go low or do nothing OM0OL0

ENG3640 Fall Basic Principles of Output Compare  Two or three actions result from an Output Compare  (TCNT = OC REG) 1. The OC n output bit changes. 2. The output compare flag is set 3. An interrupt is requested (only when mask is 1) TCNT OC reg

ENG3640 Fall Output Compare: Functional Diagram 16-bit Comparator OMn:OLn

ENG3640 Fall Compare to TCNT OCxF

ENG3640 Fall A Simple Delay Using Output Compare  Let fixed be the number of cycles you wish to wait (i.e the delay).  The steps to create the delay are: 1. Read the current 16-bit TCNT 2. Calculate TCNT + fixed 3. Set the 16-bit output compare register to TCNT + fixed 4. Clear the output compare flag 5. Wait for the output compare flag to be set (polling or interrupt)

ENG3640 Fall Output Compare: PWM  Pulse Width Modulation or Variable Duty Cycle Actuator is a technique used to control motors by controlling the amount of power supplied (varying the duty cycle) Small Middle Large

ENG3640 Fall Enabling/Using the OC Module TSWAITSBCKTFFCA TSCR $ The first step to use the Output Compare Module is to set the bit ``TEN” in the TSCR to “1”. This allows the TCNT to increment.  TSWAI: When set to ‘1’ the TCNT stops in wait mode.  TSBCK: When set to `1` the TCNT stops in background mode.  TFFCA: (Timer fast flag clear all) TEN

ENG3640 Fall Enabling/Using the OC Module 2. The second step is to set the prescaler in TMSK2  This determines the rate by which TCNT counts ValuePR[2:1:0]Prescaler Divisor TCNT Clock ns ns ns micro s micro s micro s PR2PR1PR0

ENG3640 Fall Enabling/Using the OC Module IOS7IOS6IOS5IOS4 TIOS $0080 IOS3IOS0IOS1IOS2 3. The third step is to select the pin on the timer PT0.. PT7 as an output compare by placing a `1` in the corresponding bit of the TIOS register.  There is also a direction register DDRT, for Port T, which means we should set the corresponding bits for the output compare.  Make sure also to specify the action (toggle, set or reset) determined by Omx:OLx

ENG3640 Fall Enabling/Using the OC Module 4. The fourth step is to write time you want event to occur to TCn register I. To have event occur on Pin 4 when TCNT == 0x000: TC4 = 0x000; II. To have next event occur on Pin 4 (500 cycles later): TC4 = TC ;  When TCNT == TCn, the specified action will occur, and flag Cfn will be set.  To clear the flag, write a 1 to the bit you want to clear C7FC6FC5FC4F TFLG1 C3FC0FC1FC2F $008E

ENG3640 Fall Enabling/Using the OC Module $008C C7IC6IC5IC4I TMSK1 C3IC0IC1IC2I 5. The fifth step is to arm or disarm the output compare interrupts by initializing the TMSK1 register.

ENG3640 Fall Square Wave Generation: Example  This example generates a 50% duty cycle square wave using output compare.  The output is high for Period cycles, then low for Period cycles.  Toggle mode is used to create the 50% duty cycle square wave.  In this mode, the output compare pin is toggled whenever the output compare latch matches TCNT PT3/OC3

ENG3640 Fall Square Wave Generation: Example ; M68HC812A4. (Initialization routine) Init sei ; disable interrupt (make atomic) bset TIOS, $08 ; Selecting pin 3 as OC bset DDRT, $08 ; PT3 output movb #$80, TSCR ; enable timer (bit “TEN”) movb #$32, TMSK2 ; 500 ns clk (prescalar) bset TMSK1, $08 ; Arm OC3 bset TCTL2, $40 ; OL3 = 1 bclr TCTL2, $80 ; OM3 = 0 (Toggle output line) movb #$08, TFLG1 ; clear Flag for OC3 (IMPORTANT) ldd TCNT ; current time addd #50 ; in 25 micro seconds std TC3 cli ; enable interrupts rts

ENG3640 Fall ; MC68HC812A4 OC3Han movb #$08, TFLG1 ; Ack [4] ldd TC3 [3] addd Period ; next [3] std TC3 [2] rti [8] org $FFE8 ; timer channel 3 fdb OC3Han Square Wave Generation: ISR

ENG3640 Fall Basic Components of Input Capture  The EDGnA, EDGnB specify whether the rising or falling, or both will trigger a capture event.  The module is armed when the mask bit is 1 (i.e. an interrupt will be requested on a capture event).  The flag bit is set when a capture event occurs. TCNT IC reg 015 EdgnAEdgnBflagmask ICn

ENG3640 Fall Basic Principles of Input Capture  Two or three actions result from a capture event 1. The current TCNT value is copied into the input capture register. 2. The input capture flag is set 3. An interrupt is requested (only when mask is 1) TCNT IC reg

ENG3640 Fall Input Capture: Functional Block Diagram TCNT EDGnA:EDGnB TCn

ENG3640 Fall Input Capture: Control Registers Each channel can specify which edge to capture count on EDG0BEDG0A

ENG3640 Fall Input Capture Actions

ENG3640 Fall Input Capture: Applications  The input capture can be used in the following applications 1. Perform a rising edge capture, then a falling edge capture, subtract the two measurements and get pulse width Pulse Width  The minimum pulse width for the input capture input is greater than two module clocks.

ENG3640 Fall Input Capture: Applications  The input capture can also be used in the following applications 1. Perform two rising edge input captures and subtract the two measurements to get period. 2. The subtraction of Second – First remains valid even if the TCNT overflows and wraps around. Period First Second

ENG3640 Fall Enabling/Using the Input Capture Module TSWAITSBCKTFFCA TSCR $ The first step to use the Input Capture Module is to set the bit ``TEN” in the TSCR to “1”. This allows the TCNT to increment.  TSWAI: When set to ‘1’ the TCNT stops in wait mode.  TSBCK: When set to `1` the TCNT stops in background mode.  TFFCA: (Timer fast flag clear all) TEN

ENG3640 Fall Enabling/Using IC: Clock Source 2. The second step is to set the prescaler in TMSK2 Register.  The input to the timer module can be adjusted to produce different time bases (PR2, PR1, PR0)  TOI: Timer Overflow Interrupt Enable  TPU: Timer pull-up resistor enable (0  no pull-up on Port T input)  TDRB: Timer Drive Reduction (0  normal current)  TCRE: Timer counter reset enable (0  free running counter) ValuePR[2:1:0]Prescaler Divisor TCNT Clock ns ns ns micro s micro s micro s PR2PR1PR0

ENG3640 Fall Enabling/Using the IC Module IOS7IOS6IOS5IOS4 TIOS $0080 IOS3IOS0IOS1IOS2 3. The third step is to select the pin on the timer PT0.. PT7 as an input capture by placing a `0` in the corresponding bit of the TIOS register.  There is also a direction register DDRT, for Port T, which means we should clear the corresponding bits for the input capture inputs.  Make sure also to specify the active edge using the TCTL3 and TCTL4 (discussed earlier).

ENG3640 Fall Enabling/Using the IC Module $008C C7IC6IC5IC4I TMSK1 C3IC0IC1IC2I 4. The fourth step is to arm or disarm the input capture interrupts by initializing the TMSK1 register.  Our software can determine if an input capture has occurred by reading the TFLG1 register.  The flags are cleared by writing a 1 to them. C7FC6FC5FC4F TFLG1 C3FC0FC1FC2F $008E

ENG3640 Fall Interrupt Registers  The timer overflow has a separate flag in TFLG2 TOF

ENG3640 Fall Input Signal Conditioning  An op-amp can serve as a signal converter, transforming a sine wave into a square wave that can then be measured  Could be used for period measurement.

ENG3640 Fall Period Measurement: Initialization external signal to PT1/IC1 Period rmb 2 ; units 500 ns First rmb 2 ; TCNT at first edge Done rmb 1 ; set each rising Init sei ; make atomic bset TIOS, $02 ; PT1 = input capture bset DDRT, $02 ; PT1 is input movb #$80, TSCR ; enable timer (bit “TEN”) movb #$32, TMSK2 ; 500 ns clk ldaa TCTL4 ; read old value of programmable edge reg anda #$F3 ; Clear EDG1B = 0 oraa #$04 ; Set EDG1A = 1 staa TCTL4 ; Program PT1 on rise of IC1 movw TCNT, FIRST ; init global var clr Done movb #$02, TFLG1 ; clear IC1F bset TMSK1, $02 ; ARM IC1F cli ; enable interrupts rts

ENG3640 Fall Period Measurement: ISR ; MC68HC812A4 IC1Han movb #$08, TFLG1 ; clear C3F [4] ldd TC1 [3] subd First [3] std Period [3] movw TC1, First [6] movb #$FF, Done [4] rti [8] org $FFEC ; timer channel 1 fdb IC1Han

ENG3640 Fall Smallest Period Process the interrupt (cycles/us) 9 = us Execute entire handler (cycles/us) 31 = us Minimum period (cycles/us) 40 = 5 us  To calculate the smallest period that can be handled by the assembly language input capture interrupt we have to find: 1. The time to process the interrupt 2. The time to execute the entire handler

ENG3640 Fall Optoelectronic Position/Motion Detection  In many cases the MCU uses input capture to monitor and control speed.  A common type of speed sensor is an encoder disk mounted on a shaft.  Disk is located between a slotted optical switch  When encoder disk rotates the switch outputs pulses  Eight pulses correspond to one disk revolution so pulse frequency is related to speed of revolution.

ENG3640 Fall

ENG3640 Fall Timer Module: Input Capture and Output Compare  Input Capture: “captures the time at which an external event occurs”. Can be used to: 1. Generate interrupts and 2. Measure period or pulse width 3. Measure Frequency  Output Compare: “can generate a periodic pulse with a programmable polarity, duration, and frequency”. Can be used to: 1. Create periodic interrupts, 2. Generate: Pulses, Square waves

ENG3640 Fall Issues: Overflow  If the period is 8192 us, the IC1 interrupts will be requested every 16,384 cycles (assuming we increment TCNT every 500ns.)  The difference between TIC1 latch values will be = $4000.  This subtraction remains valid even if the TCNT overflows and wraps around in between IC1 interrupts.  $2000 – $E000  $4000  This method will not operate properly if the period is larger than 65,535 cycles, or 32,767 us.

ENG3640 Fall Input Frequency Measurement: Frequency Resolution  The frequency resolution, (delta f) is defined to be the smallest change in frequency that can be reliably measured by the system.  For the system to detect a change, the frequency must increase (or decrease) enough so that there is one more (or less) pulse during the fixed time interval.  Therefore, the frequency resolution is  delta f = 1/fixed time  So if we count pulses in 10-ms time interval, the frequency resolution is 100HZ.  The frequency would have to increase to 600 Hz or decrease to 400 Hz for the change to be detected.

ENG3640 Fall Input Pulse Measurement

ENG3640 Fall Frequency Measurement  The direct measurement of frequency involves counting input pulses for a fixed amount of time.  The basic idea is to use  Input capture to count pulses  Output compare to create the fixed time interval.

ENG3640 Fall Input Frequency Measurement  We could initialize Input Capture to interrupt on every rising edge of input signal.  During the input capture handler, we could increment a Counter (at the beginning of our fixed time interval, the Counter is initialized to zero).  Counting the number of inputs capture during the time interval, you can calculate input frequency f = counter/fixed time

ENG3640 Fall Output Compare 7 Config Registers  PT7/OC7 on the 6812 can be configured such that an output compare event on it will cause changes on some or all of the other output compare pins.  Usage?  This can be used for synchronization, i.e. creating pulses that start together or end together. Setting OC7Mx bit configures TIMPORT pin to be output. The value in OC7D  output pin when TC7 matches TCNT

ENG3640 Fall Output Compare 7 : Example  movb #$0F, OC7M  movb #$0A, OC7D  Will cause the timer to clear PT0 and PT2 and set PT1 and PT3 when a compare event occurs on Timer Channel

ENG3640 Fall Pulse Accumulator  The pulse accumulator consists of a 16-bit counter that can be used to count events on PT7 or measure the time between events on PT7

ENG3640 Fall Pulse Accumulator vs. Input Capture  The pulse accumulator is most useful as an event counter for high-speed signals.  The input capture can be used to count input events but the problem is the large CPU load required for high event rates.  When using the Pulse Accumulator, there is no CPU load while counting events.  The only limitation on the event rate is the Pulse Accumulator Pulse Width specification, which is typically close to the E-clock period.  Typical Applications: Monitoring tape position in VCR

ENG3640 Fall Pulse Accumulator: Control Registers  The pulse accumulator is enabled by the PAEN bit in PACTL  The mode of the pulse accumulator is controlled by the PAMOD bit in the PACTL register 1.PAMOD = 0  Event Counting Mode 2.PAMOD = 1  Gated Time Mode PAMODPAENPEDGE

ENG3640 Fall Pulse Accumulator in Event Counting Mode PAI PAEN = 1, PAMOD = 0 16-bit counter PAOVI PAOVF PAIF

ENG3640 Fall Pulse Accumulator: Modes  Gated Time Accumulation Mode  PAMOD = 1, counter clock source  E-clock/64,  The signal on PT7 is used to enable the clock, so in effect the counter measures the length of the enable signal on PT7.  If PEDGE = 0, a one on the input enables counting.  If PEDGE = 1, a zero on the input enables counting.  The PAOVF flag is set when the pulse accumulator counter overflows.

ENG3640 Fall Pulse-Width Modulation Module, 8-Bit Mode

ENG3640 Fall PWM Module, 8-Bit Mode  The M68HC12A4 MCU has an on-chip, pulse-width modulation (PWM) module that can generate up to four PWM signals with no CPU loading.  Once the PWM module is configured, the PWM signals are generated without software intervention except to change the duty cycle.  The PWM module can be configured as: 1. Four 8-bit PWM channels 2. Two 16-bit PWM channels 3. One 16-bit and Two 8-bit PWM channels.

ENG3640 Fall Pulse Accumulator: Modes  Event Counter Mode  Gated-Time Accumulation Mode. 1. Event counter Mode  PAMOD = 0, counter clock source  PT7 input  Counts the number of rising or falling edges on the input depending on PEDGE bit (and PAOVF is set)  Overflow after 0xFFFF can generate an interrupt if PAI is set.