Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
157 views

8051 Programming Using Embedded C

This document provides an introduction to programming 8051 microcontrollers using Embedded C. It discusses why Embedded C is commonly used, describes the basic data types used in 8051 programming, and gives a brief overview of the features and pin configuration of the 8051 microcontroller. It also discusses how to create time delays in 8051 programs using timers or for loops.

Uploaded by

renukaprasadj6
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
157 views

8051 Programming Using Embedded C

This document provides an introduction to programming 8051 microcontrollers using Embedded C. It discusses why Embedded C is commonly used, describes the basic data types used in 8051 programming, and gives a brief overview of the features and pin configuration of the 8051 microcontroller. It also discusses how to create time delays in 8051 programs using timers or for loops.

Uploaded by

renukaprasadj6
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

8051

PROGRAMMING
USING
EMBEDDED C
Training Material
1

INDEX

S.No. Topics Page No.

1 Introduction 1

2 8051 Programming using C 4

3 Datatypes for 8051 4

4 Brief Description about 8051 6

5 Keyboard Interfacing 15

6 16*2 LCD Panel details 17

7 Seven Segment LED Display 20

8 Basic Programs 23

9 LED Toggling 27

10 Display name on 16*2 LCD Panel 28

11 Keyboard Interfacing 31

12 7 Segment LED Display Interfacing 36

13 Interrupt programs 38

14 Required Software and Procedures 42

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


2

Introduction
What is an Embedded System?
An Embedded System can be best described as a system which has both the
hardware and software and is designed to do a specific task. A good example for
an Embedded System, which many households have, is a Washing Machine.

Programming Embedded Systems


As mentioned earlier, Embedded Systems consists of both Hardware and
Software. If we consider a simple Embedded System, the main Hardware Module
is the Processor. The Processor is the heart of the Embedded System and it can
be anything like a Microprocessor, Microcontroller, DSP, CPLD (Complex
Programmable Logic Device) or an FPGA (Field Programmable Gated Array).

All these devices have one thing in common: they are programmable i.e., we can
write a program (which is the software part of the Embedded System) to define
how the device actually works.

Embedded Software or Program allow Hardware to monitor external events


(Inputs / Sensors) and control external devices (Outputs) accordingly. During this
process, the program for an Embedded System may have to directly manipulate
the internal architecture of the Embedded Hardware (usually the processor) such
as Timers, Serial Communications Interface, Interrupt Handling, and I/O Ports etc.

Embedded C is perhaps the most popular languages among Embedded Program-


mers for programming Embedded Systems. There are many popular program-
ming languages like Assembly, BASIC, C++, Python etc. that are often used for de-
veloping Embedded Systems but Embedded C remains popular due to its effi-
ciency, less development time and portability.

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


3

Factors for Selecting the Programming Language


The following are few factors that are to be considered while selecting the
Programming Language for the development of Embedded Systems.

 Size: The memory that the program occupies is very important as Embed-
ded Processors like Microcontrollers have a very limited amount of ROM
(Program Memory).
 Speed: The programs must be very fast i.e., they must run as fast as possi-
ble. The hardware should not be slowed down due to a slow running soft-
ware.
 Portability: The same program can be compiled for different processors.
 Ease of Implementation
 Ease of Maintenance
 Readability

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


4

Why Programming 8051 in C?


Compilers produce hex file that is downloaded to ROM of microcontroller. The size of
hex file generated by the compiler is the main concern, for two reasons:
 Microcontrollers have limited on-chip ROM
 Code space for 8051 is limited to 64K bytes .
The Assembly language produces a hex file that is much smaller than C. But when both
are compared, programming in assembly is tedious and time consuming while C
programming is less time consuming, but has larger hex file size.
The reasons for writing programs in C instead of assembly are:
 It is easier and less time consuming to write in C than Assembly
 C is easier to modify and update
 You can use code available in function libraries
 C code is portable to other microcontroller with little or no modification
C Data types for 8051:
A good understanding of C data types for 8051 can help programmers to create smaller
hex files. The different data types are:
 unsigned char
 signed char
 unsigned int
 signed int
 sbit (single bit)
 bit and sfr
unsigned char:
As 8051 is an 8-bit microcontroller, the character data type is the most natural choice for
many applications. The unsigned char is an 8-bit data type takes a value in the range of 0
– 255 (00H – FFH) and the most widely used data type for 8051. The unsigned char data
type can be used in situations such as setting a counter value and ASCII characters,
instead of signed char. It is important to specify the keyword unsigned infront of the char
else compiler will use the signed char as the default. As 8051 has a limited number of

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


5

registers and data RAM locations, using the int in place of char can lead to a larger size
hex file. This is not a significant issue in high level language.
unsigned int: The unsigned int is a 16-bit data type that takes a value in the range of 0 to
65535 (0000 – FFFFH). This is used to define 16-bit variables such as memory addresses,
set counter values of more than 256. Since registers and memory accesses are in 8-bit
chunks, the misuse of int variables will result in a larger hex file. However, for 8051
programming, do not use unsigned int in place where unsigned char will do the job. Also
in situations where there is no need for signed data we should use unsigned int instead of
signed int. Remember, C compiler uses signed int as default if unsigned keyword is not
used.
signed int: Signed int is a 16-bit data type that uses the MSB D15 to represent – or +value.
We have 15 bits for the magnitude of the number from –32768 to +32767.
sbit (Single bit): The 8 bit keyword is a widely used 8051 C data types which is used to
access single-bit addressable register. It allows access to the single bits of the SFR
registers. As we know and have studied, some of the SFRs are bit addressable. Among
the SFRs that are widely used are also bit addressable ports P0-P3.
bit and sfr: The bit data type allows access to single bits of bit-addressable memory
spaces 20 – 2FH. To access the byte-size SFR registers, we use the sfr data type. Table 1
summaries the different data types used for 8051 programming in C

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


6

Brief Description About 8051

Features of 8051 Microcontroller:


An 8051 microcontroller comes bundled with the following features −
4KB bytes on-chip program memory (ROM)
128 bytes on-chip data memory (RAM)
Four register banks
128 user defined software flags
8-bit bidirectional data bus
16-bit unidirectional address bus
32 general purpose registers each of 8-bit
16 bit Timers (usually 2, but may have more or less)
Three internal and two external Interrupts
Four 8-bit ports,(short model have two 8-bit ports)
16-bit program counter and data pointer
8051 may also have a number of special features such as UARTs, ADC, Op-
amp, etc.

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


7

PIN CONFIGURATION OF 8051

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


8

Sample connection Using Proteus Software

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


9

Time Delays
There are two ways to create a time delay in 8051 C:
1. Using the 8051 timer
2. Using a simple for loop
In either case, the delays can be observed either on the oscilloscope or using a simulator.
for loop is carried out. In creating a time delay using for loop three factors need to be
considered that can affect the accuracy of the delay.
1. Number of machine cycles and number of clocks period per machine cycle. 2. Crystal
frequency connected between XTAL1 and XTAL
2. Duration of the clock period for the machine cycle is the function of crystal frequency.
3. Compiler choice. The third factor that affects the accuracy of the time delay is the
compiler used to compile the C program. In assembly language programming, we can
know and control the delay generated, as, the number of instructions and the cycles per
instruction is known to us. In case of C program, the C compiler will convert the C
statements and functions to assembly language instructions. Hence, different compilers
produce different code.
Delay using for loop:
void MSDelay(unsigned int dtime)
{
unsigned int i, j;
for (i = 0; i < dtime; i++)
for (j = 0; j< 1275; j++);
}
Programming 8051 Timers:
The 8051 has two timers/counters; they can be used either as Timers to generate a time
delay or as event counters to count events happening outside the microcontroller.
Basic Timers of 8051: Both Timer 0 and Timer 1 are 16 bits wide. Since 8051 has an 8-
bit architecture, each 16-bits timer is accessed as two separate registers of low byte and
high byte. The low byte register is called TL0/TL1 and the high byte register is called
TH0/TH1.

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


10

Accessing timer registers in C :


In 8051 C we can access the timer registers TH, TL and TMOD directly using the reg51.h
header file. (The general purpose registers such as R0-R7, A and B are not directly
accessible by the C compiler, while the SFRs and RAM space 80-FFH is directly
accessible using 8051 C statements.) For example:
TL1=0xFE;
TH1=0xA5;

TMOD (timer mode) Register:


Both timers 0 and 1 use the same register, called TMOD (timer mode), to set the various
timer operation modes.
TMOD is an 8-bit register. The lower 4 bits are for Timer 0 and the upper 4 bits are for
Timer 1.
In each case, the lower 2 bits are used to set the timer mode the upper 2 bits to specify
the operation. The TMOD register is as shown in figure 2 below:

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


11

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


12

Types of Interrupts in 8051 Microcontroller


The 8051 microcontroller can recognize five different events that cause the
main program to interrupt from the normal execution. These five sources of
interrupts in 8051 are:

1.Timer 0 overflow interrupt- TF0

2.Timer 1 overflow interrupt- TF1

3.External hardware interrupt- INT0

4.External hardware interrupt- INT1erial communication interrupt- RI/TI


Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)
13

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


14

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


15

Keyboard Interfacing with 8051


The key board here we are interfacing is a matrix keyboard. This key board is
designed with a particular rows and columns. These rows and columns are
connected to the microcontroller through its ports of the micro controller
8051. Normally the port pin is high but when a switch is pressed the controller pin gets
a Low signal and we come to know that a switch has been pressed. One end of switch is
connected to the port pin whereas the other end is connected to the Ground.
In case of matrix Keypad both the ends of switches are connected to the port Pin. Over
here we have considered a 4x3 matrix keypad i.e. four rows and three columns. So in all
twelve switches have been interfaced using just seven lines.
The adjoining figure shows the diagram of a matrix keypad and how it is interfaced with
the controller. As you can see no pin is connected to ground, over here the controller pin
itself provides the ground.
We pull one of the Column Pins low & check the row pins if any of the Pin is low then
we come to know which switch is pressed.
Suppose we make column 1 pin low and while checking the rows we get Row 3 is low
then we come to know switch 7 has been pressed.

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


16

ALGORITHM
1. Start.
2. Make All Pins High.
3. Make Column 1 pin low.
4. Check if Row 1 is low, if yes then Switch 1 has been pressed.
5. Check if Row 2 is low, if yes then Switch 4 has been pressed.
6. Check if row 3 is low if yes then Switch 7 has been pressed.
7. Check if row 4 is low if yes then Switch 10 has been pressed.
8. Make Column 1 Pin high & Column 2 Pin Low.
9. Check if Row 1 is low, if yes then Switch 2 has been pressed.
10. Check if Row 2 is low, if yes then Switch 5 has been pressed.
11. Check if row 3 is low if yes then Switch 8 has been pressed.
12. Check if row 4 is low if yes then Switch 11 has been pressed.
13. Make Column 2 Pin high & Column 3 Pin Low.
14. Check if Row 1 is low, if yes then Switch 3 has been pressed.
15. Check if Row 2 is low, if yes then Switch 6 has been pressed.
16. Check if row 3 is low if yes then Switch 9 has been pressed.
17. Check if row 4 is low if yes then Switch 12 has been pressed.
18. Make column 3 pin high.
19. Stop.
Please make sure at any time only one column Pin should remain Low

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


17

16X2 LCD Panel:

An electronic device that is used to display data and the message is known as
LCD 16×2. As the name suggests, it includes 16 Columns & 2 Rows so it can
display 32 characters (16×2=32) in total & every character will be made with 5×8
(40) Pixel Dots. So the total pixels within this LCD can be calculated as 32 x 40
otherwise 1280 pixels.

Pin
Function Name
No.

1 Ground (0V) Ground

2 Supply voltage; 5V (4.7V – 5.3V) Vcc

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


18

Contrast adjustment; . Rotate the potentiometer knob


3 Vo / VEE
forward and backwards to adjust the LCD contrast.

RS
4 Selects command register when low, and data register when high (Register
Select )

Read/writ
5 Low to write to the register; High to read from the register
e

Sends data to data pins when a high to low pulse is given; Extra
voltage push is required to execute the instruction and EN(enable)
6 signal is used for this purpose. Usually, we set en=0, when we want Enable
to execute the instruction we make it high en=1 for some
milliseconds. After this we again make it ground that is, en=0.

7 DB0

8 DB1

9 DB2

10 DB3
8-bit data pins
11 DB4

12 DB5

13 DB6

14 DB7

15 LED Backlight VCC (5V) Led+

16 LED Backlight Ground (0V) Led-

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


19

Important command codes for LCD

Sr. Hex
Command to LCD instruction Register
No. Code

1 01 Clear display screen

2 02 Return home

3 04 Decrement cursor (shift cursor to left)

4 06 Increment cursor (shift cursor to right)

5 05 Shift display right

6 07 Shift display left

7 08 Display off, cursor off

8 0A Display off, cursor on

9 0C Display on, cursor off

10 0E Display on, cursor blinking

11 0F Display on, cursor blinking

12 10 Shift cursor position to left

13 14 Shift cursor position to right

14 18 Shift the entire display to the left

15 1C Shift the entire display to the right

16 80 Force cursor to beginning ( 1st line)

17 C0 Force cursor to beginning ( 2nd line)

18 38 2 lines and 5×7 matrix

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


20

Command Register: The command register stores the command


instructions given to the LCD. A command is an instruction given to LCD to
do a predefined task.

Data Register: The data register stores the data to be displayed on the LCD.
The data is the ASCII value of the character to be displayed on the LCD.
When we send data to LCD it goes to the data register and is processed
there.

Seven Segment Led Displays:


Seven segment displays are the output display device that provides a way to
display information in the form of images or text or decimal numbers which is an
alternative to the more complex dot matrix displays. It is widely used in digital
clocks, basic calculators, electronic meters, and other electronic devices that
display numerical information. It consists of seven segments of light-emitting
diodes (LEDs) which are assembled like numerical 8.
Seven segment displays are of two types, common cathode and common
anode. In common cathode type , the cathode of all LEDs are tied together to a
single terminal which is usually labeled as ‘com‘ and the anode of all LEDs are left
alone as individual pins labeled as a, b, c, d, e, f, g & h (or dot) . In common anode
type, the anode of all LEDs are tied together as a single terminal and cathodes are
left alone as individual pins.

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


21

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


22

PROGRAMMS

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


23

1)Write an 8051 C program to send values 00H -0AAH to port P1 Program:

#include <reg51.h> // contains all the port registers and internal


// RAM of 8051declared
void main (void)
{
unsigned char z; // z is allotted a space of 1 byte
for (z=0; z<=170;z++)
P1=z; // values 00H-AAH sent to port 1
}
Using delay function:
#include <reg51.h>
void delay();
void main ()
{
unsigned char z; // z is allotted a space of 1 byte
while(1){
for(z=0;z<170;z++){
P1=z;
delay();
}
}
}

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


24

void delay()
{
unsigned int i,j;
for(i=0;i<500;i++)
for(j=0;j<5000;j++);
}
2) Write an 8051 C program to send hex values for ASCII characters of 0, 1, 2, 3,
4, 5, A, B, C, D to port P1.
#include <reg51.h>
void main(void)
{
unsigned char mynum[]="012345ABCD";
unsigned char z;
for (z=0;z<=10;z++)
P1=mynum[z];
}
3)Write an 8051 C program to send values of –4 to +4 to port P1
#include <reg51.h>
void MSDelay(unsigned int);
void main(void)
{
char mynum[]={+1,-1,+2,-2,+3,-3,+4,-4};
unsigned char z;
for (z=0;z<8;z++)
{
P1=mynum[z];
MSDelay(2500);

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


25

}
}
void MSDelay(unsigned int itime)
{
unsigned int i, j;
for (i = 0; i < itime; i++)
for (j = 0; j< 1275; j++);
}
Note: The negative values will be displayed in the 2’s complement for as 1, FFH, 2,
FEH, 3, FDH, 4, FCH.
4)Write an 8051 C program to get a byte of data form P1, wait ½ second (i.e., 500
ms) and then send it to P2.
#include <reg51.h>
void MSDelay(unsigned int);
void main( )
{
unsigned char mybyte;
P1= 0xFF; //make P1 input port
while (1)
{
mybyte = P1; //get a byte from P1
MSDelay(500); //wait for ½ second.
P2 = mybyte; //send it to P2
}
}
void MSDelay(unsigned int itime)
{

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


26

unsigned int i, j;
for (i = 0; i < itime; i++)
for (j = 0; j< 1275; j++);
}
5)Write an 8051 C program to toggle bit 0 of the port P1 (P1.0) 50,000 times
#include <reg51.h>
void MSDelay(unsigned int);
sbit MYBIT=P1^0;
void main(void) {
unsigned int z;
for (z=0;z<50000;z++) {
MYBIT=0;
MSDelay();
MYBIT=1;
MSDelay();
}
}
void MSDelay(unsigned int itime)
{
unsigned int i, j;
for (i = 0; i < itime; i++)
for (j = 0; j< 1275; j++);
}

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


27

6)Write an 8051 C program to toggle LEDs connected at P3 ports continuously


with a 250 ms.

#include <reg51.h>
void MSDelay(unsigned int); //delay routine definition
void main( )
{
while (1) //repeat forever
{
P3= 0xFF;
MSDelay(250);
P3= 0x00;
MSDelay(250);
}
}
// delay routine implimentation
void MSDelay(unsigned int itime)
{
unsigned int i, j;
for (i = 0; i < itime; i++)
for (j = 0; j< 1275; j++);
}

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


28

7)Write a c program to display your name on a 16*2 LCD panel


#include <reg51.h>
void delay (char x);
void lcd_command (char c);
void lcd_data (char d);
int main(void)
{
lcd_command(0X38);
lcd_command(0X80);

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


29

lcd_command(0X0C);
lcd_command(0X06);
lcd_command(0X01);
delay(5);

lcd_data('R');
lcd_data('A');
lcd_data('J');
lcd_data('E');
lcd_data('E');
while(1){}
}
void delay (char x)
{
int i,j;
for(j=0;j<x;j++)
for(i=0;i<3000;i++);
}
void lcd_command(char c)
{
P3=c;
P2&=~(1<<6);
P2|=(1<<7);
delay(2);
P2&=~(1<<7);
}
void lcd_data(char d)
Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)
30

{
P3=d;
P2|=(1<<6);
P2|=(1<<7);
delay(2);
P2&=~(1<<7);
}

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


31

8)Write an embedded c program to identify key pressed and display it on LCD


panel.

#include <reg51.h>
void delay (char x);
void lcd_command (char c);
void lcd_data (char d);
int main(void)
{
char i;
lcd_command(0X38);
lcd_command(0X80);
lcd_command(0X0C);
lcd_command(0X06);
lcd_command(0X01);
delay(2);
while(1)
{
P1=0x7f;
i=P1 & 0X0F;
switch(i)
{
case 0x0E:
lcd_data('7');
break;

case 0x0D:

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


32

lcd_data('4');
break;

case 0x0B:
lcd_data('1');
break;

case 0x07:
lcd_data('N');
break;
}

P1=0XBF;
i=P1 & 0X0F;
switch(i)
{
case 0x0E:
lcd_data('8');
break;

case 0x0D:
lcd_data('5');
break;

case 0x0B:
lcd_data('2');
break;
Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)
33

case 0x07:
lcd_data('0');
break;
}

P1=0XDF;
i=P1& 0X0F;
switch(i)
{
case 0x0E:
lcd_data('9');
break;

case 0x0D:
lcd_data('6');
break;

case 0x0B:
lcd_data('3');
break;

case 0x07:
lcd_data('=');
break;
}

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


34

P1=0XEF;
i=P1& 0X0F;
switch(i)
{
case 0x0E:
lcd_data('/');
break;

case 0x0D:
lcd_data('*');
break;

case 0x0B:
lcd_data('-');
break;

case 0x07:
lcd_data('+');
break;
}
}
}

void delay (char x)


{
int i,j;
for(j=0;j<x;j++)
Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)
35

for(i=0;i<3000;i++);
}

void lcd_command(char c)
{
P3=c;
P2&=~(1<<6);
P2|=(1<<7);
delay(2);
P2&=~(1<<7);
}

void lcd_data(char d)
{
P3=d;
P2|=(1<<6);
P2|=(1<<7);
delay(14);
P2&=~(1<<7);
}

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


36

9)Write a program to display numbers from 0 to 9 on a 7 segment LED Display


#include<reg51.h>
void delay(int k) //delay function
{
int i,j;
for(i=0;i<k;i++)
for(j=0;j<1275;j++);
}
void main()
{
unsigned char i;
unsigned char arr[11]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,0x7f,0x67};
P2=0x00;

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


37

while(1)
{
for(i=0;i<10;i++)
{
P2=arr[i];
delay(100);
}

}
}

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


38

10)Write an embedded c program to blink an LED while receiving an interrupt.


#include<reg51.h>
sbit MYBIT=P1^1;
void my_Isr(void) interrupt 0
{
MYBIT =!MYBIT;
}

void main()
{
IE=0X81;
IT0=1;
MYBIT=0;
while(1)
{}
}

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


39

11)Write an 8051 C program to toggle all the bits of port P1 continuously with
some delay in between. Use Timer 0, 16-bit mode to generate the delay .
#include <reg51.h>
void T0Delay(void);
void main(void)
{
while (1) { //repeat forever
P1=0x55;
T0Delay(); //toggle all the bits of P1
P1=0xAA; // delay unknown
T0Delay();
Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)
40

}
}
void T0Delay()
{
TMOD=0x01; // timer 0 mode 1
TL0=0x00;
TH0=0x35; // load TH and TL
TR0=1; // turn on T0
while (TF0==0); // wait for TF0 to roll over
TR0=0; // turn off T0
TF0=0; // clear TF0
}
12)Write an 8051 C program to toggle all bits of P2 continuously every 500 ms. Use
Timer 1, mode 1 to create the delay.
Assume XTAL=11.0592 MHz=> T=1.085μs Count=500ms/1.085μs =460829 >65536 Let
us divide this delay as = 20x25ms, hence count for 25ms can be calculated. Count =
25ms/1.085μs = 23042 Initial count = 65536 – 23042= 42494 Count in hex = A5FEH

#include <reg51.h>
void T1M1Delay(void);
void main(void)
{
unsigned char x;
P2=0x55;
while (1)
{
P2=~P2;

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


41

for (x=0;x<20;x++)
T1M1Delay();
}}
void T1M1Delay(void)
{
TMOD=0x10;
TL1=0xFE;
TH1=0xA5;
TR1=1;
while (TF1==0);
TR1=0;
TF1=0;
}

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


42

Procedure of using Keil:


Step 1: Download the Keil Uvision IDE
Step 2: To initiate the programming you must create a project using the keil
Uvision IDE
Step 3: Selecting the type of device you are working with
Step 4: Adding C files to your project
Step 5: Coding in C
Step 6: Compiling and building the C project using Keil Uvision IDE
Step 7: Generating the hex file using Keil Uvision IDE
In order to generate the hex code, right click on the ‘Target 1’ folder and
select options for target ‘Target 1’. Select the Output tab in the target
‘Target 1’ dialog box. Make sure Create Hex File option is checked and the
HEX format should be HEX-80. Click OK. Again rebuild your project by
pressing F7. Your required hex file would have been generated with the
same as your project in the Objects folder.
Step 8: Burning the hex code into 8051 microcontroller.

Proteus Software
Steps:

1. Open Proteus ISIS Schematic Capture


2. Select the Component Mode from the left Toolbar
3. Click On P (Pick from Libraries)
4. Add all the required components
5. Place the components on the workspace
6. Wire up the circuit
7. Double click on Microcontroller and select corresponding hex file.
8. Click on Play Button on the bottom left to start simulation

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)


43

Nuvoton ISP-ICP Utility


The nuvoTon w78E052D controller comes with UART boot-loader
and can be directly programmed from the serial port. Use "Nuvoton
ISP-ICP Utility software" to program the target MCU.

Now open the Nuvoton ISP-ICP Utility software and follow the below
steps.

1. Select the ISP by COM port option for flashing the .hex file
through COM port.
2. Select the COM port from the drop down. Check device
manager for the com port number.
3. Choose the required controller. In this case, it is
W78E052D.
4. Browse and select the .hex file by clicking on Load File.
5. Finally click on Update chip to flash the .hex file.

----------------------------------------------------------------------------------------------------------

Indian Institute of Embedded Systems – IIES (Koramangala – Bangalore)

You might also like