ECE 371- Unit 11 Timers and Counters (“Stop Watches and Alarm Clocks”)

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
EET 2261 Unit 10 Enhanced Capture Timer  Read Almy, Chapter 20.  Homework #10 and Lab #10 due next week.  Quiz next week.
ECE 371 – Unit 15 Data Acquisition Systems A/Ds in MC9S12DPS56B Microcontroller.
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.
5-1 Timer/Counters In many microprocessor systems we need to:  count logic pulses  measure the frequency of signals  measure the period of pulses 
Timers and Interrupts Shivendu Bhushan Summer Camp ‘13.
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.
Timer Interface Module MTT TIMER INTERFACE MODULE (TIM)
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.
ENG3640 Microcomputer Interfacing Week #6 Timing Generation and Measurement.
UNIT 8 Keypad Interface Contact Closure Counter Exceptions (Interrupts and Reset)
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)
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.
Timer/counter Chapter 12
ARM Timers.
System Clocks.
Timers ELEC 330 Digital Systems Engineering Dr. Ron Hayne
Chapter 4 TIMER OPERATION
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.
MCU: Interrupts and Timers Ganesh Pitchiah. What’s an MCU ?
ECE 447 Fall 2009 Lecture 10: TI MSP430 Timers and Capture Modes.
Lecture 11: TI MSP430 Timers Compare Modes
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.
1 68HC11 Timer Chapter HC11 Timer Subsystem Several timing functions: Basic timing Basic timing Real time interrupts Real time interrupts Output.
Lecture 12 Multi-Function Timer Pulse Unit 2 (MTU2a)
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.
1 68HC11 Timer HC11 or HC12: Chapter HC11 Timer Subsystem  Several timing functions: Basic timing Basic timing Real time interrupts Real time.
UNIT 7 - INTRODUCTION TO I/O INTERFACING. TWO MAJOR TYPES OF I/O INTERFACING ISOLATED I/O - DEDICATED I/O INSTRUCTIONS ARE USED TO ACCESS I/O DEVICES.
#1 of 10 Tutorial Introduction PURPOSE -To explain how to configure and use the Timer Interface Module in common applications OBJECTIVES: -Identify the.
HCS12 TIMER FUNCTIONS Razvan Bogdan Embedded Systems.
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.
Seth Schwiethale James Crosetto James Ellison.  square pulse of ms, repeats every 20 ms  It is the same for both steering and acceleration 
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?
V.V.P. ENGINEERING COLLEGE,RAJKOT
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
PWM and DC Motor Control
Timer and Interrupts.
BITS EDU. CAMPUS , VARNAMA
AVR Addressing Modes Subject: Microcontoller & Interfacing
MSP432 ARM Timer Programming
RX 8-Bit Timer (TMR) 4/20/2011 Rev. 1.00
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.
Timer/Counter Modified from Dr. Lam Phung’s Slides.
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
8-bit Timer/Counter2 with PWM and Asynchronous Operation
Lecture 12 Multi-Function Timer Pulse Unit 2 (MTU2a)
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:

ECE 371- Unit 11 Timers and Counters (“Stop Watches and Alarm Clocks”)

Port T – Enhanced Capture Timer 8 Input Capture – Output Compare Pins

“INPUT CAPTURE” Used to measure the characteristics of an input signal - Measure the width of an input pulse - Determine the period or duty cycle of an input signal. Pulse width

“INPUT CAPTURE” Used to measure the characteristics of an input signal - Measure the width of an input pulse - Determine the period or duty cycle of an input signal. AB Duty Cycle = A/(A + B) * 100%

“OUTPUT COMPARE” Permits generation of output signals to user specification - Single pulse - Square wave - PWM Signal Single pulse - can control width of pulse and/or delay from trigger signal T1 Trigger T2

“OUTPUT COMPARE” Permits generation of output signals to user specification - Single pulse - Square wave - PWM Signal Square wave - can control period. Period

“OUTPUT COMPARE” Permits generation of output signals to user specification - Single pulse - Square wave - PWM Signal PWM signal- can control duty cycle = T1/ (T1 + T2) T1 T2

Main Components of Timer Module Free Running 16-Bit Up-Counter Eight Input/Output Lines (Channels) Pulse Accumulator System

16-bit Free Running Counter 2 MHz

INPUT CAPTURE (“Stop Watch”/Signal Analysis) Captures the Current Count on the Free-Running Counter When a User-Specified Event Occurs on the Channel Pin Types of “events” - rising edge on channel pin - fall edge on channel pin - rising or falling edge

INPUT CAPTURE When the User-Specified Event Occurs: 1.Current value of free-running counter is latched into the Timer Channel Register. 2.Channel Flag is set. 3.Channel will generate an Interrupt (if the Channel’s Interrupt Enable Bit has been set.)

OUTPUT COMPARE (“Alarm Clock”/Signal Generation) Causes a change on the channel output pin when the value in the Free- Running Timer matches the value in the Channel Register. Actions that can activated at the channel output pin: Go high Go low Toggle

“Output Compare System for One Channel”

Special Channel for Output Compare: Channel 7 The state of ALL channel output pins can be affected when value of free-running timer matches the target value of Channel 7. Two special registers make this possible: - Mask Register (selects which Channels are to be included in this feature. - “Value” Register (indicates what value each included channel is to take on)

Software interacts with Counter/Timer using registers: Main Registers: Count Register (contains value of free-running counter) Control Registers (used to select option) Interrupt Mask Register (used to enable interrupts) Flags Registers (used to indicate when an interrupt condition has occurred and when free-running counter rolls over) Input Capture/Output Compare Registers (Alarm Clock/Stop Watch value for each timer) Input Capture/Output Compare Select Registers (used to specify whether a given channel is to be used as Input Capture or Output Compare.

16-bit Timer Counter Register Bus Clock Prescaler PR2,PR1,PR0 16-bit Timer Count Register TCNT TOF (free-running counter)

Timer Count Register Timer Count Register (TCNT) is constantly being incremented TOF is set when TCNT overflows Bus Clock Frequency and Prescaler determine the rate of increment

Timer Resolution 2 Mhz Bus Clock –Prescale=0b000 => 0.5 usec resolution TOF Set every usec. –Prescale=0b001 => 1.0 usec resolution TOF Set every usec. –Prescale=0b111 => 64 usec resolution TOF Set every Seconds

TSCR2 Example Code /* Bit 4 – TCRE */ TSCR2 = TSCR2 & 0xF7; // Set TCRE=0 (prevent special channel 7 // from reseting counter) /* Bits 2, 1, 0 – Prescale */ int prescale; // 3-bit prescale value /* Set Prescale Value */ TSCR2 = (TSCR2 & 0xF8) | (0x07 & prescale); /* TSCR2 = xxxx xxxx 0xF8 = TSCR2&0xF8 = xxxx x000 0x07 = prescale = yyyy yyyy Prescale&0x7= yyy TSCR2 = xxxx xyyy */

TSCR2 Example Code /* Bit 7 – TOI */ TSCR2 = TSCR2 | 0x80; //Set TOI=1 to enable Timer Overflow Interrupts /* TSCR2 = xxxx xxxx 0x80 = TSCR2|0x80 = 1xxx xxxx */ TSCR2 = TSCR2 & 0x7F; //Set TOI=0 to disable Timer Overflow Interrupts /* TSCR2 = xxxx xxxx 0x7F = TSCR2&0x7F= 0xxx xxxx */

/* Clear TOF - Timer Overflow Flag */ TFLG2 = 0x80;

TSCR1 = TSCR1 | 0x80; // Enable Timer TSCR1 = TSCR1 & 0x7F; //Disable Timer

Timer Overflow Interrupt Example ---- long unsigned int tnctof = 0; // count timer overflows void tof_isr(void) __attribute__ ((interrupt); // forward reference int main(void) {// initialize timer SETVECT(0xFFDE,tof_isr); // initialize TOF Interrupt vector TSCR1 = TSCR1 |0x80; // Enable timer (TEN = 1) TSCR2= 0x80; // Enable interrupt on overflow, prescaler=0 (TOI=1) TFLG2 = 0x80; // Clear Timer Overflow (TOF=1) ENABLE(); --- } void tof_isr(void) { TFLG2 = 0x80; // Clear Timer Overflow Flag (TOF=1) tnctof++; // count number of overflows }

16-bit Register Per Channel

Input Capture/Output Compare Selection Register

Example – I/O Selection /* Input Capture/Output Compare Module Selection Timer 7, 6. 3, 1 – Input Capture Timer 5, 4, 2, 0 – Output Compare */ TIOS = 0x35; // 0x35 = 0b /* Change Timer 6 to Output Compare */ TIOS = TIOS | 0x40; // 0x40 = 0b /* Change Timer 2 to Input Capture */ TIOS = TIOS & 0xFB; // 0xFB = 0b

Using Channel X for Input Capture Initialization Set Timer Channel X to Input Capture in the TOS Register Select Input Edge Capture Control Mode in Timer Control Register 3 or 4 Operation On selected Edge, TCNT is copied into TCx, Flag is Set Software Action After Flag is Set: Program reads TCx Program clears Flag

Timer Control Register

Input Capture Edge Control

Example 1 – Input Capture /* Input Capture/Output Compare Module Selection Timer 1 – Input Capture Timer 1 - Capture on “0” to “1” */ TIOS = TIOS & 0xFD; // Clear Bit 1 – Set Timer 1 for Input /* Let TIOS = xxxx xxxx 0xFD = TIOS & 0xFD = xxxx xx0x */ TCTL4 = (TCTL4&0xF3) | 0x04; // Use Rising Edge Trigger /* Let TCTL4 = xxxx xxxx 0xF3 = TCTL4&0xF3 = xxxx 00xx 0x04 = (TCTL4&0xF3)|0x04 = xxxx 01xx */

TFLG1 = 0x02; // clear Timer 1 Interrupt Flag while((TFLG1&0x02)==0); // Wait for input trigger /* TFLG1 = xxxx xxxx 0x02 = TFLG1&0x02 = */ unsigned int time1; time1 = TC1; // Read in time of capture

Example 2 – Input Capture Pulse Width Measurement /* Input Capture/Output Compare Module Selection Timer 0 – Alternate between: - Capture on “0” to “1” - Capture on “1” to “0” */ TIOS = TIOS & 0xFE; // Clear Bit 0 – Use Timer 0 as Input /* TIOS = xxxx xxxx 0xFE = TIOS&0xFE = xxxx xxx0 */ TCTL4 = (TCTL4&0xFC) | 0x01; // Trigger on rising edge TFLG1 = 0x01; // clear Timer 0 Flag while((TFLG1&0x01)==0); // Wait for input trigger on PT1

Example 2 – Pulse Width unsigned int time1, time2, pw; time1 = TC0; // Read time of capture TFLG1 = 0x01; //clear Timer 0 Flag TCTL4 = (TCTL4&0FC) | 0x02; // Change Trigger condition to “falling edge” while((TFLG1&0x01)==0); // Wait for input trigger time2 = TC0; // read time of capture /* 2 Cases – tcnt overflow */ If(time2 > time1) pw = time2 - time1; else pw = 0x time2 – time1;

Example 2 – PW Calculation …… 0xFFFF …. 0xFFFF ….. TCNT 0x0100 0xC000 Case 1 Case 2 0xD000 0x2000 PW = 0xC000-0x0100 = 0xBF00 PW = 0x x2000 – 0xD000 = 0x5000

Timer Interrupt Enable Register

Input Capture Example Using Interrupts /* Use Channel 0 Input Capture to Measure Pulse Width */ --- unsigned int pw=0; void ic0_isr(void) __attribute__ ((interrupt)); // forward reference --- int main() {// init timer, code not shown // init input capture channel 0 TIOS = TIOS & 0xFE; // Set Channel 0 to Input (Clear Bit 0) TCTL4 = (TCTL4&0xFC)| 0x01; // Trigger Channel 0 on rise TFLG1 = 0x01; // Clear Channel 0 Flag TIE = TIE | 0x01; // Enable Channel 0 Interrupt SETVECT(0xFFEE,ic0_isr); // Init Channel 0 Interrupt Vector ENABLE();

Input Capture Using Interrupts pw=0; while(pw==0); // wait for pulse width measurement =pw; // use pulse width pw=0; }

Input Capture Interrupt Example void ic0_isr() {unsigned int temp; if((TCTL4&0x03)==0x01) {// time was rise temp=TC0; TCTL4=(TCTL4&0xFC)|0x2; // set fall TFLG1=0x01; // clear flag--return from this point leaves processing in “while” loop } else // time was fall {if(temp<TC0) pw=TC0-temp; else pw=0x10000+TC0-temp; TCTL4=(TCTL4&0xFC)|0x01; // set rise TFLG1=0x01; // clear flag }

Output Compare Set Channel X to Output Compare –TIOS = TIOS | (1<<X); // Set Bit X to “1 Initialize TCx to Time for Compare Select Mode

Timer Output Control Register

Example – Output Compare Generate Square Wave /* Initialize Output Compare Channel 2 Complement on Each Compare Timer has been initialized */ TIOS = TIOS | 0x04: // Set Channel 2 to Output Compare – Bit 2 TCTL2 = (TCTL2&0xCF) | 0x10; // Toggle Output Line 2 TFLG1 = 0x04; // Clear Channel 2 Flag TC2 = TCNT + T/2; // Complement in T/2 Seconds while(1) { while((TFLG1&0x04)==0); // Wait for Channel 2 flag to go high TC2 = TC2 + T/2; // Complement T/2 from now TFLG1 = 0x04; // Clear Channel 2 Flag }

Square Wave Generation Using Interrupts SETVECT(0xFFEA, oc2_isr); // set interrupt vector ---- void oc2_isr(void) {TC2 = TC2 + T/2; // Complement T/2 from now TFLG1 = 0x04; // Clear Channel 2 Flag }

#define TIOS _P(0x40) // timer input/output select #define CFORC _P(0x41) // timer compare force #define OC7M _P(0x42) // timer output compare 7 mask #define OC7D _P(0x43) // timer output compare 7 data #define TCNT _LP(0x44) // timer counter register (2 bytes) #define TSCR _P(0x46) // timer system control register #define TTOV _P(0x47) // reserved #define TCTL1 _P(0x48) // timer control register 1 #define TCTL2 _P(0x49) // timer control register 2 #define TCTL3 _P(0x4A) // timer control register 3 #define TCTL4 _P(0x4B) // timer control register 4 Enhanced Timer Port Definitions

#define TMSK1 _P(0x4C) // timer interrupt mask 1 (TIE) #define TMSK2 _P(0x4D) // timer interrupt mask 2 (TSCR2) #define TFLG1 _P(0x4E) // timer flags 1 #define TFLG2 _P(0x4F) // timer flags 2 #define TC0 _LP(0x50) // timer capture/compare register 0 #define TC1 _LP(0x52) // timer capture/compare register 1 #define TC2 _LP(0x54) // timer capture/compare register 2 #define TC3 _LP(0x56) // timer capture/compare register 3 #define TC4 _LP(0x58) // timer capture/compare register 4 #define TC5 _LP(0x5A) // timer capture/compare register 5 #define TC6 _LP(0x5C) // timer capture/compare register 6 #define TC7 _LP(0x5E) // timer capture/compare register 7 Enhanced Timer Port Definitions

#define PACTL _P(0x60) // pulse accumulator controls #define PAFLG _P(0x61) // pulse accumulator flags #define PACN3 _P(0x62) // pulse accumulator counter 3 #define PACN2 _P(0x63) // pulse accumulator counter 2 #define PACN1 _P(0x64) // pulse accumulator counter 1 #define PACN0 _P(0x65) // pulse accumulator counter 0 #define MCCTL _P(0x66) // Modulus down conunter control #define MCFLG _P(0x67) // down counter flags #define ICPAR _P(0x68) // Input pulse accumulator control #define DLYCT _P(0x69) // Delay count to down counter #define ICOVW _P(0x6A) // Input control overwrite register #define ICSYS _P(0x6B) // Input control system control Enhanced Timer Port Definitions

#define TIMTST _P(0x6D) // timer test register #define PBCTL _P(0x70) // Pulse accumulator B control #define PBFLG _P(0x71) // Pulse accumulator B flags #define PA3H _P(0x72) // Pulse Accumulator holding register 3 #define PA2H _P(0x73) // Pulse Accumulator holding register 2 #define PA1H _P(0x74) // Pulse Accumulator holding register 1 #define PA0H _P(0x75) // Pulse Accumulator holding register 0 #define MCCNT _LP(0x76) // Modulus down counter register #define TCOH _P(0x78) // Capture 0 holding register #define TC1H _P(0x7A) // Capture 1 holding register #define TC2H _P(0x7C) // Capture 2 holding register #define TC3H _P(0x7E) // Capture 3 holding register Enhanced Timer Port Definitions

16-bit Transfers A full access for the counter registers or the input capture/output compare registers should take place in one clock cycle. Accessing high byte and low byte separately for all of these registers may not yield the same result as accessing them in one word.

16-bit Transfers #define _LP(x) *(unsigned int *)(x) unsigned int i; _LP(0x240) = i; i = _LP(0x240);