Embedded Systems & Micro-Controllers-1-01-12-2020
Embedded Systems & Micro-Controllers-1-01-12-2020
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
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
Now Suppose we want to have a Red LED turn ON for 10 seconds and Turn OFF
for 10 seconds and repeat the cycle.
Can you Identify whether the Port Lines in both the diagram are Output lines
or Input Lines OR Both?
Vcc
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
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
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
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
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
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
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
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.
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
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
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
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
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
Scan Line
Scan Line
Scan Line
Scan Line