Applications of Microcontroller-I Prof. Ramakant Patil

TE SEM-V Instrumentation Engineering

Module-2/MCS-51 microcontroller

2.1 What is 8051 Microcontroller?

8051 is an 8-bit family of microcontroller developed by Intel in the year 1981. This is one of the most popular families of microcontroller being used all across the world. This microcontroller was also referred as “system on a chip” because it has 128 bytes of RAM, 4Kbytes of ROM, 2 Timers, 1 Serial port, and four ports on a single chip. The CPU can work for only 8bits of data at a time because 8051 is an 8-bit processor.In case the data is larger than 8 bits then it has to be broken into parts so that the CPU can process conveniently.Most manufacturers have put 4Kbytes of ROM even though the quantity of ROM can be exceeded up to 64 K bytes.

The 8051 has been in use in a wide number of devices, mainly because it is easy to integrate into a project or build a device around. The following are the main areas of focus:

i.Energy Management: Efficient metering systems help in controlling energy usage in homes and industrial applications. These metering systems are made capable by incorporating microcontrollers.

ii.Touch screens:A high number of microcontroller providers incorporate touch-sensing capabilities in their designs. Portable electronics such as cell phones, media players and gaming devices are examples of microcontroller-based touch screens.

iii.Automobiles:The 8051 finds wide acceptance in providing automobile solutions.They are widely used in hybrid vehicles to manage engine variants. Additionally, functions such as cruise control and anti-brake system have been made more efficient with the use of microcontrollers.

iv.Medical Devices:Portable medical devices such as blood pressure and glucose monitors use microcontrollers will to display data, thus providing higher reliability in providing medical results.

2.2 Features ofMicrocontroller 8051:

Following are the main features ofMicro controller 8051 which makes it most efficientMicrocontroller chip:

1)It Includes Boolean Processing Engine. Thus internal registers and RAM can carry Boolean logic operations directly and efficiently.

2)It gives us many functions in a single chip. (e.g. CPU,RAM,ROM,I/O, Interrupt, Timer etc)

3)It has 8 bit Data bus.

4)It have 8 bit Stack Pointer

5)It have 16 bit Program Counter.

6)It has 16 bit address bus which can access almost 65,536 memory locations.

7)Data memory or RAM of 128 bytes.(On-Chip).

8)Program Memory or ROM of 4 KB.(On-Chip).

9)Bi-Directional I/O port of 4 bytes.

10)It has 4separateRegister Sets.

11)Serial Port or UART.

12)It features Power Saving Mode which saves power.

13)Two Timers/Counters each of 16 bit.

14)Internal and External Interrupt Sources.

15)2 levels interrupt priority.

2.3 Comparison between Microprocessor and Microcontroller:

Sr.No. / Microprocessor / Microcontroller
1 / The architecture uses data lines more than control lines. / The architecture uses control lines more that data lines.
2 / No RAM and ROM on the chip / RAM and ROM along with processor are present
3 / It has single or dual bit instructions / It has multi bit instructions
4 / Access time to memory and I/O devices is high / Access time to memory and I/O devices is less
5 / Less number of pins are multiplexed / More number of pins are multiplexed
6 / Consists of single memory map for data and code / Consists of separate memory map for data and code
7 / High clock frequency / Low clock frequency
8 / External peripheral are to interfaced to enhance functionality / These are application specific and so no need of external peripherals
9 / Can perform all the computation in any field / Can perform only a specific task
10 / Cost is high / Cost is low
11 / I/O communication needs external peripherals / I/O communication ports are inbuilt
12 / Serial communication is not possible / Serial communication ports are inbuilt
13 / Boolean operations cannot be performed directly / Boolean operations can be performed directly

2.4 Comparison between Microcontroller Families:

Features / 8051 / 8031 / 8032 / 8052 / 8751 / 8752
ROM / 4K / 0 / 0 / 8K / 4KEPROM / 8KEPROM
RAM / 128 / 128 / 256 / 256 / 128 / 256
Timers / 2 / 2 / 3 / 3 / 2 / 3
I/O pins / 32 / 32 / 32 / 32 / 32 / 32
Serial Port / 1 / 1 / 1 / 1 / 1 / 1
Interrupt Sources / 6 / 6 / 5 / 8 / 5 / 6
89XX / 8951 / 8952 / 8953 / 8955 / 898252 / 891051 / 892051
ROM / 4k / 8k / 12k / 20k / 8k / 1k / 2k
RAM / 128 / 256 / 256 / 256 / 256 / 64 / 128
TIMER / 2 / 3 / 3 / 3 / 3 / 1 / 2
Interrupt Sources / 6 / 8 / 9 / 8 / 9 / 3 / 6
I/O pins / 32 / 32 / 32 / 32 / 32 / 16 / 16
Other / - / - / Watch Dog Timer / Watch Dog Timer / In System Programmable / Analog Comparator / Analog Comparator

2.5 Block diagram of 8051 microcontroller:
Following is the block diagram ofMicrocontroller 8051.

Microcontroller 8051 - Block Diagram

Explanation:
Central Processor Unit(CPU): It monitors and controls all operations that are performed in theMicrocontroller. User has no control over the work of CPU. It reads program written in ROM memory and executes them and do the expected task.
Interrupts: As its name suggests, Interrupt is a subroutine call that interruptsMicrocontroller's main operation or work and causes it to execute some another program which is more important at that time. The feature of Interrupt is very useful as it helps in cases of emergency. Interrupts gives us a mechanism to put on hold the ongoing operation, execute a subroutine and then again resumes normal program execution.
TheMicrocontroller 8051 can be configured in such a way that it temporarily terminates or pause the main program at theoccurrence of interrupt. When subroutine is completed then the execution of main program starts as usual. There are five interrupt sources in 8051Microcontroller. 2 of them are external interrupts, 2 timer interrupts and one serial port interrupt.
Memory:Microcontroller requires a program which is a collection of instructions. This program tellsMicrocontroller to do specific tasks. These programs require a memory on which these can be saved and read byMicrocontroller to perform specific operation. The memory which is used to store the program ofMicrocontroller is known as code memory or Program memory. It is known as 'ROM'(Read Only Memory).
Microcontroller also requires a memory to store data or operands temporarily. The memory which is used to temporarily store data for operation is known as Data Memory and we uses 'RAM'(Random Access Memory) for this purpose.Microcontroller 8051 has 4K of Code Memory or Program memory that is it has 4KB Rom and it also have 128 bytes of data memory i.e. RAM.

Bus: Basically Bus is a collection of wires which work as a communication channel or medium for transfer of Data. These buses consist of 8, 16 or more wires. Thus these can carry 8 bits, 16 bits simultaneously. Buses are of two types:

  • Address Bus
  • Data Bus

Address Bus:Microcontroller 8051 has a 16 bit address bus. It used to address memory locations. It is used to transfer the address from CPU to Memory.

Data Bus:Microcontroller 8051 has 8 bits data bus. It is used to carry data.

Oscillator: As we knowMicrocontroller is a digital circuit device, therefore it requires clock for its operation. For this purpose,Microcontroller 8051 has an on-chip oscillator which works as a clock source for Central Processing Unit. As the output pulses of oscillator are stable therefore it enables synchronized work of all parts of 8051Microcontroller.

Input/output Port: As we know that Microcontroller is used in embedded systems to control the operation of machines. Therefore to connect it to other machines, devices or peripherals we require I/O interfacing ports inMicrocontroller. For this purposeMicrocontroller 8051 has 4 input output ports to connect it to other peripherals.

Timers/Counters:Microcontroller 8051 has 2 16 bit timers and counters. The counters are divided into 8 bit registers. The timers are used for measurement of intervals, to determine pulse width etc.

2.6 PIN Diagram of the 8051:


Port 0(p0.0 to p0.7):
It is 8-bit bi-directional I/O port. It is bit/ byte addressable. During external memory access, it functions as multiplexed data and low-order address bus AD0-AD7.
Port 1 (p1.0 to p1.7):
It is 8-bit bi-directional I/O port. It is bit/ byte addressable. When logic '1' is written into port latch then it works as input mode. It functions as simply I/O port and it does not have any alternative function.
Port 2 (p2.0 to p2.7):
It is 8-bit bi-directional I/O port. It is bit/ byte addressable. During external memory access it functions as higher order address bus (A8-A15).
Port 3(p3.0 to port 3.7):
It is 8-bit I/O port. In an alternating function each pins can be used as a special function I/O pin.
P3.0-RxD:
It is an Input signal. Through this I/P signal microcontroller receives serial data of serial communication circuit.
P3.1-TxD:
It is O/P signal of serial port. Through this signal data is transmitted.
P3.2- (INT0):
It is external hardware interrupt I/P signal. Through this user, programmer or peripheral interrupts to microcontroller.
P3.3-(INT1):
It is external hardware interrupt I/P signal. Through this user, programmer or peripheral interrupts to microcontroller.
P3.4- T0:
It is I/P signal to internal timer-0 circuit. External clock pulses can connects to timer-0 through this I/P signal.
P3.5-T1:
It is I/P signal to internal timer-1 circuit. External clock pulses can connects to timer-1 through this I/P signal.

P3.6-[WR(bar)]:
It is active low write O/P control signal. During External RAM (Data memory) access it is generated by microcontroller. when [WR(bar)]=0, then performs write operation.
P3.7-[RD(bar)]:
It is active low read O/P control signal. During External RAM (Data memory) access it is generated by microcontroller. when [RD(bar)]=0, then performs read operation from external RAM.
XTAL1 and XTAL2:
These are two I/P line for on-chip oscillator and clock generator circuit. A resonant network as quartz crystal is connected between these two pin. 8051 microcontroller also drives from external clock, then XTAL2 is used to drive 8051 from external clock and XTAL1 should be grounded.
[EA(bar)]/VPP:
It is and active low I/P to 8051 microcontroller. when (EA)= 0, then 8051 microcontroller access from external program memory (ROM) only. When (EA) = 1, then it access internal and external program memories (ROMs).
[PSEN(bar)]:
It is active low O/P signal. It is used to enable external program memory (ROM). When [PSEN(bar)]= 0, then external program memory becomes enabled and microcontroller read content of external memory location. Therefore it is connected to (OE) of external ROM. It is activated twice every external ROM memory cycle.
ALE:
Address latch enable: It is active high O/P signal. When it goes high, external address latch becomes enabling and lower address of external memory (RAM or ROM) latched into it. Thus it separates A0-A7 address from AD0-AD7. It provides properly timed signal to latch lower byte address. The ALE is activated twice in every machine cycle. If external RAM & ROM is not accessed, then ALE is activated at constant rate of 1/6 oscillator frequency, which can be used as a clock pulses for driving external devices.
RESET:
It is active high I/P signal. It should be maintained high for at least two machine cycle while oscillator is running then 8051 microcontroller resets.

2.7 Architecture of 8051 Microcontroller:

An 8051 microcontroller has the following 12 major components:
1. ALU (Arithmetic and Logic Unit)
2. PC (Program Counter)
3. Registers
4. Timers and Counters
5. Internal RAM and ROM
6. Four general purpose parallel input/output ports
7. Interrupt control logic with five sources of interrupt
8. Serial data communication
9. PSW (Program Status Word)
10. Data Pointer (DPTR)
11. Stack Pointer (SP)
12. Data and Address bus.

1.ALU- All arithmetic and logical functions are carried out by the ALU.Addition, subtraction with carry, and multiplication come under arithmetic operations.Logical AND, OR and exclusive OR (XOR) come under logical operations.

2.Program Counter (PC)- A program counter is a 16-bit register and it has no internal address. The basic function of program counter is to fetch from memory the address of the next instruction to be executed. The PC holds the address of the next instruction residing in memory and when a command is encountered, it produces that instruction. This way the PC increments automatically, holding the address of the next instruction.

3.Registers-Registers are usually known as data storage devices. 8051 microcontroller has 2 registers, namely Register A and Register B. Register A serves as an accumulator while Register B functions as a general purpose register. These registers are used to store the output of mathematical and logical instructions.The operations of addition, subtraction, multiplication and division are carried out by Register A. Register B is usually unused and comes into picture only when multiplication and division functions are carried out by Register A. Register A also involved in data transfers between the microcontroller and external memory.8051 microcontroller also has 7 Special Function Registers (SFRs). They are:i. Serial Port Data Buffer (SBUF)ii. Timer/Counter Control (TCON)iii. Timer/Counter Mode Control (TMOD)iv. Serial Port Control (SCON)v. Power Control (PCON)vi. Interrupt Priority (IP)vii. Interrupt Enable Control (IE)

4.Timers and Counters- Synchronization among internal operations can be achieved with the

help of clock circuits which are responsible for generating clock pulses. During each clock pulse a particular operation will be carried out, thereby, assuring synchronization among operations. For the formation of an oscillator, we are provided with two pins XTAL1 and XTAL2 which are used for connecting a resonant network in 8051 microcontroller device. In addition to this, circuit also consists of four more pins. They are,i. EA: External enablesii. ALE: Address latch enableiii. PSEN: Program store enable andiv. RST: Reset.Internal operations can be synchronized using clock circuits which produce clock pulses. With each clock pulse, a particular function will be accomplished and hence synchronization is achieved. There are two pins XTAL1 and XTAL2 which form an oscillator circuit which connect to a resonant network in the microcontroller. Quartz crystal is used to generate periodic clock pulses.

5. InternalRAMand ROM

ROM

A code of 4K memory is incorporated as on-chip ROM in 8051. The 8051 ROM is a non-volatile memory meaning that its contents cannot be altered and hence has a similar range of data and program memory, i.e, they can address program memory as well as a 64K separate block of data memory.

RAM

The 8051 microcontroller is composed of 128 bytes of internal RAM. This is a volatile memory since its contents will be lost if power is switched off. These 128 bytes of internal RAM are divided into 32 working registers which in turn constitute 4 register banks (Bank 0-Bank 3) with each bank consisting of 8 registers (R0 - R7). There are 128 addressable bits in the internal RAM.

6. Four General Purpose Parallel Input/output Ports

The 8051 microcontroller has four 8-bit input/output ports. These are:PORT P0: When there is no external memory present, this port acts as a general purpose input/output port. In the presence of external memory, it functions as a multiplexed address and data bus. It performs a dual role.PORT P1: This port is used for various interfacing activities. This 8-bit port is a normal I/O port i.e. it does not perform dual functions.PORT P2: Similar to PORT P0, this port can be used as a general purpose port when there is no external memory but when external memory is present it works in conjunction with PORT PO as an address bus. This is an 8-bit port and performs dual functions.PORT P3: PORT P3 behaves as a dedicated I/O port

7. Interrupt ControlAn event which is used to suspend or halt the normal program execution

for a temporary period of time in order to serve the request of another program

orhardwaredevice is called an interrupt. An interrupt can either be an internal or external

event which suspends the microcontroller for a while and thereby obstructs the sequential

flow of a program.

There are two ways of giving interrupts to a microcontroller – one is by sending software

instructions and the other is by sending hardware signals. The interrupt mechanism keeps the

normal program execution in a "put on hold" mode and executes a subroutine program and

after the subroutine is executed, it gets back to its normal program execution. This subroutine

program is also called an interrupt handler. A subroutine is executed when a certain event

occurs.In 8051, 5 sources of interrupts are provided. They are:a) 2 external interrupt sources connected through INT0 and INT1b) 3 external interrupt sources- serial port interrupt, Timer Flag 0 and Timer Flag 1.The pins connected are as follows:i. ALE (Address Latch Enable) - Latches the address signals on Port P0ii. EA (External Address) - Holds the 4K bytes of program memoryiii. PSEN (Program Store Enable) - Reads external program memoryiv. RST (Reset) - Reset the ports and internal registers upon start up8. Serial Data Communication

A method of establishing communication among computers is by transmitting and receiving data bits is a serial connection network. In 8051, the SBUF (Serial Port Data Buffer) register holds the data; the SCON (Serial Control) register manages the data communication and the PCON (Power Control) register manages the data transfer rates. Further, two pins - RXD and TXD, establish the serial network.The SBUF register has 2 parts – one for storing the data to be transmitted and another for receiving data from outer sources. The first function is done using TXD pin and the second function is done using RXD pin.There are 4 programmable modes in serial data communication. They are:i. Serial Data mode 0 (shift register mode)ii. Serial Data mode 1 (standard UART)iii. Serial Data mode 2 (multiprocessor mode)iv. Serial Data mode 3

9. PSW (Program Status Word)

Program Status Word or PSW is a hardware register which is a memory location which holds a program's information and also monitors the status of the program this is currently being executed. PSW also has a pointer which points towards the address of the next instruction to be executed. PSW register has 3 fields namely are instruction address field, condition code field and error status field. We can say that PSW is an internal register that keeps track of the computer at every instant.Generally, the instruction of the result of a program is stored in a single bit register called a 'flag'. The are7 flags in the PSW of 8051. Among these 7 flags, 4 are math flags and 3 are general purpose or user flags.The 4 Math flags are:• Carry (c)• Auxiliary carry (AC)• Overflow (OV)• Parity (P)The 3 General purpose flags or User flags are:• FO• GFO• GF 1

10. Data Pointer (DPTR)

The data pointer or DPTR is a 16-bit register. It is made up of two 8-bit registers called DPH and DPL. Separate addresses are assigned to each of DPH and DPL. These 8-bit registers are used for the storing the memory addresses that can be used to access internal and external data/code.

11. Stack Pointer (SP)

The stack pointer (SP) in 8051 is an 8-bit register. The main purpose of SP is to access the stack. As it has 8-bits it can take values in the range 00 H to FF H. Stack is a special area of data in memory. The SP acts as a pointer for an address those points to the top of the stack.

12. Data and Address Bus

A bus is group of wires using which data transfer takes place from one location to another