Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 40

INTRODUCTION TO 8051

The Intel 8051 is an 8-bit microcontroller which means that most available
operations are limited to 8 bits. There are 3 basic "sizes" of the 8051: Short, Standard, and
Extended. The Short and Standard chips are often available in DIP (dual in-line package)
form, but the Extended 8051 models often have a different form factor, and are not "drop-
in compatible". All these things are called 8051 because they can all be programmed using
8051 assembly language, and they all share certain features (although the different models
all have their own special features).
Some of the features that have made the 8051 popular are:

 4 KB on chip program memory.


 128 bytes on chip data memory(RAM)
 4 reg banks
 128 user defined software flags.
 8-bit data bus
 16-bit address bus
 16 bit timers (usually 2, but may have more, or less).
 3 internal and 2 external interrupts.
 Bit as well as byte addressable RAM area of 16 bytes.
 Four 8-bit ports, (short models have two 8-bit ports).
 16-bit program counter and data pointer.
 1 Microsecond instruction cycle with 12 MHz Crystal.
8051 models may also have a number of special, model-specific features, such as
UART, ADC, Op_Amps, etc... it is a very powerful micro controller.
Typical Applications:
8051 chips are used in a wide variety of control systems, telecom applications,
robotics as well as in the automotive industry. By some estimations, 8051 family chips
make up over 50% of the embedded chip market.

Basic Pins:
PIN 9: PIN 9 is the reset pin which is used to reset the microcontroller‟s internal registers
and ports upon starting up. (Pin should be held high for 2 machine cycles.)
PINS 18 & 19: The 8051 has a built-in oscillator amplifier hence we need to only connect
a crystal at these pins to provide clock pulses to the circuit.
PIN 40 and 20: Pins 40 and 20 are VCC and ground respectively. The 8051 chip needs
+5V 500mA to function properly, although there are lower powered versions like the
Atmel 2051 which is a scaled down version of the 8051 which runs on +3V.
PINS 29, 30 & 31: As described in the features of the 8051, this chip contains a built-in
flash memory. In order to program this we need to supply a voltage of +12V at pin 31. If
external memory is connected then PIN 31, also called EA/VPP, should be connected to

Dept of ECE, Prasiddha College of Engineering and Technology


1
ground to indicate the presence of external memory. PIN 30 is called ALE (address latch
enable), which is used when multiple memory chips are connected to the controller and
only one of them needs to be selected.We will deal with this in depth in the later chapters.
PIN 29 is called PSEN. This is "program store enable". In order to use the external memory
it is required to provide the low voltage (0) on both PSEN and EA pins.

Ports:
There are 4 8-bit ports: P0, P1, P2 and P3.
PORT P1 (Pins 1 to 8): The port P1 is a general purpose input/output port which can be
used for a variety of interfacing tasks. The other ports P0, P2 and P3 have dual roles or
additional functions associated with them based upon the context of their usage.The port 1
output buffers can sink/source four TTL inputs. When 1s are written to portn1 pins are
pulled high by the internal pull-ups and can be used as inputs.
PORT P3 (Pins 10 to 17): PORT P3 acts as a normal IO port, but Port P3 has additional
functions such as, serial transmit and receive pins, 2 external interrupt pins, 2 external
counter inputs, read and write pins for memory access.
PORT P2 (pins 21 to 28): PORT P2 can also be used as a general purpose 8 bit port when
no external memory is present, but if external memory access is required then PORT P2
will act as an address bus in conjunction with PORT P0 to access external memory. PORT
P2 acts as A8-A15, as can be seen from figure.
PORT P0 (pins 32 to 39): PORT P0 can be used as a general purpose 8 bit port when no
external memory is present, but if external memory access is required then PORT P0 acts
as a multiplexed address and data bus that can be used to access external memory in
conjunction with PORT P2. P0 acts as AD0-AD7, as can be seen from figure.
PORT P10: asynchronous communication input or Serial synchronous communication
output.
Oscillator Circuits:
The 8051 requires an external oscillator circuit. The oscillator circuit usually runs
around 12MHz, although the 8051 (depending on which specific model) is capable of
running at a maximum of 40MHz. Each machine cycle in the 8051 is 12 clock cycles,
giving an effective cycle rate at 1MHz (for a 12MHz clock) to 3.33MHz (for the maximum
40MHz clock). The oscillator circuit generates the clock pulses so that all internal
operations are synchronized.
One machine cycle has 6 states. One state is 2 T-states. Therefore one machine
cycle is 12 T-states. Time to execute an instruction is found by multiplying C by 12 and
dividing product by Crystal frequency.
T=(C*12d)/crystal frequency

Dept of ECE, Prasiddha College of Engineering and Technology


2
Internal Architecture:

Data and Program memory:


The 8051 Microcontroller can be programmed in PL/M, 8051 Assembly, C and a
number of other high-level languages. Many compilers even have support for compiling
C++ for an 8051.
Program memory in the 8051 is read-only, while the data memory is considered to
be read/write accessible. When stored on EEPROM or Flash, the program memory can be
rewritten when the microcontroller is in the special programmer circuit.

Dept of ECE, Prasiddha College of Engineering and Technology


3
Program Start Address:
The 8051 starts executing program instructions from address 0000 in the program
memory. The A register is located in the SFR memory location 0xE0. The A register works
in a similar fashion to the AX register of x86 processors. The A register is called the
accumulator, and by default it receives the result of all arithmetic operations.

Special Function Register:


The Special Function Register (SFR) is the upper area of addressable memory, from
address 0x80 to 0xFF. A, B, PSW, DPTR are called SFR. This area of memory cannot be
used for data or program storage, but is instead a series of memory-mapped ports and
registers. All port input and output can therefore be performed by memory mov operations
on specified addresses in the SFR. Also, different status registers are mapped into the SFR,
for use in checking the status of the 8051, and changing some operational parameters of the
8051.

General Purpose Registers:


The 8051 has 4 selectable banks of 8 addressable 8-bit registers, R0 to R7. This
means that there are essentially 32 available general purpose registers, although only 8 (one
bank) can be directly accessed at a time. To access the other banks, we need to change the
current bank number in the flag register.

A and B Registers:
The A register is located in the SFR memory location 0xE0. The A register works
in a similar fashion to the AX register of x86 processors. The A register is called the
accumulator, and by default it receives the result of all arithmetic operations. The B register
is used in a similar manner, except that it can receive the extended answers from the
multiply and divide operations. When not being used for multiplication and Division, the B
register is available as an extra general-purpose register. The A and B registers can store up
to 8-bits of data each.

Dept of ECE, Prasiddha College of Engineering and Technology


4
INTRODUCTION TO KEIL SOFTWARE
This tutorial will assist in writing first 8051 Assembly language program using the popular
Keil Compiler. Keil offers an evaluation package that will allow the assembly and
debugging of files 2K or less.
1. Open Keil from the Start menu

2. Select New project from the project menu

Dept of ECE, Prasiddha College of Engineering and Technology


5
3. Give the Project name

4. Click on the save button


5. The device window will be displayed. Select the part you will be using to test with.
For now we will use Generic. Double Click on the Generic

Dept of ECE, Prasiddha College of Engineering and Technology


6
6. Scroll down and select the 8051(all Variants)part
7. Click OK

8. The dialog box will appear as below press YES

Dept of ECE, Prasiddha College of Engineering and Technology


7
9. Click File menu and select NEW

10. A new window will open up in the Keil IDE

Dept of ECE, Prasiddha College of Engineering and Technology


8
11. Asm program on the editor

12. Click file menu and save

Dept of ECE, Prasiddha College of Engineering and Technology


9
13. Name the file blinking.asm and click the save button

14. Expand Target 1 in the tree menu

Dept of ECE, Prasiddha College of Engineering and Technology


10
15. Right click on source group and click on add files to group „Source Group 1‟…

16. Change file type to asm source file(*.a*;*.src) and click on blinking.asm
17. Click add button and click close button

Dept of ECE, Prasiddha College of Engineering and Technology


11
18. The source group 1 in the tree menu to ensure that the file was added t project

19. Click the project menu and click translate current active file

Dept of ECE, Prasiddha College of Engineering and Technology


12
20. After click translate in the build window shows the errors and warnings if any.

21. Click on target 1 in tree menu


22. Click on the project menu and select the options for Target1

Dept of ECE, Prasiddha College of Engineering and Technology


13
23. Select Target tab and change Xtal (Mhz) from 12.0 to 11.0592

24. Select Output Tab and Click on create Hex file Check Box
25. Click ok button

Dept of ECE, Prasiddha College of Engineering and Technology


14
26. Click on the project menu select build target in the build window it should report
errors and warnings, if any.

27. Click on the debug menu and select start/stop debug session

Dept of ECE, Prasiddha College of Engineering and Technology


15
28. The keil debugger should be now be running

29. Click on peripherals. Select I/O ports

Dept of ECE, Prasiddha College of Engineering and Technology


16
30. A new window should port will pop up. This represent the port and pins

31. Press F5 on the keyboard to run the program.

32. To exit out, Click on debug menu and select start/stop debug session.

Dept of ECE, Prasiddha College of Engineering and Technology


17
HELLO WORLD

Aim: To write an embedded c program to print” Helloworld” using keilµVision software.

Apparatus:
1. Desktop PC
2. Keil µVision software
3. Protous software
Procedure:
1. Open the keil µVision software in PC.
2. Create a new project and select the target board from the list.
3. Write the c code in the test file, compile and build it.
4. Open the Serial Window UART #1 to display the application’s output.
5. Click the Run button of the Debug Toolbar or choose Debug – Run to start the “Hello”
program. “Hello World” is printed to the Serial Window and the program enters into an endless
loop.
6. Click the Stop button to halt the program. Alternatively, press the Esc key while in the
Command Line of the Command Window.
7. Use the Insert/Remove Breakpoint command to set or clear a breakpoint.
8. Test the Reset command to reset the simulated microcontroller. If the program is still running,
it halts at the first breakpoint.
9. Single-step through the program using the Step buttons. The current instruction, which will
execute next, is marked with a yellow arrow. The yellow arrow moves each time you step.
Program:
#include <reg51.h>
#include <stdio.h>void main(void)
{
SCON=0x52; // SET SERIAL PORT TO MODE 1
TMOD=0x20; // TIMER1 MODE 2
TH1=-13; // RELOAD COUNTER FOR 2400 BAUD RATE
TR1=1; // START TIMER1
// Write your code here
while (1)

printf("Hello World\n");

RESULT: Thus the hello world program has been compiled, debugged and executed
successfully.

Dept of ECE, Prasiddha College of Engineering and Technology


18
LOOP APPLICATION TO COPY A VALUE FROM P1 TO P2

Aim: To write an embedded c program to copy a value from P1 to P2 of 8051 using keil µVision.

Apparatus:
1. Desktop PC.
2. Keil µVision software.
3. 8051 target board.
4. Protous software
Procedure:
1. Open the keil µVision software in PC.
2. Create a new project and select the target board from the list.
3. Write the c code in the test file, compile and build it.
4. Open the Serial Window UART #1 to display the application’s output.
5. Click the Stop button to halt the program. Alternatively, press the Esc key while in the Command
Line of the Command Window.
6. Use the Insert/Remove Breakpoint command to set or clear a breakpoint.
7. Test the Reset command to reset the simulated microcontroller. If the program is still running, it
halts at the first breakpoint.
8. Single-step through the program using the Step buttons. The current instruction, which will
execute next, is marked with a yellow arrow. The yellow arrow moves each time you step.
Program:
#include<reg51.h>
#include<string.h>
#define lcd_data P1
sbit lcd_rs = P1^2;
sbit lcd_en = P1^3;
sbit sw=P2^0;
unsigned char count=0;
void Lcd4_Decimal3(unsigned char);
void delay(unsigned int v)
{
unsigned int i,j;
for(i=0;i<=v;i++)
for(j=0;j<=1275;j++);
}
void lcdcmd(unsigned char value) // LCD COMMAND
{
lcd_data=value&(0xf0); //send msb 4 bits
lcd_rs=0; //select command register
lcd_en=1; //enable the lcd to execute command
delay(3);
lcd_en=0;
lcd_data=((value<<4)&(0xf0)); //send lsb 4 bits
lcd_rs=0; //select command register
lcd_en=1; //enable the lcd to execute command
delay(3);
lcd_en=0;

}
Dept of ECE, Prasiddha College of Engineering and Technology
19
void lcd_init(void)
{
lcdcmd(0x02);
lcdcmd(0x02);
lcdcmd(0x28); //intialise the lcd in 4 bit mode*/
lcdcmd(0x28); //intialise the lcd in 4 bit mode*/

lcdcmd(0x0e); //cursor blinking


lcdcmd(0x06); //move the cursor to right side
lcdcmd(0x01); //clear the lcd

void lcddata(unsigned char value)

lcd_data=value&(0xf0); //send msb 4 bits


lcd_rs=1; //select data register
lcd_en=1; //enable the lcd to execute data
delay(3);
lcd_en=0;
lcd_data=((value<<4)&(0xf0)); //send lsb 4 bits
lcd_rs=1; //select data register
lcd_en=1; //enable the lcd to execute data
delay(3);
lcd_en=0;

delay(3);
}
void msgdisplay(unsigned char b[]) // send string to lcd
{
unsigned char s,count=0;
for(s=0;b[s]!='\0';s++)
{
count++;
if(s==16)
lcdcmd(0xc0);
if(s==32)
{
lcdcmd(1);
count=0;
}
lcddata(b[s]);
}
}
void main(void)
{
lcd_init();
lcdcmd(0x01);
lcdcmd(0x80);
msgdisplay("welcome to ");
Dept of ECE, Prasiddha College of Engineering and Technology
20
lcdcmd(0xc0);
msgdisplay("prasidda clg ");
delay(100);delay(100);
lcdcmd(0x01);

while(1)
{
if(sw==1)
{
count=0;
}

if(sw==0)
{
count++;
}
lcdcmd(0x80);
Lcd4_Decimal3(count);

}}
void Lcd4_Decimal3(unsigned char val)
{
unsigned int Lcd_h,Lcd_hr,Lcd_t,Lcd_o;

Lcd_h=val/100;
Lcd_hr=val%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;

lcddata(Lcd_h+0x30);
lcddata(Lcd_t+0x30);
lcddata(Lcd_o+0x30);
}

RESULT: Thus the loop application to copy a value from P1 to P2 program has been compiled,
debugged and executed successfully.

Dept of ECE, Prasiddha College of Engineering and Technology


21
COUNTING THE NUMBER OF TIMES THAT A SWITCH IS PRESSED AND RELEASED.

Aim: To write an embedded c program for counting the number of times that a switch is pressed and
released in using keil µVision.
Apparatus:
1. Desktop PC.
2. Keil µVision software.
3. 8051 target board.
4. Protous software
Procedure:
1. Open the keil µVision software in PC.
2. Create a new project and select the target board from the list.
3. Write the c code in the test file, compile and build it.
4. Open the Serial Window UART #1 to display the application’s output.
5. Click the Stop button to halt the program. Alternatively, press the Esc key while in the Command
Line of the Command Window.
6. Use the Insert/Remove Breakpoint command to set or clear a breakpoint.
7. Test the Reset command to reset the simulated microcontroller. If the program is still running, it
halts at the first breakpoint.
8. Single-step through the program using the Step buttons. The current instruction, which will
execute next, is marked with a yellow arrow. The yellow arrow moves each time you step.
Program:
#include<reg51.h>
#include<string.h>
#define lcd_data P1
sbit lcd_rs = P1^2;
sbit lcd_en = P1^3;
sbit sw=P2^0;
unsigned char count=0;
void Lcd4_Decimal3(unsigned char);

void delay(unsigned int v)


{
unsigned int i,j;
for(i=0;i<=v;i++)
for(j=0;j<=1275;j++);
}

void lcdcmd(unsigned char value) // LCD COMMAND


{
lcd_data=value&(0xf0); //send msb 4 bits
lcd_rs=0; //select command register
lcd_en=1; //enable the lcd to execute command
delay(3);
lcd_en=0;
lcd_data=((value<<4)&(0xf0)); //send lsb 4 bits
lcd_rs=0; //select command register
lcd_en=1; //enable the lcd to execute command
delay(3);
lcd_en=0;

Dept of ECE, Prasiddha College of Engineering and Technology


22
}

void lcd_init(void)
{
lcdcmd(0x02);
lcdcmd(0x02);
lcdcmd(0x28); //intialise the lcd in 4 bit mode*/
lcdcmd(0x28); //intialise the lcd in 4 bit mode*/
lcdcmd(0x0e); //cursor blinking
lcdcmd(0x06); //move the cursor to right side
lcdcmd(0x01); //clear the lcd

}
void lcddata(unsigned char value)
{
lcd_data=value&(0xf0); //send msb 4 bits
lcd_rs=1; //select data register
lcd_en=1; //enable the lcd to execute data
delay(3);
lcd_en=0;
lcd_data=((value<<4)&(0xf0)); //send lsb 4 bits
lcd_rs=1; //select data register
lcd_en=1; //enable the lcd to execute data
delay(3);
lcd_en=0;
delay(3);
}
void msgdisplay(unsigned char b[]) // send string to lcd
{
unsigned char s,count=0;
for(s=0;b[s]!='\0';s++)
{
count++;
if(s==16)
lcdcmd(0xc0);
if(s==32)
{
lcdcmd(1);
count=0;
}
lcddata(b[s]);
}
}
void main(void)
{
lcd_init();
sw=1;
lcdcmd(0x01);
lcdcmd(0x80);
msgdisplay("welcome to ");
lcdcmd(0xc0);
msgdisplay("prasidda clg ");

Dept of ECE, Prasiddha College of Engineering and Technology


23
delay(100);delay(100);
lcdcmd(0x01);

while(1)
{

if(sw==0)
{
count++;
}
lcdcmd(0x80);
Lcd4_Decimal3(count);
lcdcmd(0xc0);
msgdisplay("welcomes u ");

}}

void Lcd4_Decimal3(unsigned char val)


{
unsigned int Lcd_h,Lcd_hr,Lcd_t,Lcd_o;

Lcd_h=val/100;
Lcd_hr=val%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;

lcddata(Lcd_h+0x30);
lcddata(Lcd_t+0x30);
lcddata(Lcd_o+0x30);
}

RESULT: Thus the counting the number of times that a switch is pressed and released program has
been compiled, debugged and executed successfully.

Dept of ECE, Prasiddha College of Engineering and Technology


24
CREATE A PORTABLE HARDWARE DELAY

Aim: To write an embedded program to create a portable hardware delay using keil µVision.
Apparatus:
1. Desktop PC.
2. Keil µVision software.
3. 8051 target board.
4. Protous software
Procedure:
1. Open the keil µVision software in PC.
2. Create a new project and select the target board from the list.
3. Write the c code in the test file, compile and build it.
4. Open the Serial Window UART #1 to display the application’s output.
5. Click the Stop button to halt the program. Alternatively, press the Esc key while in the
Command Line of the Command Window.
6. Use the Insert/Remove Breakpoint command to set or clear a breakpoint.
7. Test the Reset command to reset the simulated microcontroller. If the program is still running,
it halts at the first breakpoint.
8. Single-step through the program using the Step buttons. The current instruction, which will
execute next, is marked with a yellow arrow. The yellow arrow moves each time you step.
Program:
#include<reg51.h>
#include<string.h>
#define lcd_data P1
sbit lcd_rs = P1^2;
sbit lcd_en = P1^3;

void delay(unsigned int v)


{
unsigned int i,j;
for(i=0;i<=v;i++)
for(j=0;j<=1275;j++);
}

void lcdcmd(unsigned char value) // LCD COMMAND


{
lcd_data=value&(0xf0); //send msb 4 bits
lcd_rs=0; //select command register
lcd_en=1; //enable the lcd to execute command
delay(3);
lcd_en=0;
lcd_data=((value<<4)&(0xf0)); //send lsb 4 bits
lcd_rs=0; //select command register
lcd_en=1; //enable the lcd to execute command
delay(3);
lcd_en=0;

Dept of ECE, Prasiddha College of Engineering and Technology


25
void lcd_init(void)
{
lcdcmd(0x02);
lcdcmd(0x02);
lcdcmd(0x28); //intialise the lcd in 4 bit mode*/
lcdcmd(0x28); //intialise the lcd in 4 bit mode*/

lcdcmd(0x0e); //cursor blinking


lcdcmd(0x06); //move the cursor to right side
lcdcmd(0x01); //clear the lcd

void lcddata(unsigned char value)

lcd_data=value&(0xf0); //send msb 4 bits


lcd_rs=1; //select data register
lcd_en=1; //enable the lcd to execute data
delay(3);
lcd_en=0;
lcd_data=((value<<4)&(0xf0)); //send lsb 4 bits
lcd_rs=1; //select data register
lcd_en=1; //enable the lcd to execute data
delay(3);
lcd_en=0;

delay(3);
}

void msgdisplay(unsigned char b[]) // send string to lcd


{
unsigned char s,count=0;
for(s=0;b[s]!='\0';s++)
{
count++;
if(s==16)
lcdcmd(0xc0);
if(s==32)
{
lcdcmd(1);
count=0;
}
lcddata(b[s]);
}
}
void main(void)
{

lcd_init();

Dept of ECE, Prasiddha College of Engineering and Technology


26
lcdcmd(0x01);
lcdcmd(0x80);
msgdisplay("welcome to ");
lcdcmd(0xc0);
msgdisplay("prasidda clg ");
delay(100);delay(100);
lcdcmd(0x01);

while(1)
{

lcdcmd(0x80);
msgdisplay("ECE Dept. ");
lcdcmd(0xc0);
msgdisplay("welcomes u ");

}}
RESULT: Thus to create a portable hardware delay program has been compiled, debugged and executed
successfully.

Dept of ECE, Prasiddha College of Engineering and Technology


27
TEST LOOP TIME OUTS

Aim: To write an embedded program to write a C program to test loop time outs using keil µVision.
Apparatus:
1. Desktop PC.
2. Keil µVision software.
3. 8051 target board.
4. Protous software
Procedure:
1. Open the keil µVision software in PC.
2. Create a new project and select the target board from the list.
3. Write the c code in the test file, compile and build it.
4. Open the Serial Window UART #1 to display the application’s output.
5. Click the Stop button to halt the program. Alternatively, press the Esc key while in the
Command Line of the Command Window.
6. Use the Insert/Remove Breakpoint command to set or clear a breakpoint.
7. Test the Reset command to reset the simulated microcontroller. If the program is still running,
it halts at the first breakpoint.
8. Single-step through the program using the Step buttons. The current instruction, which will
execute next, is marked with a yellow arrow. The yellow arrow moves each time you step.
Program:
#include<reg51.h>
timer0_init()
{
TMOD = 0x01;
TH1 = 0x0;
TL1 = 0x0;
}
void delay(unsigned char i)
{
unsigned int j;
for(i;i>0;i--){
for(j=0;j<123;j++)
{
//do nothing as this function is to produce simple delay
}
}
}

main()
{
int timer_value = 0;
timer0_init();
TR1 = 1; //start the timer before testing a loop
delay(5); //here we assumed delay() function as loop to be tested. What ever the loop to be tested
shuld be placed instead of delay
TR1 = 0; //stop the timer
timer_value = (TH0<<8) | TLO;
/*********************************
Note: Now we have total cycles consumed by the testing_loopi.e.,Delay() function in this
program.
Dept of ECE, Prasiddha College of Engineering and Technology
28
Calculation follows:
Assume crystal frequency = 12MHZ
Timer works 1/12 of clock frequency so 12MHz/12 is 1MHZ. As a result, each clock has a period
of T=1/f i.e., T = 1/1MHz = 1us.
Timer 0 counts up each 1us resulting in delay = timer_value x 1us.
***********************************/
}

RESULT: Thus the test loop time outs program has been compiled, debugged and executed
successfully.

Dept of ECE, Prasiddha College of Engineering and Technology


29
TRAFFIC LIGHT

Aim: To write an embedded program to write a C program to test TRAFFIC LIGHT using keil µVision.
Apparatus:
1. . Desktop PC.
2. Keil µVision software.
3. 8051 target board
4. Protous software
Procedure:
1. Open the keil µVision software in PC.
2. Create a new project and select the target board from the list.
3. Write the c code in the test file, compile and build it.
4. Open the Serial Window UART #1 to display the application’s output.
5. Click the Stop button to halt the program. Alternatively, press the Esc key while in the
Command Line of the Command Window.
6. Use the Insert/Remove Breakpoint command to set or clear a breakpoint.
7. Test the Reset command to reset the simulated microcontroller. If the program is still
running, it halts at the first breakpoint.
8. Single-step through the program using the Step buttons. The current instruction, which
will execute next, is marked with a yellow arrow. The yellow arrow moves each time
you step.
Program:
#include <reg52.h>
void msdelay(unsigned int t)
{
unsigned int i,j;
for(i=0;i<t;i++)
for(j=0;j<1275;j++);
}
void main()
{
while(1)
{
P1=0X0F;
P2=0X00;
msdelay(250);
P1=0X00;
P2=0X0F;
msdelay(250);
}
}

RESULT: Thus the TRAFFIC LIGHT outs program has been compiled, debugged and executed
successfully.

Dept of ECE, Prasiddha College of Engineering and Technology


30
LCD

Aim: To write an embedded program to write a C program to test LCD using keil µVision.
Apparatus:
1. Desktop PC.
2. Keil µVision software.
3. 8051 target board
4. Protous software
Procedure:
1. Open the keil µVision software in PC.
2. Create a new project and select the target board from the list.
3. Write the c code in the test file, compile and build it.
4. Open the Serial Window UART #1 to display the application’s output.
5. Click the Stop button to halt the program. Alternatively, press the Esc key while in the
Command Line of the Command Window.
6. Use the Insert/Remove Breakpoint command to set or clear a breakpoint.
7. Test the Reset command to reset the simulated microcontroller. If the program is still
running, it halts at the first breakpoint.
8. Single-step through the program using the Step buttons. The current instruction, which
will execute next, is marked with a yellow arrow. The yellow arrow moves each time
you step.
Program:
#include<reg51.h>
#define display_port P2 //Data pins connected to port 2 on microcontroller
sbit rs = P3^2; //RS pin connected to pin 2 of port 3
sbit rw = P3^3; // RW pin connected to pin 3 of port 3
sbit e = P3^4; //E pin connected to pin 4 of port 3
sbit c3 = P1^6;
sbit c2 = P1^5;
sbit c1 = P1^4;
sbit r4 = P1^3;
sbit r3 = P1^2;
sbit r2 = P1^1;
sbit r1 = P1^0;

void msdelay(unsigned int time) // Function for creating delay in milliseconds.


{
unsigned i,j ;
for(i=0;i<time;i++)
for(j=0;j<1275;j++);
}
void lcd_cmd(unsigned char command) //Function to send command instruction to LCD
{
display_port = command;
rs= 0;
rw=0;
e=1;
msdelay(1);
e=0;
}

Dept of ECE, Prasiddha College of Engineering and Technology


31
void lcd_data(unsigned char disp_data) //Function to send display data to LCD
{
display_port = disp_data;
rs= 1;
rw=0;
e=1;
msdelay(1);
e=0;
}

void main()
{
lcd_init();
while(1)
{
r1=0;
if(c1==0)
{
lcd_cmd(0X80);
lcd_data('1');
msdelay(1000);
}
if(c1==0)
{
lcd_cmd(0X80);
lcd_data('2'); msdelay(1000);
}
if(c3==0)
{
lcd_cmd(0X80);
lcd_data('3'); msdelay(1000);
}
}

RESULT: Thus the PORT to interfacing consisting of LED outs program has been compiled, debugged
and executed successfully.

Dept of ECE, Prasiddha College of Engineering and Technology


32
TEST HARDWARE BASED TIME OUT LOOPS

Aim: To write an embedded program to write a C program to test hard ware loop time outs using keil
µVision.
Apparatus:
1. Desktop PC.
2. Keil µVision software.
3. 8051 target board.
4. Protous software

Procedure:
1. Open the keil µVision software in PC.
2. Create a new project and select the target board from the list.
3. Write the c code in the test file, compile and build it.
4. Open the Serial Window UART #1 to display the application’s output.
5. Click the Stop button to halt the program. Alternatively, press the Esc key while in the
Command Line of the Command Window.
6. Use the Insert/Remove Breakpoint command to set or clear a breakpoint.
7. Test the Reset command to reset the simulated microcontroller. If the program is still
running, it halts at the first breakpoint.
8. Single-step through the program using the Step buttons. The current instruction, which will
execute next, is marked with a yellow arrow. The yellow arrow moves each time you step.
Program:
#include<reg51.h>
#include<string.h>

#define lcd_data P1

sbit lcd_rs = P1^2;


sbit lcd_en = P1^3;
sbit sw=P2^0;
unsigned char count=0;

void Lcd4_Decimal3(unsigned char);

void delay(unsigned int v)


{
unsigned int i,j;
for(i=0;i<=v;i++)
for(j=0;j<=1275;j++);
}

void lcdcmd(unsigned char value) // LCD COMMAND


{

lcd_data=value&(0xf0); //send msb 4 bits


Dept of ECE, Prasiddha College of Engineering and Technology
33
lcd_rs=0; //select command register
lcd_en=1; //enable the lcd to execute command
delay(3);
lcd_en=0;
lcd_data=((value<<4)&(0xf0)); //send lsb 4 bits
lcd_rs=0; //select command register
lcd_en=1; //enable the lcd to execute command
delay(3);
lcd_en=0;

void lcd_init(void)
{
lcdcmd(0x02);
lcdcmd(0x02);
lcdcmd(0x28); //intialise the lcd in 4 bit mode*/
lcdcmd(0x28); //intialise the lcd in 4 bit mode*/

lcdcmd(0x0e); //cursor blinking


lcdcmd(0x06); //move the cursor to right side
lcdcmd(0x01); //clear the lcd

void lcddata(unsigned char value)

lcd_data=value&(0xf0); //send msb 4 bits


lcd_rs=1; //select data register
lcd_en=1; //enable the lcd to execute data
delay(3);
lcd_en=0;
lcd_data=((value<<4)&(0xf0)); //send lsb 4 bits
lcd_rs=1; //select data register
lcd_en=1; //enable the lcd to execute data
delay(3);
lcd_en=0;

delay(3);
}

void msgdisplay(unsigned char b[]) // send string to lcd


{
unsigned char s,count=0;
for(s=0;b[s]!='\0';s++)
{
count++;
if(s==16)

Dept of ECE, Prasiddha College of Engineering and Technology


34
lcdcmd(0xc0);
if(s==32)
{
lcdcmd(1);
count=0;
}
lcddata(b[s]);
}
}

void main(void)
{

lcd_init();

lcdcmd(0x01);
lcdcmd(0x80);
msgdisplay("welcome to ");
lcdcmd(0xc0);
msgdisplay("prasidda clg ");
delay(100);delay(100);
lcdcmd(0x01);

while(1)
{
if(sw==1)
{
count=0;
}

if(sw==0)
{
count++;
}
lcdcmd(0x80);
Lcd4_Decimal3(count);

}}

void Lcd4_Decimal3(unsigned char val)


{
unsigned int Lcd_h,Lcd_hr,Lcd_t,Lcd_o;

Lcd_h=val/100;
Lcd_hr=val%100;

Dept of ECE, Prasiddha College of Engineering and Technology


35
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;

lcddata(Lcd_h+0x30);
lcddata(Lcd_t+0x30);
lcddata(Lcd_o+0x30);
}

RESULT: Thus the test hardware loop time outs program has been compiled, debugged and executed
successfully.

Dept of ECE, Prasiddha College of Engineering and Technology


36
INTRUDER ALARM SYSTEM

Aim: To write an embedded program to write a C program to test LCD using keil µVision.
Apparatus:
1. Desktop PC.
2. Keil µVision software.
3. 8051 target board
4. Protous software
Procedure:
1. Open the keil µVision software in PC.
2. Create a new project and select the target board from the list.
3. Write the c code in the test file, compile and build it.
4. Open the Serial Window UART #1 to display the application’s output.
5. Click the Stop button to halt the program. Alternatively, press the Esc key while in the
Command Line of the Command Window.
6. Use the Insert/Remove Breakpoint command to set or clear a breakpoint.
7. Test the Reset command to reset the simulated microcontroller. If the program is still
running, it halts at the first breakpoint.
8. Single-step through the program using the Step buttons. The current instruction, which
will execute next, is marked with a yellow arrow. The yellow arrow moves each time
you step.
Program:
#include<reg51.h>
#include<string.h>

#define lcd_data P1

sbit lcd_rs = P1^2;


sbit lcd_en = P1^3;
sbit ir=P2^0;
sbit buzzer=P2^7;
unsigned char count=0;

void Lcd4_Decimal3(unsigned char);

void delay(unsigned int v)


{
unsigned int i,j;
for(i=0;i<=v;i++)
for(j=0;j<=1275;j++);
}

void lcdcmd(unsigned char value) // LCD COMMAND


{

lcd_data=value&(0xf0); //send msb 4 bits


lcd_rs=0; //select command register
lcd_en=1; //enable the lcd to execute command
Dept of ECE, Prasiddha College of Engineering and Technology
37
delay(3);
lcd_en=0;
lcd_data=((value<<4)&(0xf0)); //send lsb 4 bits
lcd_rs=0; //select command register
lcd_en=1; //enable the lcd to execute command
delay(3);
lcd_en=0;

void lcd_init(void)
{
lcdcmd(0x02);
lcdcmd(0x02);
lcdcmd(0x28); //intialise the lcd in 4 bit mode*/
lcdcmd(0x28); //intialise the lcd in 4 bit mode*/

lcdcmd(0x0e); //cursor blinking


lcdcmd(0x06); //move the cursor to right side
lcdcmd(0x01); //clear the lcd

void lcddata(unsigned char value)

lcd_data=value&(0xf0); //send msb 4 bits


lcd_rs=1; //select data register
lcd_en=1; //enable the lcd to execute data
delay(3);
lcd_en=0;
lcd_data=((value<<4)&(0xf0)); //send lsb 4 bits
lcd_rs=1; //select data register
lcd_en=1; //enable the lcd to execute data
delay(3);
lcd_en=0;

delay(3);
}

void msgdisplay(unsigned char b[]) // send string to lcd


{
unsigned char s,count=0;
for(s=0;b[s]!='\0';s++)
{
count++;
if(s==16)
lcdcmd(0xc0);
if(s==32)

Dept of ECE, Prasiddha College of Engineering and Technology


38
{
lcdcmd(1);
count=0;
}
lcddata(b[s]);
}
}

void main(void)
{

lcd_init();

lcdcmd(0x01);
lcdcmd(0x80);
msgdisplay("welcome to ");
lcdcmd(0xc0);
msgdisplay("prasidda clg ");
delay(100);delay(100);
lcdcmd(0x01);

while(1)
{
if(ir==0)
{
lcdcmd(0x80);
msgdisplay("Intruder came ");
buzzer=1;
}

if(ir==1)
{
lcdcmd(0x80);
msgdisplay("no Intruder "); buzzer=0;
}

}}

void Lcd4_Decimal3(unsigned char val)


{
unsigned int Lcd_h,Lcd_hr,Lcd_t,Lcd_o;

Lcd_h=val/100;
Lcd_hr=val%100;
Lcd_t=Lcd_hr/10;
Lcd_o=Lcd_hr%10;

lcddata(Lcd_h+0x30);
lcddata(Lcd_t+0x30);
lcddata(Lcd_o+0x30);

Dept of ECE, Prasiddha College of Engineering and Technology


39
}

RESULT: Thus the Intruder Alarm System outs program has been compiled, debugged and executed successfully.

Dept of ECE, Prasiddha College of Engineering and Technology


40

You might also like