0% found this document useful (0 votes)
4 views31 pages

Unit III Different Peripherals (IO and Timers)

The document provides an overview of the 8051 microcontroller's I/O ports, detailing sourcing and sinking concepts, and the specifications of the ports. It also covers programming timers, including modes of operation and steps to generate specific delays, as well as examples of interfacing LEDs and switches using assembly language and embedded C programs. Key programming steps and calculations for generating square waves and controlling LEDs based on switch input are included.

Uploaded by

arjadhav06
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views31 pages

Unit III Different Peripherals (IO and Timers)

The document provides an overview of the 8051 microcontroller's I/O ports, detailing sourcing and sinking concepts, and the specifications of the ports. It also covers programming timers, including modes of operation and steps to generate specific delays, as well as examples of interfacing LEDs and switches using assembly language and embedded C programs. Key programming steps and calculations for generating square waves and controlling LEDs based on switch input are included.

Uploaded by

arjadhav06
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIT III

DIFFERENT PERIPHERALS (I/O AND


TIMERS)
SOURCING AND SINKING, SPECIFICATION AND
ISOLATION

The 8051 has four 8-bit I/O ports (P0-P3), each with specific
characteristics.
Sourcing vs. Sinking:
⚫ Sourcing: The port pin provides current ( ) to a
device (e.g., LED anode connected to pin).

⚫ Sinking: The port pin provides a ground path ( ),


allowing current to flow into the microcontroller (e.g., LED
cathode connected to pin). Port 0 requires external pull-up resistors
for sourcing, while ports 1-3 have internal pull-ups.

Specification: I/O pins are TTL-compatible and can typically


handle a limited amount of current (typically a few mA per
pin).
PORT 0 STRUCTURE
PORT 1 STRUCTURE
PORT2 STRUCTURE
PORT3 STRUCTURE
PROGRAMMING 8051 TIMERS

Timer 0 Registers
PROGRAMMING 8051 TIMERS

Timer 1 Registers
TCON REGISTER
TMOD REGISTER
MODE 0
MODE 1
MODE 2
MODE 3
Mode 3 is also known as a split timer mode. Timer 0 and 1 may be
programmed to be in mode 0, 1 and 2 independently of similar mode for
other timer.

This is not true for mode 3; timers do not operate independently if mode 3 is
chosen for timer .

Placing timer 1 in mode 3 causes it to stop counting; the control bit TR1 and
the timer 1 flag TF1 are then used by timer 0.
TIMER PROGRAMMING

MODE 1 PROGRAMMING: It is a 16 bit Timer mode.

STEPS TO PROGRAM IN MODE 1:


1. Load the TMOD value register with mode and timer 0 or 1.
2. Load registers TLx and THx with initial count corresponding to delay.
3. Start the timer.
4. Continuously monitor the timer flag (TFx) if it is raised, if it is
raised(TFx=1) then get out of the loop.
5. Stop the timer.
6. Clear the TFx flag for the next round.
7. Go back to Step 2 to load THx and TLx again.
STEPS TO CALCULATE COUNT TO LOAD INTO THX-TLX TO
GENERATE DESIRED DELAY

(Assume XTAL = 11.0592 MHz)

Steps for finding the TH, TL registers values


▪ Divide the desired time delay by 1.085 us
▪ Perform 65536 – n, where n is the decimal value we got in Step 1
▪ Convert the result of Step 2 to hex, say we get yyxx.
▪ yyxx is the initial hex value to be loaded into the timer’s register.
▪ Set TL = xx and TH = yy
PROGRAM TO GENERATE A SQUARE WAVE OF 5 KHZ FREQUENCY ON PIN P1.0

CLOCK FREQUENCY =11.0592 MHZ

▪ Square wave frequency=5 kHz


▪ Clock frequency=11.0592 MHz
▪ Step 1: Calculate the Time delay T=1/f=1/5 kHz =0.2 ms T=0.2 ms which is
the period of square wave T/2 =0.2/2=0.1 ms delay for high and low
▪ Step 2: Divide the desired time delay by 1.085 us Count=0.1ms/1.085 us =
921
▪ Step 3: Perform 65536 – n TH0-TL0= 65536-921=64615=FC67H
▪ Step 4: Set TL = xx and TH = yy Here xx=FC and yy=67. Hence,TH0=FCh,
TL0=67h
MOV TMOD,#01
AGAIN: MOV TL1,#67H
MOV TH1,#0FCH
SETB TR1
CPL P1.0
CLR TR1
CLR TF1
SJMP AGAIN
INTERFACING LED TO I/O PORTS
ASSEMBLY LANGUAGE PROGRAM
ORG 0000H ; Start of program
START:
MOV P0, #00H ; Initialize Port 0 (all LEDs OFF assuming active HIGH)
MAIN_LOOP:

MOV P0, #0FFH ; Turn ON all LEDs


ACALL DELAY
MOV P0, #00H ; Turn OFF all LEDs
ACALL DELAY
SJMP MAIN_LOOP ; Repeat forever
; -------- Delay Subroutine --------
DELAY:
MOV R2, #0FFH
D1: MOV R1, #0FFH
D2: DJNZ R1, D2
DJNZ R2, D1
RET
END
EMBEDDED C PROGRAM
include <reg51.h>

void delay() {
unsigned int i, j;
for(i = 0; i < 500; i++) {
for(j = 0; j < 500; j++);
}
}

void main() {
while(1) {
P0 = 0xFF; // Turn ON all LEDs (active HIGH)
delay();

P0 = 0x00; // Turn OFF all LEDs


delay();
}
}
INTERFACING OF SWITCH AND LED TO 8051
ASSEMBLY LANGUAGE PROGRAM
ORG 0000H ; Start of program

MAIN:
SETB P1.1 ; Configure P1.1 as input (write 1 to use as input)

CHECK_SWITCH:
JB P1.1, LED_OFF ; If switch = 1 (not pressed), jump to LED_OFF

; If switch is pressed (P1.1 = 0)


SETB P2.1 ; Turn ON LED
SJMP CHECK_SWITCH

LED_OFF:
CLR P2.1 ; Turn OFF LED
SJMP CHECK_SWITCH ; Keep checking switch

END
EMBEDDED C PROGRAM

#include <reg51.h>

sbit SWITCH = P1^1; // Switch connected to P1.1


sbit LED = P2^1; // LED connected to P2.1

void main()
{
SWITCH = 1; // Configure P1.1 as input

while(1)
{
if(SWITCH == 0)
{
LED = 1; // Turn ON LED
}
else
{
LED = 0; // Turn OFF LED

}}}

You might also like