Sei sulla pagina 1di 11

Lab 2 8051 Registers, Stack, Ports, Trainer, and Program Download

INTRODUCTION:
While assemblers/compilers allow you to assemble a given program, you must run the program on an actual microcontroller to test its operation. The assembler creates an executable file with the .hex extension. This hex file must be downloaded into a microcontroller. In older systems, the microcontroller had to first be taken out of the system board and placed into an eraser to erase its contents. After the contents were erased, a PROM burner was used to burn the hex file into the chip. With the advent of Flash memory in recent years, microcontrollers are equipped with a loader that enables a program to be downloaded into the chip without removing it from the system board. The DS89C450 chip used in the 8051 Trainer has a built-in serial bootloader that allows the hex file to be downloaded into the chip with the use of a x86 PC serial port. Windows HyperTerminal software is used to access the chips contents via the serial port of the x86 PC. The HyperTerminal enables you to both delete and program the chips contents. This lab shows the process of accessing the Flash contents of the 8051 Trainer chip, deleting the old contents, and downloading a new hex file into it. You will use a simple program such as toggling bits to verify the operation of the 8051 Trainer. This will ensure the port is operating properly before you connect the port to devices such as an LCD or a stepper motor. Note: Before connecting a microcontroller to any device, first run a toggle program to make sure the CPU and ports are working properly.

OBJECTIVES:
To examine the flag bits of the PSW To examine the stack To learn the operation of the MDE 8051 Trainer To download a program that tests the 8051 ports

REFERENCE:
Mazidi, Muhammed Ali, Janice Mazidi, and Rolin McKinlay. The 8051 Microcontroller and Embedded Systems. Upper Saddle River, NJ: Pearson Prentice Hall, 2006. o Chapters 2, 3, and 4

MATERIALS:
8051 Assembler 8051 Trainer 1

Lab 2 8051 Registers, Stack, Ports, Trainer, and Program Download


ACTIVITY 1
1. Write and assemble the following program and examine the PSW. The program will add three pairs of numbers. At each operation, the PSW will update based on the contents of the ACC and the changes to the ACC: PSW.CY bit is high when there is a carry from ACC bit 7. PSW.AC bit is high when there is a carry from ACC bit 3 to ABB bit 4. PSW.P bit is high when ACC has an odd number of ones.
ORG MOV ADD MOV ADD MOV ADD NOP 0000H A, #38H A, #2FH A, #9CH A, #64H A, #88H A, #93H

;A=67H, CY=0, AC=1, P=1 ;A=00H, CY=1, AC=1, P=0 ;A=1bH, CY=1, AC=0, P=0 ;No operation- helps with debugging

2. Use the simulator to single-step and examine the registers, paying close attention to the PSW registers. Start Debug and make sure the Main Registers window and Bit window are opened. View > Main Registers, Main > Data Dump > Bit View. Locate Bank D0; this is the PSW. The LSB is to the left, MSB to the right as follows:
P -OV RS0 RS1 F0 AC CY

Initially, the PSW should read 01 because the ACC contains an odd number of ones. D0 will appear as follows: 01 00 00 00 00 00 00 00 because the P bit is high. Review Figure 2-1.

Lab 2 8051 Registers, Stack, Ports, Trainer, and Program Download

Figure 2-1 3. Step through the program while carefully examining the PSW and D0 registers. 4. Click on each bit in D0 and examine the message at the bottom of the Bit window. Repeat this activity until you understand the relationship between the contents of ACC, PSW, and D0.

Lab 2 8051 Registers, Stack, Ports, Trainer, and Program Download


ACTIVITY 2
1. Write and assemble the following program and examine the SP and stack operations:
ORG 0000H MOV R0, #18H MOV R1, #12H MOV R2, #99H MOV R3, #10H MOV R4, #FFH MOV R5, #FFH MOV R6, #FFH MOV R7, #FFH ;SP=07 PUSH 3 ; R3 to Stack- SP=08 PUSH 1 ; R1 to Stack- SP=09 PUSH 0 ; R0 to Stack- SP=0A PUSH 2 ; R2 to Stack- SP=0B POP 4 ; top of stack to R4SP=0A POP 5 ; top of stack to R5SP=09 POP 6 ; top of stack to R6SP=08 POP 7 ; top of stack to R7SP=07 NOP

2. Make and debug the program. View the Main Registers and the Data windows. 3. Step through the program while carefully examining the values of R0 through R7, SP, 00, and 08. 4. This program initially loads four values into registers. The values are placed on the stack in a random order. 5. Then the stack is emptied and the values are placed in a different set of registers. 6. SP increments when data is pushed onto the stack and decrements when data is pulled from the stack. 7. Notice the order of the values is different.

Lab 2 8051 Registers, Stack, Ports, Trainer, and Program Download


ACTIVITY 3
The 8051 Trainer

Figure 2-2 1. Examine the Trainer (Figure 2-2) carefully. You should observe the following features: POWER LED5on the left and PRG LED6 program mode on the right PRG / RUN switch to select run or program mode RESET button Bank of eight DIP switches Bank of eight LEDs 5VDC jack Two 9-pin DIN ports labeled Serial 0 and Serial 1 Two 20-pin male/female combination of connectors labeled from left to right: J1, J3, J4, J2 Two 8-pin male/female combinations of connectors, one set connected to the DIP switches and the other to the LEDs Two 2-pin jumpers labeled S1-RXD and S2-TXD (These are for serial communication and will be covered later.) Maxim DS89C450 microprocessor, 74HC244 tri-state buffer (to drive the LEDs), and MAX232 serial interface along with an 11-MHz crystal and various components

Lab 2 8051 Registers, Stack, Ports, Trainer, and Program Download


2. Examine the left-side 20-pin male/female combination of connectors J1 and J3. They are labeled P1.0 through P1.7 and P3.0 through P3.7. These are connected to the microprocessor ports 1 and 3, respectively. Notice that Port 3 has additional labels such as RXD and TXD. These are for serial communication signals and will be covered later. 3. Examine the right-side 20-pin male/female combination of connectors J4 and J2. They are labeled P0.0 through P0.7 and P2.0 through P2.7. These are connected to the microprocessor ports 0 and 2, respectively. Notice that these ports also have additional labels such as AD0 and A15. Port 0 acts as both an 8-bit address bus and an 8-bit data bus. Port 2 can also function as the MSB of a 2-byte external address bus. These will be covered more detail later. 4. Examine the 8-pin male/female combination labeled J6 LED CONNECTORS. Pin 1 is connected to LED 1, pin 2 to LED 2, and so on. Applying VCC on any pin of J6 will illuminate the related LED. These pins will be connected to the ports to view the output of the microprocessor and to verify the program operation. 5. Examine the 8-pin male/female combination labeled J5 DIP SWITCH CON. Pin 1 is connected to switch 1, pin 2 to switch 2, and so on. Each pin has either GND or VCC available, depending on the switch setting. These pins will be connected to the ports to provide input to the microprocessor to verify your program operation. 6. The PRG / RUN switch transfers the Trainer from program to run mode. Leave the Trainer in RUN mode by default. 7. The reset button resets the Trainer. 8. The 9-pin din connector labeled SERIAL 0 will be connected to the PC. The other serial port will be discussed later. At this point, you should be ready to establish a connection between the PC and the Trainer.

Lab 2 8051 Registers, Stack, Ports, Trainer, and Program Download


ACTIVITY 4
You will learn to interface the Trainer to the x86 PC and establish a terminal connection. You will complete the Trainer tutorial How to Test, Download, and Run a Program on the MDE8051 Trainer Board in Appendix K. Before beginning the tutorial, read and complete the following: 1. Attach or verify that the interface (RS232 cable or USB-to-RS232 cable) is connected at J10- SERIAL 0 and the correct port on the PC. Your instructor will verify the connection. NOTE: Some PC systems will not have a COM port and will use a USB-to- RS232 cable. 2. Make sure the PRG / RUN switch is in the RUN position. 3. Using the supplied transformer, apply power to the Trainer. Make sure the POWER LED illuminates. 4. If the PCs operating system is Windows Vista, HyperTerminal will not be the default terminal service. In this case, your instructor will provide the operating instructions to create a terminal connection. 5. If the lab setup uses a USB cable, you will need to determine the correct COM port. Your instructor will assist in this activity. You will need to view the Ports setting in the Windows Device Manager to determine the proper COM port. After completing this activity, you should be able to download and execute the programs.

Lab 2 8051 Registers, Stack, Ports, Trainer, and Program Download


ACTIVITY 5
Test the operation of the ports of your 8051 Trainer as follows: 1. Ensure the Trainer is interfaced correctly to the PC. 2. Prepare the Trainer by connecting eight wires from Port 0 to the LED bank as follows: J4 J8 P0.0 to -1 P0.1 to -2 through P0.7 to -8 3. Assemble the following test program:
MAIN: ORG 0000H MOV P0, #55H MOV P1, #66H MOV P2, #33H ACALL DELAY MOV P0, #0AAH MOV P1, #099H MOV P2, #0CCH ACALL DELAY SJMP MAIN ;-----------------------------R5, #250 R4, #29 R3, #100 ;Maximum value for R5 is 255 ;Change to 44, 88, 176 ;NOTE THESE NUMBERS ARE DECIMAL (NO H)

DELAY: MOV LOOP3: MOV LOOP2: MOV LOOP1: NOP NOP NOP DJNZ DJNZ DJNZ RET END

R3, LOOP1 R4, LOOP2 R5, LOOP3 MAIN

4. The 8051 requires executable code to be in hexadecimal format. Make sure the hex option in the linker is checked. See the last page of Appendix I, Basic ProView Software Tutorial, in the textbook. After making the project executable (Project > Make F9), verify that a hex file is created and saved in the appropriate project folder. You should find multiple files: .asm, .lst, .src, .hex and others. 5. Establish a connection to the Trainer and download the hex file to the Trainer.

Lab 2 8051 Registers, Stack, Ports, Trainer, and Program Download


Follow the steps shown in Appendix K of this lab manual to download the hex file into the 8051 Trainer. 6. Move the slide switch to the RUN position and press the RESET button to run the test program. 7. The program is alternately updating Port 0s even and odd bits. LED bits 0, 2, 4, 6 should be alternately flashing with bits 1, 3, 5, 7. 8. Examine the program closely. You can see that Ports 0, 1, and 2 are alternately loaded with 55H and AAH. Reviewing the binary values of each should help you understand what is happening on the Trainer. 9. The Trainer LEDs are demonstrating what is happening at Port 0. Port 1 and 2 have different patterns. You can rewire the LED bank to those ports. Power the Trainer off, move the wires, re-apply power, and press reset. 10. Examine the program closely. You can see three nested loops at LOOP3, LOOP2, and LOOP1. LOOP1 will repeat the six instructions from LOOP1: To the RET until R3 is decremented down to 0, which is 100 repetitions. LOOP2 will repeat LOOP1 until R4 is decremented down to 0, which is 29 times, for a total of 29 times 100, or 2900 total repetitions. LOOP3 will repeat LOOP2 until R5 is decremented down to 0, which is 250 times, for a total of 250 times 29 times 100, or 735000 total repetitions. You can figure out the time it takes for the ports to update by understanding the length of a clock cycle and the number of cycles within the loop. Multiply that value by the total repetitions to obtain the frequency of the port update. 11. Change the value of R3 on the line of code at LOOP3. Save and remake the changes. Download to the Trainer. You may need to power cycle the Trainer first. Run the new program. What changed? 12. Can you determine what value of R3 would yield a frequency of 1 Hz? Try it and see if you can obtain 1 Hz flashing at the LEDs.

Lab 2 Worksheet
Name: _____________________________ Date: ____________Class: _____________

1. Find the value of the CY flag after the execution of the following code. (a) MOV A,#85H ADD A,#92H

(b)

MOV A,#15H ADD A,#72H

(c)

MOV A,#0F5H ADD A,#52H

(d)

MOV A,#0FF INC A

2. Upon processor reset, what is the value in the SP register? ____________

3. Upon pushing data onto the stack, the SP register is _______________________. (decremented, incremented)

4. Upon popping data from the stack, the SP register is _______________________. (decremented, incremented)

5. Can you change the value of the SP register? If yes, explain why you would want to do that. ____________________________________________________________

6. The stack uses the same area of RAM as bank ______.

7. List the names of all the 8051 I/O ports.

10

Lab 2 Worksheet
8. Which I/O ports were tested in this lab?

9. Name the pins of P3, I/O Port 3, that are alternatively used for TxD and RxD.

10. Which I/O ports of the 8051 have internal pull-up resistors?

11. Which of the 8051 I/O ports require the connection of external pull-up resistors in order to be used for I/O?

12. Upon reset, all of the 8051 I/O ports are configured as _____________. (output, input)

11

Potrebbero piacerti anche