Microcontroller Lab BECL456A Manual-PPP
Microcontroller Lab BECL456A Manual-PPP
BECL456A (AEC)
2023-Batch
Page 1 of 27
II. C Programming
1 Write an 8051 C program to find the sum of first 10 Integer Numbers.
2 Write an 8051 C program to find Factorial of a given number.
3 Write an 8051 C program to find the Square of a number (1 to 10) using Look-Up Table.
4 Write an 8051 C program to count the number of Ones and Zeros in two consecutive memory
locations.
Page 2 of 27
INTRODUCTION
Brief History of 8051 Microcontroller
• In 1976, Intel Corporation introduced an 8-bit microcontroller under the name MCS-48. Later they
released an improved version under the name MCS-51.
• In 1981, Intel Corporation introduced an 8-bit microcontroller called 8051.
• The 8051 is a Harvard architecture, CISC instruction set, single chip microcontroller (µC).
• 8051 belongs to the MCS-51 family of microcontrollers by Intel.
• 8051 had processor 8 bit, RAM 128 bytes, ROM - 4K, timer-2, serial port 1, IO port 4 (4*8 = 32 IO
pins), interrupt source 6.
• It was referred as system on chip.
• Intel allowed other manufactures to make and market any flavour of 8051 with condition that they
remain code compatible with original 8051.
• There are different flavours of 8051 in terms of speed, ROM, RAM, timer and interrupt.
8052 and 8031 are the two other members of 8051 family of microcontrollers.
• Comparison of 8051 family microcontrollers.
Page 3 of 27
8051 Features
• 8bit ALU
• 8 bit accumulator
• 8 bit registers
• One 16 bit register with special move instruction
• 8 bit data bus
• 16 bit address bus
• 16 bit program counter
• 16 bit data or stack pointer
• Boolean processor with 17 instructions, 1 bit accumulator, 32 registers (4 bit addressable 8 bit), 144 special
1 bit addressable RAM variables (18 bit addressable 8 bit)
• Multiply, Divide and compare instruction
• 4 register bank with 8 registers each
• 3 Internal and 2 external interrupt sources
• Interrupt with 2-level priority and optional register bank switching
• 128 bytes of on-chip RAM (Data memory)
• 4K bytes of on-chip ROM (Program Memory)
• Four 8 bit bidirectional input/output ports
• Serial port - UART
• Two 16 bit counter/timers
• Power saving mode
• 1 Microsecond instruction cycle with 12 MHz Crystal
Page 4 of 27
Page 5 of 27
Page 6 of 27
• 8051 have 4K bytes Program Memory, 128 Data Memory and Special Function Registers.
• Program and Data memory share the same address space but are accessed via different instruction
types.
• IRAM from 0x00 to 0x7F can be accessed directly or indirectly.
• Special function registers (SFR) from 0x80 to 0xFF can be accessed directly.
Page 7 of 27
Page 8 of 27
Page 9 of 27
8051 starts executing program instructions from address 0000H in the program memory.
EA pin should be connected to Vcc to fetch instructions from internal memory initially.
Control will automatically move to external memory to fetch instruction from them.
EA pin should be connected to ground to fetch instruction from external memory only.
Internal program memory can be locked and protected.
Page 10 of 27
Page 11 of 27
The 8051 is packaged in a 40-pin DIP. It is important to note that many pins of 8051 are used for
more than one function. The alternative functions of pins are shown in bold letters.
The 8051 has 32 I/O pins configured as four eight-bit parallel ports (P0, Pl, P2 and P3). All four ports
are bidirectional i.e. each pin will be configured as input or output (or both). All port-pins are
multiplexed except the pins of port 1. Each port consists of a latch, an output driver and an input
buffer.
Page 12 of 27
Port 0 (Pins 32 - 39) : Port 0 pins can be used as I/O pins. The output drives and input buffers of
port 0 are used to access external memory. Port 0 outputs the low order byte of the external memory
address, time multiplexed with the data being written or read. Thus, port 0 can be used as a
multiplexed address/data bus.
Port 2 (Pins 21 - 28) : The output drives of port 2 are used to access external memory. Port 2 outputs
the high order byte of the external memory address when the address is 16 bits wide. Otherwise,
port 2 is used as an I/O port.
Port 3 (Pins 10 - 17) : All port pins of port 3 are multifunctional. Therefore, each pin of port 3 can
be programmed to use as I/O or as one of the alternate function. They have special functions as shown
Page 13 of 27
below including two external interrupts, two counter inputs, two special data lines and two timing
control strobes.
Power-supply Pins VCC (Pin 40) and VSS (Pin 20) : 8051 operates on d.c. power supply of +5 V with
respect to ground. The +5 V is to be connected to pin VCC and ground to pin VSS with rated power
supply current of 125 mA.
Oscillator Pins XTAL2 (Pin 18) and XTAL1 (Pin 19) : For generating an internal clock signal, the
external oscillator is connected at these two pins.
ALE (Address Latch Enable, Pin 30) : AD0 to AD7 lines are multiplexed. To demultiplex these lines
and for obtaining lower half of an address, an external latch and ALE signal of 8051 is used.
RST (Reset, Pin 9) : This pin is used to reset 8051. For proper reset operation, reset signal must be
held high at least for two machine cycles, while oscillator is running.
(Program Store Enable, Pin 29) : It is the active low output control signal used to activate
the enable signal of the external ROM/EPROM. It is activated every six oscillator periods while
reading the external memory. Thus, this signal acts as the read strobe to external program memory.
(External Access, Pin 31) : When the pin is high (connected to VCC), program fetches to
addresses 0000H through 0FFFH are directed to the internal ROM and program fetches to addresses
Page 14 of 27
1000H through FFFFH are directed to external ROM/EPROM. When is low (grounded), all
addresses (0000H to FFFFH) fetched by program are directed to the external ROM/EPROM.
Page 15 of 27
Page 16 of 27
8051 Stack
Stack Memory
Page 17 of 27
Loading of data from CPU registers to the stack is done by PUSH instruction. Loading the
contents of the stack back in to CPU registers is done by POP instruction.
Stack grows upward; the SP is incremented before pushing and decremented after popping a
value.
Page 18 of 27
Page 19 of 27
Name: USN:
Program No. 1.
Program: Write an ALP to move a block of n bytes of data from source (20h) to destination (40h) using
Internal-RAM.
Faculty Signature
Page 20 of 27
MEMORY WINDOW:
Before execution:
D:0x20H: 22 AB 3D 44 55 00
D:0X40H: 00 00 00 00 00 00
After execution:
D:0x20H: 22 AB 3D 44 55 00
D:0X40H: 22 AB 3D 44 55 00
Page 21 of 27
Name: USN:
Program No. 2.
Program: Write an ALP to move a block of n bytes of data from source (2000h) to destination (2050h) using
External RAM.
Faculty Signature
Page 22 of 27
MEMORY WINDOW:
Before execution:
X:0x2000H: 22 AB 3D 44 55 00
X:0X2050H: 00 00 00 00 00 00
After execution:
X:0x2000H: 22 AB 3D 44 55 00
X:0X2050H: 22 AB 3D 44 55 00
Page 23 of 27
Name: USN:
MICROCONTROLLER LAB BECL456A
Program No. 3.
Program: Write an ALP To exchange the source block starting with address 20h, (Internal RAM) containing N
(05) bytes of data with destination block starting with address 40h (Internal RAM).
Faculty Signature
Page 24 of 27
MEMORY WINDOW:
Before execution:
D:0x20H: 22 AB 3D 44 55 00
D:0X40H: 11 CD E4 66 77 00
After execution:
D:0x20H: 11 CD E4 66 77 00
D:0X40H: 22 AB 3D 44 55 00
Page 25 of 27
Name: USN:
MICROCONTROLLER LAB BECL456A
Program No. 4.
Program: Write an ALP to exchange the source block starting with address 10h (Internal memory),
containing n (06) bytes of data with destination block starting at location 00h (External memory).
Faculty Signature
Page 26 of 27
MEMORY WINDOW:
Before execution:
D:0x10H: 22 AB 3D 44 55 00
X:0X0000H: 11 CD E4 66 77 00
After execution:
D:0x10H: 11 CD E4 66 77 00
X:0X0000H: 22 AB 3D 44 55 00
Page 27 of 27