Hardware Reference
In-Depth Information
4.11.3 Generating a Digital Waveform Using I/O Pins
A periodic digital waveform can be easily generated by manipulating I/O pin voltage
level and inserting an appropriate delay between the two voltage levels. For example,
a 1-kHz periodic square wave can be generated from the PT5 pin using the following
algorithm:
Step 1
Configure the PT5 pin for output.
Step 2
Pull the PT5 pin to high.
Step 3
Wait for 0.5 ms.
Step 4
Pull the PT5 pin to low.
Step 5
Wait for 0.5 ms.
Step 6
Go to step 2.
Example 4.15
Write an assembly program to generate a 1-kHz periodic square wave from the PT5 pin.
Solution: The following assembly program implements the algorithm described previously:
#include
“c:\miniide\hcs12.inc”
org
$1500
start
lds
#$1500
bset
DDRT,BIT5
; configure PT5 pin for output
forever
bset
PTT,BIT5
; pull PT5 pin to high
ldy
#10
; wait for 0.5 ms
jsr
delayby50us
; ''
bclr
PTT,BIT5
; pull PT5 pin to low
ldy
#10
; wait for 0.5 ms
jsr
delayby50us
; ''
bra
forever
#include
“c:\miniide\delay.asm”
;
org
$FFFE
; uncomment for CodeWarrior
;
dc.w
start
; uncomment for CodeWarrior
end
By connecting the PT5 pin to a speaker (or a buzzer) and making the frequency in the
audible range, a sound can be made. Since the frequency of the square wave generated in
Example 4.15 is in the audible range, a sound can be heard if a speaker is connected to the
PT5 pin.
By alternating the frequency of the generated waveform from the I/O pin between two dif-
ferent values, a two-tone siren can be generated. The duration of the siren tone is variable. The
siren would sound more urgent if the tone duration were shorter. The following example gener-
ates a two-tone siren that alternates between 250 Hz and 500 Hz with each tone lasting half of
a second.
 
Search WWH ::




Custom Search