MP & MC Module-4
MP & MC Module-4
MODULE-4
Microcontroller 8051 Peripherals
V.PRAKASH
Asst. Professor(Sr.), SENSE,
VIT Chennai
➢ Hence, total 32 input/output pins allow the microcontroller to be connected with the
peripheral devices.
➢ Except port 1 all other ports are used for dual purpose
➢ All ports are bidirectional and they are constructed with a D type output latch.
➢ All the ports upon RESET are configured as input, ready to be used as input ports.
➢ Dual role of port-0: Port 0 is also designated as AD0-AD7, allowing it to be used for both
address and data. If ALE=1, then it is address or else data.
➢ Port 1
➢ Port 1 occupies a total of 8 pins (pins 1-8) and it can be used for input or output.
➢ In contrast to port 0, this port does not need any pull-up resistors since it already has
pull-up resistors internally.
➢ Just like P1, P2 does not need any pull-up resistors since it already has pull-up resistors
internally.
➢ Port 2 is also designated as A8 –A15, indicating its dual function and Port 0 provides the
lower 8 bits via A0 –A7.
➢ Port 3
➢ Port 3 occupies a total of 8 pins, (pins 10-17) and it can be used as input or output.
➢ P3 does not need any pull-up resistors, the same as P1 and P2 did not.
➢ Port 3 has the additional function of providing some extremely important signals such as
interrupts.
MODULE-4 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 6
I/O PORTS
➢ I/O Pin’s Current Limitations:
➢ When configured as outputs (logic 0), single
port pins can receive a current of 10mA.
PORT 0 BITS P0.7 P0.6 P0.5 P0.4 P0.3 P0.2 P0.1 P0.0
BIT ADDRESS 87H 86H 85H 84H 83H 82H 81H 80H
PORT 1 BITS P1.7 P1.6 P1.5 P1.4 P1.3 P1.2 P1.1 P1.0
BIT ADDRESS 97H 96H 95H 94H 93H 92H 91H 90H
PORT 2 BITS P2.7 P2.6 P2.5 P2.4 P2.3 P2.2 P2.1 P2.0
BIT ADDRESS A7H A6H A5H A4H A3H A2H A1H A0H
PORT 3 BITS P3.7 P3.6 P3.5 P3.4 P3.3 P3.2 P3.1 P3.0
BIT ADDRESS B7H B6H B5H B4H B3H B2H B1H B0H
Exercise: Write an 8051 assembly code to generate a square wave of 25% duty cycle on bit 0 of port 1.
Exercise: A switch is connected to pin P1.0 and an LED to pin P2.7. Write an 8051program to get the status
of the switch and send it to the LED
MODULE-4 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 12
TIMERS / COUNTERS
13
➢ 8051 has two timers/counters registers (Timer 0 and Timer 1), each 16-bits wide and
accessed as two separate registers of low byte and high byte
✓ The low byte register is called TL0/TL1 and
✓ The high byte register is called TH0/TH1
➢ Both timers 0 and 1 use the same register, called TMOD (timer mode), to set the various
timer operation modes
➢ Software: First set GATE=0, then start and stop of the timer are controlled by the TR(timer
start) bits TR0 and TR1. The SETB starts it, and it is stopped by the CLR instruction.
➢ Hardware: The starting and stopping the timer by an external source is achieved by
making GATE=1 in the TMOD register.
MODULE-4 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 18
TIMERS / COUNTERS
TMOD REGISTER
Gating Control:
▪ If GATE=1 – Timer/Counter is
enable only while INTx pin is HIGH M1 M0 Operating Mode
and the TRx control pin is SET 13-bit Timer Mode (Mode-0): 8-bit Timer/Counter
0 0
THx with TLx as 5-bit prescaler
▪ If GATE=0, the timer is enabled 16-bit Timer Mode (Mode-1): 16-bit Timer/Counter
whenever the TRx control bit is set 0 1
THx and TLx are cascaded, no prescaler
➢ This is one of the rarities being kept only for the purpose of compatibility with the
previous versions of microcontrollers.
➢ This mode configures timer 0 as a 13-bit timer which consists of all 8-bits of TH0 and the
lower 5-bits of TL0.
➢ How does it operate? Each coming pulse causes the lower register bits to change their
states. After receiving 32 pulses, this register is loaded and automatically cleared, while
the higher byte (TH0) is incremented by 1.
➢ This process is repeated until registers count up 8192 pulses. After that, both registers are
cleared and counting starts from 0
➢ It is a 16-bit timer; therefore it allows values from 0000 to FFFFH to be loaded into the
timer’s registers TL and TH.
➢ After TH and TL are loaded with a 16-bit initial value, the timer must be started.
➢ After the timer is started. It starts count up until it reaches its limit of FFFFH.
➢ When it rolls over from FFFF to 0000H, it sets high a flag bit called TF (timer flag).
➢ When a timer is in mode 2, THx holds the "reload value" and TLx operates as a timer.
➢ Thus, TLx starts counting up. When TLx reaches 255 and is subsequently incremented,
instead of resetting to 0 (as in the case of modes 0 and 1), it will be reset to the value
stored in THx.
➢ The auto-reload mode is very commonly used for establishing a baud rate.
➢ In order to register each 55th pulse, the best solution is to write the number 200 to the
TH0 register and configure the timer to operate in mode 2.
➢ Mode 3 configures timer 0 so that registers TL0 and TH0 operate as separate 8-bit timers.
➢ In other words, the 16-bit timer 0 consisting of two registers TH0 and TL0 is split into two
independent 8-bit timers.
➢ The TL0 timer turns into timer 0, while the TH0 timer turns into timer 1.
➢ In addition, all the control bits of 16-bit Timer 1 (consisting of the TH1 and TL1 register),
now control the 8-bit Timer 1.
➢ While Timer 0 is in split mode, you may not start or stop the real timer 1 since the bits that
do that are now linked to TH0.
MODULE-4 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 27
TIMERS / COUNTERS
TIMER 0 MODE 3 (SPLIT TIMER)
2. Load registers TL and TH with initial count value. MOV TH0,#FFH; MOV TL0,#FCH.
4. Keep monitoring the timer flag (TF). AGAIN: JNB TF0, AGAIN.
Solution:
• To get the largest delay we make TL and TH both 0. This will count up from 0000 to
FFFFH and then roll over to zero
• Making TH and TL both zero means that the timer will count from 0000 to FFFF, and
then roll over to raise the TF flag.
• Since XTAL = 11.0592 MHz, the counter counts up every 1.085 us.
• We must identify how many times 1.085 us intervals should be repeated to achieve
5 ms delay.
• To Achieve that we need to load into TL and TH the value 65536 – 4608 = 60928.
• Convert 60928 into HEX i.e EE00. Therefore, we have TH = EE and TL = 00.
ACALL DELAY
SJMP BACK
Exercise: Generate a square wave with TON of 5ms and TOFF of 2ms on all pins of port 0.
Assume an XTAL of 11.0592 MHz.
MODULE-4 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 34
TIMERS / COUNTERS
EXAMPLE-3
Assuming XTAL = 22MHz write a program to generate a square pulse of frequency 100kHz
on port pin P1.2 using timer-1 mode 2.
For 22MHz , one timer cycle time is MOV TMOD, #20H
• 22MHz / 12 = 1.83MHz MOV TH1, #0F7H
• T = 1/1.83M = 0.546 μs
SETB TR1
For 100kHz square wave,
BACK: JNB TF1, BACK
• T=1/f = 0.01ms = 10μS
CPL P1.2
• TON = TOFF = 10uS/2 = 5μS
CLR TF1
• 5 μs/ 0.546 μs = 9 cycles
• 256 – 9 = 247 = F7H
SJMP BACK
Exercise: Assume that XTAL = 11.0592 MHz, write a 8051 program to generate a square wave
of 2 kHz frequency on pin P1.7
MODULE-4 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 35
TIMERS / COUNTERS
COUNTERS
➢ Timers can also be used as counters counting events happening outside the 8051
➢ In counter, it is a pulse outside of the 8051 that increments the TH, TL registers
➢ TMOD and TH, TL registers are the same as for the timer discussed previously
➢ Programming the timer in the last section also applies to programming it as a counter
except the source of the frequency
➢ The C/T bit in the TMOD registers decides the source of the clock for the timer
➢ When C/T = 1, the timer is used as a counter and gets its pulses from outside the 8051
➢ The counter counts up as pulses are fed from pins 14 and 15, these pins are called T0
(timer 0 input) and T1 (timer 1 input).
ORG 0000H
SETB P3.5 ; make T1 input
MOV P1,#00H ; Make Port 1 as output port
MOV TMOD, #60H ; counter 1, mode 2;C/T=1 external pulses
MOV TH1, #0 ; clear TH1
AGAIN: SETB TR1 ; start the counter
BACK: MOV A, TL1 ; get copy of TL
MOV P1, A ; display it on port 2
JNB TF1, BACK ; keep doing, if TF=0
CLR TR1 ; Stop the counter 1
CLR TF1 ; make TF=0
SJMP AGAIN ; keep doing it
39
➢ To reduce the number of pins in a package, many ICs use a serial bus to transfer data
when speed is not important.
➢ Some examples of such low-cost serial buses include UART, USART, SPI, I²C etc.,
➢ There are special IC chips made by many manufacturers for serial communications
▪ UART (universal asynchronous Receiver-transmitter)
▪ USART (universal synchronous-asynchronous Receiver-transmitter)
MODULE-4 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 40
SERIAL COMMUNICATION
➢ 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 and is defined as the number of
signal changes per second. The baud rate and bps are the same, used inter changeably.
➢ 8051 has two pins (11 and 10) that are used specifically for transferring (Tx) and
receiving(Rx) data serially as a part of the port 3 group (P3.1 and P3.0).
➢ To allow data transfer between two UART devices, we must make sure that the baud rate
of both devices matches.
➢ Different baud rate levels are, 150, 300, 600, 1200, 2400, 4800, 9600, 19200 etc.,
➢ With XTAL=11.0592 MHz, find the TH1 value needed to set different baud rate.
▪ 28800/3 = 9600 Where -3=FD (hex) is loaded into TH1
▪ 28800/6 = 4800 Where -6=FA (hex) is loaded into TH1
▪ 28800/12 = 2400 Where -12=F4 (hex) is loaded into TH1
▪ 28800/24 = 1200 Where -24=E8 (hex) is loaded into TH1
➢ SBUF is an 8-bit register used to hold a data during transmit and receive operation
MOV SBUF, #‘D’ ; load SBUF=44H, ASCII for ‘D’
MOV SBUF, A ; copy Accumulator into SBUF
MOV A, SBUF ; copy SBUF into Accumulator
➢ This register contain not only the mode selection bits but also the 9th data bit for transmit
and receive (TB8 and RB8) and the serial port interrupt bits (TI and RI).
2. The TH1 is loaded with one of the values to set baud rate for serial data transfer
3. Load SCON with 50H, indicating mode 1, where an 8-bit data is framed with start and stop bits
6. The TI flag bit is monitored using JNB TI, xx to check the character is transferred completely or not
ORG 0000H
MOV TMOD,#20H ;timer1,mode 2(auto reload)
MOV TH1,#-6 ;4800 baud rate
MOV SCON,#50H ;8-bit, 1 stop, REN enabled
SETB TR1 ;start timer 1
AGAIN: MOV SBUF, #‘A’ ;letter “A” to transfer
HERE: JNB TI,HERE ;wait for the last bit
CLR TI ;clear TI for next char
SJMP AGAIN ;keep sending A
2. The TH1 is loaded with one of the values to set baud rate for serial data transfer
3. Load SCON with 50H, indicating mode 1, where an 8-bit data is framed with start and stop bits
5. The RI flag bit is monitored using JNB RI, xx to check the character is received completely or not
56
➢ Polling can monitor the status of several devices and serve each of them as certain
conditions are met
▪ The polling method is not efficient, since it wastes much of the microcontroller’s time by
polling devices that do not need service, ex. JNB TF, target
➢ 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
➢ The advantage of interrupts is that the microcontroller can serve many devices (not all at
the same time).
MODULE-4 BECE204L – MICROPROCESSORS AND MICROCONTROLLERS 57
INTERRUPTS
➢ 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 memory location called 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
▪ The problem with this method is that the microcontroller is tied down while waiting for
TF to be raised, it can’t do anything else
▪ Using interrupts solves this problem and, avoids tying down the controller
Delay calculation: Use timer 0 in mode 2 (auto reload). TH0 = 100/1.085 us = 92 =A4H
;--upon wake-up go to main, avoid using memory allocated to Interrupt Vector Table
ORG 0000H
LJMP MAIN ;by-pass interrupt vector table
➢ The interrupt vector table locations 0003H and 0013H are set aside for INT0 and INT1
➢ There are two activation levels for the external hardware interrupts
1. In Level trigged Mode, INT0 and INT1 pins are normally high, if a low-level signal is
applied to them, it triggers the interrupt
ORG 0000H
LJMP MAIN
ORG 30H
MAIN: SETB P3.3 //Delay of 500ms
CLR P1.3 DELAY: MOV R2,#04H ;LOAD R2 WITH 07 HEX
CLR P1.5 HERE3: MOV R1,#0FFH ;LOAD R1 WITH 0FF HEX
MOV IE,#10000100B HERE2: MOV R0,#0FFH ;LOAD R2 WITH 0FF HEX
HERE: CLR P1.3 HERE1: DJNZ R0,HERE1 ;DECREMENT R0
SETB P1.5 DJNZ R1,HERE2 ;DECREMENT R1
ACALL DELAY DJNZ R2,HERE3 ;DECREMENT R2
CLR P1.5 RET ;RETURN
ACALL DELAY END
SJMP HERE
Exercise: Write a program in which the 8051 reads data from P1 and writes it to P2 continuously while giving a copy
of it to the serial COM port to be transferred serially. Assume that XTAL=11.0592. Set the baud rate at 9600.
ORG 0
LJMP MAIN
ORG 23H
LJMP SERIAL ;jump to serial interrupt ISR