You are on page 1of 60

MALLAREDDY ENGINEERING AND TECHNOLOGY

DESCRIPTION ABOUT ES CONCEPTS:


Embedded systems are designed to do some specific task, rather than be a general-purpose computer for multiple tasks. Some also have real-time performance constraints that must be met, for reason such as safety and usability; others may have low or no performance requirements,allowing the system hardware to be simplified to reduce costs. Embedded systems are not always separate devices. Most often they are physically built-in to the devices they control. The software written for embedded systems is often called firmware, and is stored in readonly memory or Flash memory chips rather than a disk drive. It often runs with limited computer hardware resources: small or no keyboard, screen, and little memory. Embedded systems range from no user interface at all dedicated only to one task to full user interfaces similar to desktop operating systems in devices such as PDAs. Simple embedded devices use buttons, LEDs, and small character or digit only displays, often with a simple menu system

MALLAREDDY ENGINEERING AND TECHNOLOGY

INTRODUCTION
This is users manual for SC51 Trainer Kit based on 89V51RD2 (8051) micro-controller. This trainer board is designed to perform lab practical and has a facility to download the user programs into the on-chip flash memory of the micro-controller. It is strongly recommended to read this manual carefully before you start using the SC51 board.

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 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 microcontroller, 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 microcontroller has 128 or 256 bytes internal RAM,respectively. 5. In the same window, the Crystal frequency field is also displayed with default value as 12MHz. 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.
2

MALLAREDDY ENGINEERING AND TECHNOLOGY 6. Click on Assembler / Linker Options to display that part of the dialog window. UnderProgram 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.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. 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, the 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
3

MALLAREDDY ENGINEERING AND TECHNOLOGY 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.

MALLAREDDY ENGINEERING AND TECHNOLOGY

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!

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.

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

MALLAREDDY ENGINEERING AND TECHNOLOGY

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

MALLAREDDY ENGINEERING AND TECHNOLOGY 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.

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

MALLAREDDY ENGINEERING AND TECHNOLOGY Internal RAM (Bytes) selectionautomatically 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.

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.
8

MALLAREDDY ENGINEERING AND TECHNOLOGY 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 labelel Generate Absolute OMF file. This will create .AOM file, which is required by the ICE.

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

MALLAREDDY ENGINEERING AND TECHNOLOGY will indicatethat 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.

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

10

MALLAREDDY ENGINEERING AND TECHNOLOGY

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.

Here we write a program for serial communication.

Aim: Write a C program to send WELCOME on serial port continuously. Description:

11

MALLAREDDY ENGINEERING AND TECHNOLOGY

2) USING THE VISUAL CODE GENERATOR (VCG):


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

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

12

MALLAREDDY ENGINEERING AND TECHNOLOGY

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 TIMER1 for Baud Rate Generation. Click on Ok. Paste the code in the program.

13

MALLAREDDY ENGINEERING AND TECHNOLOGY

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. Enable TR1: Check to run Timer 1 Timer 1 Mode: 2 8-Bit Auto Reload Mode Click on Generate Code

14

MALLAREDDY ENGINEERING AND TECHNOLOGY

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

15

MALLAREDDY ENGINEERING AND TECHNOLOGY

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.

16

MALLAREDDY ENGINEERING AND TECHNOLOGY

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.

17

MALLAREDDY ENGINEERING AND TECHNOLOGY 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 */ } } From the Compile menu, select Build

18

MALLAREDDY ENGINEERING AND TECHNOLOGY

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

19

MALLAREDDY ENGINEERING AND TECHNOLOGY

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.

20

MALLAREDDY ENGINEERING AND TECHNOLOGY

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

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

21

MALLAREDDY ENGINEERING AND TECHNOLOGY

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.

22

MALLAREDDY ENGINEERING AND TECHNOLOGY

4) USING THE SIMULATOR:

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.

23

MALLAREDDY ENGINEERING AND TECHNOLOGY

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

24

MALLAREDDY ENGINEERING AND TECHNOLOGY

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.

25

MALLAREDDY ENGINEERING AND TECHNOLOGY

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.

26

MALLAREDDY ENGINEERING AND TECHNOLOGY

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.

27

MALLAREDDY ENGINEERING AND TECHNOLOGY

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.

28

MALLAREDDY ENGINEERING AND TECHNOLOGY

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.

29

MALLAREDDY ENGINEERING AND TECHNOLOGY

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.

30

MALLAREDDY ENGINEERING AND TECHNOLOGY

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.

31

MALLAREDDY ENGINEERING AND TECHNOLOGY

32

MALLAREDDY ENGINEERING AND TECHNOLOGY

DOWNLOADING AND RUNNING USER PROGRAMS


Microcontroller P89V51RD2 includes on-chip flash for storing user program and nonvolatile data. Thisflash is In-System-Programmable (ISP). Therefore it is possible to download user program into on-chipflash, 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)

33

MALLAREDDY ENGINEERING AND TECHNOLOGY

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.
34

MALLAREDDY ENGINEERING AND TECHNOLOGY 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:

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.
35

MALLAREDDY ENGINEERING AND TECHNOLOGY 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

36

MALLAREDDY ENGINEERING AND TECHNOLOGY

EXPERIMENT NO: 1 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 MRIET
WELCOMES U On 16x2 LCD Source code:
#include <Intel\8051.h> #include <standard.h> BIT RS P3.2 BIT RW P3.1 BIT EN P3.0 #define DATAPORT P2 unsigned char g_byData = 0xff; void lcd_cmd(unsigned char p_byCmd) { RS=0; RW=0; DATAPORT = p_byCmd; EN=1; asm nop asm nop EN=0; } void lcd_dat(unsigned char p_byData) { RS=1; RW=0; DATAPORT = p_byData; EN=1; asm nop asm nop

37

MALLAREDDY ENGINEERING AND TECHNOLOGY

EN=0; } void lcd_init() { lcd_cmd(0x38); delay_ms(30); lcd_cmd(0x38); delay_ms(30); lcd_cmd(0x38); delay_ms(30); lcd_cmd(0x08); delay_ms(6); lcd_cmd(0x0c); delay_ms(6); lcd_cmd(0x06); delay_ms(6); } void display_row(unsigned char p_byRow, unsigned char p_byDataArr[16]) { unsigned char count, ch; if(p_byRow) lcd_cmd (0xc0); else lcd_cmd(0x80); delay_ms(250); for (count = 0;count<16;count++) { if(ch=p_byDataArr[count]) { lcd_dat(ch); } else { while(count<16) { lcd_dat(' '); 38

MALLAREDDY ENGINEERING AND TECHNOLOGY

count++; } } } } void main () { lcd_init(); while(1) { display_row(0,"MRIET"); display_row(1,"WELCOMES U"); } }

39

MALLAREDDY ENGINEERING AND TECHNOLOGY

EXPERIMENT NO: 2 Program to toggle all the bits of Port P1 continuously with 250 mS delay

Aim: Write a program to toggle all bits of ports p1 continuously with 250ms delay.
#include <REG51.H> // special function register declarations for the intended 8051 derivative #define LEDPORT P1

void Delay(void);

//Function prototype declaration

void main (void) { LEDPORT = 0x00; while(1) { LEDPORT = 0x30; Delay(); LEDPORT = 0xF2; Delay(); } } void Delay() { unsigned char i = 0; TMOD = 0x10; for(i = 0; i < 5; i++) { TF1 = 0; TR1 = 0; TH1 = 0x3C; TL1 = 0xAF; TR1 = 1; while(TF1 != 1); } } 40 //11110010binary //00110000 binary

MALLAREDDY ENGINEERING AND TECHNOLOGY

EXPERIMENT NO: 3 Program to toggle only the bit P1.5 continuously with some delay. Use Timer 0, mode 1 to create delay. Aim: Write a Program to toggle only the bit P1.5 continuously with some delay. Use
Timer 0, mode 1 to create delay
#include <REG51.H> sbit LEDPIN = P1^5; void Delay(void); void main (void); { LEDPIN = 0; while(1) { LEDPIN = 1; Delay(); LEDPIN = 0; Delay(); } } void Delay(void) { TMOD = 0x01; for(i = 0; i < 20; i++) { TF0 = 0; TR0 = 0; TH0 = 0x3C; TL0 = 0xAF; TR0 = 1; while(TF0 != 1); } }

41

MALLAREDDY ENGINEERING AND TECHNOLOGY

EXPERIMENT NO: 4 Program to interface a switch and a buzzer to two different pins of a Port such that the buzzer should sound as long as the switch is pressed. Aim: Write a Program to interface a switch and a buzzer to two different pins of a
Port such that the buzzer should sound as long as the switch is pressed
#include <REG51.H> sbit SW1 = P1^4; sbit BUZZER = P3^6; void main (void) { BUZZER = 0; while(1) { if(SW1 == 0) { BUZZER = 1; } else BUZZER = 0; } }

42

MALLAREDDY ENGINEERING AND TECHNOLOGY

EXPERIMENT NO: 5 Program to interface LCD data pins to port P1 and display a message on it.
Aim: Write a Program to interface LCD data pins to port P1 and display a message on it.
#include <reg51.h> #include "lcd.h" // refer LCD.H code for more to about LCD void Delay_sec(unsigned char s); main( ) { LCD_init(); while(1) { LCD_clear(); Delay_sec(1); LCD_row1(); LCD_puts(" HELLO LCD123. "); LCD_row2(); LCD_puts(" TEST PROGRAM "); Delay_sec(5); LCD_clear(); Delay_sec(1); LCD_clear(); LCD_row1(); LCD_puts("UNISTRING TECH "); LCD_row2(); LCD_puts("SOLUTION Pvt Ltd"); Delay_sec(5); } } void Delay_sec(unsigned char s) { unsigned char n; for (n=0; n<s; n++) { LCD_delay(250); LCD_delay(250); } }

43

MALLAREDDY ENGINEERING AND TECHNOLOGY

EXPERIMENT NO: 6 Program to interface keypad. Whenever a key is pressed, it should be displayed on LCD.
Aim: Write a Program to interface keypad. Whenever a key is pressed, it should be displayed on LCD.
#include <reg51.h> #include"lcd.h"

sbit C1 = P1^0; sbit C2 = P1^1; sbit C3 = P1^2; sbit C4 = P1^3; sbit R1 = P1^4; sbit R2 = P1^5; sbit R3 = P1^6; sbit R4 = P1^7; unsigned char key; void Delay(unsigned int); void delay() { unsigned int i; for (i=0; i<10; i++); /* For 1 ms */ } unsigned char READ_SWITCHES (void) // initialize the port for inputs { // P1.0 to p1.3 are outpot; and P1.4 to P1.7 are inputs // the keybad is connected to port 1 // make all rows = 1 R4=1; R3=1; R2=1; R1=0; //test row 1 if (C1 == 0) { // key 1 is presed delay(); //debounce while (C1==0); //wait until release the key return 1; } if (C2 == 0) { //key 2 is pressed delay(); //debounce while (C2==0); //wait until release the key 44

MALLAREDDY ENGINEERING AND TECHNOLOGY


return 2; } if (C3 == 0) { delay(); //key 3 is pressed //debounce

while (C3==0); //wait until release the key return 3; } if (C4 == 0) { //key 4 is pressed delay(); //debounce while (C4==0); //wait until release the key return 4; } //test row 2 R4=1; R3=1; R2=0; R1=1; if (C1 == 0) {

//key 5 is pressed delay(); //debounce while (C1==0); //wait until release the key return 5;

} if (C2 == 0) { delay(); //key 6 is pressed //debounce while (C2==0); //wait until release the key return 6;

} if (C3 == 0) { delay(); while (C3==0); return 7; } if (C4 == 0) { delay(); while (C4==0); return 8; } //test row 3 R4=1; //key 7 is pressed //debounce //wait until release the key //key 6 is pressed //debounce //wait until release the key

45

MALLAREDDY ENGINEERING AND TECHNOLOGY


R3=0; R2=1; R1=1; if (C1 == 0) { //key 8 is pressed delay(); //debounce while (C1==0); //wait until release the key return 9; } if (C2 == 0) { //key 9 is pressed

delay(); //debounce while (C2==0); //wait until release the key return 10; } if (C3 == 0) { //key A is pressed delay(); //depounce while (C3==0); //wait until release the key return 11; } if (C4 == 0) { //key B is pressed delay(); //depounce while (C4==0); //wait until release the key return 12; } //test row 4 R1=1; R2=1; R3=1; R4=0; if (C1 == 0) { //key C is pressed delay(); //depounce while (C1==0); //wait until release the key return 13; } if (C2 == 0) { delay(); while (C2==0); return 14; } if (C3 == 0) { delay(); while (C3==0); return 15; } 46 //key E is pressed //depounce //wait until release the key //key D is pressed //depounce //wait until release the key

MALLAREDDY ENGINEERING AND TECHNOLOGY

if (C4 == 0) {

//key C is pressed delay(); //depounce while (C4==0); //wait until release the key return 16; // Means no key has been pressed

} return 0; }

void main (void) { P1 =0x0f; P3=0x00; LCD_init(); LCD_row1(); LCD_puts("< SERIAL KEYPAD >"); LCD_row2(); LCD_puts(" INTERFACING "); Delay(3); while(1) { key=READ_SWITCHES(); if(key) { P3 = key-1; LCD_clear(); LCD_row1(); LCD_puts("< SERIAL KEYPAD >"); LCD_row2(); LCD_puts("KEY : "); switch(key) { case 1: { LCD_putc('0'); break; } case 2: { LCD_putc('1'); break; } case 3: { LCD_putc('2'); break; 47

MALLAREDDY ENGINEERING AND TECHNOLOGY

case 4: { LCD_putc('3'); break; } case 5: { LCD_putc('4'); break; } case 6: { LCD_putc('5'); break; } case 7: { LCD_putc('6'); break; } case 8: { LCD_putc('7'); break; } case 9: { LCD_putc('8'); break; } case 10: { LCD_putc('9'); break; } case 11: { LCD_putc('A'); break; } case 12: { LCD_putc('B'); break; }

48

MALLAREDDY ENGINEERING AND TECHNOLOGY


case 13: { LCD_putc('C'); break; } case 14: { LCD_putc('D'); break; } case 15: { LCD_putc('E'); break; } case 16: { LCD_putc('F'); break; } }// switch }//if }//while

}//main void Delay(unsigned int duration) { unsigned int r2; for (r2 = 0; r2<= duration;r2++) { LCD_delay(250); LCD_delay(250); } }

49

MALLAREDDY ENGINEERING AND TECHNOLOGY

EXPERIMENT NO: 7 Program to interface seven segment display unit. Aim: Write a Program to interface seven segment display unit.
#include <REG51.H> #include <stdio.h> #define LEDPORT sbit CTRL0 = P1^0; sbit CTRL1 = P1^1; sbit CTRL2 = P1^2; sbit CTRL3 = P1^3; #define ZERO #define ONE 0x9E #define TWO #define THREE 0x0C #define FOUR 0x98 #define FIVE 0x48 #define SIX #define SEVEN 0x1E #define EIGHT 0x00 #define NINE 0x18 #define TEN 0x10 #define ELEVEN #define TWELVE #define THIRTEEN #define FOURTEEN #define FIFTEEN void Delay(void); void main (void) { CTRL0 = 1; CTRL1 = 1; CTRL2 = 1; CTRL3 = 1; while(1) { LEDPORT = ZERO; Delay(); LEDPORT = ONE; Delay(); LEDPORT = TWO; Delay(); LEDPORT = THREE; Delay() 50 0x02 0x24 P0

0x40

0xC0 0x62 0x84 0x60 0x70

MALLAREDDY ENGINEERING AND TECHNOLOGY

LEDPORT = FOUR; Delay(); LEDPORT = FIVE; Delay(); LEDPORT = SIX; Delay(); LEDPORT = SEVEN; Delay(); LEDPORT = EIGHT; Delay(); LEDPORT = NINE; Delay(); LEDPORT = TEN; Delay(); LEDPORT = ELEVEN; Delay(); LEDPORT = TWELVE; Delay(); LEDPORT = THIRTEEN; Delay(); LEDPORT = FOURTEEN; Delay(); LEDPORT = FIFTEEN; Delay(); } } void Delay(void) { int j; int i; for(i=0;i<10;i++) { for(j=0;j<10000;j++) { } } }

51

MALLAREDDY ENGINEERING AND TECHNOLOGY

EXPERIMENT NO: 8 Program to transmit a message from Microcontroller to PC serially using RS232. Aim: Write a Program to transmit a message from Microcontroller to PC serially using
RS232.
#include <REG51.h> /* define 8051 registers */ void SendChar(unsigned char x); void DisplayMesPC(unsigned char *); void serial_init(void); unsigned char *mes; void main (void) /* main program start */ { serial_init(); mes = "\r\nUniString Tech Solutions Pvt Ltd\r\n\r\nWelcome to Serial communication Demo \r\n"; DisplayMesPC(mes); mes = "\r\nif above txt visible, then transmission sucess\r\n"; DisplayMesPC(mes); while(1); }

void SendChar(unsigned char x) // transmit function to send character to PC { SBUF =x; // wrting the character into the serial buffer TI = 0; // Clearing the Transmit empty flag while(!TI); // wating for end of trasmission. after transmission the TI flag will set. } void DisplayMesPC(unsigned char *mes) { int counter; for (counter=0;mes[counter]!='\0';counter++) { SendChar(mes[counter]); } } void serial_init() { TMOD = 0x20; /* GATE OFF,C/#T = 0, M1 M0 = 10(8 BIT AUTO RELOAD) TIMER 0 ,TIMER 1 IN MODE 2(AUTO RELOAD MODE)*/ SCON = 0x50; /* SERIAL PORT IN MODE2 8-BIT UART VARIABLE BAUDRATE */ TH1 = 0xfd; /* TIMER 1 FOR BAUD RATE GEN(9.6K)*/ TR1 = 1; /* baud rate timer start*/ }

52

MALLAREDDY ENGINEERING AND TECHNOLOGY

EXPERIMENT NO: 9 Program to receive a message from PC serially using RS232. Aim:Write a Program to receive a message from PC serially using RS232.
#include <reg51.h> /* define 8051 registers */

void SendChar(unsigned char x); void DisplayMesPC(unsigned char *); void serial_init(void); unsigned char *mes; void main (void) { /* main program */ unsigned char temp; serial_init(); mes = "\r\nUniString Tech Solutions Pvt Ltd\r\n\r\nSerial data transmission and reception Demo\r\n\r\nPlease type in characters to see on the terminal\r\n\r\n"; DisplayMesPC(mes); while (1) { while (!RI); // waiting for character from PC temp = SBUF; // reading the character into temp variable from Serial Buffer RI = 0; // Resetting the Receive Flag SendChar(temp); // Calling function for retransmitting the character back to PC //-----------------------------------------SendChar(temp); retransmitting the character back to PC } } void SendChar(unsigned char x) // transmit function to send character to PC { SBUF =x; // wrting the character into the serial buffer TI = 0; // Clearing the Transmit empty flag while(!TI); // wating for end of trasmission. after transmission the TI flag will set. } void DisplayMesPC(unsigned char *mes) { int counter; for (counter=0;mes[counter]!='\0';counter++) { // Calling function for

53

MALLAREDDY ENGINEERING AND TECHNOLOGY

SendChar(mes[counter]); } } void serial_init() { TMOD = 0x20; /* GATE OFF,C/#T = 0, M1 M0 = 10(8 BIT AUTO RELOAD) TIMER 0 ,TIMER 1 IN MODE 2(AUTO RELOAD MODE)*/ SCON = 0x50; /* SERIAL PORT IN MODE2 8-BIT UART VARIABLE BAUDRATE */ TH1 = 0xfd; /* TIMER 1 FOR BAUD RATE GEN(9.6K)*/ TR1 = 1; /* baud rate timer start*/ }

54

MALLAREDDY ENGINEERING AND TECHNOLOGY

EXPERIMENT NO: 10 Program to get analog input from Temperature sensor and display the temperature value on PC Monitor. Aim: Write a Program to get analog input from Temperature sensor and display the
temperature value on PC Monitor.
#include <reg51.h> #include "lcd.h" sbit rd=P1^0; sbit wr=P1^1; sbit intr=P1^2; /* define 8051 registers */

//Read signal P1.0 //Write signal P1.1 //INTR signal P1.3

void conv(); //Start of conversion function void read(); //Read ADC function void display(); void Delay(int a); void SendChar(unsigned char x); void DisplayMesPC(unsigned char *); unsigned char *mes; unsigned char adc_val; unsigned int value,temp,temp1,temp2,temp3; void Delay(int a) { char i; for(i=0;i<a;i++) { LCD_delay(250); LCD_delay(250); } } void serial_init() { TMOD = 0x20; /* GATE OFF,C/#T = 0, M1 M0 = 10(8 BIT AUTO RELOAD) TIMER 0 ,TIMER 1 IN MODE 2(AUTO RELOAD MODE)*/ SCON = 0x50; /* SERIAL PORT IN MODE2 8-BIT UART VARIABLE BAUDRATE */ TH1 = 0xfd; /* TIMER 1 FOR BAUD RATE GEN(9.6K)*/ TR1 = 1; /* baud rate timer start*/ } void PowerOn() { unsigned char inner, outer; for (outer = 0x00; outer < 0x10; outer++) { for (inner = 0x00; inner < 0xFF; inner++); } 55

MALLAREDDY ENGINEERING AND TECHNOLOGY

LCD_init(); for (inner = 0; inner < 10; inner++) LCD_delay(2); serial_init(); }

void main() { P0=0xFF; P1=0x04; PowerOn(); while(1) { LCD_clear(); LCD_row1(); LCD_puts("Temperature sen "); DisplayMesPC("\r\nTemparature Value : "); conv(); //Start conversion read(); //Read ADC display(); //Send the read value PC and LCD Delay(4); } } void conv() { wr = 0; wr = 1; while(intr); } void read() { rd = 0; adc_val = P0; rd = 1; }

//Make WR low //Make WR high //Wait for INTR to go low

//Make RD low //Read ADC port //Make RD high

void display() { // display the adc vvalue in the form of milli volts LCD_row2(); LCD_puts("Temp :"); value = adc_val; value = value*2; //adc_value=(adc_value*20)/10; where 20 is if(value >= 1000) { temp = value / 1000; adc_val = temp + 48; LCD_putc(adc_val); 56

MALLAREDDY ENGINEERING AND TECHNOLOGY


SendChar(adc_val); } temp1=value%1000; if(temp1>=100&&temp1<=999) { temp=temp1; temp=temp/100; adc_val=temp+48; LCD_putc(adc_val); SendChar(adc_val); } else { LCD_putc('0'); SendChar('0'); } temp2=temp1%100; if(temp2>=10&&temp<=99) { temp=temp2; temp=temp/10; adc_val=temp+48; LCD_putc(adc_val); SendChar(adc_val); } else { LCD_putc('0'); SendChar('0'); } temp3=temp2%10; if(temp3>0&&temp3<10) { temp=temp3; adc_val=temp+48; LCD_putc(adc_val); SendChar(adc_val); } LCD_putc(0XDF); SendChar('*'); SendChar(' '); SendChar('C'); LCD_putc('*'); LCD_putc('C'); } void SendChar(unsigned char x) // transmit function to send character to PC { SBUF =x; // wrting the character into the serial buffer TI = 0; // Clearing the Transmit empty flag

57

MALLAREDDY ENGINEERING AND TECHNOLOGY


while(!TI); } // wating for end of trasmission. after transmission the TI flag will set.

void DisplayMesPC(unsigned char *mes) { int counter; for(counter=0;mes[counter]!='\0';counter++) { SendChar(mes[counter]); } }

58

MALLAREDDY ENGINEERING AND TECHNOLOGY

EXPERIMENT NO: 11 Program to interface Stepper Motor to rotate the motor in clockwise and anticlockwise Directions. Aim: Write a Program to interface Stepper Motor to rotate the motor in clockwise and
anticlockwise Directions.

#include <reg51.h> sbit SW1 = P1^0; sbit SW2 = P1^1; void motor_delay(int sec) ; void main(void) { P3=0x00; // SW1 = 0; // SW2 = 0; while(1) { if(SW1 == 1) { P3 = 0x50; motor_delay(2); P3 = 0x90; motor_delay(2); P3 = 0xA0; motor_delay(2); P3 = 0x60; motor_delay(2); P3 = 0x60; motor_delay(2); P3 = 0xA0; motor_delay(2); P3 = 0x90; motor_delay(2); P3 = 0x50; motor_delay(2); } else if(SW2 == 0) { P3 = 0x60; motor_delay(2); P3 = 0xA0; motor_delay(2); P3 = 0x90;

59

MALLAREDDY ENGINEERING AND TECHNOLOGY


motor_delay(2); P3 = 0x50; motor_delay(2); } } } void motor_delay(int sec) { int i,j,secd; for (secd=0;secd<=sec;secd++) for(i=0;i<=4;i++) for (j= 0;j<=800;j++) { } }

60

You might also like