EPT 462/4 – Mechatronics Laboratory Module EXPERIMENT 2 INTRODUCTION TO MICROCONTROLLER OBJECTIVE: 1. To understand the required input signal and expected output signal of microcontroller. 2. To understand the functionality of input and output ports of microcontroller. 3. To expose the student basic of microcontroller programming. INTRODUCTION Micro-controller (also MCU or µC) is a functional computer system-on-a-chip. It contains a processor core, memory, and programmable input/output peripherals. Microcontrollers include an integrated CPU, memory (a small amount of RAM, program memory, or both) and peripherals capable of input and output. It emphasizes high integration, in contrast to a microprocessor which only contains a CPU (the kind used in a PC). In addition to the usual arithmetic and logic elements of a general purpose microprocessor, the microcontroller integrates additional elements such as read-write memory for data storage, read-only memory for program storage, Flash memory for permanent data storage, peripherals, and input/output interfaces. Microcontrollers are used in automatically controlled products and devices, such as automobile engine control systems, remote controls, office machines, appliances, power tools, and toys. By reducing the size, cost, and power consumption compared to a design using a separate microprocessor, memory, and input/output devices, microcontrollers make it economical to electronically control many more processes. (Wikipedia, 2008) 1 EPT 462/4 – Mechatronics Laboratory Module Assembly language Microcontrollers were originally programmed only in assembly language, but various high-level programming languages are now also in common use to target microcontrollers. These languages are either designed specially for the purpose, or versions of general purpose languages such as the C programming language. Compilers for general purpose languages will typically have some restrictions as well as enhancements to better support the unique characteristics of microcontrollers. Some microcontrollers have environments to aid developing certain types of applications. Microcontroller vendors often make tools freely available to make it easier to adopt their hardware. An assembly language is a low-level language for programming computers. It implements a symbolic representation of the numeric machine codes and other constants needed to program a particular CPU architecture. This representation is usually defined by the hardware manufacturer, and is based on abbreviations (called mnemonics) that help the programmer remember individual instructions, registers, etc. An assembly language is thus specific to certain physical or virtual computer architecture (as opposed to most high-level languages, which are usually portable). Instructions (statements) in assembly language are generally very simple, unlike those in high-level languages. Each instruction typically consists of an operation or opcode plus zero or more operands. Most instructions refer to a single value, or a pair of values. Generally, an opcode is a symbolic name for a single executable machine language instruction. Operands can be either immediate (typically one byte values, coded in the instruction itself) or the addresses of data located elsewhere in storage. This is determined by the underlying processor architecture: the assembler merely reflects how this architecture works. (Wikipedia, 2008) COMPONENTS AND EQUIPMENTS: 1. 2. 3. 4. 5. 6. 7. 8. AT89S52 Microcontroller Board Power Supply Jumper wire Breadboard LED – 8 pcs RS232 Cable Computer Screw driver PROCEDURE: Sample program 1 – Blink LED Connect one LED to Port P2.0 and write the following program and program to your AT89S52 board. CPU “8051.TBL” INCL “8051.INC” ORG 0000H 2 EPT 462/4 – Mechatronics REPEAT: LOOP1: LOOP: LOOP1: LOOP: Laboratory Module SETB P2.0 MOV R0, #255 MOV R1, #255 DJNZ R1, LOOP DJNZ R0, LOOP1 CLR P2.0 MOV R0, #255 MOV R1, #255 DJNZ R1, LOOP DJNZ R0, LOOP1 JMP REPEAT Sample program 2 – use of subroutine CPU “8051.TBL” INCL “8051.INC” ORG 0000H REPEAT: SETB P2.0 CALL DELAY CLR P2.0 CALL DELAY JMP REPEAT DELAY: LOOP1: LOOP: MOV R0, #255 MOV R1, #255 DJNZ R1, LOOP DJNZ R0, LOOP1 RET END Sample program 3 – Blink LED when switch is ON (logic 1) and STOP blinking when switch is OFF (logic 0). Use of CPL bit and JNB instructions. Connections: LED - Port P2.0 Switch - Port P1.0. CPU “8051.TBL” INCL “8051.INC” WAIT: REPEAT: DELAY: LOOP1: LOOP: ORG 0000H JNB P1.0, WAIT CPL P2.0 CALL DELAY JMP REPEAT MOV R0, #255 MOV R1, #255 DJNZ R1, LOOP DJNZ R0, LOOP1 RET END 3 EPT 462/4 – Mechatronics Laboratory Module Sample program 4 – Simple input and output operation Connections: 4 LED 4-bit switches - P2.0 – P2.3 P1.0 – P1.3 Write the following program and program to your AT89S52 board. CPU “8051.TBL” INCL “8051.INC” START: ORG 0000H MOV A, P1 MOV P2, A JMP START END ;Read data from 4-bit switches ;Display to LED Sample program 5 – LED start blinking when the pushbutton switch is pressed. The values of the 4-bit switches will determine how many times the LED should blink. Connections: LED Pushbutton 4-bit switches - P2.0 P3.0 P1.0 – P1.3 Write the following program and program to your AT89S52 board. CPU “8051.TBL” INCL “8051.INC” WAIT: REPEAT: DELAY: LOOP1: LOOP: ORG 0000H JNB P3.0, WAIT MOV A, P1 ANL A, #00001111B MOV B, A SETB P2.0 CALL DELAY CLR P2.0 CALL DELAY DJNZ B, REPEAT JMP WAIT MOV R0, #255 MOV R1, #255 DJNZ R1, LOOP DJNZ R0, LOOP1 RET END 4 EPT 462/4 – Mechatronics Laboratory Module Name : _____________________________________ Date : _______________ Matrix No. : ___________________ 1. Using eight LED and one switch, write a program to design a simple running light system. The following table shows the operation of the system. Switch OFF (Logic “0”) ON (Logic “1”) LED‘s STOP running START running 2. Figure 1 illustrates a pedestrian crossing system. The state diagram shows the flow of the process to control the main traffic light and the pedestrian traffic light. Use LED for the traffic light. Write a program in assembly language to control the system. PB = 0 R2 G2 Pedestrian Traffic Light Pushbutton (PB) R1 Y1 G1 G1 R2 15 sec 20 sec Y1 R2 R1 G2 Main Traffic Light 5 sec Figure 1 5 PB = 1 EPT 462/4 – Mechatronics Laboratory Module DISCUSSION: CONCLUSION: REFERENCES 1. Wikipedia. The Free Encyclopedia. Retrieved at: http://en.wikipedia.org/wiki/ Microcontroller. 2008 2. Abdul Rahman Mohd. Saad. Skill Development Course on MICROCONTROLLER SYSTEMS. 2008 6