The only "construction" you'll need to do is connect the output of the PN4601 module to the IC1 pin. The real challenge lies in the program because it requires you to coordinate the operation of the input capture and output compare interrupts.
To give you a good starting place, we've modified the
kernel
for lab 10 to take care of the time-tick and
transmission functions. In particular, the global variable
_Time
is now incremented by the interrupt handler
OC2han
. The OC2
output compare is enabled by
init()
and remains enabled forever. It increments
the global variable _Time
once every 256 hardware
time ticks.
The OC4
interrupt handler has been
rewritten to transmit a single frame containing the
character stored in the global variable sdata
. The
frame is configured as indicated above in figure
1. In the kernel, we've
assumed that the start bit lasts hardware time
ticks, the data bit lasts hardware time ticks, and
that there is a stop-bit of duration hardware time
ticks. The OC4
output compare interrupt is enabled
from a new kernel function xmit_data_pa7()
. The
handler disables itself once it has completed transmitting
a single frame of data. The handler assumes that the data
to be transmitted is held in global variable sdata
.
The kernel
function called xmit_data_pa7()
is
used to initiate transmission of a single frame. This
function has the prototype
void xmit_data_pa7(void);The function takes the value in global variable
sdata
and enables the OC4 interrupt which is then
used to control the transmission of a single frame from
output pin PA7. You would use the function as shown in the
following code.
void main(void){ int istep; init(); sdata=0; rdata=0; while(1){ istep=butcontrol(); sdata=(sdata+istep)%8; display(sdata,rdata); xmit_data_pa7(); } }This is a particularly simple program. The global variable
sdata
contains the data to be transmitted and the
global variable rdata
contains the data to be
received. The program consists of a single while
loop that repeatedly checks the state of the buttons,
increment or decrements the data to be transmitted,
displays the received and transmitted data to the dual
digit LED display, and then enables the OC4
interrupt. The interrupt handler OC4han()
then
transmits what's in sdata
out of pin PA7
.
You
should be able to use this simple program, along with the
kernel we've provided you to start your system.
In order to finish this program, you'll need to write a
pair of interrupt handlers. You will first need to write
an input capture interrupt that detects the start bit of a
frame. Upon detecting the start bit, your input capture
interrupt should then disable itself and then begin an
output compare interrupt that periodically reads the state
of the input data pin. This output compare interrupt will
decode the frame and should store the result in a global
variable, rdata
. This variable represents the
received data and can be displayed. Since you know that
each frame of data consists of exactly eight bits, you know
exactly when to stop looking for data bits in the frame.
Once your output compare interrupt has finished reading the
data bits, it should disarm itself and it should re-arm the
input capture interrupt to allow the system to begin
looking for start bits again.
A more specific guideline to writing these two interrupt handlers is given below.
IC1
input capture to
detect the frame's start bit. Remember that the start bit
(see figure 1) is half the
size of a regular data bit. Your interrupt handler will
need to measure the width of every received bit. Your
interrupt handler IC1
should be initially armed in
init()
. It will disarm itself after it has detected
the start bit and it should then arm the output compare
interrupt OC3
. The input capture interrupt will
then be re-armed once the OC3 interrupt has finished its
work.
OC3
output
compare interrupt to periodically read the input pin (IC1).
Remember that there are 8 data bits that occur at regular
intervals after the start bit. So you can use the
OC3
interrupt handler to periodically read the
logical state of the IC1 input pin. Each time it reads the
state, your interrupt handler should set the appropriate
bit in a global variable like rdata
. After the
eighth bit has been read, you interrupt handler should
disarm itself and then re-arm the IC1
interrupt, so
your system can begin looking for a start bit again.
main
program probably needs
little (if any) modification because all of the work
associated with decoding a received data frame is done by
the interrupt handlers.
In order to finish this lab, you'll need to demonstrate that your system transmits and receives the requested data. You should be able to demonstrate that if the data link is broken that you link will stop working. You should also be able to transmit information to other people across the room.