Embedded Systems - 8051 Microcontroller
Embedded Systems - 8051 Microcontroller
Microcontroller
Embedded Systems - 8051 Microcontroller
Brief History of 8051
Oscillators
As we know microcontroller is a digital circuit device hence it requires clock
for operation. 8051 has an on chip oscillators which work as clock source for
CPU. The output pulse of oscillator are stable therefore it enables
synchronized work of all parts of 8051 microcontroller.
Input/Output Port
The microcontroller 8051 has 4 input, output ports to connect it to the other
peripheral.
I/O Ports and their Functions
The four ports P0, P1, P2, and P3, each use 8 pins, making them 8-bit ports. Upon RESET,
all the ports are configured as inputs, ready to be used as input ports. When the first 0 is
written to a port, it becomes an output. To reconfigure it as an input, a 1 must be sent to
a port.
Port 0 is also designated as AD0–AD7, as it can be used for both data and address
handling. While connecting an 8051 to external memory, Port 0 can provide both
address and data. In case of 8031 (i.e. ROMless Chip), when we need to access the
external ROM, then P0 will be used for both Address and Data Bus. ALE (Pin no 31)
indicates if P0 has address or data. When ALE = 0, it provides data D0-D7, but when ALE
= 1, it has address A0-A7.
Port 1 (Pin 1 through 8)
It is an 8-bit port (pin 1 through 8) and can be used either as input or output. It
doesn't require pull-up resistors because they are already connected internally.
Upon reset, Port 1 is configured as an input port. To configure it as an output
port , Port bits must be set i.e. a high bit must be sent to all ports pins.
Besides working as I/O, Port P2 is also used to provide 16-bit address bus for
external memory along with Port 0. When the 8051 is connected to an external
memory, it provides path for upper 8-bits of 16-bits address, and it cannot be
used as I/O. Upon reset, Port 2 is configured as an input port.
Port 3 (Pins 10 through 17)
•It is also of 8 bits and can be used as Input/Output. This port provides some
extremely important signals.
•P3.0 and P3.1 are RxD (Receiver) and TxD (Transmitter) respectively and are
collectively used for Serial Communication.
•P3.2 and P3.3 pins are used for external interrupts.
•P3.4 and P3.5 are used for timers T0 and T1 respectively.
•P3.6 and P3.7 are Write (WR) and Read (RD) pins. These are active low pins,
means they will be active when 0 is given to them and these are used to provide
Read and Write operations to External ROM in 8031 based systems.
In 8051, I/O operations are done using four ports and 40 pins. The following pin
diagram shows the details of the 40 pins. I/O operation port reserves 32 pins
where each port has 8 pins. The other 8 pins are designated as Vcc, GND, XTAL1,
XTAL2, RST, EA (bar), ALE/PROG (bar), and PSEN (bar).
It is a 40 Pin PDIP (Plastic Dual Inline Package)
Pins 1 to 8 − These pins are known as Port 1. This port doesn’t serve any other
functions. It is internally pulled up, bi-directional I/O port.
Pin 9 − It is a RESET pin, which is used to reset the microcontroller to its initial
values.
Pins 10 to 17 − These pins are known as Port 3. This port serves some
functions like interrupts, timer input, control signals, serial communication
signals RxD and TxD, etc.
Pins 18 & 19 − These pins are used for interfacing an external crystal to get the
system clock.
Pin 20 − This pin provides the power supply to the circuit.
Pins 21 to 28 − These pins are known as Port 2. It serves as I/O port. Higher
order address bus signals are also multiplexed using this port.
Pin 29 − This is PSEN pin which stands for Program Store Enable. It is used to
read a signal from the external program memory.
Pin 30 − This is EA pin which stands for External Access input. It is used to
enable/disable the external memory interfacing.
Pin 31 − This is ALE pin which stands for Address Latch Enable. It is used to
demultiplex the address-data signal of port.
Pins 32 to 39 − These pins are known as Port 0. It serves as I/O port. Lower
order address and data bus signals are multiplexed using this port.
Pin 40 − This pin is used to provide power supply to the circuit.
Addressing Modes in 8085
These are the instructions used to transfer the data from one register to another register,
from the memory to the register, and from the register to the memory without any
alteration in the content. Some addressing modes are-
Example: MOV AL, 35H (move the data 35H into AL register)
Register mode: In register addressing the operand is placed in one of 8 bit or 16 bit
general purpose registers. The data is in the register that is specified by the instruction.
Example: MOV AX,CX (move the contents of CX register to AX register)
Register Indirect mode: In this addressing the operand’s offset is placed in any one of
the registers BX,BP,SI,DI as specified in the instruction. The effective address of the
data is in the base register or an index register that is specified by the instruction.
XTAL1, XTAL2 (Pin no 18 & Pin no 19) − 8051 has on-chip oscillator but
requires external clock to run it. A quartz crystal is connected between the
XTAL1 & XTAL2 pin of the chip. 8051 IC is available in various speeds and it all
depends on this Quartz crystal, for example, a 20 MHz microcontroller
requires a crystal with a frequency no more than 20 MHz.
RST (Pin No. 9) − It is an Input pin and active High pin. Upon applying a high
pulse on this pin, that is 1, the microcontroller will reset and terminate all
activities. This process is known as Power-On Reset.
EA or External Access (Pin No. 31) − It is an input pin. This pin is an active low
pin; upon applying a low pulse, it gets activated. In case of microcontroller
(8051/52) having on-chip ROM, the EA (bar) pin is connected to Vcc.
PSEN or Program store Enable (Pin No 29) − This is also an active low pin, i.e., it
gets activated after applying a low pulse. It is an output pin and used along with
the EA pin in 8031 based (i.e. ROMLESS) Systems to allow storage of program
code in external ROM.
ALE or (Address Latch Enable) − This is an Output Pin and is active high. It is
especially used for 8031 IC to connect it to the external memory. It can be used
while deciding whether P0 pins will be used as Address bus or Data bus.
Embedded Programming
Data types
The data type refers to an extensive system for declaring
variables of different types like integer, character, float, etc. The
embedded C software uses four data types that are used to store
data in the memory.
Keywords
There are certain words that are reserved for doing specific
tasks. These words are known as keywords. They are standard
and predefined in the Embedded C.
Keywords are always written in lowercase. These keywords must
be defined before writing the main program. The basic keywords
of an embedded software are given below:
Keywords
sbit: This data type is used in case of accessing a single bit of SFR
register.
Syntax: sbit variable name = SFR bit ;
Ex: sbit a=P2^1;
Explanation: If we assign p2.1 as ‘a’ variable, then we can use ‘a’
instead of p2.1 anywhere in the program, which reduces the
complexity of the program.
Bit: This data type is used for accessing the bit addressable
memory of RAM (20h-2fh).
Syntax: bit variable name;
Ex: bit c;
Explanation: It is a bit sequence setting in a small data area that
is used by a program to remember something.
comments
preprocessor directives
global variables
main() function
{
local variables
statements
…………..
…………..
}
fun(1)
{
local variables
statements
…………..
…………..
}
Comments: In embedded C programming language, we can place
comments in our code which helps the reader to understand the
code easily.
C=a+b; /* add two variables whose value is stored in another
variable C*/
Local variable
A
local variable is a variable declared within a function, and it is
. valid only to be used within that function
Main () function
The execution of a program starts with the main function. Every program
uses only one main () function
Advantages of embedded C program
•Its takes less time to develop application program.
•It reduces complexity of the program.
•It is easy to verify and understand.
•It is portable in nature from one controller to another.
(1)LED BLINKING
#include<reg51.h>
void main(void)
{
unsigned int i;
P1=0xff;
while(1)
{
P1=0x00;
for(i=0;i<500;i++);
P1=0xff;
for(i=0;i<500;i++);
}
}
(2) LED BLINKING
#include<reg51.h>
sbit a=P1^2;
void main(void)
{
unsigned int i;
unsigned char x;
a=0x00;
while(1)
{
a=0x01;
for(i=0;i<255;i++);
a=0x00;
for(i=0;i<255;i++);
}
}
3. Binary counter
#include<reg51.h>
void delay(int time); // delay() function prototype, this function generates
//delay = (time x
1msec)
// For example delay(500). Generates delay of (500 x 1msec) = 500msec
void main()
{
P1 = 00000000; // Initialize Port 1 as Output Port
while(1)
{
P1++; // Increment Port 1 (Binary Counter)
delay(100);
} // i.e. 1000 x 1msec = 1sec.
}
void delay(int time)
{
int i,j;
for(i=0;i<=time;i++)
for(j=0;j<=23;j++);
}