0% found this document useful (0 votes)
16 views93 pages

Embedded Systems & Micro-Controllers-1-01-12-2020

Uploaded by

kk950856
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)
16 views93 pages

Embedded Systems & Micro-Controllers-1-01-12-2020

Uploaded by

kk950856
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
You are on page 1/ 93

Embedded Systems & Micro-controllers

ECL 3091 (3-0-0) = 3 Credits

DR. SUMEET GUPTA


ASSOCIATE PROFESSOR
SCHOOL OF ELECTRONICS & COMMUNICATION ENGINEERING
SHRI MATA VAISHNO DEVI UNIVERSITY, KATRA
Syllabus
Introduction: Introduction to Embedded Computing, Issues and Challenges in Embedded
System Design, Trends: SoC, custom designed chips, configurable processors and multi-core
processors.
Embedded Processor Architecture (Intel 8051 Platform-8 bit): Harvard Architecture, RISC
v/s CISC, µProcessor v/s µController, CPU Architecture and instruction sets : Hardware
architecture- program memory consideration – register file structure and addressing modes
– CPU Register – instruction set – Port architecture, Timer/Counter Block Configuration &
Interrupts, Serial Port Configuration & Interrupts, External interrupts
Embedded Processor Architecture (Freescale S12X Platform-16 bit): Introduction to the
S12X Microcontroller, Core Architecture, Clock Generation & Resets, Port Architecture,
Timer functions, Serial Communication Interface (SCI), Serial Peripheral Interface (SPI),
Inter-Integrated Circuit (I2C) Interface, Interrupts, Analog-to-Digital Converter,
Embedded Applications & Interfacing: Embedded System Applications using Keyboards,
display, Relays, Motors, Sensor Interface, ADC, DAC, SCI, SPI, RTC, I2C, Interrupts with 8051
& S12X
Course Outcome
CO No. Course Outcome

CO1 Understand the architecture of 8-bit/16-bit Microcontrollers & related


peripherals
CO2 Understand and implement principles of Embedded Design

CO3 Do Hardware Interface Design using 8-bit and 16-bit Microcontrollers

CO4 Write code in Assembly language or Embedded C for 8-bit and 16-bit
Microcontroller
Pedagogy
1. Flipped Class – Pre-loaded Video Lectures which you will view before coming
to the live class
2. Live Classes - For discussions and doubt clearing
3. Quizzes – Multiple Choice Types
4. Assignments - Zero plagiarism policy – Copying will get you a zero
5. Lab Work – To the extent possible
6. Project – Simulation Based
Microprocessors v/s Microcontrollers
8051 Platform Block Diagram
8051 Platform

Consists of:
1. CPU + ALU + Registers
2. 4K ROM
3. 128 Bytes of RAM
4. 4 Parallel Ports viz Port 0, 1,2 & 3, each of 8 bits
so 32 Port lines
5. 01 Serial Port
6. 01 Interrupt Controller
7. 02 Timers/Counters viz Timer 0 & Timer 1
8051 Pin Out Description (EA/)
The Program written for a 8051 microcontroller can be fully stored:
1. In an external ROM outside 8051
2. or it can be stored partially in 8051 ROM and some part in external ROM

Q. After turning ON the 8051, how does the CPU know where the first instruction is to be
fetched from?
Ans. The CPU of 8051 fetches the first instruction from 0000H. The CU looks at the voltage level
of the EA/ pin (External Access Bar).
If this pin is externally connected to Ground, the CPU, at start-up knows that the first instruction
is to be fetched from address 0000H which is in External ROM
If this pin is externally connected to Supply, the CPU, at start-up knows that the first instruction
is to be fetched from address 0000H which is in Internal ROM
8051 Pin Out Description (EA/)

If this pin is externally connected to Supply, the CPU, at start-up If this pin is externally connected to Ground, the CPU, at
knows that the first instruction is to be fetched from address start-up knows that the first instruction is to be fetched
0000H which is in Internal ROM. Subsequent instructions from address 0000H which is in External ROM. Internal
could be fetched from Internal or External ROM ROM is not used at all in this case.
8051 Pin Out Description (PSEN/)
1. The PSEN/ (Program Status Enable) signal is an output signal generated by the
microcontroller
2. This signal is generated (becomes low) whenever the microcontroller fetches an
instruction or data from the ROM
3. Because of the presence of the PSEN/ signal, it is possible to interface a 64KB ROM
& a 64KB RAM with the 8051 microcontroller at the same time even though the
number of Address Lines is 16 only.

Question: Will the PSEN/ signal be activated if the micro-controller featches the
instructions and data from internal ROM?
8051 Pin Out Description (ALE)
1. The ALE (Address Latch Enable) signal is an output signal generated by the
microcontroller
2. This signal is generated by the microcontroller to enable the de-multiplexing
the Multiplexed Address-Data Bus (AD0-AD7)
3. The behaviour of the ALE signal is exactly the way it was in the 8085
microprocessor
8051 Pin Out Description (RST)
1. The RST (Reset) signal is an input signal applied to the microcontroller
2. For reset to happen, the reset input pin (pin 9) must be active high for at-least
2 machine cycles.
During a reset operation :-
A) Program counter is cleared and it starts from 00H,
B) Register bank #0 is selected as default,
C) Stack pointer is initialized to 07H,
D) All ports are written with FFH.
8051 Interface with External RAM & ROM
8051 Interface with External RAM & ROM

Steps during Memory Write


Lets take the example of execution of STA 800H of 8085 Microprocessor – Store the contents of the
Accumulator (ACC) into memory location 8000H
Step 0: Initially AD7-AD0 is in tri-state, A15-A8 is in tri-state, ALE is Zero, WR signal is 1
Step 1: Microprocessor puts 80H on A15-A8 & 00H on AD7-AD0
Step 2: ALE pulse is generated. Because of this 00H is stored in the Latch and on A7-A0
Step 3: Thus the address 8000H is now available on A15-A0
Step 4: The contents of ACC are now put in the AD7-AD0 bus i.e. D7-D0 Bus
Step 5: Now WR signal is made active i.e. 0
Step 6: In response to this the data on the Data Bus D7-D0 is stored in memory location 8000H
Step 7: AD7-AD0 goes to tri-state
Step 8: Address Bus A15-A8 also becomes tri-state (High Impedance State – Neither 0 Nor 1)
What are Ports in a Microcontroller?

Ports are represented by registers inside the microcontroller, and allow the
program to control the state of the pins, or to read the state of the pins if they
are configured as inputs.
08 Port Pins

There is a one-to-one correspondence between


the pins on the microcontroller and the bits in its
registers
8 bit
Register
Comparison between Parallel Port & Data Bus
Parallel Ports are static in nature while Data buses are transient in nature
Lets see what it means when we say Data Bus is transient
1. Normally Data Bus is in tristate (High Impedance State – Neither 0 Nor 1)
2. Data Bus is used to fetch and execute an instruction
3. Suppose the instruction STA 8000H is to be fetched and executed.
4. Let’s assume this instruction is stored at ROM locations 2050H, 2051H & 2052H
Address ROM Locations Containing the Instruction Address RAM Location
2050H 3AH 8000H XXH
2051H 50H
2052H Accumulator
20H
90H
Timing diagram for STA 8000H
8051 Interface with External RAM & ROM
Comparison between Parallel Port & Data Bus

Now Suppose we want to have a Red LED turn ON for 10 seconds and Turn OFF
for 10 seconds and repeat the cycle.

8 Bit Data Not possible as the 8 Bit Port Easy to implement as


Bus content on the Data Register data once stored in the
line is transient and Latch will remain till
changing continuously the time it is changed.
Parallel Port- In put Port Line OR Output Port Line

Can you Identify whether the Port Lines in both the diagram are Output lines
or Input Lines OR Both?
Vcc

8 Bit 8 Bit Port


Register Register
Parallel Port- In put Port Line OR Output Port Line

Can you Identify whether the Port Line in this case is Output line or Input Line
OR Both?
Vcc

8 Bit
Register
Port 0 of 8051

1. Port 0 is called a truly bi-directional Port


2. Individual lines can be configured as Input or Output Port Lines
3. External Pull-up Resistors are required if the Port line is to be configured as Output Line
4. Alternate function of the Port 0 lines is AD7-AD0
Therefore the Port 0 line can be
1. Configured to work as an output Port Line
2. Configured to work as an input Port Line
3. Configured to work as AD7-Ado line which is bi-directional
Structure of Port 0
Inside the IC

Port Pin
Port 0 – Port Line As Input Port Line
Inside the IC

SWITCH OFF
0 Port Pin
Buffer is OFF A
0
1 1 1
B
0 0 Read Data from Pin
SWITCH OFF

Buffer is ON
Port 0 – Port Line As Output Port Line – Output ‘1’
Inside the IC

SWITCH OFF
0 Port Pin
Buffer is OFF
0
1 1 1
B
0 0 Read Data from Pin
SWITCH OFF

Can you see the Problem?


Buffer is OFF
Port 0 – Port Line As Output Port Line – Output ‘1’
Vcc

Inside the IC
External
Pull-Up
SWITCH OFF Resistor
0 Port Pin
Buffer is OFF
0
1 1 1
B
0 0 Read Data from Pin
SWITCH OFF

Problem is fixed. Output is 1


Buffer is OFF
Port 0 – Port Line As Output Port Line – Output ‘0’
Vcc

Inside the IC
External
Pull-Up
SWITCH OFF Resistor
0 Port Pin
Buffer is OFF
0
0 0 0
B
1 1 Read Data from Pin
SWITCH ON

Problem is Fixed. Output is 0


Buffer is OFF
Port 1 – Port Line As Output Port Line, Output ‘0’
Inside the IC

SWITCH OFF
0 Port Pin
Buffer is OFF
0
1 1 1

0 0
SWITCH OFF

Buffer is ON
Port 1 – Port Line As Output Port Line, Output ‘1’
Inside the IC

SWITCH OFF
0 Port Pin
Buffer is OFF
0
1 1 1

0 0
SWITCH OFF

Buffer is ON
Port 1 – Port Line As Input Port Line,
Inside the IC

SWITCH OFF
0 Port Pin
Buffer is OFF
0 A
1 1 1

0 B
0
SWITCH OFF

Buffer is ON
Port 1 – Port Line As Output Port Line, Output ‘0’
Inside the IC

SWITCH OFF
0 Port Pin
Buffer is OFF
0
1 1 1

0 0
SWITCH OFF

Buffer is ON
Port 1 – Port Line As Output Port Line, Output ‘1’
Inside the IC

SWITCH OFF
0 Port Pin
Buffer is OFF
0
1 1 1

0 0
SWITCH OFF

Buffer is ON
Port 1 – Port Line As Input Port Line,
Inside the IC

SWITCH OFF
0 Port Pin
Buffer is OFF
0 A
1 1 1

0 B
0
SWITCH OFF

Buffer is ON
Write a program in Embedded C for 8051 such that it performs
the sum of two numbers already stored at memory location
2000H & 2002H and store the sum at 2004H
xdata unsigned int a _at_ 0x2000;
xdata unsigned int b _at_ 0x2002;
xdata unsigned int c _at_ 0x2004;

Void main()
{ c=a+b;
}
Timer /Counter Block in 8051
Timer /Counter Block in 8051

Timer 0 is made up of two 8 bit Synchronous UP counters called TL0 and TH0
They can be used independently as two separate 08 bit registers or together as a single 16 bit register

Timer 1 is made up of two 8 bit Synchronous UP counters called TL1 and TH1
They can be used independently as two separate 08 bit registers or together as a single 16 bit register
Timer /Counter Block in 8051 – TMOD Register

The mode of working of the Timer/Counter Block can be defined by using Bits in the TMOD Register
If C/T = 0 then the block works as Timer i.e. it counts the number of Clock Pulses/12
If C/T =1 then block works as Counter i.e counts external pulses arriving on Pin T0 or T1
GATE bit can be set as 0 or 1 here
Using M1 & M0 bits its possible to set the Mode of operation to Mode 0, 1, 2 or 3
Timer /Counter Block in 8051 – TMOD Register
Timer /Counter Block in 8051 – Block Diagram
Timer /Counter Block in 8051 – Block Diagram
Timer /Counter Block in 8051 – Block Diagram
Timer /Counter Block in 8051 – Block Diagram
Timer /Counter Block in 8051 – TCON Register
We want to configure Timer 1 as Counter in Mode 1 and Timer 0 as Timer in Mode 0

What will be the value that needs to be stored in TMOD Register


TMOD=0x50;
TR0=1;
TR1=1;
Programs using Timer /Counter Block
Write a program in embedded C for 8051 to generate a square wave of 10KHz frequency on
Port P1.0. Assume Crystal frequency of 12MHZ.

Port P1.0

50 µS

100 µS
So the problem essentially involves changing the output on Port Line P1.0 after every 50µsec.
First question to ask: Are we going to use the Timer/Counter Block as Counter or Timer?
Answer: Here we are interested in measuring time and not counting something, so we are using
Timer/Counter Block as Timer. We are interested in measuring time interval of 50 µS
Algorithm:
Make P1.0 =0
Generate delay of 50 µS - This will be done using the Timer Block
Make P 1.0=1
Generate delay of 50 µS - This will be done using the Timer Block
Repeat
Programs using Timer /Counter Block
Method 1: Use Timer 0 in Mode 1 i.e. 16 bit mode
We will use Timer 0 in timer Mode , so GATE=0, C/T=0
50 µS
Crystal=12MHz, Divided by 12 so Clock signal to 16 bit counter is 1MHz i.e. a
clock pulse is applied to the 16 bit counter after every 1 µS 100 µS
We are interested in finding out if 50 µS time period is over so that we can
change output of P1.0, repeatedly
For Timer 0 in 16 bit counter mode count will go from 0000H to FFFFH in 65535
pulses i.e. 65535µS and it will overflow after every 65536µS. This means that the
TF0 flag becomes 1 after every 65536µS.
However, we want the overflow to happen after every 50µS. This means that the
count should not start from 0 but should start from 65536-50 = 65486 i.e. 0xFFCE
So if we set TH0=0xFF, TL0=0xCE such that Timer 0 is initialized at 0xFFCE(65486)
then after 50 clock pulses i.e. 50µS, the Timer 0 will overflow and TF0 flag will
become 1.
Programs using Timer /Counter Block

Write a program to generate a delay of 20µsec in embedded C for 8051. Assume Crystal
frequency of 12MHZ.
Write a program to generate a delay of 20µsec in embedded C for 8051. Assume Crystal
frequency of 12MHZ.

So the problem essentially involves changing the output on Port Line P1.0 after every 50µsec.
First question to ask: Are we going to use the Timer/Counter Block as Counter or Timer?
Answer: Here we are interested in measuring time and not counting something, so we are using
Timer/Counter Block as Timer. We are interested in measuring time interval of 50 µS
Algorithm:
Make P1.0 =0
Generate delay of 50 µS - This will be done using the Timer Block
Make P 1.0=1
Generate delay of 50 µS - This will be done using the Timer Block
Repeat
TL0 will overflow / 1 µS
00H - > FFH and then on next pulse, it overflows 256µS
So if Timer 0 is in Mode 2 and TH0=TL0=00H and when TR0=1 then overflow will happen every
256µS
256 pulses -50 = 206 if my initial value in TH0=TL0=206 = 0xCE
So if Timer 0 is in Mode 2 and TH0=TL0=0xCEH and when TR0=1 then overflow will happen every
50µS
Suppose, Clock is coming every 3µS
00H - > FFH and then on next pulse, it overflows 256 pulses x 3µS = 768µS
So it should go back 17 pulses why? Because 17 pulses x 3µS = 51µS

So now my starting count should be 256-17 = 239 = 0XEF;


Write code in embedded C to generate a delay of 420µS using the Timer /Counter Block of 8051 with
Timer 1 in
i) Mode 1
ii) Mode 2
Assume Crystal frequency of 12MHZ

If we start from 0000 then 65536 pulses for the overflow to happen. Since each pulse duration is 1µS,
thus overflow happens after 65536µS
Now if we want the overflow to happen after 420 µS then my initial count must be 65536-420 =
65116, this should be the initial value in TH1 and TL1 which in hex is 0xFE5C
TH1=0xFE
TL1=0x5C
TR1=1
Serial Communication Port of 8051
What is serial Communication?
Serial communication means that the information transfers in or out is sequentially one
bit at a time. This is in contrast to a parallel communication where multiple bits move
simultaneously in parallel.
Which is better, Serial or Parallel Communication?
Parallel communication has higher throughput as number of bits pe second is higher
than serial communication.
However, in slightly long distance transmission, parallel communication is meaningless
as larger number of wires will be required while serial communication requires very few
wires.
Serial Communication Port of 8051
How is speed of serial Communication measured?
Bit rate is an important parameter used to indicate the rate at which information is sent
over serial port e.g. 2400bps, 4800bps, 9600bps, 19200bps, 38400bps, 100Kbps,
1Mbps etc.
Types of Serial Communication Transmission Modes
Simplex
In a simplex transmission mode, the communication between sender and receiver
occurs only in one direction. The sender can only send the data and the receiver can
only receive the data. The receiver cannot reply to the sender.
Types of Serial Communication Transmission Modes
Half Duplex
The communication between sender and receiver occurs in both the directions in a half
duplex transmission but, one at a time. The sender and receiver both can send and
receive the information but, only one is allowed to send at a time. Half duplex is still
considered a one-way road, in which a vehicle traveling in the opposite direction of the
traffic has to wait till the road is empty.
Types of Serial Communication Transmission Modes
Full Duplex
In a full duplex transmission mode, the communication between sender and receiver can
occur simultaneously. The sender and receiver can both transmit and receive at the
same time. The full duplex transmission mode is like a two-way road in which traffic can
flow in both directions at the same time.
Types of Serial Communication Transmission Modes
Types of Serial Communication
Synchronous Communication
The transmitter and the receiver have the same clock signal. E.g.
Serial Communication
Serial Communication
Serial Communication
Serial Port Communication Block of 8051
Serial Port Communication Block of 8051
❖Both the internal registers are called SBUF, but one if Write-Only (transmitter) while
other is Read-Only (Receiver)
❖The baud rate generator for the serial Port is the Timer 1 Timer/Counter of 8051,
working in auto-reload mode (Mode 2). The frequency of the Oscillator and the auto-
reload count in TH1 & TL1 decide the baud rate
❖Serial communication Port can be configured using the SCON register (Serial Control)
❖Serial Port can be made to work in Mode 0, 1,2 or 3 and this is chosen in SCON
❖In normal operation, the transmitter section of the 8051 Serial Port is enabled. If you
want to received data then the REN bit in the SCON register has to be made 1
Serial Port Communication Block of 8051

Mode 0: In this mode the serial port function as half duplex serial port with
fixed baud rate. The 8- bit serial data is received and transmitted
through RxD pin and the controller output the shift clock through TxD pin
during reception and transmission. The baud rate is fixed at 1 / 12 of the
oscillator frequency.

Mode 1: In this mode the serial port function as full duplex serial port with
variable baud rate. In this mode one data consists of 10 bits, which
includes one start bit, eight data bit and one stop bit. During reception
the stop bit is stored as RB8 in SCON register. Baud rate in mode-1
depends on the value of SMOD bit in PCON register and the Timer 1
overflow rate
Baud Rate = (2^ SMOD) * Oscillator Frequency / (384* (256-TH1))
Serial Port Communication Block of 8051

Mode 2: In this mode the serial port function as full duplex serial port with a
baud rate of either 1/32 or 1/64 of the oscillator frequency. In this mode
one data consists of 11 bits which includes one start bit, eight data bit, a
programmable 9th data bit and one stop bit. During transmission the
TB8 of SCON register is added as 9th data bit and during reception the
9th data bit is stored as RB8 in SCON register. The baud rate depends on
the value of SMOD bit in PCON register
Baud Rate = (2^ SMOD) * Oscillator Frequency / (384* (256-TH1))
Mode 3: Mode 3 is same as mode-2, except the baud rate. In mode-3, the baud
rate is variable. The baud rate depends on the value of SMOD bit in PCON
register and the timer- 1 overflow rate.
Serial Port Communication Block of 8051

Mode 2: In this mode the serial port function as full duplex serial port with a
baud rate of either 1/32 or 1/64 of the oscillator frequency. In this mode
one data consists of 11 bits which includes one start bit, eight data bit, a
programmable 9th data bit and one stop bit. During transmission the
TB8 of SCON register is added as 9th data bit and during reception the
9th data bit is stored as RB8 in SCON register. The baud rate depends on
the value of SMOD bit in PCON register
Mode 3: Mode 3 is same as mode-2, except the baud rate. In mode-3, the baud
rate is variable. The baud rate depends on the value of SMOD bit in PCON
register and the timer- 1 overflow rate.
Baud Rate = (2^ SMOD) * Oscillator Frequency / (384* (256-TH1))
Serial Port Communication Block of 8051

Division factor to achieve different baud rates with Oscillator Frequency of 11.0592MHz

Baud Rate TH1 (Hex)

9600 FD

4800 FA

2400 F4

1200 E8
Serial Port of 8051

Q. Write program in Embedded C to transmit the byte 0x5A on the Serial Port of
8051, once, @2400baud with Mode 1 operation of Serial Port. Assume
Oscillator frequency of 11.0592MHZ.

Set Timer 1 in Mode 2 operation using TMOD register TMOD=0x20;


Initialize value of TH1 and TL1 depending on desired baud rate TH1=TL1=0xF4;
Initialize the SCON Register depending upon the Mode of operation of serial port that is needed
Start the Timer 1 by TR1=1
Serial Port of 8051

Q. Write program in Embedded C to receive 10 bytes of data on the Serial Port


of 8051, @2400baud with Mode 1 operation of Serial Port and store the same in
Memory location starting from 2000H. Assume Oscillator frequency of
11.0592MHZ.
Serial Port of 8051

Q. Write program in Embedded C to receive 10 bytes of data on the Serial Port


of 8051, @2400baud with Mode 1 operation of Serial Port and store the same in
Memory location starting from 2000H. However, the 10 bytes are to be received
only after the byte “:” is received. Assume Oscillator frequency of 11.0592MHZ.
Interrupts in 8051
Sources of Interrupts
1. External Interrupts
◦ A) INT0 - Active low Interrupt on Pin P3.2
◦ B) INT1 – Active low Interrupt on Pin P3.3

2. Internal Interrupts
A) Timer 0 Overflow Interrupt – Occurs when Timer 0 Overflows and TF0=1 provided Timer
0 Interrupts is Enabled

B) Timer 1 Overflow Interrupt– Occurs when Timer 0 Overflows and TF1=1 provided Timer 0
Interrupts is Enabled

C) Serial Port Interrupt – Occurs when 01 Full byte is transmitted out of Serial Port (TI=1) or
when 01 Full byte is received on serial Port (RI=1), provided Interrupt is Enabled
Priority & Vector Address of Interrupts in 8051

Interrupt Vector
Interrupt Flag Priority Interrupt Number
Address

INT0 (Ext. int. 0) IE0 0003H Highest 0

Timer 0 Overflow Interrupt TF0 000BH 1

INT1 (Ext. int. 1) IE1 0013H 2

Timer 1 Overflow Interrupt TF1 001BH 3


Serial Port Interrupt TI/RI 0023H Lowest 4
Interrupt Register (IE) in 8051

Note : 1 is for Enabling the


Interrupt while 0 is for disabling
the interrupt
Interrupt Priority Register (IP) in 8051

Bit 7,6,5 – Reserved bits.


Bit 4 – PS: Serial Interrupt Priority Bit
1 = Assign a high priority to serial interrupt.
0 = Assign low priority to serial interrupt.
Bit 3 – PT1: Timer1 Interrupt Priority Bit Using IP Register the Interrupt
1 = Assign high priority to Timer1 interrupt. Priority can be changed
0 = Assign low priority to Timer1 interrupt.
Bit 2 – PX1: External Interrupt 1 Priority Bit
temporarily
1 = Assign high priority to External1 interrupt.
0 = Assign low priority to External1 interrupt.
Bit 1 – PT0: Timer0 Interrupt Priority Bit
1 = Assign high priority to Timer0 interrupt.
0 = Assign low priority to Timer0 interrupt.
Bit 0 – PX0: External0 Interrupt Priority Bit
1 = Assign high priority to External0 interrupt.
0 = Assign low priority to External0 interrupt.
Regarding External Interrupts in 8051
External Interrupts INTO and INT1 can be triggered by following two
mechanisms:

Low level triggered


Whenever a low level is detected on the INT0/INT1 pin while global
and external interrupts are enabled, the controller jumps to interrupt service
routine (ISR) to serve interrupt.
Falling edge triggered
Whenever falling edge is detected on the INT0/INT1 pin while global
and ext. interrupts are enabled, the controller jumps to interrupt service
routine (ISR) to serve interrupt.

This setting can be done in the TCON Register


TCON Register Regarding Interrupts in 8051

Bit 3- IE1:
External Interrupt 1 edge flag, set by hardware when interrupt on INT1 pin occurred and cleared by
hardware when interrupt get processed.
Bit 2- IT1:
This bit selects the external interrupt event type on INT1 pin,
1= sets interrupt on falling edge
0= sets interrupt on low level
Bit 1- IE0:
Interrupt0 edge flag, set by hardware when interrupt on INT0 pin occurred and cleared by
hardware when an interrupt is processed.
Bit 0 - IT0:
This bit selects the external interrupt event type on the INT0 pin.
1= sets interrupt on falling edge
0= sets interrupt on low level
Problem Statement
Write a program in embedded C to count the number of pulses arriving on Port Pin 3.2 (INT0) and
store the count in memory location 2000H.

Write a program in embedded C to count the difference between the number of pulses arriving at
Port Pin 3.2 (INT0) and Port Pin P3.3 (INT1) and store the count in memory location 2000H.

A classroom has two doors, one for entry of students and one for exit. Infrared sensors are connected
to both the doors which generate a pulse whenever a student enters/exits the class. Use 8051
microcontroller to create a system so that the count for the number of students in the class is
available at memory location 2000H.

Write a program in embedded C to generate a square wave of 10KHz on Port P1.2 using Timer 0 in
Mode 2. Use Interrupts. Assume Crystal frequency of 12MHz.
Write a program in embedded C to transmit 10 bytes of data starting from memory location 2000H on
the Serial Port in Mode 1 @9600 baud. Use Interrupts. Assume Crystal frequency of 11.0592MHz.
Interfacing Peripherals with 8051

How to interface LEDs with 8051?


How to interface 7 Segment LED Display with 8051?
How to interface a Relay with 8051?
How to interface a Key with 8051?
How to interface multiple keys as Keyboard Matrix with 8051?
How to interface ADC with 8051?
How to interface a DC Motor with 8051?
Interfacing LEDs with 8051 Platform

Vcc
Q1. Write a program in Embedded C to blink all the three LEDs simultaneously at intervals of
500mS.
#include<reg52.h>
sbit p00=P0^0;
sbit p20 = P2^0;
sbit p24=P2^4;
void main()
{ while(1)
{ p00=p20=0; //LED off
p24=1; //LED Off
delay(); //Call delay function to get delay of 500mS
p00=p20=1; //LED On
p24=0; //LED On
delay(); //Call delay function to get delay of 500mS
}
}
Next Slide shows the delay function and related calculations
Here the delay for 500mS is shown

Clock to Timer block is of 1MHz i.e. 1µsec


Timer 0 in Mode 2, if starting from a count of 0x06, will overflow after 250µsec.
If overflow is counted 2000 times then the time elapsed will be 500msec = 250µsec x 2000
void delay()
{ unsigned int i=0;
TMOD=0x02;
TH0=TL0=0x06;
TR0=1;
for(i=0;i<=1999;i++) //2000 times overflow is counted
{ while(TF0==0);
TF0=0;
}
}
Q2. Write a program in Embedded C to blink One LED at interval of 1 second while the other 2 LEDs to
blink at intervals of 500mS.
#include<reg52.h>
sbit p00=P0^0;
sbit p20 = P2^0;
sbit p24=P2^4;
void main()
{ p00=p20=0; //LED off
p24=1;//LED Off
while(1)
{
delay(); //Call delay function to get delay of 500mS
p00=p20=1; //LED On
delay();
p24=!p24; //LED On
p00=p20=0; //LED off
}
}
7 Segment LED Display – Common Anode
7 Segment LED Display – Common Cathode
Interfacing 7 Segment LED (Common anode) with 8051
Vcc

Vcc

Vcc
Interface of 7 segment LED display
Q1. Write a program in Embedded C to display 1,2 and 3 on the display after delay of 500mS
each in an infinite loop

#include<reg52.h>
void main()
{ P2=0xFF; //All LED off
while(1)
{
P2=0x9F; //Displays 1
delay(); //Call delay function to get delay of 500mS
P2=0x25; //Displays2
delay();
P2=0x0D; //Displays3
delay();
}
}
Interface of 7 segment LED display
Q1. Write a program in Embedded C to display 0-9 and repeat with delay of 500mS
between each count (Use look up table)
Interface of 7 segment LED display
Q1. Interface 03 Nos. 7 segment LED display with a 8051. Write a program in
Embedded C to read a byte from memory location 2000H and display the decimal
value on the display
Interface of 7 segment LED display
Converting a byte into its 3 digit decimal number

Take the number and divide by 100


Store the Quotient as the first digit of the answer.
Divide the remainder by 10
Store the Quotient as the second digit of the answer.
Store the remainder as the third digit of the answer.
Interfacing Keys & 7 Segment LED (Common anode) with 8051
Vcc

Vcc

Vcc
Q1. Interface two keys to Port 1 and a 7 segment display at Port 3. write a program such that a
0 is displayed if no key is pressed and a 1 is displayed if any of the two keys is pressed and
display 2 if both the keys are pressed.

Q2. Interface two keys to Port 1 (Key K1 & K2) and a 02 Nos. 7 segment display at Port 0 & 3.
Write a program such that the count at memory location 2000H is incremented every time K1 is
pressed and is decremented every time K2 is pressed. Further, the count in 2000H should be
displayed on the two digits. Assume that count does not go beyond 99.
Interfacing Keys & 7 Segment LED (Common anode) with 8051
Vcc
K1
Vcc

K2

Vcc Vcc
Interfacing Keyboard Matrix with 8051
Vcc

Scan Line

Scan Line

Scan Line

Scan Line

P2.0-P2.3 are Receive Lines (Input Port Lines)


Interfacing Keyboard Matrix with 8051
Vcc

Scan Line

Scan Line

Scan Line

Scan Line

P2.0-P2.3 are Receive Lines

You might also like