You are on page 1of 91

Embedded Lab Manual For 8051

SPJ Embedded Technologies Pvt. Ltd.


101, Beaver Grandeur, Baner Road, Pune 411045 (INDIA) Tel. +91-20-27293002 Fax. +91-20-27293003 http://www.spjembedded.com

Embedded Lab Manual

DISCLAIMER
Embedded Lab Manual for 8051 and other documentation:
This manual and the accompanying documentation such as schematic diagrams in soft or hard form contains descriptions of copyrighted products, which are not explicitly indicated as such. The absence of the trademark symbol does not infer that a Product is not protected. Additionally, registered patents and trademarks are similarly not expressly indicated in these documents. The information in these documents has been carefully checked and is believed to be entirely reliable. However, SPJ Embedded Technologies Pvt. Ltd. (henceforth Company) assumes no responsibility for any inaccuracies. The Company, neither gives any guarantee nor accepts any liability whatsoever for consequential damages resulting from the use of these documents or associated product. The Company reserves the right to alter the information contained herein without prior notification and accepts no responsibility for any damages that might result. Additionally, The Company offers no guarantee nor accepts any liability for damages arising from the improper usage or improper installation of the hardware or software. The Company further reserves the right to alter the layout and/or design of the hardware without prior notification and accepts no liability for doing so. SPJ Embedded Technologies Pvt. Ltd. 101, Beaver Grandeur, Baner Road, Baner, Pune 411045. INDIA Phone : +91 (20) 27293002 Fax : +91 (20) 27293003 Email : spj@spjsystems.com Web Site: http://www.spjembedded.com

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 2 of 91

Embedded Lab Manual

Table of Contents
1. Introduction ........................................................................................................................................... 5 2. Getting Started ...................................................................................................................................... 6 2.1 SC51 Installation: ............................................................................................................................ 6 2.2 Creating 8051 Assembly Language applications:........................................................................... 6 2.3 Creating C applications for 8051: Quick Start............................................................................. 9 2.4 Creating C applications for 8051: In Detail ............................................................................... 11 2.4.1 Starting the IDE (SIDE51): ...................................................................................................... 11 2.4.2 Using the Visual Code Generator (VCG): ............................................................................... 19 2.4.3 Why use while(1) : ................................................................................................................... 25 2.4.4 Using the Simulator: ................................................................................................................ 30 3. Hardware Description ........................................................................................................................ 40 3.1 Mini51-PE: ..................................................................................................................................... 40 3.2 Connector Details of Mini51-PE: ................................................................................................. 41 4. Downloading and Running User Programs...................................................................................... 43 5. Accessories ........................................................................................................................................... 46 5.1 SDIO8:............................................................................................................................................ 46 5.2 Seven Segment Interface Board (7SEG-4x4KBD): ...................................................................... 46 5.3 LCD Interface Board (LCD-KBD): ............................................................................................... 47 5.4 Stepper Motor Driver Circuit (STP51):......................................................................................... 49 5.5 ADC Interface Board (SADC08/09): ............................................................................................ 50 5.6 DAC Interface Board (SDAC08):.................................................................................................. 51 5.7 Traffic Light Control Simulation (TCS51): .................................................................................. 52 5.8 Relay Interface Board (SRL4):...................................................................................................... 52 6. Assembly Language Practicals .......................................................................................................... 53 Assignment 1: Write a program to copy a block of 10 bytes of data from RAM locations starting at 15h to RAM locations starting at 25h. .................................................................................................. 53 Assignment 2: Write a Program to find the frequency of square wave generated on pin P1.0. ......... 54 Assignment 3: Write a Program to generate a square wave of 50 Hz frequency on pin P1.2 using interrupt for timer. ................................................................................................................................ 55 Assignment 4: Write a Program to connect INT 1 pin to a switch that is normally high whenever it goes low LED should turn ON which is connected to P3.2 & LED is normally OFF. LED should be ON as long as switch is pressed............................................................................................................ 56

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 3 of 91

Embedded Lab Manual 7. C Language Practicals ........................................................................................................................ 57 Experiment 1: To develop and execute the program to output data on the port lines of the Microcontroller 8051. ........................................................................................................................... 57 Experiment 2: To develop and execute the program for arithmetic operation for the Microcontroller 8051....................................................................................................................................................... 59 Experiment 3: To develop and execute the program for demonstrating input and output operations using keyboard interface for the Microcontroller 8051. ...................................................................... 61 Experiment 4: To develop and execute the program to interface 7 segments LED display to the Microcontroller 8051. ........................................................................................................................... 63 Experiment 5: To develop and execute the program to generate square wave on port lines of Port 1 of the Microcontroller 8051. ..................................................................................................................... 65 Experiment 6: To develop and execute the program for demonstrating logical operations performed by the Microcontroller 8051. ................................................................................................................ 67 Experiment 7: To develop and execute the program to interface 16X2 LCD display to the Microcontroller 8051. ........................................................................................................................... 69 Experiment 8: To develop and execute the program to demonstrate serial communication, using RS232C standard, for the Microcontroller 8051. ..................................................................................... 73 Experiment 9: To develop and execute the program to interface stepper motor to the Microcontroller 8051....................................................................................................................................................... 75 Experiment 10: To develop and execute the program to interface ADC 0809 to the Microcontroller 8051....................................................................................................................................................... 77 Experiment 11: To develop and execute the program to interface DAC 0808 to the Microcontroller 8051....................................................................................................................................................... 80 Experiment 12: To develop and execute the program to interface relays to the Microcontroller 8051. ............................................................................................................................................................... 82 Experiment 13: To develop and execute the program to interface 4*4 matrix keyboard to the Microcontroller 8051. ........................................................................................................................... 84 Experiment 14: To develop and execute the program to output the result of addition of contents of registers R0 and R1 on all four ports for the Microcontroller 8051. ................................................... 88 Experiment 15: Assume 1 Hz. Frequency pulse is connected to I/P P3.4 Write a Program to display count on LCD. ....................................................................................................................................... 90

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 4 of 91

Embedded Lab Manual

1. Introduction
This is users manual for Embedded Lab for 8051 Microcontroller. Development tools for this lab include a. SC51, b. Mini51-PE c. Accessories SC51 is C compiler for 8051 microcontroller. Using SC51 you can write 8051 applications in C, Assembly or have mix programming of Assembly and C. It also includes Simulator to debug your software without the target hardware. Mini51-PE is a general purpose Evaluation Board. This board has a facility to download HEX file into the on-chip flash, without the need of removing the chip from the socket. These boards are offered using different variants of 8051 micro-controller families. Thus, introducing the students to various derivatives of 8051. Different accessories can be interfaced with Mini51-PE board viz.SDIO8, 7SEG-4x4KBD, LCD-KBD, STP51, SADC08/09, SDAC08, TCS51, SRL4, EXTMEM etc. CAUTION: These boards contain components that are sensitive to Electro-Static Discharge (ESD). The board must be handled carefully, so as not to subject it to ESD. As far as possible, do not touch any conducting part on the board including any component or connector pins as this may damage parts of the board permanently. While handling the board, hold it only by its edges. If you must touch any of the parts, make sure to discharge yourself to earth. Parts damaged due to ESD are not covered by the limited warranty. WARRANTY: Its 1 year against manufacturing defects only.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 5 of 91

Embedded Lab Manual

2. Getting Started
2.1 SC51 Installation:
As a part of the SC51 software package, you should have received a CDROM. Please insert it into the CD-ROM drive and run SETUP.EXE from it. Once you start the SETUP program, follow the instructions on the screen to complete the installation. The setup program will: Copy the required files to your hard disk If there are any compressed files, un-compress them Create a program group for SC51 Once you have successfully installed the software on your hard disk, you can run it by clicking on Start/Programs/SPJ Systems SC51/SIDE51. However, we recommend, that you go through this users manual before you actually start using it.

Before first Use:


After installation of SC51 go to Start/Programs/SPJ Systems SC51/Click here before first use. It will install drivers required for the HASP. After installation of drivers connect HASP. It will be detected and red LED will glow in HASP. Now you can use SC51. The SC51 software package includes Assembler, Linker, Simulator and C Compiler for 8051. Besides IDE it also includes tools like Visual Code Generator (VCG) and terminal emulation cum ISP utility (SPJTerm).

About Project:
What is a project? A project is a file in which SIDE51 stores all information related to an application. E.g. it stores the name(s) of C and/or Assembler source file(s), memory size to be used and other options for compiler, assembler and linker. Opening a project: To open an existing project file, select Project / Open Project from the menu. Creating a new project: To create a new project, select Project / New Project from the menu. Changing project settings: To change the project settings (such as adding or removing C and/or Assembler source file(s), changing memory settings etc.), select Project / Settings from the menu.

2.2 Creating 8051 Assembly Language applications:


1. Start the SIDE51 program (i.e. the Integrated Development Environment) 2. If any project is shown open in the editor, then From Project menu, select Close project. 3. From File menu, select New File. An empty editor window will automatically open. You may type the assembly language program here. 4. When you finish typing the assembly language program, select Save As from File menu. The Save As dialog window will be displayed. Select the desired path where you wish to save this

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 6 of 91

Embedded Lab Manual program (for example, C:\SPJ). CAUTION: The pathname must not contain space or other special characters such as tab, comma, semicolon etc. Then in the File name field, type the name of the file with extension .ASM (for example, TEST1.ASM). Just below the File name field, there is the selection list box Save as type. From this list, select All files (*. *). Then click on the Save button. 5. From the Project menu, select New Project. The Open dialog window will be displayed. Select the same path where you saved the program in previous step. In the File name field, type the name of the project, without any extension. The convention is to use the same name as the program filename. E.g. TEST1. Then click on the Open button. 6. The action in the previous step will display the Project Settings dialog window. This dialog window has 3 different tabs named Compiler Options, Assembler / Linker Options, and Source Files. Any of these 3 tabs can be displayed by clicking on the corresponding name near the top of this dialog window. Currently, the Compiler Options will be automatically displayed. If the target micro-controller (must be a member of 8051 family) is known, you may select the appropriate Manufacturer from the list; and then select the appropriate part number from the list. If the target micro-controller is not known or if you cannot find it in the list, then you may simply select Intel as the manufacturer and 8051 or 8052 as the micro-controller. Note that when you select 8051 as the micro-controller, the Internal RAM (Bytes) selection automatically changes to 128. Similarly, when you select 8052, the Internal RAM (Bytes) selection automatically changes to 256. Select 8051 or 8052, depending on whether your target micro-controller has 128 or 256 bytes internal RAM, respectively. 7. In the same window, the Crystal frequency field is also displayed with default value as 12 MHz. If your target uses different crystal frequency, then you may change it. 8. Click on Assembler / Linker Options to display that part of the dialog window. Since you wish to use the simulator for debugging, it is necessary to check the box labeled Generate Debug Info from .ASM file. 9. Under Program Memory (Code Memory) map: you may enter the start and end address of program memory available in your target. This program memory maybe internal (onchip) or external. E.g. if the target uC is Atmel 89C52 without external memory, then you may enter start address as 0000 and end address as 1FFF. 10. Similarly, enter start and end addresses of the XDATA (i.e. external data memory). This XDATA memory maybe on-chip or on-board. Only start and end addresses are important, but not physical location. 11. The Compiler / Assembler automatically produces Intel HEX format file (.HEX file). If you also require the binary image file (.BIN file), then check the box labeled Generate .BIN file (ROM image). 12. If you plan to use In-Circuit-Emulator (ICE) for debugging, then check the box labeled Generate Absolute OMF file. This will create .AOM file, which is supported by most of the emulators. 13. Click on Source Files to display that part of the dialog window. This window will indicate that IDE has automatically added 2 files in this new project: TEST1.C and STARTUP.ASM. The STARTUP.ASM file is automatically created by the IDE and is required if you wish to write program in C. Since we have written an Assembly language program, we do not need it. So click on the STARTUP.ASM filename to select it and then click on the Remove file button. Similarly, the IDE may have automatically created an empty C file (TEST1.C), which we do not

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 7 of 91

Embedded Lab Manual need. So click on TEST1.C filename and then click on the Remove file button. Now we wish to add our Assembly language program in this new project. Hence, click on the Add file button. Select the file TEST1.ASM (which we created in an earlier step) and then click on Open button. Now the Project Settings dialog will indicate that TEST1.ASM file has been added into the project. Now click OK button to create this new project. 14. From the Compile menu, select Build. This will invoke the assembler to assemble the file TEST1.ASM; and further (assuming no errors) invoke the linker to create the .HEX and other files. If there are any errors or warnings during the process of assembling or linking, then those will be displayed in the output window (below the editor window). If there are errors, then you may correct those by making appropriate changes to the program; select Save from File menu to save the changes and then again select Build from Compile menu. Repeat this until there are no errors. 15. Now we are ready to simulate this program. To do so, simply select Simulator from the Tools menu. Simulator for 8051 is an independent program and it will be launched by this action and the current project (TEST1) will be automatically opened. The Simulator will automatically display 5 windows (clockwise, from left-top corner): Program window, Serial window, Internal RAM watch window, SFR watch window, and Symbols watch window. As the names indicate, these windows display the corresponding information. So, the same program that you typed will be visible in the Program window. 16. To single step through the program, you may select Single Step from the Run menu (pressing F7 key is also equivalent). When you do so, the program window will indicate that one instruction has been executed and the next instruction will be highlighted. You may repeat the same to single step through the entire program. 17. While single stepping through the program, the contents of general purpose registers (R0-R7) as well SFRs will be displayed in the SFR Watch Window. The register / SFR contents can be seen changing as effect of each executed instruction. It is also possible to modify register / SFR contents (except PC) by double clicking on its name. 18. Similarly, it is possible to observe contents of internal RAM while single stepping through the program. To do so, right click in the Internal RAM Watch Window and then select Add from the pop-up menu. Then enter the starting address of the 16-byte block of Internal RAM that you wish to observe (e.g. 30) and press OK button. Upon doing so, the entered memory address followed by contents of 16 consecutive locations will be displayed. It is also possible to modify the contents of Internal RAM by double clicking on the address in this Internal RAM Watch Window. 19. Similarly, it is possible to observe contents of external RAM while single stepping through the program. To do so, it is necessary to first make the External RAM Watch Window visible. To do so, select External RAM Watch from View menu. Then, the remaining procedure is similar to that followed for Internal RAM Watch Window. 20. When you are through with debugging, select Exit from File menu to close the Simulator. Then (if necessary) you may edit the program, build it again and start the simulator to debug again. Please note, only one instance of Simulator program should be started at any time. It is recommended to close the Simulator while making any changes to the program.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 8 of 91

Embedded Lab Manual

2.3 Creating C applications for 8051: Quick Start


1. Start the SIDE51 program (i.e. the Integrated Development Environment) 2. From Project menu, select Close project. 3. From Project menu, select New Project. The Open dialog window will be displayed. Select the desired path where you wish to create this new project. (For example, C:\SPJ). CAUTION: the path and filename must not contain space or other special characters such as tab, comma, semicolon etc. In the File name field, type the name of the project, without any extension. For example, you may type PROG1. Then click on the Open button. 4. The action in the previous step will display the Project Settings dialog window. This dialog window has 3 different parts named Compiler Options, Assembler /Linker Options, and Source Files. Any of these 3 parts can be displayed by clicking on the corresponding name near the top of this dialog window. Currently, the Compiler Options will be automatically displayed. If the target micro-controller (must be a member of 8051 family) is known, you may select the appropriate Manufacturer from the list; and then select the appropriate micro-controller from the device list. If the target micro-controller is not known or if you cannot find it in the list, then you may simply select Intel as the manufacturer and 8051 or 8052 as the microcontroller. Note that when you select 8051 as the micro-controller, the Internal RAM (Bytes) selection automatically changes to 128. Similarly, when you select 8052 as the microcontroller, the Internal RAM (Bytes) selection automatically changes to 256. Select 8051 or 8052, depending on whether your target micro-controller has 128 or 256 bytes internal RAM, respectively. 5. In the same window, the Crystal frequency field is also displayed with default value as 12 MHz. If your target uses different crystal frequency, then you may change it. However, the current version of Compiler / Assembler does not actually use this value. 6. Click on Assembler / Linker Options to display that part of the dialog window. Under Program Memory (Code Memory) map: you may enter the start and end address of program memory available in your target. This program memory maybe internal (inside the microcontroller) or external. For example, if the target microcontroller is Atmel 89C52 without any external memory, then you may enter start address as 0000 and end address as 1FFF. 7. Similarly, enter start and end addresses of the XDATA (i.e. external data memory). This XDATA memory maybe on-chip or off-chip. Only start and end addresses are important, but not physical location. 8. The Compiler / Assembler automatically produces Intel HEX format file (.HEX file). If you also require the binary image file (.BIN file), then check the box labeled Generate .BIN file (ROM image). 9. If you plan to use In-Circuit-Emulator (ICE) for debugging, then check the box labeled Generate Absolute OMF file. This will create .AOM file, which is required by the ICE. 10. Click on Source Files to display that part of the dialog window. This window will indicate that IDE has automatically added 2 files in this new project: PROG1.C and STARTUP.ASM. The STARTUP.ASM file is automatically created by the IDE and is required for all C projects. Similarly, the IDE has automatically created an empty C file (PROG1.C). If the file PROG1.C already exists in the same path, then IDE would not create it or modify it; but it will anyway add it to the project automatically. If you wish to add more files in this project, then click on the Add file button, select the desired filename and then click on Open button. Now the Project Settings dialog will indicate that selected file has been added into the project. When all necessary files have been added to the project, click OK button to create this new project.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 9 of 91

Embedded Lab Manual 11. The PROG1.C file created by the IDE will be an empty file containing only the frame of main function. You may write the desired program statements in this file (or other files that you may have added to the project). When done, select Save from File menu. If you have modified more than one source files, then select Save All from File menu. 12. From the Compile menu, select Build. This will invoke the Compiler to compile the file PROG1.C; and further (assuming no errors) invoke the linker to create the .HEX and other files. If there are any errors or warnings during the process of compiling, assembling or linking, then those will be displayed in the output window (below the editor window). If there are errors, then you may correct those by making appropriate changes to the program; select Save from File menu to save the changes and then again select Build from Compile menu. Repeat this until there are no errors. 13. Now we are ready to simulate this program. To do so, simply select Simulator from the Tools menu. Simulator for 8051 is an independent program and it will be launched by this action and the current project (PROG1) will be automatically opened. The Simulator will automatically display 5 windows (clockwise, from left-top corner): Program window, Serial window, Internal RAM watch window, SFR watch window, and Symbols watch window. As the names indicate, these windows display the corresponding information. So, the same program that you typed (PROG1.C) will be visible in the Program window. 14. To single step through the program, you may select Single Step from the Run menu. Pressing F7 key is also equivalent to selecting Single Step from the Run menu. When you do so, the program window will indicate that one C statement has been executed and the next statement will be highlighted. You may repeat the same to single step through the entire program. 15. While single stepping through the program, the contents of general purpose registers (R0-R7) as well SFRs will be displayed in the SFR Watch Window. The register /SFR contents can be seen changing as effect of each executed instruction. It is also possible to modify register / SFR contents by double clicking on its name. (Contents of PC cannot be modified). 16. Similarly, it is possible to observe values of variables. To do so, right click in the Variables Watch Window and select Add from the pop-up menu. Select the desired variable(s) to watch and click OK button. This will display the names and values of the selected variables in the variables watch window. It is also possible to modify a variable value by double-clicking on its name. 17. The Serial window displays the activity related to 8051 serial port. i.e. any character transmitted to the serial port is displayed in this window. Similarly, it is possible to simulate receiving characters. To do so, simply click inside the Serial window and type any character(s) that you wish to simulate as received. 18. When you are through with debugging, select Exit from File menu to close the Simulator. Then (if necessary) you may edit the program, build it again and start the simulator to debug again. Please note, only one instance of Simulator program should be started at any time. It is recommended to close the Simulator while making any changes to the program.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 10 of 91

Embedded Lab Manual

2.4 Creating C applications for 8051: In Detail


Want a quick start? Read this section sitting in front of your Computer. Please follow the instructions in the given example. By the time you finish with the example, you will have acquired enough knowledge about the features of this software. That will enable you to actually start using the software for your applications. Here we go!

2.4.1 Starting the IDE (SIDE51):


Click on START button and select Programs / SPJ Systems SC51 / SIDE51. The screen should look something like this. There are 3 different windows viz. Editor window, Workspace window and Output window as shown below. Workspace Window Editor Window

Output Window

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 11 of 91

Embedded Lab Manual

From Project menu, select Close project. The screen should look something like this.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 12 of 91

Embedded Lab Manual

To create a new project, from Project menu, select New Project. The screen should look something like this.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 13 of 91

Embedded Lab Manual

The Open dialog window will be displayed. Select the desired path where you wish to create this new project. (For example, C:\SPJ). CAUTION: the path and filename must not contain space or other special characters such as tab, comma, semicolon etc. In the File name field, type the name of the project, without any extension. For example, you may type PROG1. Then click on the Open button.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 14 of 91

Embedded Lab Manual

The action in the previous step will display the Project Settings dialog window. This dialog window has 3 different parts named Compiler Options, Assembler /Linker Options, and Source Files. Any of these 3 parts can be displayed by clicking on the corresponding name near the top of this dialog window. Currently, the Compiler Options will be automatically displayed. If the target micro-controller (must be a member of 8051 family) is known, you may select the appropriate Manufacturer from the list; and then select the appropriate micro-controller from the device list. If the target micro-controller is not known or if you cannot find it in the list, then you may simply select Intel as the manufacturer and 8051 or 8052 as the microcontroller. Note that when you select 8051 as the micro-controller, the Internal RAM (Bytes) selection automatically changes to 128. Similarly, when you select 8052 as the microcontroller, the Internal RAM (Bytes) selection automatically changes to 256. Select 8051 or 8052, depending on whether your target micro-controller has 128 or 256 bytes internal RAM, respectively. In the same window, the Crystal frequency field is also displayed with default value as 12 MHz. If your target uses different crystal frequency (such as 11.0592 MHz), then you may change it.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 15 of 91

Embedded Lab Manual

Click on Assembler / Linker Options to display that part of the dialog window. Under Program Memory (Code Memory) map: you may enter the start and end address of program memory available in your target. This program memory maybe internal (inside the microcontroller) or external. For example, if the target microcontroller is Atmel 89C52 without any external memory, then you may enter start address as 0000 and end address as 1FFF. Similarly, enter start and end addresses of the XDATA (i.e. external data memory). This XDATA memory maybe on-chip or off-chip. Only start and end addresses are important, but not physical location. The Compiler / Assembler automatically produces Intel HEX format file (.HEX file). If you also require the binary image file (.BIN file), then check the box labeled Generate .BIN file (ROM image). If you plan to use In-Circuit-Emulator (ICE) for debugging, then check the box labeled Generate Absolute OMF file. This will create .AOM file, which is required by the ICE.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 16 of 91

Embedded Lab Manual

Click on Source Files to display that part of the dialog window. This window will indicate that IDE has automatically added 2 files in this new project: PROG1.C and STARTUP.ASM. The STARTUP.ASM file is automatically created by the IDE and is required for all C projects. Similarly, the IDE has automatically created an empty C file (PROG1.C). If the file PROG1.C already exists in the same path, then IDE would not create it or modify it; but it will anyway add it to the project automatically. If you wish to add more files in this project, then click on the Add file button, select the desired filename and then click on Open button. Now the Project Settings dialog will indicate that selected file has been added into the project. When all necessary files have been added to the project, click OK button to create this new project.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 17 of 91

Embedded Lab Manual

The PROG1.C file created by the IDE will be an empty file containing only the frame of main function.

Now you may write the desired program statements in this file (or other files that you may have added to the project). When done, select Save from File menu. If you have modified more than one source files, then select Save All from File menu.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 18 of 91

Embedded Lab Manual Here we write a program for serial communication. Aim: Write a C program to send WELCOME on serial port continuously. Description:

2.4.2 Using the Visual Code Generator (VCG):


To send data to serial port, first initialize serial port. To initialize serial port go to Visual Code Generator menu. It will display 3 options (Initialize TMOD/TCON, Initialize Serial Port and Interrupt Settings). Select Initialize Serial Port.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 19 of 91

Embedded Lab Manual Initialize Serial Port window will be displayed. Do proper settings to initialize serial port. v Serial Port Mode: 1 : 8-Bit UART (Baud Rate: Variable) v Enable REN (Receiver Enable) v For Baud Rate Use: TIMER 1 v Desired Baud Rate: 9600 v Crystal Frequency: 11.0592 (Depending on Hardware) v Click on Generate Code

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 20 of 91

Embedded Lab Manual Click on Copy C Code, corresponding code will be copied. Last Line is Please also initialize TMOD / TCON. We have to initialize TMOD / TCON because we have used TIMER 1 for Baud Rate Generation. Click on Ok. Paste the code in the program.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 21 of 91

Embedded Lab Manual To initialize TMOD/TCON go to Visual Code Generator menu. Select Initialize TMOD/TCON. Initialize TMOD/TCON window will be displayed. Do proper settings to initialize TMOD/TCON. v Enable TR1: Check to run Timer 1 v Timer 1 Mode: 2 8-Bit Auto Reload Mode v Click on Generate Code

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 22 of 91

Embedded Lab Manual Click on Copy C Code, corresponding code will be copied. Click on Ok. Paste the code in the program.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 23 of 91

Embedded Lab Manual Serial port is our standard input output device. printf statement will send data (WELCOME) to serial port and to continuously send data we will write printf statement in continuous loop viz. while(1). printf is compilers library function. It is included in the library stdio.h, therefore we have to include its prototype in the program.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 24 of 91

Embedded Lab Manual

2.4.3 Why use while(1) :


8051 does not have any halt instruction. In embedded systems once the program is started, it should never terminate. Only time it stops is when user switches it off. This concept has to be understood very thoroughly. The while(1) loop helps the user to achieve this. while(1) means, while the condition is true, in other words the while loop will execute all time. The only way to halt the system is to switch it off. If a task is to be performed only once, the user will have an empty while (1) loop as below while (1) { } If the application demands some actions to be performed all time then the while loop will be as follows while (1) { /* Action to be performed continuously */ } Typical C program #include <8051.h> /* Other includes */ /* Global variable declaration */ /* function prototypes */ void func1(char ch1) ; /* function definition */ void func1(char ch1) { /* Program structure */ } void main () { /* Calling Initialization routines */ /* Call functions which are to be executed only once */ while (1) { /* Call functions to be executed all time */ } }

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 25 of 91

Embedded Lab Manual From the Compile menu, select Build.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 26 of 91

Embedded Lab Manual This will invoke the Compiler to compile the file PROG1.C; and further (assuming no errors) invoke the linker to create the .HEX and other files. If there are any errors or warnings during the process of compiling, assembling or linking, then those will be displayed in the output window (below the editor window). If there are errors, then you may correct those by making appropriate changes to the program; select Save from File menu to save the changes and then again select Build from Compile menu. Repeat this until there are no errors. In which case you will get the message in Build window as below

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 27 of 91

Embedded Lab Manual If there are any errors you will see the screen as below. In this program I have removed semicolon after printf statement and again rebuilded it, so it is syntax error.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 28 of 91

Embedded Lab Manual


Scroll up the Output window to locate the Error statement. On this statement, when you

double click, the editor will take you directly to the line creating the error.

Rectify all such errors and warnings. Then again from compile option perform Build/ReBuild all to generate hex file. And if there is no any error in the code then you will get the message as in previous screen. And the output .hex file will have been generated in your working directory.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 29 of 91

Embedded Lab Manual

2.4.4 Using the Simulator:


Now we are ready to simulate this program. To do so, simply select Simulator from the Tools menu.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 30 of 91

Embedded Lab Manual Simulator for 8051 is an independent program and it will be launched by this action and the current project (PROG1) will be automatically opened. The Simulator will automatically display 5 windows (clockwise, from left-top corner): Program window, Serial window, Internal RAM watch window, SFR watch window, and Symbols watch window. As the names indicate, these windows display the corresponding information. So, the same program that you typed (PROG1.C) will be visible in the Program window.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 31 of 91

Embedded Lab Manual In Simulator you can Debug the program. Different options available in Run menu to debug the program are: v v v v v v Insert Breakpoint Single Step Step Over Run Terminate Program CPU Reset

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 32 of 91

Embedded Lab Manual To single step through the program, you may select Single Step from the Run menu. Pressing F7 key is also equivalent to selecting Single Step from the Run menu.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 33 of 91

Embedded Lab Manual When you do so, the program window will indicate that one C statement has been executed and the next statement will be highlighted. You may repeat the same to single step through the entire program.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 34 of 91

Embedded Lab Manual While single stepping through the program, the contents of general purpose registers (R0-R7) as well SFRs will be displayed in the SFR Watch Window. The register /SFR contents can be seen changing as effect of each executed instruction.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 35 of 91

Embedded Lab Manual It is also possible to modify register / SFR contents by double clicking on its name. (Contents of PC cannot be modified). Eg. To modify contents of Port 1 double click on P1 in SFR Watch Window. Edit SFR P1 window will be displayed. You can Enter Hex Value or change Binary Representation by clicking on the required bit.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 36 of 91

Embedded Lab Manual Similarly, it is possible to observe values of variables. To do so, right click in the Variables Watch Window and select Add from the pop-up menu. Select the desired variable(s) to watch and click OK button. This will display the names and values of the selected variables in the variables watch window. It is also possible to modify a variable value by double-clicking on its name.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 37 of 91

Embedded Lab Manual The Serial I/O window displays the activity related to 8051 serial port. i.e. any character transmitted to the serial port is displayed in this window. Similarly, it is possible to simulate receiving characters. To do so, simply click inside the Serial window and type any character(s) that you wish to simulate as received. Now Single Step or Run the program from Run menu. We can see WELCOME message on Serial I/O Window.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 38 of 91

Embedded Lab Manual When you are through with debugging, select Exit from File menu to close the Simulator. Then (if necessary) you may edit the program, build it again and start the simulator to debug again. Please note, only one instance of Simulator program should be started at any time. It is recommended to close the Simulator while making any changes to the program.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 39 of 91

Embedded Lab Manual

3. Hardware Description
3.1 Mini51-PE:
Unpacking: You will find following items in the package: Mini51-PE board Serial communication cable Power Supply with cable Lab Manual

26 Pin FRC1 Connector


PIN 1 PIN 1

26 Pin FRC2 Connector

PIN 1

Port 2 Port 0
PIN 1 PIN 1

Port 3 Port 1
PIN 1

Reset Serial Port / 9-Pin D type Connector

PSCON: Power Supply Connector


Pin 1

INT 0

INT 1

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 40 of 91

Embedded Lab Manual Connecting the system: The serial communication cable supplied with the board should be used to connect the board to a PC running Windows95/98/NT/ME/2000/XP/Vista Operating System. Connect the PSCON connector on Mini51-PE to the Power Supply with the supplied cable. The system is now ready to be turned ON. Specifications: The Mini51-PE board includes: Philips P89V51RD2 running at 11.0592 MHz. RS232 port (9 pin D type female connector) with serial communication cable.

3.2 Connector Details of Mini51-PE:


1) PSCON: Power Supply Connector in the sequence of +5, Gnd, +12, Gnd, -12, Gnd. 2) INT0, INT1: External Interrupt Generation keys. 3) Jumper J1-EA : a) Right Short position Internal memory access b) Left Short position External ROM access. 4) Port X (X is 0/1/2/3 means Port 0/Port 1/Port 2/Port 3): Pin Number PIN 1 PIN 2 PIN 3 PIN 4 PIN 5 PIN 6 PIN 7 PIN 8 PIN 9 PIN 10 Pin Assignment P X.0 P X.1 P X.2 P X.3 P X.4 P X.5 P X.6 P X.7 VCC GND

5) FRC1: 26 pin FRC Connector, to connect peripheral cards. FRC1 is used to connect: a) SDAC08 (DAC Interface Board) b) SEMEM (External Memory (RAM) Interface Board) These are connected to this FRC1 connector via the FRC flat cable. The pin connection details are as given below:

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 41 of 91

Embedded Lab Manual Pin Number on Connector PIN 1 PIN 3 PIN 5 PIN 7 PIN 9 PIN 11 PIN 13 PIN 15 PIN 17 PIN 19 PIN 21 PIN 23 PIN 25 Pin Assignment P1.0 P1.2 P1.4 P1.6 EA PSEN +12V GND -12V GND +5V GND ADC CLK Pin Number on Connector PIN 2 PIN 4 PIN 6 PIN 8 PIN 10 PIN 12 PIN 14 PIN 16 PIN 18 PIN 20 PIN 22 PIN 24 PIN 26 Pin Assignment P1.1 P1.3 P1.5 P1.7 ALE NC +12V GND -12V GND +5V GND RST (Reset)

6) FRC2: 26 pin connector to connect Accessories. Following Accessories use FRC2 for connecting the CPU card: a) LCD-KBD (LCD Interface Board), b) 7SEG-KBD (7 Segment Interface Board), c) STP51 (Stepper Motor Driver Circuit), d) SADC0809 (ADC Interface Board), e) SDAC08 (DAC Interface Board) f) SEMEM (External Memory (RAM) Interface Board) These are connected to this FRC2 connector via the FRC flat cable. The pin connection details are as given below: Pin Number on Connector PIN 1 PIN 3 PIN 5 PIN 7 PIN 9 PIN 11 PIN 13 PIN 15 PIN 17 PIN 19 PIN 21 PIN 23 PIN 25 Pin Assignment P2.0 P2.2 P2.4 P2.6 P0.0 P0.2 P0.4 P0.6 P3.0 P3.2 P3.4 P3.6 +5V Pin Number on Connector PIN 2 PIN 4 PIN 6 PIN 8 PIN 10 PIN 12 PIN 14 PIN 16 PIN 18 PIN 20 PIN 22 PIN 24 PIN 26 Pin Assignment P2.1 P2.3 P2.5 P2.7 P0.1 P0.3 P0.5 P0.7 P3.1 P3.3 P3.5 P3.7 GND

Note: Pin 1 of FRC1 and FRC2 connector can be identified by triangle marked on the same connectors or they are also shown in the Mini51-PE board image.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 42 of 91

Embedded Lab Manual

4. Downloading and Running User Programs


Microcontroller P89V51RD2 includes on-chip flash for storing user program and non-volatile data. This flash is In-System-Programmable (ISP). Therefore it is possible to download user program into on-chip flash, through serial port connected to PC. This section describes how to use the software Flash Magic to download program into P89V51RD2.

Download program using Flash Magic into P89V51RD2:


Install Flash Magic from CD. After installation, open it. In Flash Magic go to Options -> Advanced Options-> Hardware Config. Disable Use DTR and RTS to control RST and PSEN. Click on OK. To download program follow the steps as below: 1. Connect power supply. 2. Keep the board in switched off mode. 3. Connect the RS232 port (9-Pin D type Male connector) of Mini51-PE, to COM1 or COM2 of a PC, using the serial communication cable (supplied with the board). 4. Start Flash Magic. You will get the screen as below (Flash magic version 3.42.179)

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 43 of 91

Embedded Lab Manual 5. Do proper settings in Flash Magic (COM Port: Suppose COM1, Baud Rate: 4800, Device: 89V51RD2, Interface: None (ISP), Enable Erase blocks used by Hex File). 6. Click on Verify After Programming. This option will verify that the program loaded in the flash is same as the hex file selected. Thus if any corruption of data has occurred then it can be detected. 7. IMPORTANT: DO NOT SELECT: Set Security Bit 1. This will block the further erase and write cycles on the flash of P89V51RD2. DO NOT SELECT: Fill unused Flash, this will consume lot of time in programming. DO NOT SELECT: Gen Block checksums. DO NOT SELECT: Prog Clocks Bit

8. Click on Browse button. 9. Use the browse option to go to the required directory. 10. Click on the hex file you want to download to the target hardware.

11. Click on open button. Once you do this the file will be selected in the Flash Magic. 12. Click on the Start button. Following message will be displayed:

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 44 of 91

Embedded Lab Manual

13. At this point you should power ON the board. If the board is already powered ON, then you should press the Reset Switch (RST KEY) on the board. 14. After the above, the above message will dis-appear and the programming will start. 15. Once the file is loaded it will be verified. 16. Finally you will get the message on the bottom of the Flash Magic Screen as Finished.

17. To Run the program, RESET the board or Power OFF and Power ON the board again.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 45 of 91

Embedded Lab Manual

5. Accessories
5.1 SDIO8:
Digital I/O simulation board with 8 LEDs and 8 switches. All the LEDs are connected by common anode method. That means the positive leg of each LED is connected to Vcc and negative leg to the port pins of the microcontroller. A logic 0 on the port pin will make LED ON and logic 1 will make it OFF. Vcc and Gnd are marked on 10 pin connector of the board. SDIO8 can be interfaced to all the ports (Port 0, Port 1, Port 2 and Port 3) on Mini51-PE board.

5.2 Seven Segment Interface Board (7SEG-4x4KBD):


This is 7 Segment interface board, with 4 7Segment displays. 7 Segment is just 7 LEDs that have been combined into one case to make a convenient device for displaying numbers and some letters. The 7 Segment used is a common anode version. That means the positive leg of each LED is connected to Vcc and negative leg to the port pins of the microcontroller. At logic 0 LED in segment will become ON and at logic 1 it will become OFF. By doing proper combination of logic 0s you can see numbers and some letters on 7 segments.

Connections: To select individual 7 segment displays:


To select Display 1, make P3.7 = 0 To select Display 2, make P3.6 = 0 To select Display 3, make P3.5 = 0 To select Display 4, make P3.4 = 0 a ---f | g |b ---e | |c ---- . dot d Segment a b c d e f g dot Port Pin P2.5 P2.4 P2.2 P2.1 P2.0 P2.6 P2.7 P2.3

Port 2 of 8051 microcontroller is connected to 7 segments of all the 4 displays.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 46 of 91

Embedded Lab Manual This board also contains 16 keys arranged in a matrix form (4*4). 7 Segment LED Displays (4 Nos.) through 8 parallel lines

Display 1

26 Pin FRC2 Connector

Display 4 4*4 Matrix Keyboard

In Matrix keyboard, as shown above we have scan line and return lines. On the scan lines the user has to drive the data and on return lines receive the data. Matrix keyboard is connected to Port 0 of 8051 microcontroller. Pins P0.0 to P0.3 are scan lines and pins P0.4 to P0.7 are return lines. The port pins by default are at logical high level. We want to detect a key press. When you press a key the respective scan line is shorted to respective return line. And the status of the return line is to be read. As by default all lines are high, so to identify the key pressed, we need to make that particular scan line low, thus when we read the return line status, that particular line will be low and all others will be high. So we force the scan line to level 0, one at a time. And read the status of the return lines. If we detect any of the return lines as level 0, then we have detected a key pressed. We wait for the de-bouncing. Then the scan code, the code we have sent on the scan line together with the return code, the value we read from the return lines. This together, scan code + return code will identify a unique key.

5.3 LCD Interface Board (LCD-KBD):


16 char X 1 row, 16 char X 2 rows, 16 char X 4 rows LCD modules can be interfaced to Mini51-PE board using LCD Interface board. The LCD is interfaced in 8-bit mode and I/O mapped. While writing a command to LCD make sure RS (register select) is 0, RW is 0 and afterwards write the data and Enable LCD by taking it high and back to low.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 47 of 91

Embedded Lab Manual While writing a data to LCD makes sure RS is 1, RW is 0, write the data and Enable LCD by taking it high and back to low. This board also contains 16 keys arranged in a matrix form (4*4). When any is pressed corresponding lines goes to logic 0 otherwise they are at logic 1. Matrix keyboard is connected to Port 0 of 8051 microcontroller. Pins P0.0 to P0.3 are scan lines and pins P0.4 to P0.7 are return lines.

VRLCD: POT to adjust LCD Contrast 16*2 LCD Display

26 Pin FRC2 Connector

4*4 Matrix Keyboard

LCD Connections:
The pin connections of LCD are as followsPin Number of LCD 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Port Pin Description Ground. Vcc (+5V) Power supply to control LCD contrast through pot RS RW E DB0 DB1 DB2 DB3 DB4 DB5 DB6 DB7

P3.2 P3.1 P3.0 P2.0 P2.1 P2.2 P2.3 P2.4 P2.5 P2.6 P2.7

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 48 of 91

Embedded Lab Manual

5.4 Stepper Motor Driver Circuit (STP51):


The Stepper Motor is a transducer that converts electrical pulse input into mechanical motion. The stepper motor does not move continuously. The stepper motor moves in steps of fixed size. The most common stepper motor has four stator windings that are paired with centre tapped common. Such type of stepper motor is known as four phases or uni-polar stepper motor. The power is applied in appropriate polarity to each stator winding. To apply the power, additional motor driver switches are used along with Microcontroller 8051. These switches are operated in typical sequence to rotate the motor.

26 Pin FRC1 Connector

SMCON: Stepper Motor Connector

26 Pin FRC2 Connector

SM-CON: Stepper motor connector. The pin connections are as followsPin Number 1 2 3 4 5 6 7 8 Pin assignment No connection. Power Supply of Stepper Motor. P0.0 P0.1 P0.2 P0.3 Power Supply of Stepper Motor. Ground.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 49 of 91

Embedded Lab Manual

5.5 ADC Interface Board (SADC08/09):


The Analog to Digital Converter (ADC) is important device that interfaces a Microcontroller 8051 to the analog world. The Successive Approximation type ADC is fast as compare to Dual Slope ADC. It requires N clock pulses for N bit conversion. The smallest change in an analog input (VIN) that can be detected by the ADC to get minimum one digit change at the digital output is the resolution of ADC. The resolution of ADC is also defined by the number of output bits (8 bits 0\or 12 bits or 16 bits) and reference voltage level of ADC. Here we are using ADC0808/0809. The ADC0808/0809 allows us to monitor up to 8 different transducers. It has 8-bit data output. The 8 analog input channels are multiplexed and selected by using address pins A, B and C.

Steps to program the ADC0808/0809:


Select an analog channel by providing bits to A, B and C channel select lines. Eg. To select channel 1, give A=1, B=0 and C=0, and so on. Activate the ALE pin. It needs H-to-L pulse to latch in the address. Activate SC by H-to-L pulse to initiate conversion. Monitor EOC to see whether conversion is finished. L-to-H output indicates that the data is converted and is ready to be picked up. Activate OE to read data out of the ADC chip. H-to-L pulse to OE pin will bring digital data out of the ADC chip.

Connections:
The pin connections of ADC are as followsPort Pin P2.0 P2.1 P2.2 P0.1 P0.2 P0.4 P0.0 P3 Pin assignment for ADC A (channel select 0) B (channel select 1) C (channel select 2) START (start of conversion) ALE (address latch enable) EOC (end of conversion) OE (output enable) Digital Output

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 50 of 91

Embedded Lab Manual

I/P Connector Differential Analog Input POT 26 Pin FRC2 Connector Socket to Connect Temperature Sensor LM35 Gain Adjusting POT

ADCLK: Clock Select (Left Short)

I/P Connector: This is the 8 channel connector for giving input analog signals. IN0 & IN2 are used internally for the temperature sensor LM35 & Liner POT respectively. IN3 to IN 7 are given for user to give input analog signals. The connector details are as follows: Pin 1 Pin 3 Pin 5 Pin 7 Pin 9 : IN 7 : IN 6 : IN 5 : IN 4 : IN 3 Pin 2 Pin 4 Pin 6 Pin 8 Pin 10 : Gnd : Gnd : Gnd : Gnd : Gnd

5.6 DAC Interface Board (SDAC08):


Digital to analog converter is another device that also connects the microcontroller to the analog world. Various types of DACs are used in practice. R-2R ladder DAC is very effective and popular. Similar to ADC specifications, resolution of the DAC is determined by digital input bits. Most commonly output of the DAC is in current form. External I to V converter is often used along with DAC to get the voltage output. Here we are using DAC08. The digital inputs are converted to current and by connecting a resistor to current pin; we convert the result to voltage. This voltage level can be monitored on an oscilloscope.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 51 of 91

Embedded Lab Manual Connections: 8-bit digital input data is sent to DAC08 through Port 2 of 8051 microcontroller. P2.0 is LSB and P2.7 is MSB.

26 Pin FRC1 Connector Ground (TP2) Analog Output (TP1) 26 Pin FRC2 Connector

5.7 Traffic Light Control Simulation (TCS51):


Four-way road control, LEDs to indicate various status of Traffic light. Includes cable to connect to the above boards. Includes application software for the simulation. All the LEDs are connected by common anode method. That means the positive leg of each LED is connected to Vcc and negative leg to the port pins of the microcontroller. At logic 0 LED will become ON and at logic 1 it will become OFF.

5.8 Relay Interface Board (SRL4):


Relay board with 4 relay outputs. Relay with 230VAC /1A or 24VDC 2A ratings. PNP transistor BC557 is used to drive relay. At logic 0 relay will become ON and at logic 1 it will become OFF. SRL4 can be interfaced to all the ports (Port 0, Port 1, Port 2 and Port 3) on Mini51-PE board. Relay Number RL1 RL2 RL3 RL4 Port Pin PX.0 PX.1 PX.4 PX.5

Note: X is 0/1/2/3 depending on Port 0/Port 1/Port 2/Port 3 respectively.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 52 of 91

Embedded Lab Manual

6. Assembly Language Practicals


Note: All the programs are available in the CD.

Assignment 1: Write a program to copy a block of 10 bytes of data from RAM locations starting at 15h to RAM locations starting at 25h.
Equipments: SC51, PC. Source Code:
MOV R0,#15H MOV R1,#25H MOV R3,#10 MOV A,@R0 MOV @R1,A INC R0 INC R1 DJNZ R3, BACK SJMP HERE ;source pointer ;destination pointer ;counter ;get a byte from source ;copy it to destination ;increment source pointer ;increment destination pointer ;keep doing it for all ten bytes ;continuous infinite loop

BACK:

HERE:

Procedure:
To Edit / Assemble/ Generate Hex file: Refer Section 2.2

Output:
You can see output of this program on Simulator. In the SIDE51 window from Tools menu open Simulator. In Simulator you can see Internal RAM Watch Window. In Internal RAM Watch Window double click on Right click here to add watch. Now you can see Modify Memory Contents window. All the memory contents are 00. Change memory contents from 0015H to 001EH. Click Ok in Modify Memory Contents window. Now right click on Right click here to add watch in Internal RAM Watch Window. Click on Add. Type 25 in Memory Address (Hex) window and press Enter. You can see values at the location starting from 25H in Internal RAM Watch Window. From Run menu in Simulator click on Run option. You can see contents from 0015H to 001EH are moved to 0025H to 002EH.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 53 of 91

Embedded Lab Manual

Assignment 2: Write a Program to find the frequency of square wave generated on pin P1.0.
Equipments: SC51, PC, Mini51-PE, Oscilloscope. Source Code:
MOV TMOD,#20H MOV TH0,#5 SETB TCON.6 BACK: JNB TCON.7, BACK CPL P1.0 CLR TCON.7 SJMP BACK ; T1/mode 2/8 bit auto reload ; TR1 ; TF1 ; clear TF1

Connections:
Connect one channel of Oscilloscope to P1.0 (Pin number 1 on Port1 connector).

Procedure:
To Edit / Assemble/ Generate Hex file: Refer Section 2.2 To download and run this program: Refer Chapter 4

Output:
You can see the square wave on oscilloscope. Measure its frequency. Philips microcontrollers (P89C51RD2, P89V51RD2, P89C668, P89C669) works on double speed therefore frequency is doubled. For Mini51-PE (11.0592 MHz): frequency = 2 * 1.83597 KHz = 3.67KHz

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 54 of 91

Embedded Lab Manual

Assignment 3: Write a Program to generate a square wave of 50 Hz frequency on pin P1.2 using interrupt for timer.
Equipments: SC51, PC, Mini51-PE, Oscilloscope. Source Code:
ORG 0 LJMP MAIN ORG 000BH CPL P1.2 MOV TL0,#00 MOV TH0,#0DCH RETI ORG 30H MAIN: MOV TMOD, #00000001B MOV TL0,#00 MOV TH0,#0DCH MOV IE, #82H SETB TCON.4 HERE: SJMP HERE END

; ISR for Timer 0 Interrupt ; Generate Square wave as make the pin high/low alternatively ; Timer value = DC00H

; Timer0, Mode 1 ; Timer value = DC00H ; Enable Timer0 Interrupt ; TR0

Connections:
Connect one channel of Oscilloscope to P1.2 (Pin number 2 on Port1 connector).

Procedure:
To Edit / Assemble/ Generate Hex file: Refer Section 2.2 To download and run this program: Refer Chapter 4

Output:
You can see the square wave on oscilloscope. Philips microcontrollers (P89C51RD2, P89V51RD2, P89C668, P89C669) works on double speed therefore frequency is doubled. For Mini51-PE (11.0592 MHz): frequency = 100Hz

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 55 of 91

Embedded Lab Manual

Assignment 4: Write a Program to connect INT 1 pin to a switch that is normally high whenever it goes low LED should turn ON which is connected to P3.2 & LED is normally OFF. LED should be ON as long as switch is pressed.
Equipments: SC51, PC, Mini51-PE, SDIO8. Source Code:
ORG 0000H LJMP MAIN ; bypass interrupt vector table

; ISR for hardware interrupt INT1 to turn ON led ORG 0013H CLR P3.2 MOV R3, #255 BACK: DJNZ R3, BACK SETB P3.2 RETI ; main program for initialization ORG 30H MAIN: MOV IE, #084h HERE: SJMP HERE END ; enable external INT1 ; stay here until get interrupted ; INT1 ISR ; turn ON led ; keep led ON for a while ; turn OFF led ; return from ISR

Connections:
Connect 10-pin cable supplied to you along with SDIO8 board as follows: Connect black end of 10 Pin Cable to J1 connector on SDIO8 board and white end to Port 3 on Mini51PE board.

Procedure:
To Edit / Assemble/ Generate Hex file: Refer Section 2.2 To download and run this program: Refer Chapter 4

Output:
KEY5 on SDIO8 is connected to INT1 and LED6 on SDIO8 is connected to P3.2. When KEY5 is pressed LED6 will glow otherwise it will be OFF.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 56 of 91

Embedded Lab Manual

7. C Language Practicals
Note: All the programs are available in the CD.

Experiment 1: To develop and execute the program to output data on the port lines of the Microcontroller 8051.
Aim: Write a C language program to output constant data 55H on Port 1 of Microcontroller 8051. Equipments: SC51, PC, Mini51-PE, SDIO8. Block Diagram:
Mini51-PE / 8051 Evaluation board Port 2 Port 3 Port 0 J1 Port 1
Switches / Keys

LEDs

SDIO8 / LED-Key Board

Source Code:
#include <Intel\8051.h> void main () { /* Output constant data (0x55) to port P1. */ P1 = 0x55 ; /* In 8051 there is no halt instruction. The user program should retain the control of the hardware from switch ON to switch OFF. To achieve this we have to include the below loop which will do nothing but not terminate the program. */ while(1) { /* "do nothing" infinite loop } }

*/

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 57 of 91

Embedded Lab Manual

Connections:
Connect 10-pin cable supplied to you along with SDIO8 board as follows: Connect black end of 10 Pin Cable to J1 connector on SDIO8 board and white end to Port 1 on Mini51PE board.

Procedure:
To Edit / Compile/ Generate Hex file: Refer Section 2.3 To download and run this program: Refer Chapter 4

Output:
You can see constant data 55H (0101 0101 in binary) on LEDs (on SDIO8 board) connected to Port 1 of 8051 microcontroller.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 58 of 91

Embedded Lab Manual

Experiment 2: To develop and execute the program for arithmetic operation for the Microcontroller 8051.
Aim: Write a C language program for Microcontroller 8051 to output the result of a. Addition of two data elements on Port 0, b. Subtraction of two data elements on Port 1, c. Multiplication of two data elements on Port 2, and d. Division (quotient) of two data elements on Port 3. Equipments: SC51, PC, Mini51-PE, SDIO8. Block Diagram:
Mini51-PE / 8051 Evaluation board SDIO8 / LED Key Board Port 2 Port 0

SDIO8 / LED Key Board

SDIO8 / LED Key Board

Port 3

Port 1

SDIO8 / LED Key Board

Source Code:
#include <Intel\8051.h> unsigned char g_bySum, g_byDifference, g_byProduct, g_byQuotient ; unsigned char g_byOperand1, g_byOperand2 ; /* Few of the rules followed to declare a variable g = global variable by = variable data type unsigned char (byte) rest of the name is defining the functionality of the variable void main () { g_byOperand1 = 27 ; g_byOperand2 = 9 ;

*/

// Initialize variable with a number // Initialize variable with a number

g_bySum = g_byOperand1 + g_byOperand2 ; P0 = g_bySum ; // output result to Port 0 g_byDifference = g_byOperand1 - g_byOperand2 ; P1 = g_byDifference ; // output result to Port 1 g_byProduct = g_byOperand1 * g_byOperand2 ;

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 59 of 91

Embedded Lab Manual


P2 = g_byProduct ; // output result to Port 2

g_byQuotient = g_byOperand1 / g_byOperand2 ; P3 = g_byQuotient ; // output result to Port 3 while(1) { // Do nothing loop... } }

Connections:
Connect 10-pin cable supplied to you along with SDIO8 board as follows: Connect black end of 10 Pin Cable to J1 connector on SDIO8 board and white end to the respected Port on Mini51-PE board.

Procedure:
To Edit / Compile/ Generate Hex file: Refer Section 2.3 To download and run this program: Refer Chapter 4

Output:
You can see the result of arithmetic operation on LEDs (on SDIO8 board) connected to the respected Port of 8051 microcontroller. Result (24H) of first, addition operation can be seen on Port 0. Connect black end of 10 Pin Cable to J1 connector on SDIO8 board and white end to Port 0 on Mini51-PE board. Result (12H) of second, subtraction operation can be seen on Port 1. Connect black end of 10 Pin Cable to J1 connector on SDIO8 board and white end to Port 1 on Mini51-PE board. Result (F3H) of third, multiplication operation can be seen on Port 2. Connect black end of 10 Pin Cable to J1 connector on SDIO8 board and white end to Port 2 on Mini51-PE board. Result (03H) of fourth, division operation can be seen on Port 3. Connect black end of 10 Pin Cable to J1 connector on SDIO8 board and white end to Port 3 on Mini51-PE board.

Note: For the below mentioned program see Experiment no.14 Aim: Write C language program for Microcontroller 8051 to output the result of addition of contents of register R0 and R1 on all four ports.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 60 of 91

Embedded Lab Manual

Experiment 3: To develop and execute the program for demonstrating input and output operations using keyboard interface for the Microcontroller 8051.
Aim: Write a C language program for Microcontroller 8051 to check the key depression of the key on port line 2.7 and output the key status of key on port line 2.6 of Microcontroller 8051. Equipments: SC51, PC, Mini51-PE, SDIO8. Block Diagram:
Mini51-PE / 8051 Evaluation board Port 1 Port 3 Port 0 Port 2 J1
Switches / Keys

LEDs

SDIO8 / LED-Key Board Source Code:


#include <Intel\8051.h> BIT INPUT P2.7 BIT OUTPUT P2.6 bit g_bKeyIn ;
/*

/* Define P2.7 as input /* Define P2.6 as output // Define a variable of type bit

*/ */

Few of the rules followed to declare a variable g = global variable b = variable data of type bit Rest of the name is defining the functionality of the variable

*/

void main () { INPUT = 1;


/*

/* Define Pin 2.7 as input */


*/

Continuously Read the key and send its status to the output pin

while(1) { g_bKeyIn = INPUT ; OUTPUT = g_bKeyIn ;

// Read the status of the input pin // Send the status read to the output pin

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 61 of 91

Embedded Lab Manual

} }

Connections:
Connect 10-pin cable supplied to you along with SDIO8 board as follows: Connect black end of 10 Pin Cable to J1 connector on SDIO8 board and white end to the Port 2 on Mini51-PE board. KEY1 is connected to P2.7 (INPUT) and LED2 is connected to P2.6 (OUTPUT).

Procedure:
To Edit / Compile/ Generate Hex file: Refer Section 2.3 To download and run this program: Refer Chapter 4

Output:
Status of KEY1 can be seen on LED2. If KEY1 is pressed then LED2 will become ON and if KEY is released then LED2 will become OFF.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 62 of 91

Embedded Lab Manual

Experiment 4: To develop and execute the program to interface 7 segments LED display to the Microcontroller 8051.
Aim: Write a C language program to display string of characters 1234 on four- 7 segment LED displays. Displays are interfaced in multiplexed mode. Equipments: SC51, PC, Mini51-PE, 7 Segment LED Interface Module. Block Diagram:
Mini51-PE / 8051 Evaluation board
7 Segments

FRC 2

FRC2

Source Code:
#include <Intel\8051.h> #include <standard.h>

7 Segment LED Interface Module

P2 : Connected to 7 segements of all the displays a ---a = P2.5, b = P.2.4, c = P.2.2, d = P.2.1 f | g|b ---e = P.2.0, f = P.2.6, g = P.2.7, dot = P2.3 e | |c ---- . d */ /* Use #define to define names to ports, constants so that its easy to write the code and understand too.
Also if you need to make changes to these values, you will have to change the same at only one place.

/*

*/ #define SELECTDISP #define DATAPORT P3 P2 // define P3 as port to select the display /* define P2 as data port connected to the segments as defined above */ // Select Display 1 and disable others, P3.7 = 0 // Select Display 2 and disable others, P3.6 = 0 // Select Display 3 and disable others, P3.5 = 0 // Select Display 4 and disable others, P3.4 = 0

#define SEL_DISP_1 0x7f #define SEL_DISP_2 0xbf #define SEL_DISP_3 0xdf #define SEL_DISP_4 0xef void main () { SELECTDISP = 0x0f ;

//

Define port as output

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 63 of 91

Embedded Lab Manual


// DATAPORT = 0x00 ; DATAPORT = 0xff ; delay_ms(10) ;
/* This is a library Function. To see help of this move the cursor on the function and press F1 and keep it low for approx 10*1000 machine cycles. If target hardware freq is 12 MHz, this will generate approximately 10 milli seconds delay */ /* Display the data in while loop, so that the data 1234 is continuously displayed. If we do not use this in while loop the display will become blank once it is 1234disabled (since the displays do not have latched data bus).*/

Also selects all Displays Define port as output Also all segments of all displays will be ON Make all displays off

// // //

while (1) { SELECTDISP = SEL_DISP_1 ; // Select Display 1 DATAPORT = 0xeb ; // Show 1 on display 1 (Segment b and c ON) delay_ms(5) ; SELECTDISP = SEL_DISP_2 ; // Select Display 2 DATAPORT = 0x4C ; // Show 2 on display 2 (Segment a,b,g,d,e ON) delay_ms(5) ; SELECTDISP = SEL_DISP_3 ; // Select Display 3 DATAPORT = 0x49 ; // Show 2 on display 3 (Segment a,b,g,c,d ON) delay_ms(5) ; SELECTDISP = SEL_DISP_4 DATAPORT = 0x2b ; delay_ms(5) ; } } ; // Select Display 4 // Show 2 on display 4 (Segment f, g, c ON)

Connections:
Connect FRC cable between FRC 2 connector on Mini51-PE board and FRC 2 connector on 7 Segment LED Interface Module.

Procedure:
To Edit / Compile/ Generate Hex file: Refer Section 2.3 To download and run this program: Refer Chapter 4

Output:
You can see characters 1234 on four- 7 segment LED displays.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 64 of 91

Embedded Lab Manual

Experiment 5: To develop and execute the program to generate square wave on port lines of Port 1 of the Microcontroller 8051.
Aim: Write a C language program for Microcontroller 8051 to generate square wave of frequency 1 KHz on all port lines of Port 1.Use hardware delay technique. Equipments: SC51, PC, Mini51-PE, SDIO8. Block Diagram:
Mini51-PE / 8051 Evaluation board Port 2 Port 3 Port 0 Port 1 Oscilloscope

Source Code:
#include <Intel\8051.h> #include <standard.h> unsigned char g_byData = 0; void main () {
/* Following while loop is an infinite loop. The action which you want to perform continuously, should be written in this loop. In this program we are generating square wave on all port pins of Port 1, continuously. The frequency of the Square Wave is approximately 1KHz */

/*

Declare Variable

*/

while(1) { P1 = g_byData; delay_ms(500);

P1 = ~g_byData; delay_ms(500); } }

/* Make all the port pins of Port 1 low */ /* This is a library Function. To see help of this, move the cursor on the function and press F1 and keep it low for approx 500*1000 machine cycles. If target hardware freq is 12 MHz, this will generate approximately 500 milli seconds delay */ /* Make all the port pins of Port 1 high */ /* Keep them low for approx. 500ms */

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 65 of 91

Embedded Lab Manual

Connections:
Connect one channel of Oscilloscope to any pin of Port 1.

Procedure:
To Edit / Compile/ Generate Hex file: Refer Section 2.3 To download and run this program: Refer Chapter 4

Output:
You can see the square wave of frequency 1 KHz on oscilloscope.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 66 of 91

Embedded Lab Manual

Experiment 6: To develop and execute the program for demonstrating logical operations performed by the Microcontroller 8051.
Aim: Write a C language program for Microcontroller 8051 to output result of following logical operations to the port. 1. AND operation: 35H AND OFH: output to PORT 0, 2. OR operation: 04H OR 64H: output to PORT 1, 3. Complement operation: Complement of 55H : output to PORT2, 4. SHIFT RIGHT operation: SHIFT RIGHT 9A H three times: output to PORT 3. Equipments: SC51, PC, Mini51-PE, SDIO8. Block Diagram:
Mini51-PE / 8051 Evaluation board SDIO8 / LED Key Board Port 2 Port 0 SDIO8 / LED Key Board

SDIO8 / LED Key Board

Port 3

Port 1

SDIO8 / LED Key Board

Source Code:
#include <Intel\8051.h> void main () {
/* Logically AND the data 35H and 0FH and place the result on Port 0 */

P0 = 0x35 & 0x0F;


/* Logically OR the data 04H and 64H and place the result on Port 1 */

P1 = 0x04 | 0x64;
/* Logically COMPLIMENT the data 55H place the result on Port 0 */

P2 = ~ 0x55;
/* Shift right (three times) data 9AH and place the result on Port 1 */

P3 = 0x9A >> 3;

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 67 of 91

Embedded Lab Manual

while(1) { /* "do nothing" infinite loop } }

*/

Connections:
Connect 10-pin cable supplied to you along with SDIO8 board as follows: Connect black end of 10 Pin Cable to J1 connector on SDIO8 board and white end to the respected Port on Mini51-PE board.

Procedure:
To Edit / Compile/ Generate Hex file: Refer Section 2.3 To download and run this program: Refer Chapter 4

Output:
You can see the result of logical operation on LEDs (on SDIO8 board) connected to the respected Port of 8051 microcontroller. Result (05H) of first, AND operation can be seen on Port 0. Connect black end of 10 Pin Cable to J1 connector on SDIO8 board and white end to Port 0 on Mini51-PE board. Result (64H) of second, OR operation can be seen on Port 1. Connect black end of 10 Pin Cable to J1 connector on SDIO8 board and white end to Port 1 on Mini51-PE board. Result (AAH) of third, Compliment operation can be seen on Port 2. Connect black end of 10 Pin Cable to J1 connector on SDIO8 board and white end to Port 2 on Mini51-PE board. Result (13H) of fourth, Shift right operation can be seen on Port 3. Connect black end of 10 Pin Cable to J1 connector on SDIO8 board and white end to Port 3 on Mini51-PE board.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 68 of 91

Embedded Lab Manual

Experiment 7: To develop and execute the program to interface 16X2 LCD display to the Microcontroller 8051.
Aim: Write a C language program for Microcontroller 8051 to display message WELCOME on 16 X 2 LCD display. Equipments: SC51, PC, Mini51-PE, LCD Interface Board. Block Diagram:

Mini51-PE / 8051 Evaluation board

WELCOME

LCD

FRC 2

FRC2

LCD Interface Board

Source Code:
#include <Intel\8051.h> #include <standard.h> BIT RS P3.2 BIT RW P3.1 BIT EN P3.0 #define DATAPORT P2 // Register Select. 0 = Cmd , 1 = data // Read/Write, 0 = write, 1 = read // LCD Enable // Port P2 connected to data lines of the LCD

unsigned char g_byData = 0xff ; // Variable for data void lcd_cmd (unsigned char p_byCmd) { // sends command to LCD RS = 0; RW = 0; // select command register // Make Read write in low

DATAPORT = p_byCmd ; // Generate a pulse on EN Pin EN = 1;

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 69 of 91

Embedded Lab Manual


asm nop asm nop EN = 0; }

void lcd_dat (unsigned char p_byData) { // sends data to LCD RS = 1; RW = 0; DATAPORT = p_byData ; // Generate a pulse on EN Pin EN = 1; asm nop asm nop EN = 0; } void lcd_init () { lcd_cmd(0x38) ; delay(250) ; lcd_cmd(0x0c) ; delay(250) ; lcd_cmd(0x01) ; delay(250) ; } void display_row (unsigned char p_byRow, unsigned char p_byDataArr[16]) {
/* this function is written for 16 char X 2 line LCD second row */ row == 0 means first row row == 1 means

// 2 lines and 5x7 matrix

// Display on, cursor off

// clear lcd

unsigned char count, ch ; if(p_byRow) // if row is same as saying if row is non zero, in our // case its row = 1 lcd_cmd(0xc0); // display on second line

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 70 of 91

Embedded Lab Manual


else lcd_cmd(0x80); // display on first line

delay(250) ; for(count=0;count<16;count++) { if(ch = p_byDataArr[count]) check if { lcd_dat(ch); delay(250) ; } else { while(count < 16) { lcd_dat(' ') ; delay(250) ; count ++ ; } } } } // insert spaces until end of line // its value is non zero // If not non zero send data to the lcd // equate p_byDataArr element to ch and

void main () { lcd_init (); while (1) { display_row(0,"WELCOME"); } }

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 71 of 91

Embedded Lab Manual

Connections:
Connect FRC cable between FRC 2 connector on Mini51-PE board and FRC 2 connector on LCD Interface Board.

Procedure:
To Edit / Compile/ Generate Hex file: Refer Section 2.3 To download and run this program: Refer Chapter 4

Output:
WELCOME will be displayed on LCD.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 72 of 91

Embedded Lab Manual

Experiment 8: To develop and execute the program to demonstrate serial communication, using RS-232C standard, for the Microcontroller 8051.
Aim: Write a C language program for Microcontroller 8051 to send message WELCOME serially to the personal computer. Serial communication should be carried out at baud rate of 9600 bps, 8-bit data, and 1 stop-bit. Equipments: SC51, PC, Mini51-PE. Block Diagram:
Mini51-PE / 8051 Evaluation board Port 2 Port 0 9 Pin D type Male Connector

WELCOME WELCOME

Port 3

Port 1 Serial Cable Computer 9 Pin D type Male Connector

Source Code:
/*This program uses the on-chip serial port as the standard output device; and sends "WELCOME \n" message to it*/ #include <Intel\8051.h> #include <stdio.h> void main () {
/* Initialize Serial Port Code below is generated using: Visual Code Generator (Initialize Serial Port) It initializes serial port in Mode 1: 9600 baud rate generated using Timer 1 @ 11.0592 MHz Here we are using 3 pin serial connection: Rx, Tx, Gnd. Rx = P3.0, Tx = P3.1 The connection to the other serial port are: Rx --> Tx, Tx --> Rx, Gnd --> Gnd

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 73 of 91

Embedded Lab Manual

The P89V51RD2 give TTL level for Rx and Tx. When we drive this through RS232 driver we get the RS232 levels for these signals */

TH1 = 0xfd ; TL1 = 0xfd ; PCON &= 0x7f ; SCON = 0x50 ;


/* Initialize TMOD/TCON Code below is generated using: Visual Code Generator (Initialize TMOD/TCON) It puts Timer1 in 8 bit auto-reload mode and starts it Timer1 is used for baud rate generation */

TMOD = 0x20 ; TCON = 0x40 ;


/* Serial port is now initialized and ready to use. Following while loop is an infinite loop. The action which you want to perform continuously, should be written in this loop. In this program we are sending "WELCOME !" message on serial port, continuously.

*/

while(1) { printf("WELCOME !\n") ; } } // \n will generate a new line on the terminal

Connections:
Keep the Serial cable connected as it is.

Procedure:
To Edit / Compile/ Generate Hex file: Refer Section 2.3 To download and run this program: Refer Chapter 4

Output:
You can see output on SPJTerminal. Therefore Open SPJTerminal. Go to Port -> Settings. Do proper settings (Baud Rate: 9600 (if other choose it), Data Bits: 8, Stop Bits: 1, Echo: Off, Parity: None, Com Port: Com 1 (if other choose it)). Click on OK. Go to Port -> Open. If required Reset the board. Mini51PE board will continuously transmit the message "WELCOME!.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 74 of 91

Embedded Lab Manual

Experiment 9: To develop and execute the program to interface stepper motor to the Microcontroller 8051.
Aim: Write a C language program for Microcontroller 8051 to rotate stepper motor continuously in clockwise direction. Motor is interfaced through Port 0 of Microcontroller 8051. Equipments: SC51, PC, Mini51-PE, Stepper Motor Driver Circuit, Stepper Motor. Block Diagram:
Mini51-PE / 8051 Evaluation board FRC1
LEDs showing Phase Output

FRC1

Stepper Motor

FRC2

FRC2

Stepper Motor Driver Circuit

Source Code:
#include <Intel\8051.h> #include <standard.h> #define PHASEA #define PHASEB #define PHASEC #define PHASED 0x09 0x05 0x06 0x0a

void main () { while(1) { P0 = PHASED; delay_ms(120) ; P0 = PHASEC; delay_ms(120) ; P0 = PHASEB; delay_ms(120) ;

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 75 of 91

Embedded Lab Manual


P0 = PHASEA; delay_ms(120) ; } }

Connections:
Connect FRC cable between FRC 1 connector on Mini51-PE board and FRC 1 connector on Stepper Motor Driver Circuit. Connect FRC cable between FRC 2 connector on Mini51-PE board and FRC 2 connector on Stepper Motor Driver Circuit. Connect Stepper Motor to SM-CON connector on Stepper Motor Driver Circuit. If stepper motor requires +5V or +12V external power supply then connect it to +5V or +12V supply respectively (depending on colour combination of connector pins), present on main power supply provided along with Mini51-PE board.

Procedure:
To Edit / Compile/ Generate Hex file: Refer Section 2.3 To download and run this program: Refer Chapter 4

Output:
Stepper motor will rotate in clockwise direction. Note: In the similar way by reversing the phases only we can rotate the stepper motor in anticlockwise direction. This program is included in the CD.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 76 of 91

Embedded Lab Manual

Experiment 10: To develop and execute the program to interface ADC 0809 to the Microcontroller 8051.
Aim: Write a C language program for Microcontroller 8051 to read the digital output of the ADC and output it on port 1 and on serial port. Equipments: SC51, PC, Mini51-PE, ADC Interface Board, SDIO8. Block Diagram:
Mini51-PE / 8051 Evaluation board

ADC Interface Board

Port 2

Port 0 FRC 2 FRC2

Port 3

Port 1

POT to give Analog Input LEDs


J1

Switches SDIO8 / LED-Key Board

Source Code:
#include <Intel\8051.h> #include <standard.h> /* 8 LEDs are connected to port P1. So data read from ADC should be sent to P1. ADC channel selection pins = P2.2, P2.1, P2.0 ADC start of conversion pin = P0.1 ADC ALE pin = P0.2 ADC end of conversion pin = P0.4 ADC output enable pin = P0.0 ADC data = P3

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 77 of 91

Embedded Lab Manual


*/ BIT BIT BIT BIT

start ale eoc oe

P0.1 P0.2 P0.4 P0.0

unsigned char adc_data; void initser() { TH1 = 0xfd ; TL1 = 0xfd ; PCON &= 0x7f ; SCON = 0x50 ; TMOD = 0x20 ; TCON = 0x40 ; } void main () { initser(); P2 = 0x02; /* select channel IN2 */ ale = 1; /* ale pin = high */ delay_ms(60); /* Following while loop is an infinite loop. So ADC data of channel IN2 will be continuously read and sent to P1 and serial port. Since it is connected to a POTentiometer, output will change when POT is moved. */ while(1) { start = 1; delay_ms(60); start = 0; ale = 0; while(!eoc) { /* wait until conversion completes. */ } oe = 1; /* output enable pin = high */ asm nop delay_ms(60); adc_data = P3; /* conversion is complete. Now read ADC data. oe = 0; /* output enable pin = low */ /* start of conversion pin = high */

/* /*

start of conversion pin = low */ ale pin = low */

/* monitor EOC pin. It will go high when conversion completes */

*/

printf("ADC = %bu\n",adc_data);

/* now output that data to serial port */

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 78 of 91

Embedded Lab Manual


P1 = ~adc_data; } } /* now output that data to LEDs connected to P1 */

Connections: Connect FRC cable between FRC 2 connector on Mini51-PE board and FRC 2 connector on ADC Interface Board. Procedure: To Edit / Compile/ Generate Hex file: Refer Section 2.3 To download and run this program: Refer Chapter 4 Output: Digital value of corresponding analog input can be seen on LEDs (on SDIO8 board) connected to Port 1 of 8051 microcontroller. If you vary the analog input by varying pot then corresponding digital value can be seen on LEDs. You can also see the digital value on SPJTerminal. Therefore Open SPJTerminal. Go to Port -> Settings. Do proper settings (Baud Rate: 9600 (if other choose it), Data Bits: 8, Stop Bits: 1, Echo: Off, Parity: None, Com Port: Com 1 (if other choose it)). Click on OK. Go to Port -> Open. If required Reset the board.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 79 of 91

Embedded Lab Manual

Experiment 11: To develop and execute the program to interface DAC 0808 to the Microcontroller 8051.
Aim: Write a C language program for Microcontroller 8051 to generate staircase waveform using the DAC. DAC is interfaced using port 2. Equipments: SC51, PC, Mini51-PE, DAC Interface Board, CRO. Block Diagram:
Mini51-PE / 8051 Evaluation board Port 2 Port 0 FRC1 DAC Interface Board FRC1 Test Points FRC2 Port 3 Port 1 FRC2

Oscilloscope

Source Code:
#include <Intel\8052.h> #include <standard.h> #define DATAPORT P2 unsigned char g_byData ; void main () { while(1) {
/* when we give 0 as input to DAC it generates the 0 level and when we give 255 (0xff) to DAC it generates 5V level As we increase the digital count from 0 to 255 (0xff) the voltage output will gradually increase from 0 to 5V thus showing the staircase effect You can check the output on the oscilloscope. Two points are provided on the DAC card, to connect the probe of oscilloscope */

for(g_byData=0; g_byData<256; g_byData++)

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 80 of 91

Embedded Lab Manual


{ DATAPORT = g_byData ; delay_ms(1) ; } } }

Connections:
Connect one channel of Oscilloscope to TP1 pin on DAC Interface board and connect TP2 pin on DAC Interface Board to ground pin of oscilloscope.

Procedure:
To Edit / Compile/ Generate Hex file: Refer Section 2.3 To download and run this program: Refer Chapter 4

Output:
You can see staircase waveform on oscilloscope. Note: In the similar way we can see triangular and square waveform on oscilloscope. These programs are also included in the CD.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 81 of 91

Embedded Lab Manual

Experiment 12: To develop and execute the program to interface relays to the Microcontroller 8051.
Aim: Write a C language program to interface relays to Port 1 of Microcontroller 8051. Equipments: SC51, PC, Mini51-PE, SRL4. Block Diagram:

Mini51-PE / 8051 Evaluation board Port 2 Port 0 JP4 Port 3 Port 1


1

Relays

LEDs

SRL4 / Relay Board

Source Code:
#include <Intel\8051.h> #include <standard.h> void main () { /* Following while loop is an infinite loop. The action which you want to perform continuously should be written in this loop. In this program relays will become ON and OFF after 500ms delay, continuously */ while(1) { P1 = 0x00; delay_ms(500); P1 = 0xff; delay_ms(500); } }

//all relays will become ON //this function generates delay of 500ms //all relays will become OFF //this function generates delay of 500ms

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 82 of 91

Embedded Lab Manual

Connections:
Connect 10-pin cable supplied to you along with SRL4 board as follows: Connect black end of 10 Pin Cable to JP4 connector on SRL4 board and white end to Port 1 on Mini51PE board.

Procedure:
To Edit / Compile/ Generate Hex file: Refer Section 2.3 To download and run this program: Refer Chapter 4

Output:
You can see Relays becoming ON and OFF continuously.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 83 of 91

Embedded Lab Manual

Experiment 13: To develop and execute the program to interface 4*4 matrix keyboard to the Microcontroller 8051.
Aim: Write a C language program to display keycode of 4*4 matrix keyboard on four7 segment LED displays. Equipments: SC51, PC, Mini51-PE, 7SEG4*4KBD (7 Segment LED Interface Module). Block Diagram:
Mini51-PE / 8051 Evaluation board FRC2
4*4 Matrix Keyboard 7 Segments

FRC 2

7SEG4*4KBD Interface Module Source Code: #include <Intel\8051.h> #include <standard.h> unsigned char g_byScanCode = 0xfe ; unsigned char g_byRetCode = 0x00 ; unsigned char g_byTemp; unsigned int g_bycode; void main () { /* Pin0 - Pin3 - Scan Lines Pin4 - Pin7 - Return Lines */ P3 = 0x00 ; P2 = 0x00 ; P0 = 0xff ; while (1) { g_byTemp = P0 ;

// Read P0

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 84 of 91

Embedded Lab Manual g_byTemp = g_byTemp | 0x0f ; // make all scan lines as 1 g_byTemp = g_byTemp & g_byScanCode ; // make the desired scan line as 0 P0 = g_byTemp ; // make scan line as 0 g_byRetCode = P0 ; // read the port for return lines if ((g_byRetCode & 0xf0) != 0xf0) // mask the scan lines and then compare the return // lines { // key detected if((g_byRetCode == 0xee) | (g_byRetCode == 0xec)) { P2 = 0x88; } else if((g_byRetCode == 0xed) | (g_byRetCode == 0xe9)) { P2 = 0x2b; } else if((g_byRetCode == 0xeb) | (g_byRetCode == 0xe3)) { P2 = 0x08; } else if((g_byRetCode == 0xe7) | (g_byRetCode == 0xe6)) { P2 = 0x9c; } else if((g_byRetCode == 0xde) | (g_byRetCode == 0xdc)) { P2 = 0xeb; } else if((g_byRetCode == 0xdd) | (g_byRetCode == 0xd9)) { P2 = 0x19; } else if((g_byRetCode == 0xdb) | (g_byRetCode == 0xd3)) { P2 = 0x09; } else if((g_byRetCode == 0xd7) | (g_byRetCode == 0xd6)) { P2 = 0x68; } else if((g_byRetCode == 0xbb) | (g_byRetCode == 0xb3)) { P2 = 0x48; } else if((g_byRetCode == 0xbe) | (g_byRetCode == 0xbc)) {

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 85 of 91

Embedded Lab Manual P2 = 0x4c; } else if((g_byRetCode == 0xbd) | (g_byRetCode == 0xb9)) { P2 = 0x18; } else if((g_byRetCode == 0xb6) | (g_byRetCode == 0xb7)) { P2 = 0x0c; } else if((g_byRetCode == 0x77) | (g_byRetCode == 0x76)) { P2 = 0x1e; } else if((g_byRetCode == 0x7b) | (g_byRetCode == 0x73)) { P2 = 0x38; } else if((g_byRetCode == 0x7d) | (g_byRetCode == 0x79)) { P2 = 0xcb; } else if((g_byRetCode == 0x7e) | (g_byRetCode == 0x7c)) { P2 = 0x49; } delay_ms(300) ; P0 = 0xff ; } g_byScanCode = ~g_byScanCode ; // invert the scan code g_byScanCode <<= 1 ; // shift by 1 g_byScanCode = ~g_byScanCode ; // invert the scan code to get final scan code if (g_byScanCode == 0xef) g_byScanCode = 0xfe ; } } // for debounce // make port lines as 1

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 86 of 91

Embedded Lab Manual

Connections:
Connect FRC cable between FRC 2 connector on Mini51-PE board and FRC 2 connector on 7SEG4*4KBD (7 Segment LED Interface Module).

Procedure:
To Edit / Compile/ Generate Hex file: Refer Section 2.3 To download and run this program: Refer Chapter 4

Output:
After pressing any key on 4*4 matrix keyboard, its keycode will be displayed on all 7 Segments. Note: In the similar way we can show the keycode on LCD display. This program is also included in the CD.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 87 of 91

Embedded Lab Manual

Experiment 14: To develop and execute the program to output the result of addition of contents of registers R0 and R1 on all four ports for the Microcontroller 8051.
Aim: Write C language program for Microcontroller 8051 to output the result of addition of contents of register R0 and R1 on all four ports. Equipments: SC51, PC, Mini51-PE, SDIO8. Block Diagram:
Mini51-PE / 8051 Evaluation board SDIO8 / LED Key Board Port 2 Port 0

SDIO8 / LED Key Board

SDIO8 / LED Key Board

Port 3

Port 1

SDIO8 / LED Key Board

Source Code:
#include <Intel\8051.h> /* While writing the program in C language, we can't directly access internal registers (like r0, r1 etc.) of micrcontroller. To access these registers we have to use assembly language only. Therefore we will insert assembly language statements directly into our C program using asm keyword. The asm keyword: This is an additional keyword, which allows you to insert assembly language statements directly into your C program. This keyword when used must appear as the first word of a line. Everything following this keyword - up to the end of line - is directly copied into the .ASM output file. Please note that, no syntax checking is done by the compiler on this assembly language statement, it is done later on by the assembler. */

void main () { asm mov r0,#01 asm mov r1,#02 asm mov a,r0 asm add a,r1 asm mov P0,a

// move data 01H to register r0 // move data 02H to register r1 // move data present in register r0 to accumulator // add data present in register r1 with data present in accumulator // output result to Port 0

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 88 of 91

Embedded Lab Manual


asm mov P1,a asm mov P2,a asm mov P3,a // output result to Port 1 // output result to Port 2 // output result to Port 3

while(1) { // Do nothing loop... } }

Connections:
Connect 10-pin cable supplied to you along with SDIO8 board as follows: Connect black end of 10 Pin Cable to J1 connector on SDIO8 board and white end to the respected Port on Mini51-PE board.

Procedure:
To Edit / Compile/ Generate Hex file: Refer Section 2.3 To download and run this program: Refer Chapter 4

Output:
You can see the result of addition of contents of register R0 and R1 on LEDs (on SDIO8 board) connected to the any Port of 8051 microcontroller because to all the ports same data is sent.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 89 of 91

Embedded Lab Manual

Experiment 15: Assume 1 Hz. Frequency pulse is connected to I/P P3.4 Write a Program to display count on LCD.
Aim: Write a C language program for Microcontroller 8051 to display count on LCD of 1 Hz frequency pulse connected to I/P P3.4. Equipments: SC51, PC, Mini51-PE, LCD Interface Board, Frequency generator. Block Diagram:

Mini51-PE / 8051 Evaluation board

Count Freq: 012

LCD

FRC 2 Port 3

FRC2

LCD Interface Board Function Generator 1 Hz freq. pulse connected to P3.4 (Pin 5)

Source Code:
#include <Intel\8051.h> #include "lcd.h" #include <standard.h> #include <stdio.h> unsigned char count, sztemp; void main () { lcd_init(); TMOD = 0x06 ; TCON = 0x10 ; TH0 = 0x00; T0 = 1; while(1) { TR0 = 1; if(!TF0) {

//counter 0, mode 2, C/T = 1

//make T0 (P3.4) as input

//start counter 0 //check TF0

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 90 of 91

Embedded Lab Manual


count = TL0; //get result/count from TL0 sprintf(sztemp,"Count Freq: %03bu",count); display_row(0,sztemp); } TR0 = 0; TF0 = 0; } } //stop counter 0 //clear TF0

Connections:
Connect 1 Hz frequency pulse to P3.4 (pin number 5 on Port 3 connector). Connect FRC cable between FRC 2 connector on Mini51-PE board and FRC 2 connector on LCD Interface Board.

Procedure:
To Edit / Compile/ Generate Hex file: Refer Section 2.3 To download and run this program: Refer Chapter 4

Output:
You can see the count on LCD.

SPJETPL All Rights Reserved. (www.spjembedded.com)

Page 91 of 91