\documentclass[12pt]{article}
\usepackage{latexcad}
\evensidemargin 0.50in \textwidth 5.75in

\topmargin 0in \headsep .325in \textheight 8.25in

\def\myfig#1#2#3{
 \begin{figure}[ht]
 \special{isoscale #1, \the\hsize 3.0 in}
 \vspace{3.0in}
  \caption{#2}
\label{#3}
 \end{figure}
}

\begin{document}
\title{Signal Generation}
\date{}
\author{N. Narasimhamurthi}
\maketitle
\section{Objective}
To become familiar with generating square waves.
\section{Background}
In an earlier lab, you had to generate a  square wave signal using
various interrupts. This lab builds on this. For a general square
wave signal, we define the on-time, $T_{\mbox{on}}$, the off-time,
$T_{\mbox{off}}$, and the period $T$ as shown in the figure
\ref{fig:square}. \placedrawing{pwm.lp}{0-5 volt square wave
}{fig:square} The goal of the lab is to generate such square
waves. The ratio $\frac{T_{\mbox{on}}}{T}$ is called the {\tt duty
cycle} and is expressed as a percentage. When dealing with time it
is convenient to talk in terms of clock ticks. The HC11 has a 2
MHz e-clock, or you have $2 \times 10^6$ ticks per second. Hence 1
clock tick is 0.5 microseconds.
\section{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.
\subsection{500 Hz tone generator}
Here is
a typical calculation to generate a 500 Hz signal:
\begin{eqnarray*}
T  & = & 1/500 = 2 \times 10^{-3} ~~\mbox{seconds}
=  (2 \times 10^{-3}) \times (2 \times 10^6) = 4000 ~~\mbox{ticks} \\
T_{\mbox{on}}& = & 4000 /4 = 1000 ~~\mbox{ticks} \\
T_{\mbox{off}}  & = & 4000- 1000 = 3000 ~~\mbox{ticks}
\end{eqnarray*}
To generate a 500 Hz, 25\% duty cycle signal we use two variables
called {\tt ONTIME} and {\tt OFFTIME}. Every time we get an {\tt
OC2} interrupt, we toggle the pin {\tt PA4} as before. We check to
see if we turned the pin {\tt ON} or {\tt OFF}. If we turned it
on, then we schedule the next interrupt to occur {\tt ONTIME}
ticks later. However, If we turned it off, then we schedule the
next interrupt to occur {\tt OFFTIME} ticks later. Thus, the
earlier code that was used to generate a square wave is modified
as follows:
\begin{verbatim}
 ; 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

       SEI
       LDD #1000
       STD ONTIME
       LDD #3000
       STD OFFTIME

       LDAA #%01000000
       STAA TMSK1

       CLI

; Now go about your business of printing Z's
       LDAA #'Z'
LOOP   JSR OUTA
       BRA LOOP

; End of main program

 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; INTERRUPT SERVICE

SERVICE

;       TOGGLE PA4
        LDAA #%00010000
         EORA PORTA
        STAA PORTA

;       TEST TO SEE IF THE PIN WENT HIGH
        ANDA #%00010000
        BNE WENTHIGH

;       NO, PIN WENT LOW. LOAD D WITH OFFTIME
        LDD OFFTIME
        BRA REST

WENTHIGH
;       PIN WENT HIGH. LOAD D WITH ONTIME
        LDD ONTIME

REST
        ADDD TOC2
        STD TOC2 ; BUMP ALARM SETTING

;       TURN OFF THE FLAG!
        LDAA #%01000000
        STAA TFLG1

        RTI

;       Connect the service to the interrupt ;
        ORG $00DC ; $00DC WHERE THE SERVICE STARTS
        JMP SERVICE ; JUMP TO WHERE THE SERVICE CODE ACTUALLY IS
\end{verbatim}
Assemble and run the above program. Connect {\tt PA4} to an oscilloscope
and verify that the duty cycle and the frequency are correct.
\subsection{Variable frequency generator}
We now modify the above code to create a variable frequency
generator. The program will monitor the keyboard and depending on
the number the user enters, it will change the frequency as shown
in the table below:
\begin{center}
\input{sheet1}
\end{center}
As a programmer we are only interested in the on-time and
off-time. We use {\tt FDB} to create two tables in the data
section as shown below:
\begin{verbatim}
ONTIMETBL
    FDB 1136, 1073, 1013, 956, 902
    FDB 851, 804, 759, 716, 676
OFFTIMETBL
    FDB 3409, 3217, 3037, 2866, 2706
    FDB 2554, 2410, 2275, 2147, 2027
\end{verbatim}
Note that it makes sense to enter numbers in decimal notation.
Each entry in the table requires two bytes (we use 16 bit numbers
to measure time since the HC11 clock is a 16 bit quantity). Hence
we use \verb+FDB+ instead of \verb+FCB+. Note that this also means
that  we have to index through memory in steps of {\bf two bytes}.
Suppose we want to access the element \#4 in ONTIMETBL and the
value \#4 is in the {\bf B} register (the number \#4 is used only
as an illustration. In the application the register {\bf B} will
have the value). Then we have to access the element we have to
write
\begin{verbatim}
        LDX #ONTIMETBL
        ABX
        ABX ; NEED TWO ABX'S
        ??? 0,X
\end{verbatim}
We can now modify the single tone generator to a programmable
square wave generator by making the following changes:
\begin{center}
\input{chngtbl}
\end{center}
\begin{center}
\input{chng2}
\end{center}
\subsection{Exercises}
\begin{enumerate}
\item
Make the changes shown above and run the program. Connect {\tt
PA4} to an oscilloscope. Press any of the keys {\tt 0} to {\tt 9}
and verify that the frequency changes.
\item
Change the program so that when you press any of the keys {\tt 0}
to {\tt 9}, the frequency should be fixed at 100 Hz but the duty
cycle changes. Pick 10 different duty cycles. Also, connect the
output pin to a digital voltmeter. How does the voltage change
with the duty cycle?
\end{enumerate}
\end{document}
