Sie sind auf Seite 1von 54

Timers

Group #1 Fall 2002

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Slide 1

Outline
Introduction
What is a timer? How does it work? Types

Timers and the HC11


Main Timer Output Compare Input Capture Periodic Interrupts Pulse Accumulators

Timer Applications and Examples


ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M. Slide 2

Introduction to Timers

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Slide 3

Introduction
What is a timer?
A timer is an integrated circuit that uses an oscillator measure elapsed time or count/time an external event

What is an oscillator?
Oscillators are used to generate a signal that can regenerate and sustain itself Usually made of quartz crystal
ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M. Slide 4

Introduction
How Does It Work?
Applying a direct current to the crystal causes it to vibrate The oscillators frequency is determined by the thickness and cut of the quartz The frequency can also be set by using ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M. combinations of
Metallic electrodes

Resonator plate substrate (the blank)

Slide 5

Timers
An external capacitor is used to determine the off-on time intervals of the output pulses for all IC timers The time required for the capacitor to charge to 63.7% of the applied voltage is the time

= R C
Slide 6

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Time Constant Example


Time Constant
The time constant of an RL circuit is also equal to the inductance divided by the resistance. It usually takes 5 time constants for the capacitor to charge to the applied voltage.

Example:
R = 1M C = 1 F = 1,000,000 ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M. 0.000001

Slide 7

Timer Types
Also Known As: Multivibrator
Multivibrators are a class of circuits that are designed to produce square waves or pulses.

There are 3 types of timers:


Monostable Multivibrators Astable Multivibrators Bistable Multivibrators
ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M. Slide 8

Timer Types: Monostable


A Monostable Multivibrator
A multivibrator with a single stable output state. When triggered, the output will switch to the unstable state for a set period of time, which is determined by the resistor and capacitor values. It remains in this state until a separate signal triggers the timer. One-Shot Pulse Output Delay On Output The duration of the output pulse, in seconds, is ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M. Slide 9 approximately

Timer Types: Monostable


Monostable Multivibrator Circuit

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Slide 10

Timer Types: Monostable


Missing Pulse Detector
Changes in the input pulse signal complete the timing cycle The monostable ON time is set approximately 1/3 longer than the expected time between trigger ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M. pulses

Slide 11

Timer Types: Astable


An Astable Multivibrator
A multivibrator that produces a square wave output from a DC voltage. Astable Output Pulses

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Slide 12

Timer Types: Astable


An Astable Multivibrator
The frequency of operation of the astable circuit is dependent upon the values of R1, R2, and C. f = 1/(ln(2) x C x (R1 + 2 x R2)) f = 1/T
ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M. f is in Hz. Slide R is in 13

Timer Types: Astable


Duty Cycle, D
The duty cycle is defined as the ratio of the time duration when output pulse is high to the total period

t2 R2+ R2 D= = t R2+ 2 2 R

t2= ln( ) ( R2+ R2) C 2 t2= ln( ) R2 C 2


ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M. Slide 14

Anybody recognize this?

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Slide 15

The 555 Timer


First introduced in the early 1970s the 555 timer is still widely used in many systems. It is composed of over:
20 transistors 15 resistors 2 diodes
ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M. Slide 16

The 555 Timer: Pin Functions


Pin 1 Ground
Connected directly to ground

Pin 2 Trigger
Input to the lower comparator and is used to set the latch which causes the output to go high Triggering happens when the voltage level is taken from above to below a voltage level of 1/3 V+
ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M. Slide 17

The 555 Timer: Pin Functions


Pin 3 Output
Output high is slightly lower (1.7V) than the supply voltage The state of the output pin will always reflect the inverse of the logic state of the latch

Pin 4 Reset
Used to reset the latch and return the output to a low state The reset voltage threshold is 0.7V+, and ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M. Slide 18 a sink current of 0.1mA

The 555 Timer: Pin Functions


Pin 5 Control Voltage
Direct access to the 2/3 V+ voltage divider point in voltage control mode Connect to ground through a 0.01 mF capacitor when not in use

Pin 6 Threshold
Input into the upper comparator Used to reset the latch, which drives the output low
ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M. Slide 19

The 555 Timer: Pin Functions


Pin 7 Discharge
When the npn transistor it is connected is turned on, the pin is shorted to ground The timing capacitor is usually between pin 7 and ground and is discharged when the transistor turns on

Pin 8 V+
The positive supply voltage terminal to the 555 timer IC ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M. Slide 20

The 555 Timer: Examples


Schmitt Trigger
The Schmitt trigger takes a noisy input and creates a very clean output. It uses the 1/3V+ and 2/3V+ thresholds to produce the wave output
ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M. Slide 21

The 555 Timer: Examples


A more stable timing output can be produced by adding a transistor and diode the the RC network The frequency can be varied over a wide range will using a constant 50% duty cycleSlide 22 ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

The 555 Timer: Examples


A one-shot monostable circuit that is continually retriggered by input pulses A missing pulse results in pin 3 going low until a new input pulse is received
ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M. Slide 23

The HC11 Timer

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Slide 24

Main Timer Block diagram

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Slide 25

Main Timer
16-bit free running counter

TCN T

Bit 15 Bit 7

Bit 8 Bit 0

$10 $10 0E 0F

It is a read only register using a double-byte instruction like LDD or LDX It is cleared on reset and count up continuously When $FFFF is reached, counter rolls over to $0000 ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M. Slide 26

Registers related to Main Timer


Timer counter (TCNT) Timer flag register 2 (TFLG2) TFLG $10 2 25 Timer mask register 2 (TMSK2)
PR1 PR0 $10 TMSK TOI 2 24 27 ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M. Slide
7 6 5 4 RTII PAOVI PAII 3 0 2 0 1 0 6 5 4 TOF RTIF PAOVF PAIF 7 3 0 2 0 1 0 0 0

Counter Prescaler
Allows 4 clocking rates Bus Frequency of the timer counter (E Clock) PR PR E-Clock rate divided Presc 2 MHz aler 1 0 by: 1, 4, 8, 16 Resolutio Range n At reset the default (Overflow (one ) 1 500 ns 32.77 0 0 prescale factor is 1 count) ms 2 s 1 0 Must be set during the 4 131.1 8 4 s 1 0 first 64 E-Clock cycles ms 16 8 s 1 1 after reset 262.1
ms 524.3 ms
ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M. Slide 28

Input Capture
Used to record the time an event occurs When an edge is detected at a timer input pin, the current value of the free-running counter is stored in the corresponding 16-bit input capture register
ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M. Slide 29

Can be read at any time by a pair of 8bits registers Not affected by reset

TIC1 TIC2 TIC3 TI4/O 5

Bit 15 Bit 7 Bit 15 Bit 7 Bit 15 Bit 7 Bit 15 Bit 7

Bit 8 Bit 0 Bit 8 Bit 0 Bit 8 Bit 0 Bit 8 Bit 0

$10 $10 10 11 $10 $10 12 13 $10 $10 14 15 $10 $10 1E 1F


Slide 30

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Use Time Control Register (TCTTL2) to tell which edges you want to capture
7 6 5 4 3 2 1 0 EDG4B EDG4A EDG1B EDG1A EDG2B EDG2A EDG3B EDG3A

TCTL 2

$1021

Configuration
Capture Disabled Capture on Rising Edge Only Capture on Falling Edge Only Capture on Any Edge

EDGxB EDGxA
0 0 1 1 0 1 0 1

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Slide 31

Input capture status flags are automatically set to one each time a selected 7 6 5 detected 1 0 edge is 4 3 2

IC1F IC3F TFLG1 OC1FOC2FOC3FOC4FOC5F IC2F $1023

Input capture interrupt enable bits

TMS K1

7 6 5 4 3 2 1 0 OC1IOC2IOC3IOC4IOC5IIC1IIC2IIC3I

$1022
Slide 32

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Output Compare
There are 5 output compare registers: TOC 1 TOC 2 TOC 3
Bit 15 Bit 7 Bit 15 Bit 7 Bit 15 Bit 7 Bit 15 Bit 7

TOC Bit 15 Bit 7 4 TI4/O ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Bit 8 Bit 0 Bit 8 Bit 0 Bit 8 Bit 0 Bit 8 Bit 0 Bit 8 Bit 0

$101 $101 6 7 $101 $101 8 9 $101 $101 A B $101 $101 C D $101 $101 E F Slide 33

Output capture status flags are automatically set to one each time the number stored in one of the compare registers is the same as the value of the main timer 6 5 4 3 2 1 0 7

TFLG 1
Output TMSK

OC1F OC2F OC3F OC4F OC5FIC1F IC2F IC3F

$10 23 $10 22 Slide 34

7 6 5 4 3 2 1 0 OC1IOC2IOC3IOC4IOC5I enable bits capture interruptIC1F IC2F IC3F

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

When a successful output compare occurs, pin actions on OC2-OC5 of Port A can OC1 OC1 OC1 OC1 OC1 OC1M be done: $100C
M7 M6 M5 M4 M3

OC1D

OC1 D7

OC1 D6

OC1 D5

OC1 D4

OC1 D3

$100D
OM OL 5 5

TCTL1 OM OL
2 2
OMx OLx 2 2 2 2 2 2 2 2

OM OL 3 3

OM OL 4 4

$1020

Configuration OCx Does Not Affect Pin (OC2Still May) Toggle Ocx Pin on Successful Compare Clear Ocx Pin on Successful Compare Set Ocx Pin on Successsful Compare

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Slide 35

Forced Output
If you need to change state of Port A Pin BEFORE output compare occurs use the Time Compare Force Register
FOC 1 FOC 2 FOC 3 FOC 4 FOC 5 0 0 0 CFOR C $100 B

Output Output Output Output Output Compa Compa Compa Compa Compa re 1 re 2 re 3 re 4 re 5

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Slide 36

Real-Time Interrupt
Generates hardware interrupts at a fixed B1 B0 periodic rate with: RTR RTR $1026 PACTL
RTR1 RTR0 E/213 Divided By 0 0 1 1 0 1 0 1 1 2 4 8 Nominal RTI Rate 4.10 ms 8.19 ms 16.38 ms 32.77 ms
Slide 37

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

TMSK2 $1024
Real-Time Interrupt Enable
B6

RTII

TFLG2 $1025
Real-Time Interrupt Flag

B6

RTIF

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Slide 38

Pulse Accumulator
8-bit Counter 2 operating modes
Event counting mode Gated time accumulation mode

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Slide 39

Registers
PACNT $1027
8 Bit PA Count
B7 B0

PACTL $1026
Data Direction for PA7 Pin B7 PA Enable DDR A7 PA Mode PA Edge
B6 PA EN B5 PA MOD B4 PA EDG E

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Slide 40

TFLG2 $1025
PA Overflow Flag PA Input Edge Flag

B4 B5 PA OVF PA IF

TMSK2 $1024
PA Overflow Interrupt Enable PA Input Edge Interrupt Enable
B5 PA OVI

B4 PA II

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Slide 41

Applications and Examples

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Slide 42

Applications of Timers
Timers are used for a variety of applications in many situations Required for any application requiring real-time control Can be used for both input and output timing Often multiple timers are needed for the various functions required for real-time control Timers are basically everywhere you look: watches, household appliances, ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M. Slide 43 manufacturing systems, basically

Applications of Timers
Pulse Width Modulation (PWM) is a very common method of generating an analog signal
Requires the use of timers as well as interrupts Can be used to run DC motors

Hardware interrupt timing is important for many timing applications


Reaction time or delay time between ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M. Slide 44 events

Pulse Width Modulation Example


PWM utilizes the output compare ability of the HC11 There are 5 usable output compare systems (OC1 OC5), with a 16-bit timer output register associated with each register x (TOCx) This value is used to compare with the free-running counter TCNT An interrupt is generated when these values are equal The interrupt changes the status of the ME 6405, Timers on theHutcherson, P. Michel, M. Slide 45 OCx pin Lecture, S. board

Pulse Width Modulation Example


Output Compare System TOC Register Location OC1 TOC1 OC2 TOC2 OC3 TOC3 OC4 TOC4 OC5 TOC5

$1016Axiom Board Pin 7 N/A

$1018- $101A- $101C- $101E9 B D F PA6 PA4 PA3 PA2

The OC1 system is unique and should not be used as the other output compare systems

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Slide 46

Pulse Width Modulation Example


To use the Output Compare:
The timer control register (TCTL1 @ $1020) must be set Make a copy of the count in the main timer (TCNT) Add the copied value to the desired delay time and store the sum in OCx register Finally, perform the action specified by the TCTL1 OM2 register OL2 OM3 OL3 OM4 OL4 OM5 OL5 TCTL1, $1020
OMx OLx 0 0 OCx does not affect pin 0 1 Toggle OCx pin on successful compare 1 0 Pull low the OCx pin on successful compare 1 1 Pull high the OCx pin on successful compare ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M. Slide 47

Pulse Width Modulation Example


Start

Example: Generate a 1KHz square wave form signal with a 40% duty cycle (width of high pulse is 40% of total period). A flowchart of the procedure is shown at left
Taken from: H. Huang, MC68HC11:AnIntroduction

Set OC2 pin to high Set OC2 action to toggle Clear OC2F flag start OC2 output compare with a delay of 400 s no OC2F = 1? yes Clear OC2F flag Start OC2 output compare with a delay of 600 s OC2F = 1? yes

no

Flowchart Of The Waveform Generation

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Slide 48

Pulse Width Modulation Example


regbas PORTA TOC2 TCNT TFLG1 toggle lotime hitime equ equ equ equ equ equ equ equ org ldx bset bclr ldaa staa ldd addd std brclr bclr ldd addd std $1000 $00 $18 $0E $23 $40 1200 800 $1040 #regbas PORTA,X TFLG1,X #toggle TCTL1,X TCNT,X #hitime TOC2,X TFLG1,X TFLG1,X TOC2,X #lotime TOC2,X ; ; ; ; ; ; ; ; base address of I/O register block offset of PORTA from regbas offset of TOC2 from regbas offset of TCNT from regbas offset of TFLG1 from regbas value to select the toggle action value to set low time to 600 ms (2 Mhz clock) value to set high time to 400 ms

$40 $BF

high

$40 high $BF

; ; ; ; ; ; ; ; ; ; : ; ;

set OC2 pint to high clear OC2F flag select output compare action to be toggle start an OC2 operation which toggles the OC2 pin with a delay of 800 E clock cycles wait until OC2F is set to 1 clear OC2F flag start another OC2 operation which toggles the OC2 pin with a delay of 1200 E cycles

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Slide 49

Pulse Width Modulation Example


low brclr bclr ldd addd std bra end TFLG1,X $40 low TFLG1,X $BF TOC2,X #hitime TOC2,X high ; ; ; : ; ; wait until OC2F is set to 1 clear OC2F flag start another OC2 operation which toggles the OC2 pin with a delay of 800 E cycles

Original code taken from: H. Huang, MC68HC11:AnIntroduction Modified for the Axiom CME-11E9-EVBU

It can be seen with more modification, a varying duty cycle can be used, such as one that generates a sine wave

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Slide 50

LED Flashing Example


This example will show the versatility of the timer subsystem of the HC11 microcontroller It will flash a series of 8 LEDs in a regular pattern The Program is written in C and uses the hc11e9.h header A schematic of the circuit is shown below

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Slide 51

LED Flashing Example


#include <hc11e9.h> void delay(char delay_time); void main() { _H11TFLG1 = 0x80; /*clear OC1F flag */ TOC1 = TCNT + 20000; /*Short 10 ms delay */ while(1) { unsigned char flash_tab[25][2] = {{0xFF,25}, {0x00,25}, {0xFF,25}, {0xFF,25}, {0x00,25}, {0xFF,25}, {0x00,25}, {0x80,100}, {0x40,100}, {0x20,100}, {0x10,100}, {0x08,100}, {0x04,100}, {0x04,100}, {0x02,100}, {0x01,100}, {0x01,25}, {0x02,100}, {0x04,100}, {0x08,100}, {0x10,100}, {0x20,100}, {0x40,100}, {0x80,100}, {0x00,100}}; int i; for (i=0; i<25; i++) { _H11PORTB = flash_tab[i][0]; */ delay(flash_tab[i][1]); } } }

/*Store the value in Port B

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Slide 52

LED Flashing Example


This function creates a delay of a number of milliseconds
void delay(char delay_time) { _H11TFLG1 = 0x40; _H11TOC2 = _H11TCNT + 20000; while(delay_time) { while(!(_H11TFLG1 & 0x40)){}; _H11TFLG1 = 0x40; --delay_time; _H11TOC2 += 20000; } }

ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M.

Slide 53

References
van Roon, Tony. 555 Timer Tutorial. http://www.uoguelph.ca/~antoon/ga dgets/555/555.html Huang, Han. MC68HC11:An Introduction Spasov, Peter. Microcontroller Technology: The 68HC11
ME 6405, Timers Lecture, S. Hutcherson, P. Michel, M. Slide 54

Das könnte Ihnen auch gefallen