ECE 354 - Computer System Lab II

Project 1

Part 1: Serial Interface to a Terminal

In this lab assignment you will learn how to interface a Microchip PIC 16F877 to a terminal which contains a keyboard and a CRT display. The amount of hardware required to complete the lab will be fairly minimal (a 16F877, SP233A transceiver, a clock "can", and a few resistors and capacitors), but there will be a substantial amount of software to write and debug. This page contains a discussion of the assignment and a recommended procedure for completing the work. If you have additional questions, please ask the course instructor or the TAs. Please get started on the assignment early. We expect to you be present during your four lab hours that are scheduled for each week. Also, please become familiar with the course honesty policy listed on the main ECE354 page. Directly copying code from another group is a serious honor code violation and will be treated as such.

Tasks

In order to be checked off for this lab you must complete the following tasks. Please check the lecture notes for Lab 1, for additional details.

1. Four single-bit data switches from your lab kit should be connected as input to the lower four data bits of 16F877 external port A.

2. Bits RB1 through RB4 of 16F877 external port B should drive LEDs located on your lab kit. Initially, these bits should be set to all zero (e.g. 0000).

3. The serial data output from the VT220 on your lab bench should be connected to the RX pin on the 16F877 via a MAX232 transceiver. This data pin serves as input for the 16F877 USART.

4. Software running on the 16F877 should obtain characters from the RECEIVE port of the USART using USART asynchronous mode.

5. After reset, the 16F877 should send the message “Start (Y or N)?” to the VT220. The user sends a response by pressing a character on the VT220 keyboard. If any character other than ‘Y’ is pressed the prompt should be resent. Individual characters can be sent from the 16F877 to the VT220 CRT via the USART on the 16F877 in asynchronous mode. Output pin TX drives the VT220 CRT with serial output via a MAX232 transceiver. The value of the character pressed should appear on the VT220 screen next to the ‘Start (Y or N)?’ prompt and a single blank space.

6. Once a ‘Y’ is sent, the four-bit input on port A should be echoed to port B (e.g. an input of ‘0001’ should light up the lowest-order LED). Additionally, a message “Value [HEX] set in LEDs” should be sent to the terminal. The ‘HEX’ value should represent your four-bit port A value in hexadecimal (e.g. one character, binary 1010 -> hexadecimal A).

7. Following step 6, each change of a switch attached to a port A input should trigger a repeat of the actions of step 6.

8. To successfully complete the lab, your hardware should run in "stand-alone" mode. In other words, there should be a connection from a push button to the MCLR signal on the 16F877 and the 16F877 should be free of the MPLAB-ICD emulator. After reset, the 16F877 should jump to code that sends the start prompt to the terminal, and then waits from input from the terminal keyboard. Note that the 16F877 should sample Port A often since changes in the switch setting should immediately be reflected in the LEDs attached to Port B after the ‘Y’ is pressed.

Procedure

The best way to complete the tasks listed above is through a divide-and-conquer approach. If you have completed the introductory lab exercise, you have learned that the MPLAB-ICD can be very helpful in identifying and fixing design bugs. I also highly recommend the use of MPSIM to test the functionality of your software.

Step 1: System start-up / Writing to Port B

After reviewing the lecture notes for the lab, I suggest the following simple task: writing a four bit value to Port B to illuminate four LEDs. As discussed in lecture consider the issues that must be addressed to accomplish this [setting the Reset vector at 0000h to jump to the start of code, configuring the data direction register (TRISB), and writing an four-bit value to the Port B data register (PORTB), address 0006h]. Consider using the MPLAB-ICD for single-stepping to test your hardware in addition to programming your 16F877.

Step 2: Reading from Port A

Once you have verified that your 16F877 starts up correctly from reset and is writing a value to Port B that you have hard-coded in the program, program it to echo the four data bits from Port A to the four LEDs attached to Port B. This requires the configuration of Port A using the TRISA and ADCON1 registers.

Step 3: Configuring the USART

As mentioned in lecture, several registers must be configured in order to get the USART working correctly. These registers include TXSTA, RCSTA, and SPBRG. Before attempting to read or write an eight-bit data value from/to the USART make sure these registers have been set to the correct value. Pay particular attention to setting the baud rate generator correctly for 9600 baud communication. Check the lecture notes, Chapter 11 of Peatman, and the 16F877 data sheet for more detail on setting the baud rate generator.

Step 4: Reading a character from the terminal keyboard and writing it to the terminal display

As an initial step to verify the correct operation of the USART, it is desirable to program the 16F877 to READ a character from the terminal keyboard and then immediately send the character back to the terminal display (CRT) unchanged. If you run into difficulty in performing this test, consider hard-coding a character into your code and transmitting it the display before attempting to read a character. Note that for this assignment you will be using POLLING instead of INTERRUPTs to determine the status of the receive and transmit buffers. This indicates that your code should repetitively check the full/empty bits in register PIR1 for transmission/receipt of USART data rather than using interrupts to indicate when the resources are free.

Step 5. Putting it all together

I suggest an incremental approach to completing the lab. One solution is to have one partner focus on the hardware setup including Ports A and B while the other partner writes software to configure and use the USART. The MPLAB-ICD can be used to single-step the 16F877 if you run into difficulty.

Things to keep in mind

Often it may appear that hardware is not working the way that is expected. An initial reaction would be to assume that the hardware is broken and needs to be replaced. Before going to find the TA keep the following questions in mind:

Are all the inputs on the digital components driven by some value? Often, floating signals will cause chips, such as the 16F877, to operate incorrectly.

Is there a simple test case I can try to verify functionality? Simply coding the entire lab at once or wiring the entire kit at once will likely lead to frustration. Instead, plan for simple tests that verify the functionality of specific components and software segments.

Is the chip plugged into the board correctly? Please be careful plugging the MPLAB-ICD, the 16F877, and other components into the breadboard. Aligning the parts incorrectly will lead to broken pins and hours of debugging. Carefully plug and unplug each component from the breadboard.

Demonstration

The demonstration should show communication between the 16F877 and the terminal via the MAX232 part. The specific tasks to be presented are listed above in the "Tasks" section. The 16F877 and MAX232 part should be populated on the breadboard along with a 4 MHz clock "can" which is used to drive the 16F877 clock. It is OK to use the MPLAB-ICD for debugging but the 16F877 should be populated seperately on the board for final check-off. Additional information about the demo is available here.

Report

The lab report should follow the format listed here. Please fully document your approach, any problems you encountered, and lessons learned from experimentation.