HOME

Lab components

Objective

Reading

Part 1

Part 2

Part 3

Part 4
 

Turnin

CSE 477: Digital Systems Design
(Spring 1999)
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
 
 

The ATMEL 8051 should be used or this lab. Remember to put the J7 jumper on the second two pins and use the RESET button before downloading.

The 24 MHZ clock should be used

Using the Debugger

Here is a brief tutorial on using the debugger to test out a C program before you download it.
 

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 the wires and serial connector between the microcontroller and PC. You can test your connection with the Hello.hex  in the \\ifilesrv1\cse477\lab4 folder. The given baud rate is 9600 and the program will write a sentence to the screen. There are five major components to get serial working:

  1. Xilinx project with 24 MHZ clock
  2. 9 pin serial port connector
  3. Pins on the microcontroller
  4. Multichannel RS-232 Drivers/Receivers (Max232)
  5. Hyperterminal
1. Use the Xilinx vga project from lab 4, given that your lab 4 works with the microcontroller. You can also use the vga.bit that is in the lab 4 folder. The main purpose of this project is to connect the microcontroller to the SRAM (where the instructions are stored). You just have to add one wire for the project to work.

2. The following picture indicates the use of the 9 pin serial connector. You will need to connect RxD, TxD, and GND.
 
 
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.
(for more information on serial ports)

3. The pins on the 8051 microcontroller for serial communication are as follows:

  • Pin 69 on the board is TxD (from 8051 to PC)
  • Pin 11 on the board it RxD (from PC to 8051)
4. Voltage conversion. You need the Max232 chip and here is the documentation of the chip. MAX232from www.maxim-ic.com

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:

  • COM 1
  • 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. 
 
 

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:
4/12/99

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