Title: Overview of Lecture
1Overview of Lecture
- Timer functions
- Output-compare function
- Pulse/square-wave using output-compare function
- Programming example
2(No Transcript)
3HC11 Timer Function
- Many application requires dedicated timer
functions - Input-capture function to measure duration of
pulse or period of square wave - Input-capture function to measure duty cycle
periodic waveform - Output-compare function to create time delay
- Output-compare function to generate a pulse or
square waveform - Pulse accumulator to count number of events
- Time of day tracking
- Periodic interrupt generation to remind processor
to perform routine tasks - Waveform generation
4HC11 Timer Function
- HC11 includes sophisticated timer functions
- Free-running main timer
- Input-capture functions
- Output-compare functions
- Real-time Interrupt (RTI)
- Pulse accumulator
5Free-running main timer
- Main timer (TCNT) clocked by output of a
four-stage pre-scaler (divided by 1, 4, 8 and
16 and driven by E clock signal) - Timer is cleared to 0 during a reset and is a
read only register - When count changes from FFFF to 0000, timer
overflow flag (TOF) bit in timer flag register 2
(TFLG2) is set - Timer overflow flag can be cleared by writing a 1
to it - Interrupt is enabled by setting timer overflow
interrupt enable bit (TOI) of timer interrupt
mask register 2 (TMSK2) - Pre-scale factor is for timer is selected by bits
0 and 1 of TMSK2 - Timer counter (TCNT) register is meant to be read
using a double-byte read instruction, e.g. LDD,
LDX
6Free-running main timer cont
- TCNT can be used to keep track of time
- TCNT can also be used for time delays as well
- Example
- 10-ms delay at 2-MHz clock rate is equivalent to
20K clock cycles - By adding 20K to the current value of TCNT and
waiting until this value is smaller than
incrementing TCNT, a 10ms delay is created - TCNT EQU 0E Values defined
- LDX 1000
- LDD TCNT,X
- ADDD 20000
- Loop CPD TCNT,X Compare Acc D with 16-bit
value at address - BGT Loop Branch if greater than zero
7Input-capture functions
- HC11 timer system has three input-capture
channels - Each input-capture channel includes
- a 16-bit input-capture register
- Input-edge detection logic
- Interrupt-generation logic
- Physical time is represented by the count in
16-bit free-running timer counter TCNT - Input-capture function is used to record time
when an external event occurs - When a selected edge is detected at input pin,
contents of TCNT are latched into three
input-capture registers - Captured 16-bit timer values is stored in
input-capture register these are - TIC1, TIC2 and TIC3 located at 1010-1011,
1012-1013 and 1014-1015 respectively
8Input-capture functions cont
- Pins PA0, PA1 and PA2 are used as input-capture
channels IC1, IC2 and IC3 respectively - User can select signal edge (rising, falling or
both) to capture by programming 6-bit TCTL2
register located at address 1021
Figure 7.2 Input-capture function
9Input-capture functions cont
- Example
- PA0 pin used as input-capture channel IC3. To
capture rising edge, the edge select bits (bits 1
and 0 of TCTL2) of the channel IC3 must be set to
01. Following instruction will set up TCTL2
capture time of a rising edge on PA0. - LDX 1000
- BCLR TCTL2, X 02 clear bit 1 0 without
affecting other bits - BSET TCTL2, X 01 set bit 0 to 1 without
affecting other bits
10Output-Compare Function
- Pins PA3-PA6 are used as output-compare channels
11Output-Compare Function Cont
- HC11 has 5 output-compare functions
- Control level on output pins PA7-PA3
- Each output-compare function consists of
- A 16-bit comparator
- A 16-bit compare register TOCx
- An output action pin OCx
- An interrupt request circuit
- A forced-compare function FOCx
- Control logic
12Operation of Output-Compare Function
- Main application of output-compare function
- To toggle a signal
- Turn on a switch etc
- Technique to use output-compare function
- Makes a copy of the current contents of timer
(TCNT) - Adds to this copy a value equal to the desired
delay - Stores the sum into an output-compare register
- User specifies action to be activated on selected
output-compare OCx pin by programming TCTL1
register
13Operation of Output-Compare Function cont
- TCTL1 register has the following format
- OMx OLx
- --------------------------------------------------
----------------------------- - 0 0 OCx does not affect pin
- 0 1 Toggle OCx pin on successful compare
- 1 0 Clear OCx on successful compare
- 1 1 Set OCx pin on successful compare
- --------------------------------------------------
----------------------------- - Compare value at TCNT
- Set corresponding flag bit in TFLG1 register
- Output-compare interrupt enabled by TMSK1 register
14Programming Example
- Generate an active high 1 KHz digital waveform
with 40 duty cycle from output-compare pin OC2.
Use the polling method to check the success of
the output compare. The frequency of the E clock
is 2MHz, and the pre-scale factor to the
free-running timer is 1. - An active high 1KHz waveform with 40 duty cycle
looks like this
15Duty Cycle
- Duty cycle is the percent of time that the signal
is high within a period of waveform - Example
- Duty cycle T1/T100
16C language version of the program
include lthc11.hgt include ltstdio.hgt main()
PORTA 0x40 //set OC2 pin high TCTL1
0x40 //select toggle as OC2 pin action
TOC2TCNT800 //start OC2 compare with 800
cycles //For 2Mhz E clock, 800 and 1200 clock
cycles correspond to delays of 400µs and //600µs
respectively TFLG10x40 //clear OC2F
flag while(1) while(!(TFLG1 0x40)) //wait
unit 800 cycles is over TFLG10x40 //clear
OC2F flag TOC2TOC21200 //start next
operation while(!(TFLG1 0x40)) //wait until
1200 cycles FLG10x40 TOC2TOC2800 re
turn 0
17Tutorial 6-7