Output Compare Timer Function: Generating Desired Frequencies with a Microcontroller - Pro, Study notes of Mechanical Engineering

An example of using a microcontroller's timer function as an output compare (oc) device to generate desired frequencies. The example covers setting the omx and olx bits, detecting tocx matches, and adjusting frequency accuracy. Users will learn how to determine the low and high bounds of the frequencies that can be generated.

Typology: Study notes

Pre 2010

Uploaded on 10/01/2009

koofers-user-sjm-1
koofers-user-sjm-1 🇺🇸

10 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
EMCH 367 Fundamentals of Microcontrollers Example OUTPUT COMPARE TIMER FUNCTION
EXAMPLE OUTPUT COMPARE TIMER FUNCTION
OBJECTIVE
This example has the following objectives:
Review the use of MCU Timer function as an Output Compare (OC) device
Review the setting of OMx and OLx bits to select a desired OC event (in this example, we set
OM3=0, OL3=1, to generate a toggle on the OC3 pin)
Review the detection of the TOCx match with TNCT and the corresponding OC action
Present the correlation between delay, DT, with actual frequency of the square wave.
Explore the accuracy with which frequency can be adjusted
Explore the determination of low and high bounds on the frequencies that can be generated with
the MCU
t
t
t
t
L
H
L
H
t
V
Logical 1, +5 V
Logical 0, 0 V
Figure 1 Square wave schematics showing the half-wave duration, t, and the low (L) and high (H)
states.
PROGRAM EX_OC
This program is an example of timer output compare. The program runs freely and generates repeated
toggles of the output compare pin OC3 (PA5) at equal intervals that are determined using the delay
variable DT. Thus, a square wave is generated. The program instructions are given in the first column
below.
FLOWCHART AND CODE
The program flowchart is show to the right of the program instructions. Note the variable definition block
in which DT is defined. Next, the initialization block contains reg. X initialization and timer OC
initialization. The program stores the current time (TCNT) plus the delay DT in the OC3 timer TOC3. A
loop is entered in which the OC3 flag, OC3F is checked. When OC3F is set, the loop is exited. The
OC3F is reset, and the value of TOC3 is updated with the current time plus the delay. Then, the
program loops back.
The program loops back to the beginning and waits for a new keystroke to restart the process.
The essential code for the program is shown to the right of the program flowchart. This essential code
was incorporated into the standard asm template to generate the file Ex_OC.asm.
Dr. Victor Giurgiutiu Page 111/30/2020
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Output Compare Timer Function: Generating Desired Frequencies with a Microcontroller - Pro and more Study notes Mechanical Engineering in PDF only on Docsity!

EXAMPLE OUTPUT COMPARE TIMER FUNCTION

OBJECTIVE

This example has the following objectives:

 Review the use of MCU Timer function as an Output Compare (OC) device

 Review the setting of OMx and OLx bits to select a desired OC event (in this example, we set

OM3=0, OL3=1, to generate a toggle on the OC3 pin)

 Review the detection of the TOCx match with TNCT and the corresponding OC action

 Present the correlation between delay, DT, with actual frequency of the square wave.

 Explore the accuracy with which frequency can be adjusted

 Explore the determination of low and high bounds on the frequencies that can be generated with

the MCU

t

t

t

t

L H L H

t

V

Logical 1, +5 V

Logical 0, 0 V

Figure 1 Square wave schematics showing the half-wave duration,t, and the low (L) and high (H)

states.

PROGRAM EX_OC

This program is an example of timer output compare. The program runs freely and generates repeated

toggles of the output compare pin OC3 (PA5) at equal intervals that are determined using the delay

variable DT. Thus, a square wave is generated. The program instructions are given in the first column

below.

FLOWCHART AND CODE

The program flowchart is show to the right of the program instructions. Note the variable definition block

in which DT is defined. Next, the initialization block contains reg. X initialization and timer OC

initialization. The program stores the current time (TCNT) plus the delay DT in the OC3 timer TOC3. A

loop is entered in which the OC3 flag, OC3F is checked. When OC3F is set, the loop is exited. The

OC3F is reset, and the value of TOC3 is updated with the current time plus the delay. Then, the

program loops back.

The program loops back to the beginning and waits for a new keystroke to restart the process.

The essential code for the program is shown to the right of the program flowchart. This essential code

was incorporated into the standard asm template to generate the file Ex_OC.asm.

Instructions

i) * Define variable

 Half wave duration DT = 2 bytes

ii) Initialize

 Initialize index X to REGBAS

 Initialize timer OC3 function:

set to toggle (OM3=0, OL3=1)

iii) Store initial time + DT in TOC

iv) Wait and loop until OC3F is set;

when OC3F set, proceed

v) Reset OC3F

vi) Update TOC3: add DT to current

time and store in TOC

vii) Branch back iv)

Flowchart

Define variables:

 Half wave duration DT = 2 bytes

SWI

 Initialize index X to REGBAS

 Initialize timer OC3 function:

set to toggle (OM3=0,

OL3=1)

START

Store TCNT+DT in TOC

Check if OC3F is set

LABEL

Reset OC3F

Store TCNT+DT in TOC

Code

ORG DATA

DT RMB 2

ORG PROGRAM

START LDX #REGBAS

LDAA #%

STAA TCTL1,X

LDD DT

ADDD TCNT,X

STD TOC3,X

LABEL1 LDAA TFLG1,X

ANDA #%

BEQ LABEL

LDAA #%

STAA TFLG1,X

LDD DT

ADDD TCNT,X

STD TOC3,X

BRA LABEL

SWI

c) Step further. Note that the value in TCNT is added with DT and stored in TOC3. At this point,

your screen looks like this

d) Step further. The program goes through the loop on LABEL1. As long as TCNT is less than

TOC3, the bit OC3F in TFLG1 (bit 5) remains zero, and the program loops.

e) When the value of TCNT exceeds TOC3, the bit OC3F gets set. Simultaneously, the output

compare function is activated. Recall that our program was set to toggle the value in pin OC

when output compare condition is met. Look in the Port A pins window. Notice that the pin OC

(PA5) is set. Your screen looks like this:

f) At this point, the condition for exiting the loop is met, and, when stepping further, the loop is

exited. Your program gets to $c013. The mask for OC3F is stored in TFLG1 and OC3F gets

reset. Your screen is:

Note that the clock cycles are 62 and the simulation time is 31 s. These values are entered in Table 1.

j) Run again through the program. At the next breakpoint, your screen looks like this:

Note that the clock cycles are 113 and the simulation time is 56.5 s. These values are entered again in

Table 1.

k) Run further through your program, and after each breakpoint stop, record the clock cycles and

the simulated time in Table 1. Do this until you have completed 6 L-H groups. These correspond

to 6 square waves.

l) Calculate the half-wave time, t, and the equivalent frequency, f=1/2t. (When doing this, omit

the first row in your readings.) Note that the time interval oscillates between 25 and 26 s, while

the frequency oscillates between 20.0 and 19.2 kHz, i.e., 19.9±0.2 kHz, or 19.9±1% kHz. These

oscillations are an indication of the repeatability of your process, and are due to the displayed

simulation time being limited to 1 s, while one MCU cycle takes 0.5 s.

m) Calculate the raw frequency corresponding to the delay $0020 using the formula

Hz

DT s

f raw

. Notice that the raw frequency is 31.25 kHz, while the obtained

frequency is 20 kHz. The difference is a programming overhead due to the extra cycles that are

consumed in the program between two OC events. We expect that this overhead will become

less and less significant as the delay increases, i.e. at lower frequencies.

n) Put the value $0200 in DT. Repeat the procedure above, and enter the clock cycles and simulate

time values in Table 1. Note that the effective frequency oscillated between 1.859 and 1.

kHz, i.e., 1.8625±0.0035 kHz, or 1.8625±0.2%. The repeatability of our simulation has greatly

improved. Calculate the raw frequency corresponding to the delay $0200 using the formula

Hz

DT s

f raw

. Notice that the raw frequency is 2.0 kHz, while the effective frequency

is 1.8625 kHz. The difference has reduced considerably, (-6.9% error). This confirms the

hypothesis that the programming overhead due to the extra cycles that are consumed in the

program between two OC events is less significant at higher DT values, i.e. at lower frequencies.

o) Repeat for DT=$2000. For this long delay, the frequency scale is switched from kHz to just Hz.

In just a couple of cycles, you note stable behavior, and perfect frequency accuracy. This is due

to the desired frequency (122 Hz) being much lower than the MCU clock frequency (2 MHz).

For more details of how these errors are generated in the MCU, please see Appendix 4.

GENERATING A DESIRED FREQUENCY

We will now practice to obtain a desired frequency (Table 2). We will start with a low frequency, e.g.,

100 Hz, since we have noticed that our square wave generation behaves much better at low

frequencies. Then, we will increase the desired frequency, until a practical limit is obtained.

a) Examine the row 100 Hz in Table 2. Notice that the corresponding half-period is 5000 s. Hence,

a raw estimation of the delay can be calculated with the formula

delay = half-period/0.5, and then converted to hex. The result is entered as

Raw delay in Table 2.

b) Enter the DT = $2710 into the THRSim11, reset, and run. Note that the time of the breakpoint

stop is 5012 s. Run further, and note the second time as 10024 s. These values are entered in

Table 2. The resulting half-wave duration is 5012 s, which corresponds to 99.8 Hz, i.e. a -0.2%

error. We consider this good enough and select to stop here.

c) Try to generate 200 Hz. According to Table 2, the corresponding half-period is

2500 s, and the raw delay is $1388. Since we know that the MCU adds some extra cycles of its

own, we select to round down the raw delay to the value $1380. Enter this value in the

THRSim11, reset, and run. Record the times after first and second loop. These times are

entered in Table 2 as 2510 s and 5020 s, respectively. The corresponding frequency is 199.

kHz, i.e., -0.4% in error. We improve on this error by making a minor adjustment to the DT from

$1380 to $1370. The value $1370 gives us the desired frequency of 200 Hz exactly.

d) Try to generate 500 Hz. According to Table 2, the corresponding half-period is

1000 s, and the raw delay is $07D0. Enter this value in the THRSim11, reset, and run. Record

the times after first and second loop, and enter them in Table 2 as

1012 s and 2023 s, respectively. The corresponding frequency is 495 kHz, i.e.,

-1.1% in error. We improve on this error by making a minor adjustment to the DT, from $07d0 to

$07c0. The value $07c0 gives us the frequency 499 Hz, which is only –0.3% in error. We select

to stop here.

e) Try to generate 1000 Hz and 2000 Hz. Follow Table 2, for the values of the corresponding half-

period, raw delay, and actual DT iterations. Notice how accuracy is being improved by small

adjustments in second hex digit of the DT.

f) Try to generate 5000 Hz. According to Table 2, the corresponding half-period is

100 s, and the raw delay is $00C8. This is very small delay, and we expect some difficulties!

Enter this value in the THRSim11, reset, and run. Record the times after first and second loop,

and enter them in Table 2 as 112 s and 223 s, respectively. The corresponding frequency is

4505 kHz, i.e., -9.9% in error. We try to improve on this error by making minor adjustments to the

DT, from $00c8 to $00c0, and then to $00b8, $00b0, $00b4. However, the accuracy cannot be

reduced below 2%. This indicates that we have reached a limit in our capability to fine-tune the

frequency. This limit is due to the low value of the desired DT. We select to stop here, and

conclude that 5000 Hz (5 kHz) is a practical limit in the square-wave frequency that can be

generated with the MCU within 2% accuracy.

For more details of how these errors are generated in the MCU, please see Appendix 4.

Table 2

Desired f Half-period Raw Delay DT Time,

s f=1/

t error %

Hz

s hex hex t_1 t_

t Hz

500 1000 07D0 07d0 1012 2023 1011 495 -1.1%

07c0 1007 2010 1003 499 -0.3%

1000 500 03E8 03e0 508 1015 507 986 -1.4%

03d0 503 1002 499 1002 0.2%

2000 250 01F4 01f0 260 520 260 1923 -3.8%

01e0 256 506 250 2000 0.0%

5000 100 00C8 00c8 112 223 111 4505 -9.9%

00c0 112 218 106 4717 -5.7%

00b8 108 210 102 4902 -2.0%

00b0 103 200 97 5155 3.1%

00b4 103 205 102 4902 -2.0%

WHAT YOU HAVE LEARNED

In this example, you have learned:

 The use of MCU Timer function as an Output Compare (OC) device

 The setting of OMx and OLx bits to select a desired OC event (in this example, we set OM3=0,

OL3=1, to generate a toggle on the OC3 pin)

 The detection of the TOCx match with TNCT and the corresponding OC action

 The correlation between delay, DT, with actual frequency of the square wave.

 The accuracy with which frequency can be adjusted

 The determination of low and high bounds on the frequencies that can be generated with the

MCU

EMCH 367 Fundamentals of Microcontrollers qkd57d-823709-3288346-q-20example-oc-doc.doc

OUTPUT COMPART TIMER FUNCTION