Micro Controllers
Micro Controllers
Micro Controllers
MICROCONTROLLERS
2
Microcontroller
(microcontroller)
Block diagram &
Internal Architecture of
8051 microcontroller
Internal architecture of 8051
■ The 8051 is the original member of the MCS-51family,
and is the core for all MCS-51 devices.
■ The features of the 8051 core are:
◻ 8 bit ALU, with two registers A & B
◻ 16 bit Program Counter & Data Pointer
◻ 8 bit Program Status Word
◻ 8bit Stack Pointer
◻ 128 bytes Internal RAM
◻ 4 register banks, each containing 8 registers.
◻ 16 bytes, which may be addressed at bit level.
◻ 80 bytes of gent purpose data memory.
◻ Internal ROM / EPROM / Flash memory of 4KB
◻ Can be extended separate 64KB address space for data & code
memory.
◻ 32 discrete I/O pins, grouped as four 8 bit ports.
◻ Full Duplex UART.
◻ Two l6 bit timer / counter - T0 & T1
◻ Two external & three internal interrupt sources
◻ Control registers –TCON, TMOD, SCON, PCON, and IP & IE
◻ Oscillator & clock circuits
The architectural structure of 8051
■ The programming model of 8051 is a collection of 8 bit
and 16 bit registers and 8 bit memory locations.
■ Registers:
◻ 8051 has 34 general purpose or working registers.
Two of them A & B, hold results of many instructions,
particularly Mathematical and logical operations, of
8051 CPU.
◻ The other 32 Reg. arranged as a part of internal RAM
in four banks RBO-RB3.
◻ A (Accumulator) register is used for many operations -
Addition,
◻ Reg. B is used with Reg.A for multiplication & division
operations and also for data transfer.
◻ Subtraction, integer- multiplication & division and
Boolean bit manipulations.
CY AC FO RSI RSO OV - P
■ Carry out Auxiliary General Register Banks Over flow Parity of
From carry for purpose RB0-RB3 flag Accumulator
MSB of BCD set by
ALU operations hardware to 1
Operand if it contains
odd number
of ones
■ Each Machine Cycle is made up of 6 states. A state is basic time interval for
discrete operations of µcontroller such as Opcode fetch, decode an opcode,
Execute an opcode.Two oscillator pulses define a state.
■ Program Instructions may require 1/2/3/4 machine cycles, depending on its
type.
■ RST
■ It is an input and is active High (Normally Low) by applying
a High pulse to this pin, µc will be reset and terminate all
activities.
◻ On reset PC - 0000
■ ACC - 0000
■ B - 0000
■ PSW - 0000
■ SR - 0007
■ DPTR-0000
■
F0 B F7
E8 EF
E0 ACC E7
D8 DF
D0 PSW D7
C0 C7
B8 IP BF
B0 P3 B7
A8 IE AF
A0 P2 A7
98 SCON SBUF 9F
90 P1 97
■ Though 4banks of register are kept aside, micro controller at a time can access only
one register bank, where as other register banks will be used as simple RAM
locations.
■ By default RB0 is selected, on power ON.
■ We can switch to other banks, by using D4 & D3 bits of PSW.
■ State the contents of RAM locations after the following program.
■ SetB PSW4 select bank 2
■ Mov R0, #99h
■ Mov R1, #85h
■ Mov R2, #3Fh.
■ PC can access the memory from 0000H –FFFFH. Program address higher than
0FFFH, which exceed the internal ROM capacity will cause 8051
Timer
7/12/2024 24
Timer / counter programming in the 8051:
25 7/12/2024
Programming 8051 timers:
26 7/12/2024
The 16 bit register of T0 / T1 is accessed as
low byte and high byte (TH0 / TH1)
THO TLO
DB15 DB0
27 7/12/2024
TMOD register:
Both timers t0 and t1 use the same register called TMOD to set
the various timer operating modes.
28 7/12/2024
Gate: Gating control when set timer/counter is enabled only
while the INTx pin is high and TRx pin is set. When cleared, the
timer is enabled whenever the TRx control bit is set.
÷12
oscillator
30 7/12/2024
the frequency for the timer is always 1/12th of the frequency of
crystal attached to the 8051
1) 1/12 X 12 MHZ =1MHZ and T=1/1MHZ =1μs.
2) 1/12 X 16 MHZ=1.33MHZ and T=1/1.33MHZ=0.75μs
3) 1/12 X 11.0592 MHZ=921.6KHZ and
T=1/921.6kHz=1.085μs
31 7/12/2024
Influence of gate:
32 7/12/2024
The case of gate = 1 in TMOD:
33 7/12/2024
XTAL
oscillator % 12
C/ T=0
O O
C/ T =1
T 0 Pin
Pin 3.4
TR 0
Gate O
INT0 Pin
Pin3.2
34 7/12/2024
Mode 0:
This mode is compatible for MCS 48 family. The 13 bit
counter can hold values between 0000 to 1FFF in TH and
TL registers. Therefore, when the timer reaches its
maximum of 1FFF it rolls over to 0000 and TF is raised.
35 7/12/2024
Mode 1:
The characteristics of this mode are as follows:
1) it is a 16 bit timer. It allows values of 0000 to FFFFH
into timer register TH and TL.
2) After TH and TL are loaded with 16 bit initial value.
The timer must be started. This is done by SETB TR0 for
SETB TR1 for T1.
3) After timer is started, it starts to count up. It counts
up to FFFFH . when it rolls over from FFFFH to 0000, it
sets a flag bit timer flag(TF). This flag can be monitored.
When it is raised, one option is to stop the timer with the
instruction CLR TR0 or CLR TR1.
4)After the timer reaches its limit and rolls over, in order to
repeat the process the register TH and TL must be
reloaded with the original value and TF must be reset to 0.
36 7/12/2024
Xtal ÷12 TH TL TF
osc
37 7/12/2024
Mode 1 programming :
To generate a time delay, using time mode, the following steps are
taken:
∙ load the TMOD value register indicating which timer(T0 0r T1) is
to be used and which timer mode is selected.
∙ Load register TL and TH with initial count values.
∙ Start the timer.
∙ Keep monitoring the status of timer flag (TF). Come out of the
loop, when TF becomes HIGH.
∙ Stop the timer.
∙ Clear the TF flag for the next count.
∙ Go back to step 2.
.
38 7/12/2024
NAME EXAMPLE 11
ORG 0H
MOV TMOD,#01H
HERE: MOV TL0, #0F2 H 2
MOV TH0, #OFFH 2
CPL P1.5 1
ACALL DELAY 2
SJMP HERE 2
DELAY: SETB TR0 1
AGAIN: JNB TF0,AGAIN 14
CLR TR0 1
CLR TF0 1
RET 1
27
39 7/12/2024
In the above program, observe the following:
∙ TMOD is loaded.
∙ FFF2H is loaded into TH0-TL0.
∙ P1.5 is toggled for the high and low portions of the pulse.
∙ The DELAY subroutine , timer0 is started by SETB TR0
instruction.
∙ Timer0 counts up with the passing of each clock. As timer counts
up, it goes through FFF3, ….., FFFF, 0000 states . now TF0
becomes HIGH.
∙T0 is stopped by the instruction CLR TR0.And the process is
repeated.
40 7/12/2024
FFF2 FFF3 FFF4 FFFF 0000
41 7/12/2024
PROGRAMMING 8051 TIMERS
42
Example
In the following program, we are creating a square wave of 50% duty cycle
(with equal portions high and low) on the P1.5 bit.
Timer 0 is used to generate the time delay
43
Example
The following program generates a square wave on pin P 1.5 continuously using
Timer 1 for a time delay. Find the frequency of the square wave if XTAL = 11.0592
MHz. In your calculation do not include the overhead due to the timer setup
instructions in the loop.
44
PROGRAMMING 8051 TIMERS
45
Example
Assuming XTAL = 11.0592 MHz, write a program to generate a square wave of
50 Hz frequency on pin P2.3.
■ T = 1/50 Hz = 20 ms
■ 1/2 of it for the high and low portions of
the pulse = 10 ms
■ 10 ms / 1.085 us = 9216
■ 65536 - 9216 = 56320 in decimal =
DC00H
■ TL = 00 and TH = DCH
■ The calculation for 12MHz crystal uses
the same steps
46
Example (cont)
Assuming XTAL = 11.0592 MHz, write a program to generate a square wave of
50 Hz frequency on pin P2.3.
47
PROGRAMMING 8051 TIMERS
■ Mode 2 programming
◻ 8-bit timer, allows values of 00 to FFH
◻ TH is loaded with the 8-bit value
◻ a copy is given to TL
◻ timer is started by ,"SETB TR0" or "SETB TR1“
◻ starts to count up by incrementing the TL register
◻ counts up until it reaches its limit of FFH
◻ when it rolls over from FFH to 00, it sets high TF
◻ TL is reloaded automatically with the value in TH
◻ To repeat, clear TF
◻ mode 2 is an auto-reload mode
48
PROGRAMMING 8051 TIMERS
49
Example
Assuming that XTAL = 11.0592 MHz, find (a) the frequency of the square
wave generated on pin P1.0 and (b) the smallest frequency achievable in
this program, and the TH value to do that.
50
COUNTER PROGRAMMING
51
COUNTER PROGRAMMING
■ TCON register
◻ TR0 and TR1 flags turn on or off the timers
◻ bits are part of a register called TCON (timer control)
◻ upper four bits are used to store the TF and TR bits of both
Timer 0 and Timer 1
◻ lower four bits are set aside for controlling the interrupt bits
◻ "SETB TRl" and "CLR TRl“
◻ "SETB TCON. 6" and "CLR TCON. 6“
54
COUNTER PROGRAMMING
Table 9–2 Equivalent Instructions for the Timer Control Register (TCON)
56
serial communication
7/12/2024 57
Basics of Serial Communication
■ Computers transfer data in two ways:
◻ Parallel: Often 8 or more lines (wire conductors)
are used to transfer data to a device that is only a
few feet away.
8051 Microcontroller 58
Basics of Serial Communication
■ Serial data communication uses two methods
◻ Synchronous method transfers a block of data at
a time
■ The start bit is always one bit, but the stop bit
can be one or two bits
8051 Microcontroller 61
Data Transfer Rate
■ The rate of data transfer in serial data
communication is stated in bps (bits per second).
■ Another widely used terminology for bps is baud
rate.
◻ It is modem terminology and is defined as the number of
signal changes per second
◻ In modems, there are occasions when a single change of
signal transfers several bits of data
■ As far as the conductor wire is concerned, the
baud rate and bps are the same.
8051 Microcontroller 62
8051 Serial Port
■ Synchronous and Asynchronous
■ SCON Register is used to Control
■ Data Transfer through TXd & RXd pins
■ Four Modes of Operation:
8051 Microcontroller 63
TIMER 1 TH1 VALUES FOR VARIOUS BAUD RATES
4800 -6 FA
2400 -12 F4
1200 -24 E8
64
Registers related to Serial
Communication
1. SBUF Register
2. SCON Register
3. PCON Register
8051 Microcontroller 65
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.
8051 Microcontroller 66
SBUF Register
■ Sample Program:
8051 Microcontroller 67
SCON Register
8051 Microcontroller 68
8051 Serial Port – Mode 0
The Serial Port in Mode-0 has the following
features:
8051 Microcontroller 69
8051 Serial Port – Mode 1
The Serial Port in Mode-1 has the following
features:
8051 Microcontroller 70
8051 Serial Port – Mode 2
The Serial Port in Mode-2 has the
following features:
8051 Microcontroller 73
Programming Serial Data Reception
1. TMOD register is loaded with the value 20H, indicating the use of
timer 1 in mode 2 (8-bit auto-reload) to set baud rate.
2. TH1 is loaded to set baud rate
3. The SCON register is loaded with the value 50H, indicating serial
mode 1, where an 8- bit data is framed with start and stop bits.
4. TR1 is set to 1 to start timer 1
5. RI is cleared by CLR RI instruction
6. The RI flag bit is monitored with the use of instruction JNB RI, xx
to see if an entire character has been received yet
7. When RI is raised, SBUF has the byte, its contents are moved
into a safe place.
8. To receive the next character, go to step 5.
8051 Microcontroller 74
Doubling Baud Rate
■ There are two ways to increase the baud rate
of data transfer
1. By using a higher frequency crystal
2. By changing a bit in the PCON register
•We can set it to high by software and thereby double the baud rate.
8051 Microcontroller 75
Doubling Baud Rate (cont…)
8051 Microcontroller 76
8051
Interrupts
8051 Microcontroller 77
INTERRUPTS
8051 Microcontroller 79
Interrupt Vs Polling
■ The polling method is not efficient, since it wastes much of
the microcontroller’s time by polling devices that do not need
service.
■ The advantage of interrupts is that the microcontroller can
serve many devices (not all at the same time).
■ Each devices can get the attention of the microcontroller
based on the assigned priority.
■ For the polling method, it is not possible to assign priority
since it checks all devices in a round-robin fashion.
◻ Timer 0 Overflow.
◻ Timer 1 Overflow.
◻ Reception/Transmission of Serial Character.
◻ External Event 0.
◻ External Event 1.
8051 Microcontroller 83
8051 Interrupt Vectors
8051 Microcontroller 84
8051 Interrupt related Registers
■ The various registers associated with the use of
interrupts are:
◻ TCON - Edge and Type bits for External Interrupts 0/1
8051 Microcontroller 85
Enabling and Disabling an Interrupt
■ Upon reset, all interrupts are disabled (masked),
meaning that none will be responded to by the
microcontroller if they are activated.
8051 Microcontroller 86
Interrupt Enable (IE) Register
--
• EA : Global enable/disable.
• --- : Reserved for additional interrupt hardware.
8051 Microcontroller 88
Interrupt Priority
■ When the 8051 is powered up, the priorities are assigned
according to the following.
8051 Microcontroller 89
Interrupt Priority
■ We can alter the sequence of interrupt priority by assigning a
higher priority to any one of the interrupts by programming a
register called IP (interrupt priority).
■ To give a higher priority to any of the interrupts, we make the
corresponding bit in the IP register high.
8051 Microcontroller 90
Interrupt Priority (IP) Register
Serial Port
INT 0 Pin
Timer 1 Pin
92
•The following is a program to transfer letter ‘A’ serially at
4800 baud , continuously. Observe the status of serial
window, serial port and timer port for results.
NAME EXAMPLE 16
ORG OH
MOV TMOD,#20H; Timer 1, mode 2(auto reload)
MOV TH1,# -6 H; 4800 baud rate
MOV SCON,#50H; 8 BIT,1 Stop, REN enabled
SETB TR1; Start timer 1
AGAIN: MOV SBUF, #”A”; letter A to be transferred
HERE: JNB TI,HERE ; Wait for the last bit
CLR TI; clear T1 for next char
SJMP AGAIN ; keep sending A
END
93
• The significance of TI flag: once the timer is on
TI is reset(=0). When the stop bit is transferred,
8051 rises the TI flag is set, indicating that the last
character was transmitted and ready to transfer
next character. By monitoring TI flag we make sure
that we are not overloading the SBUF register.
When 8051 finishes transferring a byte, it raises
the TI flag to indicate it is ready for the next
character. After SBUF is loaded with a new byte,
TI flag is forced to zero to transmit the new
byte.
.
94
The following is a program to transfer a message ‘YES’
serially at 9600 baud, 8-bit data,1-stop bit , continuously.
Observe the results on serial window, serial port, and timer
1.
NAME EXAMPLE 17
ORG 0H
MOV TMOD,#20H; timer 1, mode2
MOV TH1,#-3H; 9600 baud rate
MOV SCON,#50H; 8-BIT, 1-stop bit, REN
enabled
SETB TR1; start timer 1
AGAIN: MOV A, #”Y” ; Transfer ‘y’
ACALL TRANS
MOV A,#”E”
ACALL TRANS
MOV A,#”S”; TRANSFER S
95
ACALL TRANS
SJMP AGAIN ; Keep doing it
: serial letter transfer subroutine
TRANS: MOV SBUF,A; Load SBUF
HERE: JNB TI,HERE ; wait for the last bit to
transfer
CLR TI ; Get ready for next byte
RET
END
96
PROGRAMMING THE 8051 TO RECEIVE DATA SERIALLY
To transfer character bytes serially, the following steps must be
followed:
the TMOD register loaded with the value 20h, indicating the timer
1 in mode 2 to the baud rate.
The TH1 is loaded to set the baud rate for serial data transfer.
The SCON register is loaded with the value 50h,indicaitng the
mode 1 , where an 8-bit data is started with start and stop bits.
TR1 is set to start timer1.
RI is cleared by software.
The RI bit is monitored to whether entered character is received or
not.
When RI is raised, SBUF has the byte, move its contents to a safe
place.
To transfer next character, go to step 5. 97
The importance of RI flag : when the stop bit is
received during receiving, 8051 makes RI=1,
indicating that an entire byte is received.
101