1 - Microcontroller
1 - Microcontroller
UNIT – I / MICROCONTROLLER
PART – A
PART-B
Architecture of 8051:
The Architecture of 8051 is shown below
116
8-bit CPU
on-chip oscillator
4KB of ROM(program memory)
128 bytes of RAM(Data memory)
21 special fuction register.
32 I/O lines(Port P0 to P3)
64KB address space for external data memory
64KB address space for program memory
Two 16-bit timer/counter.
5 source interrupt structure
full duplex serial port
bit addressability.
powerful bit processing capability.
-51 family includes pin compatible chips like
Intel 8031
Intel 8051
Intel 8751
The 8031 is the ROM-
117
ALU (Arithmetic and logical Unit)
It does the Arithmetic function like addition, subtraction, multiplication and division. It
does the logical function like AND, OR, XOR and etc.
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 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.
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.
SFR s in 8051
ACC
B register
PSW Program status word
SP- stack pointer
DPL- Data pointer low
DPH- Data pointer high
P0
P1
P1
118
P2
P3
IP- interrupt priority
IE- interrupt enable
TMOD- timer mode control register(8 bit)
TCON timer control register (8 bit)
SCON serial ports control register (8bit)
PCON power control register (8bit)
SBUF serial data buffer
TH0 higher byte of 16 bit timer register 0
TLO - lower byte of 16 bit timer register 0
TH1 - higher byte of 16 bit timer register 1
TL1 - lower byte of 16 bit timer register 1
Program Counter
Program Counter is an engine running the program and points to the memory address
containing the next instruction to execute. After each instruction execution, the value of the
counter is incremented by 1
PSW register is one of the most important SFRs. It contains several status bits that reflect the
current state of the CPU. Besides, this register contains Carry bit, Auxiliary Carry, two
register bank select bits, Overflow flag, parity bit and user-definable status flag.
P - Parity bit. If a number stored in the accumulator is even then this bit will be automatically
set (1), otherwise it will be cleared (0). It is mainly used during data transmit and receive via
serial communication.
- Bit 1. This bit is intended to be used in the future versions of microcontrollers.
OV Overflow occurs when the result of an arithmetical operation is larger than 255 and
cannot be stored in one register. Overflow condition causes the OV bit to be set (1).
Otherwise, it will be cleared (0).
119
RS0, RS1 - Register bank select bits. These two bits are used to select one of four
register banks of RAM. By setting and clearing these bits, registers R0-R7 are stored in
one of four banks of RAM.
0 0 Bank0 00h-07h
0 1 Bank1 08h-0Fh
1 0 Bank2 10h-17h
1 1 Bank3 18h-1Fh
120
Timer register (T0 & T1)
T0 is a 16 bit register
TL0 represents the lower byte of timing register 0
TH0 represents the higher byte of timing register 0
T1 is a 16 bit register
TL1 represents the lower byte of timing register 1
TH1 represents the higher byte of timing register 1
Timing and control circuit
This unit drives all necessary timing and control signals for the internal operation of the
circuit.
S
These registers have some special functions like controlling the timer/counter, enabling
interrupts, controlling the serial port operations etc.
21 special function registers in the 8051.
Some SPF are bit addressable.
Special function registers are as below.
ACC - Accumulator
B - B Register
PSW - Program Status Word
SP - Stack Pointer
DPTR (Low) - Data Pointer Low
DPTR (High) - Data Pointer High
P0 - Port 0
P1 - Port 1
P2 - Port 2
P3 - Port 3
IP - Interrupt Priority
IE - Interrupt Enable
PMOD - Timer / Counter Mode
PCON - Timer / Counter Control
TH0 - (Timer / Counter) 0 High
TL0 - (Timer / Counter) 0 Low
121
TH1 - (Timer / Counter) 1 High
TL1 - (Timer / Counter) 1 Low
SCON - Serial Control
SBUF - Serial Data Buffer
PCON - Power Control
Accumulator:
ACC is also called A register.
There are many instructions that use the accumulator as the destination.
ACC is used as one of the operands and after the operation, the result is also stored in the
ACC.
B Register:
Used for Multiply and divide operations.
This register acts as one of the operands and after the operations a part of the result is
stored in this register.
Register is just like a scratch pad i.e. a temporary register.
Stack Pointer:
is a 8-bit wide register.
This pointer can point to any location in the internal data RAM i.e. location from 0-127.
When the chip is reset, this register is initialized to 07H.
During PUSH and CALL instructions the stack pointer is first incremented and then the
data is stored in the stack.
Data Pointer:
is a 16-bit register, consisting of a high byte (DPH) and a low byte (DPL).
This register contains a 16-bit address.
Ports 0-3:
4 bidirectional Input / Output Ports of 8-bits each.
These are directly represented as pins of the 8051 chips and are bit addressable.
These are multi functional resources that can be programmed based on the application
needs
Serial Data buffer(SBUF):
This register holds the data that has to be transmitted through the serial port and also the
data that is received.
122
this register is interconnected to two 8-bit shift registers.
When data is written in to the SBUF, it is loaded in to the transmit shift register and hence
the process of moving a data byte in to the SBUF starts the transmission process.
During reception, the data coming to the 8051 is clocked in to the receive shift register &
once all the 8-bits of data or a frame is received it is transferred to the SBUF.
Control & Status Registers:
These registers contain the control & status bit of the interrupt systems, timers & serial port
operations,
The instruction decoding & control signal generation are performed using the timing &
control circuit block.
The bidirectional ports P0 to P3 have the basic structure containing drivers & latches.
The port drivers are connected to I/O ports.
The 8051 contains 128 bytes RAM as data memory & 4KB ROM as program memory on
the chip.
Internal data RAM with address register is connected to the internal bus
The address register will get the address from the internal bus & the data will be transferred
to the specified register through the bus.
123
A bidirectional buffer has been provided to temporarily store the branch address as well as
the operand address information.
This will also facilitate the transfer of the program counter contents to other circuits
Port P0 & P2 facilitate the connection to the external memory
The lower-order address bits (A0 to A7) are send through port0(P0.0 to P0.7)
The higher-order address bits (A8 to A15) are send through port2(P2.0 to P2.7)
Data pointer register is interfaced to the address register, plays a major role in external
program and data memory addressing using MOVC & MOVX instructions.
Port 0 (P0.0-P0.7) If configured as outputs, each of these pins can be connected to up to 8 TTL
inputs. If configured as inputs, the pins can be used as high-impedance inputs as their potential is not
defined relative to ground.
124
Port 1 (P1.0-P1.7) If configured as outputs, each of these pins can be connected to up to 4 TTL
inputs. When configured as inputs, these pins act as standard TTL inputs, that is, each of them is
internally connected to the positive supply voltage via a resistor of relatively high impedance. Power
supply voltage provided on these inputs is 5V. Also, the Port 1 pins have alternate functions as shown
in
RST Logic one (1) on this pin causes the microcontroller to be reset.
ALE/PROG When external memory is used, a signal from this pin is used to latch the low byte of an
address (A0-A7) from P0. During the process of writing a program to the microcontroller, this pin
also serves as a control input.
PSEN This pin provides a signal used for accessing external program memory (ROM).
EA/VPP When this pin is connected to ground, the microcontroller reads program instructions from
external program memory. If internal program memory is used, which is the common case, this pin
should be connected to the positive power supply voltage (VCC). During the process of programming
internal Flash memory, this pin is supplied with +12V.
XTAL 1 This is internal oscillator input. It is used for the purpose of synchronizing the operation of
the microcontroller with some other circuit or for connecting external oscillator when used.
XTAL 2 This pin is connected to internal oscillator output. Therefore, it is out of use when using
external oscillator.
125
Addressing mode 0f 8051:
The CPU can access data in various ways.
The data could be in a register, or in memory(RAM or ROM , or be provided as an
immediate value.
These various ways of accessing data are called addressing mode
Five addressing mode in the 8051
1. Immediate addressing mode
2. Register addressing mode
3. Direct addressing mode
4. Register indirect addressing mode
5. Indexed addressing mode
Register addressing mode involves the use of registers to hold the data.
The source and destination registers must match in size.
Register addressing mode accesses the eight register (R0 to R7)
is illegal.
Example:
MOV A, R0
MOV R2, A
ADD A, R5
address.
126
Example:
In the register indirect addressing mode, a register is used as a pointer to the data.
That is, this register holds the RAM address of the data.
Only registers R0 and R1 can be used to hold the address of an operand located in RA.
Usually, R0 and R1 are denoted by Ri.
When R0 and R1 hold the addresses of RAM
Example:
MOV @R0, A: Move contents of A into RAM location whose address is held by R0
MOV B, @R1,: Move contents of RAM location whose address is held by R1 into B
Indexed addressing mode is widely used in accessing data elements of look-up table entries
located in the program ROM space of the 8051.
A look-up table is a ROM block where the data is given previously (then you can
access it frequently).
The instruction used for this purpose is MOVC.
DPTR can be used to access memory externally connected to the 8051. Another register used
in indexed addressing mode is the PC
Example:
MOVC A, @A+DPTR
on-chip ROM).
A+DPTR is the address of the data element stored in on-chop ROM.
Put the ROM value to A.
127
Instruction set of 8051:
Types Of Instructions
128
Arithmetic instructions.
Logical instructions.
129
Branch instructions
130
INPUT / OUTPUT PORTS CONFIGURATION:
PORT 0
Port 0 is also designated as AD0-AD7.
When connecting an 8051 to an external memory, port 0 provides both address and data.
The 8051 multiplexes address and data through port 0 to save pins.
ALE indicates if P0 has address or data.
When ALE=0, it provides data D0-D7
When ALE=1, it has address A0-A7
PORT 1
131
PORT 2
PORT-3
132
UNIT 5
INTERFACING MICROCONTROLLER
Timers/counters in 8051:
8051 has two 16-bit timers/counters
These two timers/counters can be programmed independently.
There is a bit in the TMOD special function register that specifies whether it is a timer
or a counter.
If this bit is set, the timer/counter will work as a counter.
If this bit is reset, the timer/counter will work as a timer.
Timer Mode:
In timer mode, the timer register (TH1 and/or TL1 for timer 1 or TH0 and /or TH0 for
timer 0) is incremented after every machine cycle.
It will be working at 1/12th of the oscillator frequency because each machine cycle has
got 12 oscillator periods
Counter Mode:
It is required to count the number of occurrence of a particular event.
When a timer/counter is used as an event counter, the registers are incremented
whenever a 1 to 0 transition is sensed at the T0 or T1 pins of the 8051.
Timers can function in four different modes.
Mode 0
Mode 1
Mode 2
Mode 3
These modes are achieved by setting certain bits in the TMOD register.
Mode 0:
TL0 and TH0 for timer 0 are used as a 13-bit register i.e. all the 8 bits of the TL0 or
TL1 are utilized and the five lowermost bits of the TH0 or TH1 are used for
counting purposes.
set.
This timer interrupt flag is a bit, normally TF0 in the TCON, which is a special
function register.
133
Fig: Timer 0,Mode 0-13 bit counter
If C/T = 0, the register is incremented after every machine cycle is at the rate of
1/12th of the oscillator frequency.
For the register to get incremented, the control switch should be closed, this switch
is logic controlled to close the switch, we must fulfill this logic.
To implement this logic some of the ways are given below.
Case 1
TR0 = 1 (high)
Gate = 0 (low) & INT0 = 0 (low)
Case 2
TR0 = 1 (high)
INT0 = 1 (high) & Gate = 1 (high)
Gate and TR0 are bits in the TMOD & TCON registers and can be set or reset
through the software.
INT0 is one of the interrupt pins of the 8051.
Mode 0 for both the timers/counters is the same, only change TR0 to TR1, INT0 to
INT1 and gate bit for timer 0 to gate bit for timer 1.
Mode 1:
This mode is also same for both the timers.
Mode 1 is similar to Mode 0 except that, full TL0 and TH0 for timer 0 are used for
counting.
134
Fig: Timer 0,mode 1-16 bit counter
Mode 2:
The timer register is 8 bits wide.
TL0 for timer 0 is used for this purpose.
This mode is also called the auto-reload mode as the timer generates an interrupt on
overflow and after generating the interrupt will also reload the preset value form
TH0 in to TL0.
This present value can be put in the TH0 through a software.
After generating an interrupt, it also reloads the TL0 with the value form TH0 and
then starts counting again.
135
Mode 3:
If timer 0 is put into mode 3, it act as two 8-bit counters.
All the timer 0 control bits are used by TL0 itself and TH0 register is locked in to a timer
function.
TH0 is counting machine cycles and has taken over the use of TR1 & TF1 from
timer 1.
TH0 will control timer 1 interrupt.
If timer 1 is put in to mode 3, it just hold the count.
The effect is same as setting TR1=0, hence opening the switch.
136
SERIAL PROGRAMMING:
Computers transfer data in two ways:
1. Parallel
2. Serial
It is possible to write software to use either of these methods, but the programs can be tedious and
long
There are special IC chips made by many manufacturers for serial communications
UART (universal asynchronous Receiver transmitter)
USART (universal synchronous-asynchronous Receiver-transmitter)
SBUF REGISTER :
SBUF is an 8-bit register used solely for serial communication
For a byte data to be transferred via the TxD line, it must be placed in the SBUF register
The moment a byte is written into SBUF, it is framed with the start and stop bits and
transferred serially via the TxD line
SBUF holds the byte of data when it is received by 8051 RxD line
When the bits are received serially via RxD, the 8051 deframes it by eliminating the stop and
start bits, making a byte out of the data received, and then placing it in SBUF
137
SCON REGISTER :
SCON is an 8-bit register used to program the start bit, stop bit, and data bits of data framing,
among other things
138
INTERRUPT PROGRAMMING:
An interrupt is an external or internal event that interrupts the microcontroller to inform it that a
device needs its service
A single microcontroller can serve several devices by two ways
Interrupts
Whenever any device needs its service, the device notifies the microcontroller by sending it an
interrupt signal
Upon receiving an interrupt signal, the microcontroller interrupts whatever it is doing and
serves the device
The program which is associated with the interrupt is called the interrupt service routine (ISR)
or interrupt handler
Polling
The microcontroller continuously monitors the status of a given device
When the conditions met, it performs the service
After that, it moves on to monitor the next device until every one is serviced
Polling can monitor the status of several devices and serve each of them as certain conditions
are met
For the polling method, it is not possible to assign priority since it checks all devices in a
round-robin fashion
The microcontroller can also ignore (mask) a device request for service
When an interrupt is invoked, the microcontroller runs the interrupt service routine
For every interrupt, there is a fixed location in memory that holds the address of its ISR
The group of memory locations set aside to hold the addresses of ISRs is called interrupt
vector table
For every interrupt, there must be an interrupt service routine (ISR), or interrupt handler
Upon activation of an interrupt, the microcontroller goes through the following steps
1. It finishes the instruction it is executing and saves the address of the next instruction (PC) on the
stack
2. It also saves the current status of all the interrupts internally (i.e: not on the stack)
3. It jumps to a fixed location in memory, called the interrupt vector table, that holds the address of
the ISR
4. The microcontroller gets the address of the ISR from the interrupt vector table and jumps to it .
5. It starts to execute the interrupt service subroutine until it reaches the last instruction of the subroutine
which is RETI (return from interrupt)
6. Upon executing the RETI instruction, the microcontroller returns to the place where it was interrupted
7. First, it gets the program counter (PC) address from the stack by popping the top two bytes of the stack
into the PC
8. Then it starts to execute from that address .
Six interrupts are allocated as follows
139
Upon reset, all interrupts are disabled (masked), meaning that none will be responded to by the
microcontroller if they are activated
The interrupts must be enabled by software in order for the microcontroller to respond
to them
There is a register called IE (interrupt enable) that is responsible for enabling
(unmasking) and disabling (masking) the Interrupts
140
1. The above figure shows 4×4 matrix connected to two ports.
2. The rows are connected to an output port (port1) and the columns are connected to an input port
(port2).
3. Controller access both rows and columns through ports.
4. When a key is pressed, a row and column make a contact; otherwise, there is no connection
between rows and columns.
5. If no key has been pressed, reading the input port yield 1s for all columns, since they are all
connected to high (VCC).
6. If all rows are grounded and a key is pressed, one of the columns will have 0, sine the key pressed
provides the path to ground.
7. It is the function of the microcontroller to scan the keyboard continuously to detect and identify the
key pressed.
8. To detect a pressed key, the microcontroller grounds all rows by providing 0 to the output latch,
and then it reads the column.
9. If the data read from columns is D3- D0 = 1111, no key has been pressed. And the process
continues until a key pressed is detected.
10. If D3- D0 = 1101, this means that a key in D1 column has been pressed.
11. After a key pressed is detected, the microcontroller will go through the process of identifying the
key.
12. Starting with top row, the microcontroller grounds all the rows one by one and read the columns
and checks for any zero.
Program
1. In this program, it is assumed that P1 and P2 are initialized as output and input respectively.
2. Program goes through the following major stages.
3. To make sure that preceding key has been released, 0s are the outputs to all rows at once, columns
are read and checked repeatedly until all the columns are high.
4. When all columns are found to be high, the program waits for short amount of time before a key to
be pressed.
5. If any key is pressed, the columns are scanned again and again until one of them has a 0 on it.
6. After the key press detection, it waits 20milli seconds for the bounds and then scans the columns
again.
7. After 20milli seconds delay, the key is still pressed, it goes to detect which row it belongs to.
To detect the row it grounds one row at a time, reading the columns each time.
8. If all columns are high , the pressed key can not belong to that row , Therefore it grounds the
next row and continues until it finds the row the key press belongs to.
9. After finding the row , it sets up the starting address for the look-up table holding the ASCII codes
for that row and go to the next stage to identify the key
10. Now it rotate the column bits , one bit at a time in to the carry flag and check if it is low, When
carry flag is zero, it pulls out the ASCII code for that key from look up table; otherwise it increments
the pointer to point to the next element of the look up table.
141
142
37. Define Baud rate.
The number of bits transmitted per second is called baud rate. The standard baud rates are 75,
110, 150, 300, 600, 1100, 2400, 9600 and 19200.
38.Define interfacing.
Interfacing is the designing of logic circuits (i.e. Hardware) and writing the instructions (i.e.
Software) to enable the microprocessor to communicate with I/O devices.
39. Specify the bit which differentiates between I/O mode and the BSR mode in 8255.
Control Word
D7 D0
UNIT 4
MICROCONTROLLER
PART- A
1. What are the advantages of the register indirect addressing mode in 8051 microcontroller?
Addressing mode R0 & R1 of each register bank can be used as an index or point register.
R0 &R1 points to the contents in the RAM
Determine the operand address.
used to access the program memory
Register addressing
Direct addressing
Register Indirect addressing
Immediate addressing
Base register plus Index register indirect addressing.
174
3. What happens in power down mode of 8051 microcontroller?
In power down mode all the on-chip activity are suspended. The on-chip RAM continues to hold the
data. The device draws only 10 micro ampere current.
8051 is a stand-alone high performance microcontroller intended for use in sophisticated real-
time applications, such as instrumentation, industrial control, automobiles and computer
peripherals.
Provides features like interrupts ,bit addressability and an enhanced set of instructions, which
make the chip very powerful and cost effective
Used as a controller for many applications
Interfaced with other peripheral chips, makes it quite versatile.
Holds the data that has to be transmitted through the serial port & also holds the data that is
received.
This register is interconnected to two 8=bit shift register.
when data is written in to the SBUF,it is loaded in the transmit shift register & hence the
process of moving a data byte in to the SBUF starts the transmission process
During reception,the data coming to the 8051 is clocked in to the receive shift register & once
all the 8-bits of data or a frame is received it is transferred to the SBUF.
Bit No 7 6 5 4 3 2 1 0
175
Where, CY=Carry flag
AC=Auxiliary carry flag
OV=Overflow flag
P=Odd parity flag
F0=User flag 0
RS1 RS0
0 0 Register bank 0
0 1 Register bank 1
1 0 Register bank 2
1 1 Register bank 3
8. What is the difference between a microprocessor and microcontroller?
S No Microprocessor Microcontroller
4 ALU,PC,SP & registers are available, ALU,PC,SP & registers are available,
176
10. What is the function of DPTR register?
The data pointer register (DPTR) is the 16 bit address register that can be used to fetch any 8
bit data from the data memory space. When it is not being used for this purpose, it can be used as
two eight bit registers, DPH and DPL.
12. How does the status of EA pin affect the access to internal and external program memory?
If EA = 0, 8051 can access the external program memory.
= 1, accesses the internal program memory.
13. State the difference between RET and RETI instruction in 8051.
RET Return from subroutine.
This instruction is used to return from a subroutine previously entered by CALL
instructions.
RETI Return from interrupt
This instruction is used at the end of interrupt service routine (ISR).
MSB LSB
RRCA: Rotate Accumulator Right through Carry.
Each binary bit of the accumulator is rotated right by one position through the carry
flag. LSB is placed in the carry flag and the carry flag is placed in MSB.
MSB LSB CY
177
17. What are the alternate function of Port 3 in the 8051 microcontroller?
P3.0 RxD
P3.1 TxD
P3.2 INT0
P3.3 INT1
P3.4 T0
P3.5 T1
P3.6 WR
P3.7 RD
18. What is the function of SM2 bit present in SCON register in 8051?
SM2 enables the multiprocessor communication feature in modes 2 and 3. If SM2=1, RI will
not be activated if the received 9th data bit (RB8) is 0.
In mode 1, if SM2 =1, RI will not be activated if a valid stop bit was not received.
In mode 0, SM2 should be 0.
19. What is the importance of special function registers available in 8051 microcontroller?
The 8051 operations that do not use the internal 128 byte RAM address from 00H to 7FH are
done by a group of special internal registers, SFRs (Special Function Registers), which may be
addressed like internal RAM.
24. Write a program to load accumulator A, DPH and DPL with 30H.
MOV A,#30
MOV DPH,A
MOV DPL,A
25. Write a program to subtract the contents of R1 of Bank0 from the contents
of R0 of Bank2.
MOV PSW,#10
MOV A,R0
MOV PSW,#00
SUBB A,R1
UNIT 5
INTERFACING MICROCONTROLLER
PART-A
179