0% found this document useful (0 votes)
20 views38 pages

CS3691 - ES & IOT Lab

Uploaded by

S.S.Rakesh Muthu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views38 pages

CS3691 - ES & IOT Lab

Uploaded by

S.S.Rakesh Muthu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

IT2304- EMBEDDED PROGRAMMING

LAB MANUAL

III- Semester
LIST OF EXERCISES

PART – I

ASSEMBLY LANGUAGE PROGRAM OF 8051

1.16 BIT ADDITION

2.16 BIT SUBTRACTION

3.8 BIT MULTIPLICATION

4.8 BIT DIVISION

5.DATA TRANSFER

6.BLOCK EXCHANGE

7.LARGEST/SMALLEST NUMBER

8.ASENDING/DESENDING ORDER

PART – II

EMBEDDED C LANGUAGE

1.ADDITION

2.SUBTRACTION

3.MULTIPLICATION

4.DIVISION
INTRODUCTION TO 8051 MICROCONTROLLER

8051 microcontroller is designed by Intel in 1981. It is an 8-bit microcontroller. It is built


with 40 pins DIP (dual inline package), 4kb of ROM storage and 128 bytes of RAM storage, 2
16-bit timers. It consists of are four parallel 8-bit ports, which are programmable as well as
addressable as per the requirement. An on-chip crystal oscillator is integrated in the
microcontroller having crystal frequency of 12 MHz.

The 8051 memory is organized in a Harvard Architecture. Both the code memory space
and data memory space begin at location 0x00 for internal or external memory which is different
from the Princeton Architecture where code and data share same memory space. The advantage
of the Harvard Architecture is not only doubling the memory capacity of the microcontroller
with same number of address lines but also increases the reliability of the microcontroller, since
there are no instructions to write to the code memory which is read only.

Features of 8051 Microcontroller


An 8051 microcontroller comes bundled with the following features.

 64K 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
Architecture Of 8051 Microcontroller

Figure 1.4 shows the architecture block diagram of 8051

8051 Memory Organization


8051 microcontroller has an internal program of 4K size and if needed an external
memory can be added (by interfacing) of size 60K maximum. So in total 64K size memory is
available for 8051 micro controller. By default, the External Access (EA) pin should be
connected Vcc so that instructions are fetched from internal memory initially. When the limit of
internal memory (4K) is crossed, control will automatically move to external memory to fetch
remaining instructions. If the programmer wants to fetch instruction from external memory only
(bypassing the internal memory), then he must connect External Access (EA) pin to ground
(GND).

Timers and Counters

Timer means which can give the delay of particular time between some events. For
example on or off the lights after every 2 sec. This delay can be provided through some assembly
program but in microcontroller two hardware pins are available for delay generation. These
hardware pins can be also used for counting some external events. How much times a number is
repeated in the given table is calculated by the counter.

In MC8051, two timer pins are available T0 and T1, by these timers we can give the
delay of particular time if we use these in timer mode. We can count external pulses at these
pins if we use these pins in counter mode. 16 bits timers are available. Means we can generate
delay between 0000H to FFFFH. Two special function registers are available. If we want to
load T0 with 16 bit data then we can load separate lower 8 bit in TL0 and higher 8 bit in TH0. In
the same way for T1. TMOD, TCON registers are used for controlling timer operation.

Serial Port
There are two pins available for serial communication TXD and RXD. Normally TXD is used
for transmitting serial data which is in SBUF register, RXD is used for receiving the serial data.
SCON register is used for controlling the operation.

Input Output Ports


There are four input output ports available P0, P1, P2, P3. Each port is 8 bit wide and has
special function register P0, P1, P2, P3 which are bit addressable means each bit can be set or
reset by the Bit instructions (SETB for high, CLR for low) independently. The data at any port
which is transmitting or receiving is in these registers. The port 0 can perform dual works. It is
also used as Lower order address bus (A0 to A7) multiplexed with 8 bit data bus P0.0 to P0.7 is
AD0 to AD7 respectively the address bus and data bus is demultiplex by the ALE signal and
latch which is further discussed in details. Port 2 can be used as I/O port as well as higher order
address bus A8 to A15. Port 3 also have dual functions it can be worked as I/O as well as each
pin of P3 has specific function. P3.0 – RXD – {Serial I / P for Asynchronous communication
Serial O / P for synchronous communication. P3.1 – TXD – Serial data transmit. P3.2 – INT0 –
External Interrupt 0. P3.3 – INT1 – External Interrupt 1. P3.4 – T0 – Clock input for counter 0.
P3.5 – T1 – Clock input for counter 1. P3.6 – WR – Signal for writing to external memory. P3.7
– RD – Signal for reading from external memory. When external memory is interfaced with 8051
then P0 and P2 can’t be worked as I/O port they works as address bus and data bus, otherwise
they can be accessed as I/O ports.
Oscillator
It is used for providing the clock to MC8051 which decides the speed or baud rate of MC.
We use crystal which frequency vary from 4MHz to 30 MHz, normally we use 11.0592 MHz
frequency.

Interrupts
Interrupts are defined as requests because they can be refused (masked) if they are not
used, that is when an interrupt is acknowledged. A special set of events or routines are followed
to handle the interrupts. These special routines are known as interrupt handler or interrupt service
routines (ISR). These are located at a special location in memory. • INT0 and INT1 are the pins
for external interrupts.

Pin Diagram Of 8051 microcontroller

Pin Description
The EA' (External Access) pin is used to control the internal or external memory access.
The signal 0 is for external memory access and signal 1 for internal memory access. The
PSEN' (Program Store Enable) is for reading external code memory when it is low (0) and EA
is also 0. The ALE (Address Latch Enable) activates the port 0 joined with port 2 to provide 16
bit external address bus to access the external memory. The ALE multiplexes the P0: 1 for
latching address on P0 as A0-A7 in the 16 bit address buss, 0 for latching P0 as data I/O. P0.x
is named ADx because P0 is multiplexed for Address bus and Data bus at different clock time.
WR' provides the signal to write external data memory RD' provides the signal to read external
data and code memory.
PORT P1 (Pins 1 to 8): The port P1 is a port dedicated for general I/O purpose. The other ports
P0, P2 and P3 have dual roles in addition to their basic I/O function.
PORT P0 (pins 32 to 39): When the external memory access is required then Port P0 is
multiplexed for address bus and data bus that can be used to access external memory in
conjunction with port P2. P0 acts as A0-A7 in address bus and D0-D7 for port data. It can be
used for general purpose I/O if no external memory presents.
PORT P2 (pins 21 to 28): Similar to P0, the port P2 can also play a role (A8-A15) in the
address bus in conjunction with PORT P0 to access external memory.
PORT P3 (Pins 10 to 17): In addition to acting as a normal I/O port,
 P3.0 can be used for serial receive input pin(RXD) • P3.1 can be used for serial
transmit output pin(TXD) in a serial port,
 P3.2 and P3.3 can be used as external interrupt pins(INT0’ and INT1’),
 P3.4 and P3.5 are used for external counter input pins(T0 and T1),
 P3.6 and P3.7 can be used as external data memory write and read control signal
pins(WR’ and RD’)read and write pins for memory access.

STEPS TO CREATE AND COMPILE Keil µVision-5 PROJECT:

1. Double Click on the  icon on the desktop.


2. Close any previous projects that were opened using – Project -> Close.
3. Start Project – New Project, and select the CPU from the device database
(Database-Atmel- AT89C51ED2 or AT89C51RD2 as per the board).On clicking
‘OK’, the following option is displayed. Choose ‘No’.
4. Create a source file (using File->New), type in the assembly or C program and
save this (filename.asm/filename.c) and add this source file to the project using
either one of the following two methods. (i) Project->Manage-
>Components,
Environment Books->addfiles-> browse to the required file -> OK
“OR” ii) right click on the Source Group in the Project Window and the Add
Files to Group option.

5. Set the Target options using -> Project – Options for Target opens the
µ Options for Target – Target configuration dialog. Set the Xtal
(Crystal frequency) frequency as 11.0592 MHz, and also the Options for
Target
– Debug – use either Simulator / Keil Monitor- 51 driver.

6. If Keil Monitor- 51 driver is used click on Settings -> COM Port settings select
the COM Port to which the board is connected and select the baud rate as
19200 or 9600 (recommended). Enable Serial Interrupt option if the user
application is not using on-chip UART, to stop program execution.
7. Build the project; using Project -> Build Project. 
application and links. Any errors in the code are indicated by – “Target not
created” in the Build window, along with the error line. Debug the errors. After
anerror free, to build go to Debug mode.
8. Now user can enter into Debug mode with Debug- Start / Stop Debug session

dialog. Or by clicking in the icon.


9. The program is run using the Debug-Run command & halted using Debug-Stop

Running. Also the (reset, run, halt) icons can be used. Additional

icons are (step, step over, and step into, run till cursor).
10. If it is an interface program the outputs can be seen on the LCD, CRO, motor, led
status, etc. If it is a part-A program, the appropriate memory window is opened
using View -> memory window (for data RAM & XRAM locations), Watch
window (for timer program), serial window, etc.

11. Note: To access data RAM area type address as D: 0020h. Similarly to access the
DPTR region (XRAM-present on chip in AT89C51ED2) say 9000h location type
in X: 09000H.
EXERCISE - 1
16 BIT ADDITION

AIM:

To write a assembly language program to 16 Bit addition.

SOFTWARE REQURIED:

SNO NAME QUANTITY


1 keil software 1
2 Windows Xp operating system 1

ALGORTHIM:

1. Start.
2. Write the program in KEIL.
3. If no errors and warnings.
4. Executed the program.
5. Note the address and opcode
6. Stop
PROGRAM:

16 BIT ADDITION FROM EXTERNAL MEMORY


PROGRAM

ORG 0000H
MOV DPTR,#4300H
MOVX A,@DPTR
MOV R0,A

INC DPTR
MOVX A,@DPTR
MOV R1,A
INC DPTR
MOVX A,@DPTR
MOV R2,A

INC DPTR
MOVX A,@DPTR
MOV R3,A
MOV A,R0
ADDC A,R2

MOV DPTR,#4500H
MOVX @DPTR,A
MOV A,R1
ADDC A,R3
MOV DPTR,#4501H
MOVX @DPTR,A
END
LOCATION DATA
INPUT 4300H 05
4301H 10
4302H 15
4303H 20
OUTPUT 4500H 1A
4501H 30

Result :

Assembly language program of 16 Bit Addition was Performed.


EXERCISE - 2

16 BIT SUBTRACTION

AIM:

To write a assembly language program to 16 Bit subtraction.

SOFTWARE REQURIED:

SNO NAME QUANTITY


1 keil software 1
2 Windows Xp operating system 1

ALGORTHIM:

7. Start.
8. Write the program in KEIL.
9. If no errors and warnings.
10. Executed the program.
11. Note the address and opcode
12. Stop

PROGRAM OF SUBSTRACTION:

ORG 0000H
LJMP MAIN
MAIN:
MOV A,#34H
MOV B, #12H
SUBB A, B
MOV R0,A
MOV A, #B4H
MOV B,#C2H
SUBB A, B
MOV R1,A
END

LOCATION DATA
INPUT

OUTPUT

RESULT:

Assembly language program of 16 Bit Subtraction was Performed.


EXERCISE - 3
8-BIT MULTIPLICATION

AIM:

To write a assembly language program to 8 Bit Multiplication.

SOFTWARE REQURIED:

SNO NAME QUANTITY


1 keil software 1
2 Windows Xp operating system 1

ALGORTHIM:

13. Start.
14. Write the program in KEIL.
15. If no errors and warnings.
16. Executed the program.
17. Note the address and opcode
18. Stop

PROGRAM:

ORG 0000H

MOV A,40H

MOV B,41H

MUL AB

MOV 42H,A

MOV A,B
MOV 43H,A

END

OUTPUT:

LOCATION DATA
INPUT 40H 02
41H 02
OUTPUT 42H 04
43H 00

RESULT:

Assembly language program of 8 Bit Multiplication was Performed.


EXERCISE - 4

8- BIT DIVISION

AIM:

To write a assembly language program to 8 Bit Division.

SOFTWARE REQURIED:

SNO NAME QUANTITY


1 keil software 1
2 Windows Xp operating system 1

ALGORTHIM:

19. Start.
20. Write the program in KEIL.
21. If no errors and warnings.
22. Executed the program.
23. Note the address and opcode
24. Stop

PROGRAM:

ORG 000H

MOV A,40H

MOV B,41H

DIV AB

MOV 42H,A
MOV A,B

MOV 43H,A

END

OUTPUT:
LOCATION DATA
INPUT 40H 02
41H 02
OUTPUT 42H 01
43H 00

RESULT:

Assembly language program of 16 Bit Division was Performed.


EXERCISE - 5

DATA TRANSFER
AIM:

To Transfer A Block Of Data Bytes From Source Memory To Destination Memory


Using 8051.

SOFTWARE REQURIED:

SNO NAME QUANTITY


1 keil software 1
2 Windows Xp operating system 1

PROGRAM:
MOV R0,#50H // Initialize the source memory pointer
MOV R1,#60H // Initialize the destination memory pointer

MOV R2, #05H // Initialize Iteration counter


BACK: MOV A,@R0 // Get the data from source memory pointer
MOV @R1,A // Store the data into destination memory pointer

INC R0 // Increment the source memory pointer


INC R1 // Increment the destination memory pointer
DJNZ R2, BACK // Decrement iteration count and if it
// is not zero, go to relative Address and
// repeat the same process until count become Zero
END
MEMORY WINDOW:

Before execution:

D:0x50H: 22 AB 03 44 55 00

D:0X60H: 00 00 00 00 00 00

After execution:

D:0x50H: 22 AB 03 44 55 00

D:0X60H: 22 AB 03 44 55 00

RESULT:
Transfer A Block Of Data Bytes From Source Memory To Destination Memory
Using 8051 is performed.
EXERCISE - 6

BLOCK EXCHANGE

AIM:

To Exchange Two Blocks Of Data Bytes Using 8051

SOFTWARE REQURIED:

SNO NAME QUANTITY


1 keil software 1
2 Windows Xp operating system 1

PROGRAM:
MOV R0,#50H // Initialize the source memory pointer
MOV R1,#60H // Initialize the destination memory pointer
MOV R2,#05H // Initialize Iteration counter
BACK: MOV A,@R0 // Get the data from source memory pointer and Load
// into Accumulator
XCH A,@R1 // Exchange data between Accumulator and
// destination memory pointer
MOV @R0,A // Store the data into source memory pointer
INC R0 // Increment the source memory pointer
INC R1 // Increment the destination memory pointer
DJNZ R2, BACK /* Decrement iteration count and if it is not zero,go to
relative Address and repeat the same process
until count become zero*/
END
MEMORY WINDOW:
Before execution:
D:0x50H: 01 02 03 04 05 00
D:0X60H: 06 07 08 09 10 00

After execution:
D:0x50H: 06 07 08 09 10 00
D:0X60H: 01 02 03 04 05 00

RESULT:

Exchange Two Blocks Of Data Bytes Using 8051 is Performed.


EXERCISE - 7

LARGEST ELEMENT IN AN ARRAY USING 8051

AIM:

To Find The Largest Element In An Array Using 8051

SOFTWARE REQURIED:

SNO NAME QUANTITY


1 keil software 1
2 Windows Xp operating system 1

PROGRAM TO FIND THE LARGEST NUMBER:

MOV R0,#50H // Initialize the source memory pointer


MOV R2,#05H // Initialize Iteration counter
MOV B, @R0 /* Use B Register to store largest value and initialize it to
the first value*/
BACK: MOV A,@R0 /* Get the data from source memory pointer and
Load into accumulator*/

CJNE A,B,LOOP /* Compare the data if not equal, go to relative


address(LOOP)*/
LOOP: JC LOOP1 // If carry generates, go to relative address

LOOP1MOV B,A // Store larger value into B-register


INC R0 // Increment the source memory pointer
DJNZ R2,BACK /* Decrement iteration count and if it is not zero, goto
relative address and repeat the same process until count become
zero.*/
SJMP NEXT // Go to NEXT
LOOP1:INC R0 // Increment the source memory pointer
DJNZ R2,BACK /* Decrement iteration count and if it is not zero, goto
relative address and repeat the same process until count become
zero.*/
NEXT: MOV 60H,B /* Store the largest value into memory location 60H.*/
END
MEMORY WINDOW:

Before execution:

D:0x50h: 22 AB 3D 44 55 00
D:0x60h: 00 00 00 00 00 00

After execution:

D:0x50h: 22 AB 3D 44 55 00
D:0x60h: AB 00 00 00 00 00

RESULT:

The Largest Element In An Array Using 8051 is Performed.


EXERCISE - 8

SMALLEST NUMBER

AIM:

To Find The Smallest Element In An Array Using 8051

SOFTWARE REQURIED:

SNO NAME QUANTITY


1 keil software 1
2 Windows Xp operating system 1

PROGRAM TO FIND THE SMALLEST NUMBER:

MOV R0,#50H // Initialize the source memory pointerMOV


R2,#05H // Initialize Iteration counter
MOV B, @R0 /* Use B Register to store smallest value and
initialize it to the first value*/
BACK:MOV A,@R0 /* Get the data from source memory pointer and Loadinto
accumulator*/
CJNE A,B,LOOP /* Compare the data if not equal, go to relative
address(LOOP)*/
LOOP: JNC LOOP1 // If carry generates, go to relative address

LOOP1MOV B,A // Store smaller value into B-register


INC R0 // Increment the source memory pointer
DJNZ R2,BACK /* Decrement iteration count and if it is not zero, goto relative
address and repeat the same process until count become zero.*/
SJMP NEXT // Go to NEXT
LOOP1: INC R0 // Increment the source memory pointer
DJNZ R2,BACK /* Decrement iteration count and if it is not zero, goto relative
address and repeat the same process until count become zero.*/
NEXT: MOV 60H,B /*Store the smallest value into memory location 60H.*/
END
MEMORY WINDOW:

Before execution:

D:0x50H: 22 AB 3D 44 55 00
D:0X60H: 00 00 00 00 00 00

After execution:

D:0x50H: 22 AB 3D 44 55 00
D:0X60H: 22 00 00 00 00 00

RESULT:

The Smallest Element In An Array Using 8051 is Performed.


EXERCISE - 9

ASCENDING ORDER
AIM:

To Arrange N 8-Bit Numbers In Ascending Order.

SOFTWARE REQURIED:

SNO NAME QUANTITY


1 keil software 1
2 Windows Xp operating system 1

PROGRAM:

MOV R2, #05H // Initialize the iteration counter


DEC R2 // Decrement the iteration count
BACK1: MOV R0, #50H // Initialize memory pointer1 MOV
R1, #51H // Initialize memory pointer2
MOV A, R2 // Store outer loop count
MOV R3, A // Store inner loop count
BACK: MOV A,@R0 // Get the data from memory pointer1
MOV B,@R1 // Get the data from memory pointer2
CJNE A, B, LOOP /* Compare if not equal go to relative address
(LOOP)*/
LOOP: JC LOOP1 /* If carry generates, go to relative address
(LOOP1)*/
MOV @R0,B // Exchange the data in memory pointer
MOV @R1, A
LOOP1: INC R0 // Increment the memory pointer1
INC R1 // Increment the memory pointer2
DJNZ R3, BACK // Decrement inner loop count if not zero go toback
DJNZ R2, BACK1 // Decrement outer loop count if not zero go toback1
END
MEMORY WINDOW:

Before execution:

D:0x50H: 06 04 03 07 02 01

After execution:

D:0x50H: 01 02 03 04 06 07

RESULT:

Arrange N 8-Bit Numbers In Ascending Order is Performed.


EXERCISE - 10

ASCENDING ORDER
AIM:

To Arrange N 8-Bit Numbers In Descending Order.

SOFTWARE REQURIED:

SNO NAME QUANTITY


1 keil software 1
2 Windows Xp operating system 1

PROGRAM:
MOV R2, #05H // Initialize the iteration counter
DEC R2 // Decrement the iteration count
BACK1: MOV R0, #50H // Initialize memory pointer1 MOV
R1, #51H // Initialize memory pointer2
MOV A, R2 // Store outer loop count
MOV R3, A // Store inner loop count
BACK: MOV A,@R0 // Get the data from memory pointer1 MOV
B,@R1 // Get the data from memory pointer2
CJNE A, B, LOOP // Compare if not equal go to relative address (LOOP) LOOP:
JNC LOOP1 // If carry generates, go to relative address (LOOP1)MOV @R0,B //
Exchange the data in memory pointer
MOV @R1, A
LOOP1: INC R0 // Increment the memory pointer1
INC R1 // Increment the memory pointer2
DJNZ R3, BACK /* Decrement inner loop count, if not zero go to back*/ DJNZ
R2, BACK1 /* Decrement outer loop count, if not zero go to back1*/ END
MEMORY WINDOW:

Before execution:

D:0x50H: 06 04 03 07 02 01

After execution:

D:0x50H: 07 06 04 03 02 01

RESULT:

Arrange N 8-Bit Numbers In Descending Order is Performed


EXERCISE - 11

ADDITION USING EMBEDDED C

AIM:

To write a Embedded C program to Perform addition.

SOFTWARE REQURIED:

SNO NAME QUANTITY


1 keil software 1
2 Windows Xp operating system 1

ALGORTHIM:

1. Start.
2. Write the program in KEIL.
3. If no errors and warnings.
4. Executed the program.
5. Stop
PROGRAM:

#include<reg51.h>

voidmain(void)

unsigned char x,y;

a=0x02;

b=0x03;

P1=0x00;

P1=a+b;

OUTPUT:

RESULT:
Embedded C program of Addition is Performed.
EXERCISE - 12

SUBTRACTION USING EMBEDDED C

AIM:

To write a Embedded C program to Perform Subtraction.

SOFTWARE REQURIED:

SNO NAME QUANTITY


1 keil software 1
2 Windows Xp operating system 1

ALGORTHIM:

6. Start.
7. Write the program in KEIL.
8. If no errors and warnings.
9. Executed the program.
10. Stop
PROGRAM:

#include<reg51.h>

voidmain(void)

unsigned char x,y;

a=0x05;

b=0x03;

P1=0x00;

P1=a-b;

OUTPUT:

RESULT:
Embedded C program of Subtraction is Performed.
EXERCISE - 13

MULTIPLICATION USING EMBEDDED C

AIM:

To write a Embedded C program to Perform Multiplication.

SOFTWARE REQURIED:

SNO NAME QUANTITY


1 keil software 1
2 Windows Xp operating system 1

ALGORTHIM:

11. Start.
12. Write the program in KEIL.
13. If no errors and warnings.
14. Executed the program.
15. Stop
PROGRAM:

#include<reg51.h>

voidmain(void)

unsigned char x,y;

a=0x05;

b=0x02;

P1=0x00;

P1=a*b;

OUTPUT:

RESULT:
Embedded C program of Multiplication is Performed.
EXERCISE - 14

DIVISION USING EMBEDDED C

AIM:

To write a Embedded C program to Perform Division.

SOFTWARE REQURIED:

SNO NAME QUANTITY


1 keil software 1
2 Windows Xp operating system 1

ALGORTHIM:

1. Start.
2. Write the program in KEIL.
3. If no errors and warnings.
4. Executed the program.
5. Stop
PROGRAM:

#include<reg51.h>

voidmain(void)

unsigned char x,y;

a=0x06;

b=0x02;

P1=a / b;

OUTPUT:

RESULT:
Embedded C program of Multiplication is Division.

You might also like