Abstract A microcontroller is a microprocessor that ... - Courses

100 downloads 1695 Views 21KB Size Report
A microcontroller is a microprocessor that controls everyday appliances such as microwaves and wristwatches. The microkits used by computer engineering ...
Abstract A microcontroller is a microprocessor that controls everyday appliances such as microwaves and wristwatches. The microkits used by computer engineering classes use a Motorola HC11 microcontroller for all of the processing. The HC11 microkits have 8 input switches, a single interrupt request button (IRQ), 8 output LEDs, and a two-line liquid crystal display (LCD). The HC11 kit also has a pulse accumulator, or a counter, which is incremented once per clock cycle. I wrote a program for the HC11 that computes two random numbers and adds or subtracts them, depending on the user input. The procedure I used to compute a random number is simply this: When an external interrupt is encountered (the interrupt request (IRQ) button is depressed), take the remainder of the pulse accumulator (counter) divided by 9. The result of this operation gives a number between 0 and 9. Do this twice, adding or subtracting the values and displaying the result in hexadecimal format to the LCD. Simultaneously with the addition or subtraction, step the lights once in their light pattern, flipping all of the on lights to off and vice versa. This series of instructions makes use of the pulse accumulator as well as internal interrupts (generated by my program) and external interrupts (the IRQ button). The pulse accumulator initializations were written by Stephen Petersen.

The HC11 Alexandra Carey & Richard Hughey University of California, School of Engineering Santa Cruz, CA 95064

Introduction The HC11A8 is the first microprocessor in a line of HC11 processors by Motorola. These microkits are used in a few computer engineering courses at UCSC. The first of these courses is computer engineering 12c, introduction to computer organization. In the second half of this class, the students are asked to program in HC11 assembly language, which gives students an abstract view of the way a computer works. Initially, the program (a hexadecimal adder and subtractor) prints a welcome message and enters an infinite loop waiting for an interrupt. The interrupt is received in the form of the IRQ pin by the switches, and will begin the program. Before depressing this button, select a function by entering the binary pattern string on the switches. The valid inputs are listed in Figure 1. After selecting the functionality and depressing the IRQ, the first thing the user will notice is that the light emitting diodes (LEDs) are flashing with a pattern – each light alternates from on to off. Next, the user may notice the LCD has displayed the first problem. There is a short delay before the answer is produced. The user does not need to input the answer, only to think of it or say it out loud. An example of the output is in Figure 3. The program then waits indefinitely, flashing the lights, until the IRQ is depressed again, and then performs the requested action.

Materials and Methods - Algorithm Initialization • Download and execute program. The program is now known as the resident program. • Initialize Registers. See Figure 2 for more information on the registers available to the user. • Store the address of the IRQ interrupt service routine (ISR) by storing the address of the ISR to the address of the IRQ pin on the interrupt table. The ISR is simply a procedure that will handle the interrupt – that is, when an interrupt occurs, it will be sent to this location in memory and will execute the code there until told to go back. The interrupt table holds information about all sorts of different interrupts that could occur while executing a resident program. • Clear the LCD and display welcome messages.

Main loop • Enter the main loop. Here, the light pattern flips all ones to zeros and all zeros to ones and the user input (coming from the SWITCHES) locks by storing the bit pattern to memory. The bit pattern is then checked against one of three input values: 0000 0000, 0000 0001, and 0000 0011 for add, subtract, and restart. All other values are regarded as the last operation processed. • Perform some preliminary steps before computation. If the user wishes to add, then store a plus sign (+) to a variable in memory for later use. If subtracting is selected, store a minus sign (-). Restarting will send the program back to the point of displaying initial welcome messages to the LCD. • Reset the user input and resume operation from the main loop.

Interrupt processing • When the IRQ is depressed, immediately rush to the ISR for the IRQ. Do not even wait to finish the instruction in execution! On the way there, save all of the important information (stored in the registers and accumulator) onto the system stack. • From within the interrupt, compute the random numbers, add or subtract them, and display the result. Next, clear the user input and return, restoring all saved registers.

Calculation and Display • Generate the pseudorandom numbers by finding the remainder of division of the pulse accumulator by 9. This gives a value between 0 and 8. If the two random numbers are not in the 0-9 range, add hexadecimal 37 to boost them up to the uppercase hex values on the ASCII table. Else add #$30 (hexadecimal) to make them ASCII numerals. • Sort in descending order. This will guarantee that there will be no negatives! • Print the numbers, displaying the sign (+/-) between them. Print the equal sign. • Adding or subtracting the two values usually will give a hexadecimal result between 0 and F. Convert the result to a printable character by adding the appropriate hexadecimal values. Create a short delay (thinking time!) and display the result in the same manner. • Return from interrupt to Main loop, restoring variables from stack.

Results Unanticipated Features (Bugs) 1. Lights flash slower when switches are 0000 0011. This is because the restart routine will display welcome messages to the LCD. Each message has a short delay, thus slowing the entire procedure. 2. As soon as value on SWITCHES changes to 0000 0011, the program goes to restart mode, not waiting for the IRQ to be depressed. 3. Lights do not flash while displaying answer. This is because the answer is calculated and displayed from within the interrupt, and the light flashing routine is in the main program. 4. Interrupts are not debounced. That is, one press of the IRQ may spawn several nested interrupts. The consequence is a number of result digits are displayed onto the screen. 5. Only one digit arithmetic is supported. 6. Result of addition of 8+8=G, which is not a hexadecimal value.

Future Implementations 1. Change flashing lights routine to use pulse accumulator instead of the more archaic WAIT function. This will fix both the lights not flashing while the answer is displayed and the slow flashing while in reset mode. 2. Multiple digit answer support!

Dictionary • HC11 The HC11 is a one of a line of microcontrollers produced by Motorola. The one in this demonstration is the HC11A8. • Hexadecimal Base 16. The hexadecimal digits are 0-9,A-F. • Interrupt Any asynchronous signal to the processor that tells it that something important is happening. An example of an interrupt is the IRQ button being depressed. • Interrupt Table This is a table that holds information about all sorts of different interrupts that could occur while executing a resident program. • IRQ Interrupt ReQuest. Most commonly referring to the INTERRUPT pin on the HC11 board, to the left of the switches. • ISR Interrupt Service Routine. This is the interrupt procedure that is called when the specified interrupt occurs. In this case, ISR refers to the procedure to handle the IRQ. • LCD Liquid Crystal Display. This, the small screen, is the main source of output on the HC11. • LEDs Light Emitting Diodes. A secondary source of output of the HC11. There are 8 active-low LEDs onboard the HC11. This means that when a value 0 is passed to the LED, the light is on. Inversely, when a value 1 is passed, the light is off. LEDs is also a place in memory, the physical address of the LEDs.

• Microcontroller A usually simple microprocessor used to control everyday appliances. • Pseudorandom number As random as the kit will get! Because the numbers are based off of a system clock, the randomness will always be calculated (and therefore not really random). • • Pulse Accumulator An accumulator that is incremented every 4.096ms by the system clock. • Resident Program The user program that has most recently been downloaded onto the microkit. • SWITCHES A series of input switches. Together, these produce 8 bits of binary input. When a switch is up, or towards the LEDs, then the value it produces is 1. Otherwise the switch is down and the value is 0. SWITCHES is usually uppercase because it also refers to a place in memory, the physical address of the switches. • System Stack A stack is an important abstract data type based on the principle of placing information on top of other information, thus forming a stack. The last information on the stack will be the first to be retrieved, and vice-versa. A stack pointer points to the top free location on the stack, so the user will always know where his or her data is relative to the stack pointer. The HC11 provides a handy implementation of the stack by allowing the user to modify the system stack. A benefit of using the system stack over a user-defined stack is that the system stack already exists and does not need to be

allocated by the user. HC11 stack instructions are also very user-friendly in that they increment and decrement the stack pointer for the user.

References 1. Motorola. M69HC11 Reference Manual. Motorola Inc., 1991. 2. Petersen, Stephen. http://www.cse.ucsc.edu/classes/ cmpe012/Fall99/pacc_ex.asm. 1999. Acknowledgements 1. Guilford, Jason. Debugging. 2. McIntire, Cliffton. Debugging.

Figure 1. SWITCHES input. 0000 0000 add 0000 0001 subtract 0000 0011 restart program Make the switches portray these bit patterns to perform the desired function.

Figure 2. HC11 Registers. A D

B [========|========] [=================]

X

[=================]

Y

[=================]

Accumulators A and B are the top and bottom halves of D. X and Y are index registers (normally used for addresses and simple 16-bit computations).

Figure 3. Example output. ———————HEX Calc INTR - continue ———————[press interrupt] Quick! 6+4=A INTR - continue ———————[press interrupt to loop] ———————Quick! 6+4= INTR - continue ———————[think here]

———————-

Suggest Documents