88 CHAPTER 8. SIGNAL GENERATION
8.3 Variable frequency signal generator
In this experiment, we will generate a square wave with frequency selected by the
user. We will fix the duty cycle at 25%. To get started, we will first write the
code for generating a single tone.
8.4 500 Hz tone generator
Here is a typical calculation to generate a 500 Hz signal:
T = 1/500 = 2 × 10
−3
seconds = (2 × 10
−3
) × (2 × 10
6
) = 4000 ticks
T
on
= 4000/4 = 1000 ticks
T
off
= 4000 − 1000 = 3000 ticks
To generate a 50 Hz, 25% duty cycle signal we use two variables called ONTIME and
OFFTIME. Every time we get an OC2 interrupt, we toggle the pin PA4 as before. We
check to see if we turned the pin ON or OFF. If we turned it on, then we schedule
the next interrupt to occur ONTIME ticks later. However, If we turned it off, then
we schedule the next interrupt to occur OFFTIME ticks later. Thus, the earlier
code that was used to generate a square wave is modified as follows:
; Various defines go here ...
ORG $3000 don’t forget the $
ME FCC /Your name/
FCB 10
FCC /ECE 372/
FCB 10
FCC /Date the program was last changed/
FCB 10, 10, 4
ONTIME RMB 2
OFFTIME RMB 2
ORG $2000 DONT FORGET THE $
LDX #ME
JSR OUTSTRG ; MAKE SURE YOU HAVE EQU FOR OUTSTRG
; Enable OC2 interrupt by setting OC2I (bit#6 in TMSK1)
; ALSO PERFORM ALL INITIALIZATION BETWEEN SEI/CLI
Comentarios a estos manuales