Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 12

FACULTY OF ENGINEERING

LAB SHEET

MICROCONTROLLER AND
MICROPROCESSOR SYSTEMS

ECE2216
TRIMESTER 1 (2018/2019)

MP1: Familiarization with the Trainer and Programming Fundamentals

*Note: On-the-spot evaluation may be carried out during or at the end of the experiment.
Students are advised to read through this lab sheet before doing experiment. Your
performance, teamwork effort, and learning attitude will count towards the marks.
ECE2216 Microcontroller and Microprocessor Systems Laboratory Experiment 1
__________________________________________________________________________________________

MP1: Familiarization with the Trainer and Programming Fundamentals

Objectives:
 To get familiarised with the Atmel USB programmer.
 To demonstrate program development procedures using the trainer.
 To develop assembly programs using MCS-51 instruction set.

Equipment:
 Atmel USB Programmer
 Desktop computer with USB interface
 USB cable
 5V DC Power Supply Unit
 Breadboards

Software:
 Notepad++ with MCS-51 assembler
 ProgISP (for Atmel USB Programmer)

Electronic Components:
No Component Quantity
1 Crystal 12MHz 1
2 Micro-tact switch 2
3 1K 9-way resistor network 1
4 100 ohm resistor 1
5 8.2K resistor 1
6 22pF capacitor 2
7 10uF capacitor 1
8 AT89S51 or AT89S52 1
9 Light emitting diode (LED) 6

Introduction
The AT89S51 is a low-power, high-performance CMOS 8-bit microcontroller with 4K bytes
of In-System Programmable Flash memory, which is compatible with the industry standard
MCS-51™ instruction set and Intel 8051 microcontroller pinout. It provides the following
standard features: 4K bytes of Flash, 128 bytes of RAM, 32 I/O lines, two data pointers, two
16-bit timer/counters, a five-vector two-level interrupt architecture, a full duplex serial port,
on-chip oscillator, and clock circuitry.

Assembly Language is a computer language lying between the extremes of machine language
and high-level language. In this workshop, we will focus on assembly language programming
using the MCS-51™ instruction set, which is the instruction set for the 8051 family of
microcontroller. As 8051 is an 8-bit microcontroller, its instruction set consists of 8-bit
opcodes. Some instructions have one or two additional bytes for operand (data or addresses).
In all, there are 255 instructions, of which 139 are 1-byte instructions, 92 2-byte instruction,
and 24 3-byte instruction. The way in which data and addresses are specified (either as source
or destination) in the instruction is determined by the addressing mode. In MCS-51, 8
addressing modes are available. These addressing modes are register, direct, indirect,
immediate, relative, absolute, long and indexed.
ECE2216 Microcontroller and Microprocessor Systems Laboratory Experiment 1
__________________________________________________________________________________________
Numerous programs are available to facilitate software development for the 8051
microcontroller. In this context, a cross assembler is used to translate the assembly
instructions to machine language binary codes on a host computer with a CPU other than the
8051. The source codes may be written using any text editor or a specific IDE. Nonetheless,
the assembled machine codes cannot be executed directly on the host computer if its CPU is
not an 8051. Execution of the machine codes requires either hardware emulation or software
simulation of the 8051.

Exercise A
Objective: To turn on and off a light emitting diode (LED) at a predefined rate.
Procedure:
1. Execute ‘Notepad++’ from Windows Desktop.
2. Click on ‘Language’ on the pull-down menu and select ‘Assembly_8051’.
3. Type the assembly program (case insensitive) in Table A.
Table A: Assembly program for Exercise A
Line Assembly codes
1 ORG 0000H
2 MOV P0, #0 ; turn off all LEDs
3 MOV A, #4 ; move 4 into accumulator (for 1s delay)
4 LOOP: CPL P0.0 ; complement (invert) P0.0
5 CALL DELAYS ; call subroutine DELAYS to create a delay
6 SJMP LOOP ; jump back to repeat from LOOP

7 DELAYS:
8 MOV R3, A ; load the no. of time RPT loop is repeated
9 RPT: MOV R2, #250 ; load the no. of time AGN loop is repeated
10 AGN: MOV R1, #250 ; load the no. of time HERE loop is repeated
11 HERE: NOP
12 NOP
13 DJNZ R1, HERE ; jump to HERE until R1 is decreased to 0
14 DJNZ R2, AGN ; jump is AGN until R2 is decreased to 0
15 DJNZ R3, RPT ; jump to RPT until R3 is decreased to 0
16 RET ; exit subroutine
17 END
4. Save the program as ‘EXA1.asm’.
5. Assemble and link the program by pressing ‘F6’ key and click ‘Ok’, or simply press
‘CTRL+F6’.
ECE2216 Microcontroller and Microprocessor Systems Laboratory Experiment 1
__________________________________________________________________________________________
6. If there is any error, refer to the error message to check which line of codes is in error and
refer to Table A for the correct assembly codes.
7. Carefully insert the microcontroller into the programmer (Important: make sure that the
microcontroller is inserted in the correct orientation).
8. Download the file ‘EXA1.hex’ from the PC to the microcontroller (refer to Appendix A on
how to download the file).
9. Construct the microcontroller circuit as shown in Figure A1 below.

Figure A1: Microcontroller Circuit Connection for MP1


10. Connect VCC and VSS to 5V and Gnd terminal of the DC power supply unit (PSU),
respectively.
11. Before switching on the PSU, make sure that the microcontroller circuit is constructed
correctly.
12. Switch on the PSU and verify that the program is working as expected (the LED
connected to P0.0 is blinking continuously).
13. Modify the assembly program in Table A so that the LED will blink faster.
14. After modifying the codes, repeat Step 3 to 5.
15. Next, switch off the PSU and carefully unplug the microcontroller from the breadboard
and repeat Step 6, 7 and 8.
16. Carefully insert the microcontroller into the breadboard in the correct orientation.
17. Record down the line number of the modified assembly codes as well as the modified
assembly codes in the space provided in the Answer Sheet.
ECE2216 Microcontroller and Microprocessor Systems Laboratory Experiment 1
__________________________________________________________________________________________
Exercise B
Objective: To control (turn on and off) multiple LEDs at a fixed interval

Procedure:
1. Type the program listed in Table B into ‘Notepad++’, save it as ‘EXB1.asm’and assemble
it by pressing ‘CTRL-F6’.
Table B: Assembly program for Exercise B
ORG 0000H
MOV P0, #0 ; turn off all LEDs
LOOP: SETB P0.0 ; turn on LED at P0.0
CLR P0.1 ; turn off LED at P0.1
MOV A, #16 ; move 16 into accumulator (for 4s delay)
CALL DELAYS ; call subroutine DELAYS to create a delay
CLR P0.0 ; turn off LED at P0.0
SETB P0.1 ; turn on LED at P0.1
MOV A, #4 ; move 4 into accumulator (for 1s delay)
CALL DELAYS ; call subroutine DELAYS to create a delay
SJMP LOOP ; jump back to repeat from LOOP

DELAYS:
MOV R3, A ; load the no. of time RPT loop is repeated
RPT: MOV R2, #250 ; load the no. of time AGN loop is repeated
AGN: MOV R1, #250 ; load the no. of time HERE loop is repeated
HERE: NOP
NOP
DJNZ R1, HERE ; jump to HERE until R1 is decreased to 0
DJNZ R2, AGN ; jump to AGN until R2 is decreased to 0
DJNZ R3, RPT ; jump to RPT until R3 is decreased to 0
RET ; exit subroutine
END
2. Switch off the PSU and carefully unplug the microcontroller from the breadboard.
3. Download ‘EXB1.hex’ from the computer into the microcontroller following the steps
performed in previous part.
4. Plug the microcontroller back onto the breadboard and switch on the PSU.
5. Observe and verify that the microcontroller circuit is working as expected (LEDs at P0.0
and P0.1 are turn on or off for different duration).
6. Based on the assembly program in Table B, develop an assembly program to simulate a
traffic light with the following switching pattern:
Duration Red LED (P0.0) Yellow LED (P0.1) Green LED (P0.2)
3s ON OFF OFF
1s OFF ON OFF
5s OFF OFF ON
7. Write the new and modified assembly instructions in the space provided in the Answer
Sheet.
ECE2216 Microcontroller and Microprocessor Systems Laboratory Experiment 1
__________________________________________________________________________________________
Exercise C
Objective: To control an LED (switch on or off) using a push button (micro tact switch)

Procedure:
1. Enter the program listed in Table C below into Notepad++, save it as ‘EXC1.asm’and
assemble it by pressing ‘CTRL-F6’.
Table C: Assembly program for Exercise C
ORG 0000H
MOV P0, #0 ; turn off all LEDs
SETB P1.0 ; set P1.0 as input pin
LOOP: JB P1.0, $ ; wait until S1 is pressed
JNB P1.0, $ ; wait until S1 is released
CPL P0.0 ; complement (invert) P0.0
SJMP LOOP ; jump back to repeat from LOOP
END
2. Switch off the PSU and carefully unplug the microcontroller from the breadboard.
3. Download ‘EXC1.hex’ from the computer into the microcontroller following the steps
performed in previous part.
4. Plug the microcontroller back onto the breadboard and switch on the PSU.
5. Observe and verify that the microcontroller circuit is working as expected (press and
release S1 to toggle LED at P0.0).
6. Modify the program in Table C so that each time S1 is pressed and release, it will toggle
LEDs at P0.0 and P0.1 continuously with the following pattern:
No. of times S1 is pressed & released Red LED (P0.0) Yellow LED (P0.1)
0 (Initial condition) ON OFF
1 OFF ON
2 ON OFF
7. Write the new and modified assembly instructions in the space provided in the Answer
Sheet.

Exercise D
Objective: To start a traffic light using a push button

Procedure:
1. Type the program listed in Table D into Notepad++, save it as ‘EXD1.asm’and assemble it
by pressing ‘CTRL-F6’.
Table D: Assembly program for Exercise D
ORG 0000H
MOV P0, #0 ; turn off all LEDs
SETB P1.0 ; set P1.0 as input pin
LOOP: CLR P0.1 ; turn off LED at P0.1
SETB P0.0 ; turn on LED at P0.0
JB P1.0, $ ; wait until S1 is pressed
JNB P1.0, $ ; wait until S1 is released
MOV A, #8 ; move 8 into accumulator (for 2s delay)
CALL DELAYS ; call subroutine DELAYS to create a delay
CLR P0.0 ; turn off LED at P0.0
SETB P0.2 ; turn on LED at P0.2
MOV A, #12 ; move 12 into accumulator (for 3s delay)
CALL DELAYS ; call subroutine DE:AYS to create a delay
CLR P0.2 ; turn off LED at P0.2
SETB P0.1 ; turn on LED at P0.1
MOV A, #4 ; move 4 into accumulator (for 1s delay)
CALL DELAYS ; call subroutine DELAYS to create a delay
ECE2216 Microcontroller and Microprocessor Systems Laboratory Experiment 1
__________________________________________________________________________________________
SJMP LOOP ; jump back to repeat from LOOP

DELAYS:
MOV R3, A ; load the no. of time RPT loop is repeated
RPT: MOV R2, #250 ; load the no. of time AGN loop is repeated
AGN: MOV R1, #250 ; load the no. of time HERE loop is repeated
HERE: NOP
NOP
DJNZ R1, HERE ; jump to HERE until R1 is decreased to 0
DJNZ R2, AGN ; jump to AGN until R2 is decreased to 0
DJNZ R3, RPT ; jump to RPT until R3 is decreased to 0
RET ; exit subroutine
END
2. Switch off the PSU and carefully unplug the microcontroller from the breadboard.
3. Download ‘EXD1.hex’ from the computer into the microcontroller following the steps
performed in previous part.
4. Plug the microcontroller back onto the breadboard and switch on the PSU.
5. Observe and verify that the microcontroller circuit is working as expected (traffic light
starts to operate after S1 is pressed and released).
6. Based on the assembly program in Table D, develop an assembly program to control the
traffic lights at a pedestrian crossing as shown in Figure D1.

Figure D1: Traffic lights at pedestrian crossing.


7. The flowchart depicted in Figure D2 describe the operations that should be involved in the
assembly program.
ECE2216 Microcontroller and Microprocessor Systems Laboratory Experiment 1
__________________________________________________________________________________________

Figure D2: Flowchart for Pedestrian Crossing Traffic Lights Control


8. Write the new and modified assembly instructions in the space provided in the Answer
Sheet.

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.

(End of experiment)
ECE2216 Microcontroller and Microprocessor Systems Laboratory Experiment 1
__________________________________________________________________________________________
Appendix A
Using Atmel USB Programmer
1. Place the microcontroller into the socket.

2. Launch the programmer software “ProgISP”.


3. Click the “Select Chip” drop-down list to select the correct chip.

4. Click “RD” to read the chip ID.


ECE2216 Microcontroller and Microprocessor Systems Laboratory Experiment 1
__________________________________________________________________________________________
5. Make sure that only 4 items are checked (circled in red).

6. Click “File” and select “Load Flash”.

7. Select the Hex file to download.


ECE2216 Microcontroller and Microprocessor Systems Laboratory Experiment 1
__________________________________________________________________________________________
8. Click “Auto” to erase, black check, program and verify the Hex file.

9. A text should be printed to confirm that the Hex file has been successfully downloaded.

You might also like