HOME

Lab components

Objective

Reading

Part 1

Part 2

Part 3

Part 4
 

Turnin

CSE 477: Digital Systems Design
(Spring 2002)
Lab 4: Using Serial Communications

Objectives:

In this lab will you will learn the following: 

  • Microcontroller serial interface
    • how to set up the serial interface baud rate using a timer
    • how to send and receive characters using interrupt routines
    • how to implement the putchar and getchar serial I/O functions

Part 1 -- Setting up the serial interface

Refer to the 8051 documentation to see how to set the baud rate of the serial port using timer 1. We will be using a baud rate of 9600 - figure out how the timer should be configured to implement this baud rate. 

In addition, you will need to connect to the serial connector on the XSV board to the RXD/TXD pins of the the embedded 8051 microcontroller.  This connection goes through the X95108 CPLD, which is also used to connect the parallel port to the FPGA.  We will have to use a different SVF file to program the CPLD so that this connection is made - the dnldpar.svf program only connects the parallel port. (We will have to write this!)  You can test your connection with the Serial.c  in the ...\lab4 folder. (You will have to set the timer for 9600 baud rate for this to work.)  There are five major components to get serial working:

  1. Embedded 8051 with 12.5 MHZ clock
  2. Hyperterminal
  3. Gender changer


The following picture indicates the use of the 9 pin serial connector.
 
 
The connectors
--------------

PCs have 9pin/25pin male SUB-D connectors. The pin layout is as follows
(seen from outside your PC):

       1         5
      _______________
      \  . . . . .  /
       \  . . . .  /
        -----------
         6       9

 Name (V24)    9pin  Dir  Full name               Remarks
--------------------------------------------------------------------------
    TxD         3    o   Transmit Data           Data
    RxD         2    i   Receive Data            Data
    RTS         7    o   Request To Send         Handshaking
    CTS         8    i   Clear To Send           Handshaking
    DTR         4    o   Data Terminal Ready     Status
    DSR         6    i   Data Set Ready          Status
    RI          9    i   Ring Indicator          Status
    DCD         1    i   Data Carrier Detect     Status
    GND         5    -   Signal ground           Reference level
     -          -    -   Protective ground       Don't use this one
                                                       as signal ground!

The most important lines are RxD, TxD, and GND. Others are used with
modems, printers and plotters to indicate internal states.

We will use the following FPGA pins for the TxD and Rxd signals.  There will be a new UCF entries for these signals.

  • Pin 138 on the board is TxD (from 8051 to PC)
  • Pin 145 on the board it RxD (from PC to 8051)
5. Hyperterminal is a standard windows program that allows for basic communication through the serial port. The program is usally found in the Accessories folder. When you make a new hyperterminal connection you need to have the following PROPERTIES:
  • Select the appropiate COM. (P0 corresponds to COM3)
  • Select "Set Default settings" or do the following to set the parameters
  • Baud rate (bits per second). Hyperterminal and the program on the microcontroller have to be set to the same baud rate. If the baud rates are different by a big percentage, then chances are no characters are sent either way. 
  • Data bits 8.
  • Parity NONE
  • Stop bits 1
  • Flow control none (we do not use the wires required for hardware flow control)
  • Menu: Properties->Settings-> ASCII setup (button). Check mark: echo typed character locally (otherwise only characters that are recieved by hyperterminal are displayed.

Part 2 -- Simple getchar/putchar

In this part, you will implement simple getchar() and putchar() routines. getchar() returns the character that has been received by the serial port. If there is a character waiting, it just returns it. If there is no character, then getchar() returns NULL. The user can thus call getchar() repeatedly if necessary until a character is returned, but getchar() doesn't hang if no character ever comes. 

putchar(c) sends a character c to the serial port. If the port is not ready, i.e. it is in the process of sending a character, then putchar() returns NULL.  Otherwise it sends the character and returns a 1. 

Test these routines by writing a program that reads characters from the serial interface, and writes them back, transforming lower case letters to upper case, and vice versa. In case of non-alphabetic character echo the same character back) 

(The character codes for the alphabets : 'A' = 65, 'Z'=90 , 'a'=97 and 'z'=122; )
 

Part 3 -- Serial Interface Interrupt Routine

The getchar and putchar routines in Part 2 do not have any buffering and thus do not allow a lot of concurrent processing. In this part, you will write one interrupt routine, that sends a character and that receives a character depending on when the interrupt is triggered. The interrupt call that sends a character is interrupted when the serial port send buffer is free. If there is a character waiting to be sent (use a global variable for this), then the routine sends the character and clears the global variable. If there is no character waiting to be sent, then it does nothing.

The interrupt routine is also called when a character is received. This character should be stored in a global variable. If the global variable already has a character, then an error condition should be flagged. 

You will now use the interrupt routine that you just wrote to implement getchar() and putchar(). These routines no longer look at the serial port registers directly, but rather at the global variables used by the interrupt routines. However, if putchar() is called and the global variable is empty, then putchar() must send that character explicitly since the interrupt routine will not be called again.

Test these routines by writing a program that reads characters from the serial interface, and writes them back, but transforming lower case letters to upper case, and vice versa. 

Part 4 -- Buffered readchar/putchar

In this part, you will increase the buffering from a single global variable to a buffer of size 32. This buffer should be a queue. When writing, the user adds characters into one end and the interrupt routine takes characters out of the other end.  When reading, the interrupt routine puts characters at one end and the user reads from the other.  Don't forget the error flags for cases where data gets lost.

To test the buffer you should implement a software flow-control along with the simple echo application. From hyperterminal you send an Xoff and all data afterward would end up in the buffer.  After sending an Xon, all the buffered data should be echoed back. 

In practical terms Xoff and Xon are reserved characters that you send; for example, Xon is say the '$' and Xoff is say '#'. These characters act as commands for your program.
 

What to Turnin

Demonstrate your program to the TA. Hand in a printout of the C programs you wrote. You should also be able to answer detailed questions about every part.

BACK TO TOP

 Last Updated:
5/7/2002

Contact the instructor at:cse477-webmaster@cs.washington.edu