








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 14
This page cannot be seen from the preview
Don't miss anything!









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
^
^
^
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
; enable TCNT, fast timer flag clear
movb
; 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: ^
^
^
bset
; enable OC
movb
#toggle,TCTL
; select toggle for OC5 pin action
movw
#hi_freq,delay
; use high frequency delay count first
ldd^
; start the low frequency sound
addd
delay
std^
bset
; 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
addd
delay std^
rti #include c:\miniide\delay.asm”
end
#include
"c:\miniide\hcs12.inc"
equ^
; delay count to generate G3 note (with 1:8 prescaler)
equ^
; delay count to generate B3 note (with 1:8 prescaler)
equ^
; delay count to generate C4 note (with 1:8 prescaler)
equ^
; delay count to generate C4S (sharp) note
equ^
; delay count to generate D4 note (with 1:8 prescaler)
equ^
; delay count to generate E4 note (with 1:8 prescaler)
equ^
; delay count to generate F4 note (with 1:8 prescaler)
equ^
; delay count to generate F4S note (with 1:8 prescaler)
equ^
; delay count to generate G4 note (with 1:8 prescaler)
equ^
; delay count to generate A4 note (with 1:8 prescaler)
equ^
; delay count to generate B4F note (with 1:8 prescaler)
equ^
; delay count to generate B4 note (with 1:8 prescaler)
equ^
; delay count to generate C5 note (with 1:8 prescaler)
equ^
; delay count to generate D5 note (with 1:8 prescaler)
equ^
; delay count to generate E5 note (with 1:8 prescaler)
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
; enable TCNT, fast timer flag clear
movb
; set main timer prescaler to 8
bset
; 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^
; play the first note
addd
delay
std^
bset
; enable OC5 interrupt
cli^
oc5_isr
ldd
addd
delay std^
rti ; ************************************************************************************ ; The following subroutine creates a time delay which is equal to [Y] times ; 10 ms. The timer prescaler is 1:8. ; ************************************************************************************ delayby10ms
bset
; enable OC
ldd^
again
addd
; start an output-compare operation
std^
; with 10 ms time delay
wait_lp1 brclr
TFLG1,C0F,wait_lp ldd^
dbne
y,again bclr^
; disable OC
rts^
score
dw
dw^
dw^
dw^
dw^
dw^
dw^
dw^
; ************************************************************************************** ; 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^