CS122A: Lab 5


Objectives:
Introduction
Microcontrollers often need to perform operations based upon some length of time. This can be used for communication purposes (e.g., serial communication), event counters, measuring time, or performing operations at certain intervals.

The 80c51 has 2 16-bit timer/counters registers which can be configured to perform a variety of operations. When configured as a timer, the timer will count from the user specified value up to the overflow value (65,535 if the timer is configured to 16-bit mode). Upon overflowing, an interrupt will be generated, at which time an interrupt service routine can be performed. Knowing this, you can measure more precise time intervals rather than rely on the arbitrary "delay" functions you have seen in previous labs.

In this lab you will build a reflex timer to measure a person's reaction time. The reflex timer works by displaying "Go!" on an LCD after a random length of time and the user will have to respond by pushing a button as quickly as possible. If the user presses the button within one second of "Go!" being displayed, you will output the reflex time, in milliseconds (10-3 seconds), on the LCD. Otherwise, you should notify the user that he or she was too slow.

Additionally, you should also detect early presses (before "Go!" has been displayed as cheat attempts and respond accordingly.

Part 1
Your circuit will feature an LCD (with a supporting resistor), an 8051 (with a supporting oscillator/crystal), and a button (with a supporting resistor). It is up to you to decide how you wish to wire the circuit.

You should refer to the 8051 Pinout Diagram in assembling your circuit.

Hint: You should connect the button to your circuit in the same manner that you connected the DIP switch. Additionally, the button we will provide you has four leads, two on either side. You should use the leads on a single side, like so:

Note: You should strive to keep your board wiring looking neat and structured. Your grade depends on it.

Part 2
You will now write your main reflex timer code. A more precise description of the program flow is as follows:
  1. When the 8051 is powered on and reset, the LCD should display Reflex Timer.
  2. When the user presses the button, the LCD should display Get Ready....
    • If the user presses the button during this time, display a cheater warning and end the game.
  3. After a random, non-zero period of time less than one second, Go! should be displayed on the LCD.
    • If the user presses the button within one second, display the reflex time (in ms) and end the game.
    • If one second elapses, display a notice that the user was too slow and end the game.
  4. Another game can be started by pressing the button.
Note: You do not need to provide a seed for your random numbers.

Hint: Incremental development is the path to success. Begin with something simple. Try to answer the following questions: Does the base code compile? How do I read a value from the button? What values does the button provide under different conditions? When is my timer interrupt being called? Proceed in the same vein.

Extra Credit
Your game should wait for both players to press the button (or time out), display the winner (e.g. "Player 1 Wins!"), and after a slight delay, display both players' reflex times.
Timer Register Reference
TMOD Timer Mode Register --------------.-------------- |Gate|C/T|M1|M0|Gate|C/T|M1|M0| --------------`-------------- <-- Timer 1 -> <-- Timer 0 -> Gate Gating Control. 0 = Timer enabled 1 = Timer enabled if INTx is high C/T Counter or Timer Selector 0 = Internal count source (clock/12) 1 = External count source (Tx pin) M1, M0 Mode Control M1 M0 Mode ---------------------------------- 0 0 Mode 0, 13 bit count mode 0 1 Mode 1, 16 bit count mode 1 0 Mode 2, Auto reload mode 1 1 Mode 3, Multiple mode
TCON Timer Control Receiver Register ---------------.------------------- |TF1|TR1|TF0|TR0| | | | | ---------------`------------------- <Timer Controls><Unused for timers> TRx Timer x run control 0 = Timer not running 1 = Timer running TFx Timer x flag 0 = timer has not rolled over 1 = timer has rolled over
Using the PDS51 Emulator
The PDS51 emulator provides a fast way to test your 8051 code without needing to wait in line and program a chip! The emulator provides its own clock signal and power, so you should not try to power the emulator externally. Be sure to set the power supply to 5 volts before you connect it to your breadboard. When you initially power your circuit, check to see if the "Overload" indicator lights up. If it does, immediately turn off the power supply and check your circuit for shorts. A multimeter would help in this instance. Your program is now running. There is no need to press the red "reset" button on the emulator board (doing so will interrupt the connection between your PC and the emulator).
End of Lab
Prepared 10/14/04 by Ryan Mannion