1 / 43

ME 4447/6405

Microprocessor Control of Manufacturing Systems and Introduction to Mechatronics Instructor: Professor Charles Ume Timers. ME 4447/6405. The Timer Sub-system Includes these Distinct Features: 16-bit Counter Seven-Stage Programmable Prescaling Eight Input Capture Channels

edison
Download Presentation

ME 4447/6405

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Microprocessor Control of Manufacturing Systems and Introduction to Mechatronics Instructor: Professor Charles Ume Timers ME 4447/6405

  2. The Timer Sub-system Includes these Distinct Features: 16-bit Counter Seven-Stage Programmable Prescaling Eight Input Capture Channels Eight Output Compare Channels 16-bit Pulse Accumulator Lecture Outline

  3. Look at Page 31 in Reference Manual for Actual Register Addresses Central element:16-bit free running counter At reset counter is disabled Once enabled, counter starts from $0000 and counts up continuously When $FFFF is reached, counter rolls over to $0000 Cannot be written to during operation (only writable in test mode) May be reset upon successful Output Compare 7 General Description of Main Timer Bit 15 - - - - - - Bit 8 $0044 TCNT $0045 Bit 7 - - - - - - Bit 0

  4. Timer Enable (TEN) bit determines operation of the timer and counter Writing a 1 to TEN turns on the counter, writing a 0 disables the system, reducing power consumption 7 6 5 4 3 2 1 0 TSCR1 TEN TSWAI TSFRZ TFFCA 0 0 0 0 $0046 Enabling the Free-Running Counter

  5. Allows 8 clocking rates of the timer counter E-Clock rate divided by: 1, 2, 4, 8, 16, 32, 64, 128 At reset the default prescale factor is 1 Prescale may be changed at any time Will take effect after some number of clock cycles where all prescale counter stages equal zero. (see p. 450 of Family Reference Manual for details) Counter Prescaler

  6. Trade-off between timer resolution and timer range 7 6 5 4 3 2 1 0 $004D TSCR2 TOI 0 0 0 TCRE PR2 PR1 PR0 Changing the Counter Prescaler

  7. Timer overflow flag (TOF) status bit set each time the counter rolls over from $FFFF to $0000 TOF status bit can generate an automatic interrupt request by setting the timer overflow interrupt (TOI) enable bit 7 6 5 4 3 2 1 0 TFLG2 TOF 0 0 0 0 0 0 0 $004F 7 6 5 4 3 2 1 0 TSCR2 TOI 0 0 0 TCRE PR2 PR1 PR0 $004D Counter Overflows

  8. Each of the eight I/O pins of Port T may be used as either an input capture or an output compare If IOSX is 0, the corresponding channel acts as an input capture If IOSX is 1, the corresponding channel acts as an output compare Input Capture Vs. Output Compare TIOS $0040

  9. Used to record the time an event occurs When an input signal is received, the time is stored in memory by capturing the contents of the free-running counter Input Capture Concept

  10. 16-bit registers Input edge-detection logic Interrupt generation logic Features of Input Capture Function

  11. Eight 16-bit input capture registers are available Each register has a corresponding timer input pin (TC0-TC7) located on Port T pins PT0-PT7 When an edge is detected at a timer input pin, the current value of the free-running counter is stored in the corresponding input capture register Input Capture Registers

  12. Can be read at any time as a pair of 8-bit registers using instructions like LDD or LDX Writing to register when used as Input Capture has no meaning Input Capture Registers (cont’d) Bit 15 - - - - - - Bit 8 $0050 TC0 $0051 Bit 7 - - - - - - Bit 0 … Bit 15 - - - - - - Bit 8 $005E TC7 $005F Bit 7 - - - - - - Bit 0

  13. Used to select which edge of an input is detected 7 7 6 6 5 5 4 4 3 3 2 2 1 1 0 0 TCTL4 TCTL3 EDG3B EDG7B EDG3A EDG7A EDG2B EDG6B EDG6A EDG2A EDG5B EDG1B EDG1A EDG5A EDG0B EDG4B EDG0A EDG4A $004B $004A Input Edge-Detection Logic

  14. Input capture status flags are automatically set to one each time a selected edge is detected Input capture interrupt enable bits If CXI is 1when CXF is set, interrupt condition is met 7 7 6 6 5 5 4 4 3 3 2 2 1 1 0 0 TIE TFLG1 C7F C7I C6I C6F C5I C5F C4F C4I C3F C3I C2I C2F C1I C1F C0I C0F $004E $004C Interrupt Generation Logic

  15. Period or frequency measurement Capture the time of two successive rising or falling edges Pulse width measurement Capture the time between two adjacent edges Application of Input Captures

  16. FIRST EQU $2000 DEFINE A 2-BYTE LOCATION TO STORE FIRST EDGE PERIOD EQU $2002 DEFINE A 2-BYTE LOCATION TO STORE PERIOD ORG $1000 LDAA #$80 STAA $0046 ENABLES FREE RUNNING COUNTER LDX #$0000 LDAA #$00 ENSURES PT1 IS USED AS AN INPUT CAPTURE, TIOS STAA $0040 NOTE: THIS IS THE CASE BY DEFAULT LDAA #$04 STAA $004B EDGE DETECTION FOR IC1 in TCTL4 SET TO RISING EDGES LDAA #$02 STAA $004E CLEARS ANY OLD FLAG FROM IC1F in TFLG1 LOOP1 BRCLR $4E #$02 LOOP1 LOOP HERE UNTIL FIRST RISING EDGE IS DETECTED LDD $0052 READ TIME OF FIRST CAPTURE in TC1 (Hi) STD FIRST STORE FIRST CAPTURE VALUE LDAA #$02 STAA $004E CLEAR THE IC1F FLAG BEFORE NEXT EDGE LOOP2 BRCLR $4E #$02 LOOP2 LOOP HERE UNTIL NEXT RISING EDGE IS DETECTED LDD $0052 READ TIME OF SECOND CAPTURE SUBD FIRST FIND THE TIME DIFFERENCE BEWTEEN EDGES STD PERIOD STORE THE RESULT AS THE PERIOD (cycles) : : : Short Period Measurement Code

  17. Extending the range of the 16-bit counter with an 8-bit software counter Software keeps track of counter overflows Creates a 24-bit counter (16-bit + 8-bit) Time values are stored as 3-byte numbers Measuring Long Periods Using Counter Overflow

  18. Can be used as a time reference for an output function. Input Capture records the event time Offset representing time delay is added to the input capture and stored to an output compare. Both input captures and output compares are referenced from the same counter, so software latencies do not affect the accuracy to time delay Application of Input Captures (cont’)

  19. Can be used as general purpose I/O pins when the timer functions are not needed Logic levels can be read even if the input-capture function is enabled Can serve as flexible interrupt input pins Have some advantages over the IRQ pin Other Uses of Input Capture Pins

  20. Output Compare: Basic Concept •16 Bit Register Stores a Number …8 possible Registers to store this number: •Comparator checks Number against Free Running Counter (TCNT Register) …Really 8 comparators-one for each channel …This is done in hardware, no processor time used •When Counter matches TCx Register, it triggers an event

  21. What “Event” is triggered? • Three Non-Exclusive Possibilities: • Changes the output of one or several of the pins in Port T • Set a Flag in TFLG Register • Cause an Interrupt

  22. Output Compare 0 PT0 Output Compare 1 PT1 Output Compare 2 PT2 Output Compare 3 PT3 Output Compare 4 PT4 Output Compare 5 PT5 Output Compare 6 PT6 Output Compare causes Port T Pins to change state (Part I) • Output Compares 0 to 6: • Each Output compare controls a SINGLE PIN:

  23. Output Compare causes Port T Pins to change state (Part II) • Output Compares 0 to 6: • TCTL1 ($0048) and TCTL2 ($0049) Registers Control How Each Pin Changes TCTL1 $1020

  24. Output Compare causes Port T Pins to change state (Part II)

  25. OC7M7 OC7D7 OC7M6 OC7D6 OC7M5 OC7D5 OC7M4 OC7D4 OC7D3 OC7M3 OC7M2 OC7D2 OC7D1 OC7M1 OC7D0 OC7M0 Output Compare causes Port T Pins to change state (Part III) • Output Compare 7: • Causes 8 Port T pins to change simultaneously • Notice PT0-PT6 are also used by Output Compares 0-6 Output Compare 7 Mask Register (OC7M) determines which Port T Pins will be Controlled by Successful Output Compare 7 PT0 PT2 PT1 PT7 PT6 PT5 PT4 PT3 OC7M $0042 OC7D Register sets value to be written to Port T pins selected in OC7M Register OC7D $0043

  26. 7 6 5 4 3 2 1 0 TFLG1 C7F C6F C5F C4F C3F C2F C1F C0F $004E Output Compare Causes Flag to Be Set (Part I) When Output Compare is successful it sets corresponding Flag in TFLG1 Control Register: Software must constantly poll TFG1 register to check for flags

  27. Output Compare Causes Flag to be Set part (II) You clear the Flag by writing a 1 to the corresponding Bit in TFLG1 Reg. You Must clear the Flag after it is set • Use LDAA, STA commands to write 1 to Flag But DON’T USE BSET!!!!

  28. 7 6 5 4 3 2 1 0 TIE C7I C6I C5I C4I C3I C2I C1I C0I $004C Output Compare Causes an Interrupt Successful Output compare will cause an interrupt when corresponding bit in TIE Register is set:

  29. Measuring Long Intervals: Problems with Overflow Bit You must measure this length of time t Record Start Time Record End Time time t But I only have this much time to record last overflow …then I need to record the end time! This is where timer rolls over (overflow) t=[TEND-TSTART]+(# of overflows)*(Toverflow)

  30. CFORC $0041 FOC7 FOC6 FOC5 FOC4 FOC3 FOC2 FOC1 FOC0 Forced Output If you need to change state of Port T Pin BEFORE output compare occurs Use Forced Output…software triggers compare to occur and Pin T will change state accordingly

  31. Uses PT7 for inputs 16-bit Counter Incremented by: edge on pin IOC7 (PT7) logic level of pin IOC7 Used to: Count number of events: Event Counting Mode Measure duration of pulse: Gated Time Accumulation Mode Pulse Accumulator Overview

  32. Can be read or written to at any time 2 Modes Event Counter Gated Time Accumulation Pulse Accumulator Input Pin: Port T Pin 7 Registers Key Things to Know

  33. PACNT $0062 & $0063 16 Bit PA Count PACTL $0060 PAFLG $0061 B15 B8 B7 B0 Pulse Accumulator Registers

  34. PAEN: 0 = Disabled, 1 = Enabled PAMOD: 0 = Event Counter, 1 = Gated Time Register Settings

  35. CLK[0:1]: Timer Clock Selection PAOVF: 0 = No Overflow 1 = Overflow Flag Flag bit is cleared with a write to PAFLG register with bit 1 set PAOVI: 0 = Overflow interrupt inhibited 1 = Overflow Interrupt enabled PAIF: 0 = No input Edge Interrupt 1 = Input Edge Interrupt Flag Set when selected edge is detected at IOC7 input pin. Event edge triggers PAIF. Trailing edge of gated signal at IOC7 input pin triggers PAIF. PAI: 0 = Input edge interrupt inhibited 1 = Input edge interrupt enabled Register Settings

  36. PAMOD = 0 Counts Active Edge of PAI pin Example: (PACNT = 0; PAEN = 1; PEDGE = 1: Rising Edge) PAI PT7/ IOC7 16-BIT COUNTER PACNT Value 1 2 3 4 PACNT Event Counting Mode

  37. A light emitter/detector pair can be used in an assembly line to count the number of parts going by. Event Counting Example

  38. PAMOD = 1 Free-running bus clock divided by 64 Subject to PT7/IOC7 pin being active bus/64 CLOCK (from Main Timer) Clock PT7/ IOC7 AND 16-BIT COUNTER PACNT Gated Time Accumulation Mode

  39. PACNT = 0; PAEN = 1; PEDGE = 1 PEDGE = 1 (Trailing Rising Edge) (inhibit counting when PT7/IOC7 is 1) bus/64 PT7 PACNT Value 1 2 3 4 5 6 Gated Time Example

  40. Common use of Gated mode Measure duration of single pulses - period Easier than with Input Capture Counter is zero before pulse starts After pulse, pulse time directly read (need starting and ending count for input capture) Pulse Width Measurement

  41. Gated Time Accumulation (PAMOD = 1) Set Pulse Accumulator to interrupt after 5ms Steps: Calculate time for one bus/64 cycle Divide delay by time for one bus/64 cycle Take 2’s complement and store in PACNT When input goes to active level, counter will increment until overflow Example:Interrupt at Specified Time

  42. LDAA #$01 STAA PAFLG ;Clear PAIF by writing 1 to it LDAA #$41 ;PAEN = 1, PAMOD = 0, PEDGE = 0, PAI = 1 STAA PACTL Assembly Code: Initialization to Count Negative Edges

  43. Questions???

More Related