8051 io ports

12
ECP2216 Microcontroller and Microprocessor Systems Laboratory Experiment 1 ___________________________________________________________________________ _______________ MP1: Familiarization with the SK-8051 Trainer and Programming Fundamentals Objectives: To get familiarised with the SK-8051 trainer. To demonstrate program development procedures using the trainer. To program the trainer using basic instructions. Equipment: SK-8051 Trainer Desktop computer with RS232 interface RS232 cable Software: FET89C5X IDE for MCS-51 assembly language programming Tera Term Pro terminal program for serial communication (A) Introduction to SK-8051 Trainer The SK-8051 is built around the Atmel AT89C51 microcontroller, which is compatible with the industry standard MCS-51™ instruction set and pinout. There are a lot of features on board. However, this laboratory sheet will provide only sufficient information to enable you to successfully complete the laboratory exercises. Table 1.1 gives the address map of the SK-8051. It uses the Von Neuman architecture: the 64Kbytes are used for both program and data memory. I/O devices are also memory-mapped as given in Table 1.2. Table 1.1 Address map of SK-8051 Address Description Remarks 0000..7FFF ROM Program memory (ROM) 8000..FEFF RAM Program memory & data memory (RAM) FF00..FFFF I/O I/O devices Table 1.2 I/O map of SK-8051 Address Definition Remarks FF00 LCD_CMD LCD command and data registers FF01 LCD_DATA Multimedia University Page 1 of 12

Upload: danz1994

Post on 22-Jun-2015

12 views

Category:

Documents


5 download

DESCRIPTION

document on 8051 io ports

TRANSCRIPT

Page 1: 8051 Io Ports

ECP2216 Microcontroller and Microprocessor Systems Laboratory Experiment 1__________________________________________________________________________________________

MP1: Familiarization with the SK-8051 Trainer and Programming Fundamentals

Objectives: To get familiarised with the SK-8051 trainer. To demonstrate program development procedures using the trainer. To program the trainer using basic instructions.

Equipment: SK-8051 Trainer Desktop computer with RS232 interface RS232 cable

Software: FET89C5X IDE for MCS-51 assembly language programming Tera Term Pro terminal program for serial communication

(A) Introduction to SK-8051 Trainer

The SK-8051 is built around the Atmel AT89C51 microcontroller, which is compatible with the industry standard MCS-51™ instruction set and pinout. There are a lot of features on board. However, this laboratory sheet will provide only sufficient information to enable you to successfully complete the laboratory exercises.

Table 1.1 gives the address map of the SK-8051. It uses the Von Neuman architecture: the 64Kbytes are used for both program and data memory. I/O devices are also memory-mapped as given in Table 1.2.

Table 1.1 Address map of SK-8051Address Description Remarks

0000..7FFF ROM Program memory (ROM)8000..FEFF RAM Program memory & data memory (RAM)FF00..FFFF I/O I/O devices

Table 1.2 I/O map of SK-8051Address Definition Remarks

FF00 LCD_CMD LCD command and data registersFF01 LCD_DATAFF10 KEYPAD 8 bit buffer input for keypadFF20 PORT_A 8255 PPI FF21 PORT_BFF22 PORT_CFF23 PPI_CMDFF28 BUTTON 8 bit inverted buffer inputFF2C FND 8 bit latched output for FNDFF38 L563 8 bit inverted latch output for LED

The trainer has a monitor program stored in a 32-Kbytes EPROM. The user program can be downloaded to any part of RAM with addresses from 8000H to FEFFH. This RAM is used for both program and data memory.

Multimedia University Page 1 of 9

Page 2: 8051 Io Ports

ECP2216 Microcontroller and Microprocessor Systems Laboratory Experiment 1__________________________________________________________________________________________

The SK-8051 can operate in either keypad or serial mode. In keypad mode, all the controls are issued from the trainer’s keypad. In serial mode, the terminal program at the host PC takes the user’s PC keyboard commands and sends them to the microcontroller through the RS232 serial interface.

A.1 Keypad Mode of SK-8051

After switching on the trainer, press RES to get into the keypad mode. The LCD will display the message "SK-8051 (Ver. B) Keypad Monitor..". In keypad mode, all commands come from the keypad. Table 1.3 lists the keys and the corresponding operations.

Table 1.3 SK-8051 Keypad keys and operationsKey Operation

RES Reset the systemADDR Used to jump to specific address location of the memory

INC Increases the address location by oneDEC Decreases the address location by one

DATA Used to change the data(1 byte) at the current address location GO Starts execution of the user program at the current address

locationINT0 Interrupt 0 of the microcontroller. Low when pressedT0 Timer 0 interrupt. Low when pressed

STEP Single step. Connected to INT1 of the microcontroller. Used at keypad and serial mode.

Objective: To load opcodes & operands in the memory & run the program to turn on LEDs one at a time.

Enter the following hexadecimal values at the specified memory locations using the keypad of the trainer following the steps below.The code and the data to be entered:

First screen Second screen

Multimedia University Page 2 of 9

Page 3: 8051 Io Ports

ECP2216 Microcontroller and Microprocessor Systems Laboratory Experiment 1__________________________________________________________________________________________

Procedure:1. Press RES to reset the system and return to keypad mode.2. Press ADDR to go to address 8000H.3. Press DATA to input data starting at 8000H, as shown in the first screen.4. Once all 8 bytes of data have been entered, press ADDR to change to address 8008H.5. Press DATA to input data starting at 8008H, as shown in the second screen.6. Once the data has been entered, press ADDR and change the address 8000H.7. Press GO to execute the program at 8000H. The LCD will display the message "Now

program is running at 8000H".8. Press STEP to execute the next instruction. 9. Continue to single-step the program by repeatedly pressing STEP, observe the LEDs and

complete the keypad columns of Table A1.1 in the Answer Sheet.

A.2 Serial Mode of SK-8051To use the serial mode, you need a terminal program. Run "Tera Term Pro" from the computer. Select “Serial” on “Tera Term: New Connection” dialog box. Do not change any other settings. Should you have any enquiries, ask the instructor.

Press RES and then press any numeric keys on the trainer, it will now be in serial mode. The LCD will display the message "SK-8051 (Ver. B) Serial monitor..". The terminal program will display the following message on the PC screen:

SK_8051 Serial Monitor program.Copyright reserved by SYSTEMKIT Co.

SK8051>_"H" is the command to invoke the help menu. The following commands are useful:

"D" to dump the contents of external memory. The default dump location is from 8000H. If a command like "D9000" is entered, the memory dump will start at address 9000H. "Enter" to continue dumping and "." to stop.

"M" to revise the data in the external data memory. The default starting address is 8000H. If a command like "M9000" is entered, it will start at address 9000H. "Enter" to increase the address location and "." to stop.

"G" to start the program execution. The default starting address for program execution is 8000H. If a command like "G8100H" is entered, program execution starts from 8100H.

You can assemble and download your program via the PC. Let us create and assemble the ‘led.asm’ program and download the hex code to the trainer.

Objective: To write the above program with mnemonics, to assemble, download & execute.Procedure:1. Execute ‘Fet89c5x IDE’ from Windows Desktop.2. Write the following assembly language program (case insensitive).

ORG 8000HL563 EQU 0FF38H

JNB IE1, $MOV SP, #50HMOV DPTR, #L563MOV A, #01H

LOOP: MOVX @DPTR, ARL ASJMP LOOPEND

Multimedia University Page 3 of 9

Page 4: 8051 Io Ports

ECP2216 Microcontroller and Microprocessor Systems Laboratory Experiment 1__________________________________________________________________________________________

3. Save the program as ‘led.asm’.4. Assemble & link the program by selecting ‘Compile’ from the pull-down menu and

invoke ‘Build’, or simply press ‘CTRL+F8’.5. If no error is reported, exit ‘Fet89c5x IDE’ (optional) & invoke ‘Tera Term Pro’.6. Reset the kit & press any hex key on the keypad.7. Download the file ‘led.hex’ from the PC to the kit. (At the ‘SK8051>’ prompt, press ‘L’

to signal the trainer that a program is to be downloaded. Select ‘File’ from the menu and then ‘Send File’ to select ‘led.hex’ from the dialog box. Click ‘Open’ to send the file). At the end of file downloading, the trainer will respond with the ‘SK8051>’ prompt.

8. Type ‘g8000’ at the prompt (in ‘Tera Term Pro’) and press ‘Enter’ (on the computer).9. Single-step the program by repeatedly pressing the ‘STEP’ key on the keypad & observe

the effect.10. Record the results in the serial columns of Table A1.1 and compare with the results

obtained from the keypad mode.

(B) Programming Exercises

IMPORTANT: Read Appendix A carefully on what you need to do in order to single-step through a program in this section.

B.1 Arithmetic InstructionsObjective: To observe execution of arithmetic instructions.Procedure:1. Follow the steps as outlined in Serial Mode of SK-8051 for writing, saving & assembling

the following program as ‘EXB1.asm’.ORG 0B000HJNB IE0, $MOV A, #16HMOV B, #29HADD A, BDA A ; 1st DA AMOV B, #54HADD A, BDA A ; 2nd DA AMOV A, #29HINC ADA A ; 3rd DA AMOV A, #49HMOV 30H, AADD A, 30HDA A ; 4th DA AADD A, #72HDA A ; 5th DA ASJMP $END

2. Download ‘patch.hex’ from ‘C:\SK8051”, following the steps specified in previous part.3. Next, download ‘EXB1.hex’ on to the trainer.4. Run the program by typing ‘g8000’ at the prompt (in ‘Tera Term Pro’) & press ‘Enter’.5. Single-step the program by repeatedly pressing the ‘INT0’ key on the keypad & observe

the effect.6. Single step through the instructions and note down the value of the accumulator (ACC),

program status word register (PSW), program counter (PC) and the next instruction in Table B1 of the Answer Sheet.

7. Was the value in the accumulator adjusted after every DA A instruction? Why?

Multimedia University Page 4 of 9

Page 5: 8051 Io Ports

ECP2216 Microcontroller and Microprocessor Systems Laboratory Experiment 1__________________________________________________________________________________________

B.2 Program Flow ControlObjective: To study the execution and usage of program flow control instructions.

The following program illustrates the use of the CJNE instruction. Assemble the program and download the hex file to the trainer. The program stores the value 0H in internal memory location 20H before executing the rest of the code. Run the program by typing ‘g8000’ in ‘Tera Term Pro’ and press ‘Enter’. Single-step the program by repeatedly pressing the ‘INT0’ key on the keypad and observe the effect. Note down the value of carry flag (after the CJNE instruction) and the final state of Port 1 (P1). Repeat this by changing the value stored in internal memory location 20H with 7FH and then with FFH. Record your observation in Table B2 on the Answer Sheet. What conclusion can be drawn? See Appendix B for more information.

ORG 0B000HJNB IE0, $

MOV PSW,#0H ; initialise PSW to 0HMOV 20H, #0H ; move 0H to memory location 20H

MOV P1, #0FFH ; move FFH to P1MOV R0, #20H ; move 20H to R0CJNE @R0, #7FH, J1 ; compare data pointed by R0 with immediate

data 7FH, ; jump to J1 if not equal

CLR P1.0 ; clear bit 0 of P1SJMP J3 ; short jump to J3

J1: JC J2 ; jump to J2 if carry flag is setCLR P1.1 ; clear bit 1 of P1SJMP J3 ; short jump to J3

J2: CLR P1.2 ; clear bit 2 of P1

J3: SJMP $ ; always jump back to itself (program ends)END

Important Note:Lab Assessment will be carried out during the lab session. It is the student's responsibility to complete the given tasks and report to the lab supervisor for assessment.

Multimedia University Page 5 of 9

Page 6: 8051 Io Ports

ECP2216 Microcontroller and Microprocessor Systems Laboratory Experiment 1__________________________________________________________________________________________

(End of experiment)Appendix A

Single Step Patch

The single step operation provided by the monitor does not preserve the PSW (Program Status Word). Program flows that depend on the flags (Z & C) are not correct in single step.

In order to overcome this monitor program bug download the monitor patch with file name PATCH.HEX into the trainer before you download your own program.

This patch is only for single step operation in serial monitor mode. The main monitor program handles the file downloading, system initialization, keypad mode and serial interface to the computer.

The starting address of the patch code is at 8000H. It is about 33kbytes. The user has to first download this patch upon power up if he wants to use the single step feature in serial monitor mode. The user program has to be at address B000H. The first instruction must be "JNB IE0, $" to enable single step operation.

With the patch and user program downloaded, program execution must begin at 8000H. In serial monitor mode, this is accomplished by pressing G at the keyboard. Then the monitor program displays the text "Program started at 8000H" at the terminal program. This will execute the patch initialization code. After initialization, the LCD displays a text "SK-8051 (PATCH) SERIAL MONITOR..". Then PC (Program Counter) is set to B000H to start executing the user program.

Each press of INT0 key executes one instruction at current PC. The SFRs (Special Function Registers) are displayed, followed by Internal RAM dump from 00H to 7FH. Then the next instruction at current PC is decoded and displayed at the terminal. Note that the memory contents for address above the current SP (Stack Pointer) are not preserved. The patch uses this memory area to preserve the microcontroller current context in single step operation. Also the register bank #3 is reserved for this patch.

To stop this operation, press RES key to reset the trainer to keypad monitor mode. Then press any of the hex keypad to enter serial monitor mode.

Multimedia University Page 6 of 9

Page 7: 8051 Io Ports

ECP2216 Microcontroller and Microprocessor Systems Laboratory Experiment 1__________________________________________________________________________________________

Appendix B

ADD A, <src-byte>Function: AddDescription: ADD adds the byte variable indicated to the Accumulator, leaving the result in the Accumulator. The carry and auxiliary-carry flags are set, respectively, if there is a carry-out from bit 7 or bit 3, and cleared otherwise. When adding unsigned integers, the carry flag indicates an overflow occurred.

OV is set if there is a carry-out of bit 6 but not out of bit 7, or a carry-out of bit 7 but not bit 6; otherwise, OV is cleared. When adding signed integers, OV indicates a negative number produced as the sum of two positive operands, or a positive sum from two negative operands.

Four source operand addressing modes are allowed: register, direct, register-indirect, or immediate.

CJNE <dest-byte>,<src-byte>, relFunction: Compare and Jump if Not Equal.Description: CJNE compares the magnitudes of the first two operands and branches if their values are not equal. The branch destination is computed by adding the signed relative-displacement in the last instruction byte to the PC, after incrementing the PC to the start of the next instruction. The carry flag is set if the unsigned integer value of <dest-byte> is less than the unsigned integer value of <src-byte>; otherwise, the carry is cleared. Neither operand is affected.

The first two operands allow four addressing mode combinations: the Accumulator may be compared with any directly addressed byte or immediate data, and any indirect RAM location or working register can be compared with an immediate constant

CLR bitFunction: Clear bitDescription: CLR bit clears the indicated bit (reset to 0). No other flags are affected. CLR can operate on the carry flag or any directly addressable bit.

DA AFunction: Decimal-adjust Accumulator for AdditionDescription: DA A adjusts the eight-bit value in the Accumulator resulting from the earlier addition of two variables (each in packed-BCD format), producing two four-bit digits. Any ADD or ADDC instruction may have been used to perform the addition.

If Accumulator bits 3 through 0 are greater than nine (xxxx1010-xxxx1111), or if the AC flag is one, six is added to the Accumulator producing the proper BCD digit in the low-order nibble. This internal addition sets the carry flag if a carry-out of the low-order four-bit field propagates through all high-order bits, but it does not clear the carry flag otherwise.

If the carry flag is now set, or if the four high-order bits now exceed nine (1010xxxx-1111xxxx), these high-order bits are incremented by six, producing the proper BCD digit in the high-order nibble. Again, this sets the carry flag if there is a carry-out of the high-order bits, but does not clear the carry. The carry flag thus indicates if the sum of the original two BCD variables is greater than 100, allowing multiple precision decimal addition. OV is not affected.

All of this occurs during the one instruction cycle. Essentially, this instruction performs the decimal conversion by adding 00H, 06H, 60H, or 66H to the Accumulator, depending on initial Accumulator and PSW conditions.

Note: DA A cannot simply convert a hexadecimal number in the Accumulator to BCD notation, nor does DAA apply to decimal subtraction.

Multimedia University Page 7 of 9

Page 8: 8051 Io Ports

ECP2216 Microcontroller and Microprocessor Systems Laboratory Experiment 1__________________________________________________________________________________________

INC <byte>Function: IncrementDescription: INC increments the indicated variable by 1. An original value of 0FFH overflows to 00H. No flags are affected. Three addressing modes are allowed: register, direct, or register-indirect.

Note: When this instruction is used to modify an output port, the value used as the original port data will be read from the output data latch, not the input pins.

JC relFunction: Jump if Carry is setDescription: If the carry flag is set, JC branches to the address indicated; otherwise, it proceeds with the next instruction. The branch destination is computed by adding the signed relative-displacement in the second instruction byte to the PC, after incrementing the PC twice. No flags are affected.

JNB bit, relFunction: Jump if Bit Not setDescription: If the indicated bit is a 0, JNB branches to the indicated address; otherwise, it proceeds with the next instruction. The branch destination is computed by adding the signed relative-displacement in the third instruction byte to the PC, after incrementing the PC to the first byte of the next instruction. The bit tested is not modified. No flags are affected.

MOV <dest-byte>,<src-byte>Function: Move byte variableDescription: The byte variable indicated by the second operand is copied into the location specified by the first operand. The source byte is not affected. No other register or flag is affected.

This is by far the most flexible operation. Fifteen combinations of source and destination addressing modes are allowed.

MOVX <dest-byte>,<src-byte>Function: Move ExternalDescription: The MOVX instructions transfer data between the Accumulator and a byte of external data memory, which is why “X” is appended to MOV. There are two types of instructions, differing in whether they provide an 8-bit or 16-bit indirect address to the external data RAM.

SJMP relFunction: Short JumpDescription: Program control branches unconditionally to the address indicated. The branch destination is computed by adding the signed displacement in the second instruction byte to the PC, after incrementing the PC twice.

Therefore, the range of destinations allowed is from 128 bytes preceding this instruction 127 bytes following it.

RL AFunction: Rotate Accumulator LeftDescription: The eight bits in the Accumulator are rotated one bit to the left. Bit 7 is rotated into the bit 0 position. No flags are affected.

Program Status Word (PSW) Register

CY – Carry FlagAC – Auxiliary Carry FlagF0 – Flag 0RS1/RS0 – Register Bank SelectOV – Overflow FlagP – Parity Bit

Multimedia University Page 8 of 9

Page 9: 8051 Io Ports

ECP2216 Microcontroller and Microprocessor Systems Laboratory Experiment 1__________________________________________________________________________________________

(End)

Multimedia University Page 9 of 9