Output Compare Function in ECE 3120: Computer Systems, Study notes of Microcomputers

The operation of the output compare function in the hcs12 microcontroller, including its applications, components, and control logic. It also provides an example of generating a two-tone siren using the output compare function.

Typology: Study notes

Pre 2010

Uploaded on 07/30/2009

koofers-user-m7v
koofers-user-m7v 🇺🇸

10 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ECE 3120: Computer Systems
Chapter 8: ECE-3120-A Musical
Manjeera Jeedigunta
http://blogs.cae.tntech.edu/msjeedigun21
Tel: 931-372-6181, Prescott Hall 120
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Output Compare Function in ECE 3120: Computer Systems and more Study notes Microcomputers in PDF only on Docsity!

ECE 3120: Computer Systems Chapter 8: ECE-3120-A Musical

Manjeera Jeedigunta

http://blogs.cae.tntech.edu/msjeedigun

Email: [email protected]: 931-372-6181, Prescott Hall 120

Output Compare Function †^

The HCS12 has eight output compare functions.

†^

Each output compare channel consists of „^

A 16-bit comparator „^

A 16-bit compare register TCx (also used as inout captureregister) „^

An output action pin (PTx, can be pulled high, pulled low,or toggled) „^

An interrupt request circuit „^

A forced-compare function (CFOCx) „^

Control logic

7

6

5

4

3

2

1

0

OM^

OL^

OM^

OL^

OM^

OL^

OM^

OL

valueafter reset

0

0

0

0

0

0

0

0

(^7) read: anytimewrite: anytime Figure 8.18 Timer control register 1 and 2 (TCTL1 & TCTL2)

6

5

4

3

2

1

0

OM^

OL^

OM^

OL^

OM^

OL^

OM^

OL

0

0

0

0

0

0

0

0

(a) TCTL1 register(b) TCTL2 register valueafter reset

OMn OLn : output level^0011

0 1 0 1

no action (timer disconnected from output pin)toggle OCn pinclear OCn pin to 0set OCn pin to high

†^

The action is determined by the Timer Control Register 1 & 2 (TCTL1& TCTL2):

†^

A successful compare will set the corresponding flag bit in the TFLG1register.

†^

An interrupt may be optionally requested if the associated interruptenable bit in the TIE register is set.

Operation of the Output-Compare Function

HCS12DP

PT

3.3^ μF

Buzzer

Figure 8.21 Circuit connection for a buzzer

Making Sound Using the Output-Compare Function^ †^

A sound can be generated by creating a digital waveform withappropriate frequency and using it to drive a speaker or abuzzer. †^

The circuit connection for a buzzer is shown in Figure 8.21. †^

The simplest song is a two-tone siren.

#include

"c:\miniide\hcs12.inc"

hi_freq

equ

; delay count for 1200 Hz (with 1:8 prescaler)

lo_freq

equ

; delay count for 300 Hz (with 1:8 prescaler)

toggle

equ

$^

; value to toggle the TC5 pin

org^

delay

ds.w

; store the delay for output-compare operation

org^

lds^

movw

#oc5_isr,UserTimerCh5 ; initialize the interrupt vector entry movb

#$90,TSCR

; enable TCNT, fast timer flag clear

movb

#$03,TSCR

; set main timer prescaler to 8

†^

Example 8.7 Write a program to generate a two-tone sirenthat oscillates between 300 Hz and 1200 Hz. †^

Solution: „^

Set the prescaler to TCNT to 1:8.

„^

The delay count for the low frequency tone is (

÷^ 8)

÷^300

÷

„^

The delay count for the high frequency tone is (

÷^ 8)

÷^1200

÷^ 2 = 1250.

bset

TIOS,OC

; enable OC

movb

#toggle,TCTL

; select toggle for OC5 pin action

movw

#hi_freq,delay

; use high frequency delay count first

ldd^

TCNT

; start the low frequency sound

addd

delay

;^

std^

TC

;^

bset

TIE,OC

; enable OC5 interrupt

cli^

;^

forever

ldy

#^

; wait for half a second

jsr^

delayby100ms

;^

movw

#lo_freq,delay ; switch to low frequency delay count ldy^

jsr^

delayby100ms movw

#hi_freq,delay ; switch to high frequency delay count bra^

forever

oc5_isr

ldd

TC

addd

delay std^

TC

rti #include c:\miniide\delay.asm”

end

#include

"c:\miniide\hcs12.inc"

G^

equ^

; delay count to generate G3 note (with 1:8 prescaler)

B^

equ^

; delay count to generate B3 note (with 1:8 prescaler)

C^

equ^

; delay count to generate C4 note (with 1:8 prescaler)

C4S

equ^

; delay count to generate C4S (sharp) note

D^

equ^

; delay count to generate D4 note (with 1:8 prescaler)

E^

equ^

; delay count to generate E4 note (with 1:8 prescaler)

F^

equ^

; delay count to generate F4 note (with 1:8 prescaler)

F4S

equ^

; delay count to generate F4S note (with 1:8 prescaler)

G^

equ^

; delay count to generate G4 note (with 1:8 prescaler)

A^

equ^

; delay count to generate A4 note (with 1:8 prescaler)

B4F

equ^

; delay count to generate B4F note (with 1:8 prescaler)

B^

equ^

; delay count to generate B4 note (with 1:8 prescaler)

C^

equ^

; delay count to generate C5 note (with 1:8 prescaler)

D^

equ^

; delay count to generate D5 note (with 1:8 prescaler)

E^

equ^

; delay count to generate E5 note (with 1:8 prescaler)

F^

equ^

; delay count to generate F5 note (with 1:8 prescaler)

notes

equ

toggle

equ

$^

; value to toggle the TC5 pin

The Star-Spangled Banner

org^

delay

ds.w

; store the delay for output-compare operation

rep_cnt

ds.b

; repeat the song this many times

ip^

ds.b

; remaining notes to be played

org^

lds^

; establish the SRAM vector address for OC

movw

#oc5_isr,UserTimerCh movb

#$90,TSCR

; enable TCNT, fast timer flag clear

movb

#$03,TSCR

; set main timer prescaler to 8

bset

TIOS,OC

; enable OC

movb

#toggle,tctl

; select toggle for OC5 pin action

ldx^

#score

; use as a pointer to score table

ldy^

#duration

; points to duration table

movb

#1,rep_cnt

; play the song once

movb

#notes,ip movw

2,x+,delay

; start with zeroth note

ldd^

TCNT

; play the first note

addd

delay

std^

TC

bset

TIE,C5I

; enable OC5 interrupt

cli^

;^

oc5_isr

ldd

TC

addd

delay std^

TC

rti ; ************************************************************************************ ; The following subroutine creates a time delay which is equal to [Y] times ; 10 ms. The timer prescaler is 1:8. ; ************************************************************************************ delayby10ms

bset

TIOS,OC

; enable OC

ldd^

TCNT

again

addd

; start an output-compare operation

std^

TC

; with 10 ms time delay

wait_lp1 brclr

TFLG1,C0F,wait_lp ldd^

TC

dbne

y,again bclr^

TIOS,OC

; disable OC

rts^

score

dw

D4,B3,G3,B3,D4,G4,B4,A4,G4,B3,C4S

dw^

D4,D4,D4,B4,A4,G4,F4S,E4,F4S,G4,G4,D4,B3,G

dw^

D4,B3,G3,B3,D4,G4,B4,A4,G4,B3,C4S,D4,D4,D

dw^

B4,A4,G4,F4S,E4,F4S,G4,G4,D4,B3,G3,B4,B

dw^

B4,C5,D5,D5,C5,B4,A4,B4,C5,C5,C5,B4,A4,G

dw^

F4S,E4,F4S,G4,B3,C4S,D4,D4,G4,G4,G4,F4S

dw^

E4,E4,E4,A4,C5,B4,A4,G4,G4,F4S,D4,D

dw^

G4,A4,B4,C5,D5,G4,A4,B4,C5,A4,G

; ************************************************************************************** ; Each of the following entries multiplied by 10 ms gives the duration of a note. ; ************************************************************************************** duration dw

dw^

dw^

dw^

dw^

dw^

dw^

dw^

end^