28.12.2013 Views

Serial Programming - upload.wikimedia....

Serial Programming - upload.wikimedia....

Serial Programming - upload.wikimedia....

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Serial</strong> DOS<br />

4.7 Terminal Program Revisited<br />

I'm going to go back to the serial terminal program for a bit and this time redo the application<br />

by using an interrupt service routine. There are a few other concepts I'd like to introduce<br />

as well so I'll try to put them in with this example program. From the user perspective, I<br />

would like to add the ability to change the terminal characteristics from the command line<br />

and allow an "end-user" the ability to change things like the baud rate, stop bits, and parity<br />

checking, and allow these to be variables instead of hard-coded constants. I'll explain each<br />

section and then put it all together when we are through.<br />

4.7.1 <strong>Serial</strong> ISR<br />

This is an example of a serial ISR we can use:<br />

{$F+}<br />

procedure <strong>Serial</strong>DataIn; interrupt;<br />

var<br />

InputLetter: Char;<br />

begin<br />

if (Port[ComPort[1] + LSR] and $01) > 0 then begin<br />

InputLetter := Chr(Port[ComPort[1] + RBR]);<br />

end; {if}<br />

end;<br />

{$F-}<br />

This isn't that much different from the polling method that we used earlier, but keep in<br />

mind that by placing the checking inside an ISR that the CPU is only doing the check when<br />

there is a piece of data available. Why even check the LSR to see if there is a data byte<br />

available? Reading data sent to the UART is not the only reason why the UART will invoke<br />

an interrupt. We will go over that in detail in a later section, but for now this is good<br />

programming practice as well, to confirm that the data is in there.<br />

By moving this checking to the ISR, more CPU time is available for performing other tasks.<br />

We could even put the keyboard polling into an ISR as well, but we are going to keep things<br />

very simple for now.<br />

4.7.2 FIFO disabling<br />

There is one minor problem with the way we have written this ISR. We are assuming that<br />

there is no FIFO in the UART. The "bug" that could happen with this ISR as it is currently<br />

written is that multiple characters can be in the FIFO buffer. Normally when this happens,<br />

the UART only sends a single interrupt, and it is up to the ISR to "empty" the FIFO buffer<br />

completely.<br />

Instead, all we are going to do is simply disable the FIFO completely. This can be done<br />

using the FCR (FIFO Control Register) and explicitly disabling the FIFO. As an added<br />

precaution, we are also going to "clear" the FIFO buffers in the UART as a part of the<br />

initialization portion of the program. Clearing the FIFOs look like this:<br />

68

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!