Datasheet

Using the Output Compare Function to Generate a Square Wave
MC68HC812A4 Data Sheet, Rev. 7
Freescale Semiconductor 141
12.11 Using the Output Compare Function to Generate a Square Wave
This timer exercise is intended to utilize the output compare function to generate a square wave of
predetermined duty cycle and frequency.
Square wave frequency 1000 Hz, duty cycle 50%
The program generates a square wave, 50 percent duty cycle, on output compare 2 (OC2). The signal
will be measured by the M68HC11 on the UDLP1 board. It assumes a 8.0 MHz operating frequency for
the E clock. The control registers are initialized to disable interrupts, configure for proper pin control action
and also the TC2H register for desired compare value. The appropiate count must be calculated to
achieve the desired frequency and duty cycle. For example: for a 50 percent duty, 1 kHz signal each
period must consist of 2048 counts or 1024 counts high and 1024 counts low. In essence a $0400 is
added to generate a frequency of 1 kHz.
12.11.1 Sample Calculation to Obtain Period Counts
The sample calculation to obtain period counts is:
For 1000 Hz frequency:
E-clock = 8 MHz
IC/OC resolution factor = 1/(E-clock/prescaler)
If the prescaler = 4, then output compare resolution is 0.5 µs
For a 1 kHz, 50 percent duty cycle:
1/F = T = 1/1000 = 1 ms
F for output compare = prescaler/E clock = 2 MHz
Figure 12-27. Example Waveform
12.11.2 Equipment
For this exercise, use the M68HC812A4EVB emulation board.
12.11.3 Code Listing
NOTE
A comment line is delimited by a semicolon. If there is no code before
comment, a semicolon (;) must be placed in the first column to avoid
assembly errors.
0.5 ms
NUMBER OF CLOCKS = F * D
1 ms
THEREFORE,
# CLOCKS = (2 MHz) * (0.5 ms) = 1024 = $0400