0% found this document useful (0 votes)
26 views10 pages

EEE5232 Lecture-2

The document provides an overview of the Intel 8051 microcontroller, detailing its features, memory organization, and hardware configuration. It also includes programming examples in C for various operations using the 8051, such as data types, I/O port manipulation, and serial communication. Additionally, it explains the significance of Special Function Registers (SFRs) and the setup required for using the microcontroller effectively.
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)
26 views10 pages

EEE5232 Lecture-2

The document provides an overview of the Intel 8051 microcontroller, detailing its features, memory organization, and hardware configuration. It also includes programming examples in C for various operations using the 8051, such as data types, I/O port manipulation, and serial communication. Additionally, it explains the significance of Special Function Registers (SFRs) and the setup required for using the microcontroller effectively.
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/ 10

Microprocessor Application, Organisation and Embedded

Systems (EEE5232)
Lecture – 2: The 8051 𝝁C
Requirements: Proteus & Keil software

The Intel’s MC51 microcontroller (commonly termed 8051): In 1981 Intel Corporation introduced an 8 bit
microcontroller called 8051. It had 128 bytes of RAM, 4K bytes of on-chip ROM, two timers, one serial port
and four ports (each 8bit wide) all on a single chip. It is an 8-bit processor, which means that it can process 8
bit of data at a time. The 8051 became widely popular after intel allowed other manufactures to make and
market any flavour of the 8051, but remaining code-compatible (e.g., the AT89C51 from Atmel Corporation).

Key features of the 8051:

- 64KB Program Memory and data memory Address space. 16bit address bus – 216 = 64𝐾𝐵
- 4K bytes of on-chip Program Memory (ROM =4K)
- 128 bytes of on-chip Data RAM
- 4 bidirectional and individually addressable I/0 Ports
- Two 16-bit timer/counters
- Full duplex UART
- 6-source/5-vector interrupt structure with two priority levels
- 4 separate register banks. Each bank contains 8 registers
- Has Special Function Registers (SFRs)
- On-chip clock oscillator
8051 Memory Organization

The 128 bytes of RAM 8051 are assigned addresses


00H to 7FH and divided into three groups as
follows:

1. A total of 32 bytes from location s 00H to


1FH are set aside for register banks and the
stacks.
2. A total of 16 bytes from locations 20H to
2FH are set aside for bit addressable
read/write memory.
3. A total of 80 bytes from locations 30H to
7FH are used for read and write storage, or
what is normally called a scratch pad. These
80 locations of RAM are widely used for the
purpose of storing data and parameters by
8051 programmers.

RAM allocation in the 8051 The 8051 Register Banks

For the Program Memory, When the EA=0 at RESET, the PC (program counter) starts from 0x0000 and
accesses the external addresses from the memory (from location 0000 to FFFF). When the EA=1 at RESET, the
PC starts from 0x0000 and accesses the internal memory by address 0000H to 0FFFH and also the external
memory is accessed from location 1000H to FFFFH.
The Special Function Registers (SFR): For a programmer, the SFRs are at the directly addressable special
registers. These can be accessed by their names or by their addresses. The SFRs have addresses between 80H
and FFH. These addresses are above 80H, since the addresses 00 to 7FH are addresses of RAM memory inside
the 8051. Not all the address space of 80 to FF is used by the SFR. The unused locations 80H to FFH are
reserved and must not be used by the programmer.

Note the addresses of the four IO ports on the SFR listing (P0=80H, P1=90H, P2=A0H, P3=B0H). While all the
SFR registers are byte addressable, some of them are also bit addressable. The four ports P0 - P3 are bit
addressable. We can access the port as either the entire 8 bits or any single bit/pin of I/O ports P0, P1, P2, and
P3 without altering the rest. For instance, an instruction SETB P1.5 will set bit 5 of port 1 to high.

The Timer SFRs: TH0 and TL0 are Timer/counter 0 high and low bytes. TH1 and TL1 are Timer/counter 1 high
and low bytes. TH2 and TL2 are timer/counter 2 high and low bytes. They are all only byte addressable. TMOD
and TCON are Timer modes (Mode1 and Mode2) and Timer control bytes.

SCON and SBUF are respectively serial control and serial buffer bytes. These registers need to be properly
configured for Serial data transfer through the TXD and RXD serial ports.

ACC is the Accumulator (or just A), B is the B-register; both A and B are referred to as math registers. PSW
represents the Program Status Word byte. IE and IP are Interrupt enable and Interrupt priority bytes. DPH and
DPL are data pointer high and low bytes respectively. SP represents the Stack Pointer byte. PCON is the power
control byte. For proper usage, the programmer needs to understand the function of each bit of each of the
register he intends to use. The most widely used registers are: A(Accumulator, used for all arithmetic and logic
instructions), B, R0, R1, R2, R3, R4, R5, R6, R7, DPTR (data pointer), and PC (program counter).
Minimum hardware configuration
• The 8051 has an on-chip oscillator but requires an external clock to run it. Hence, a quartz crystal
oscillator is connected to inputs XTAL1 (pin19) and XTAL2 (pin18). The quartz crystal oscillator also
needs two capacitors, typically of 30 pF values. However, if a frequency source other than a crystal
oscillator is used (such as a TTL oscillator), it will be connected to XTAL1 while XTAL2 is left
unconnected. The speed of 8051 refers to the maximum oscillator frequency connected to XTAL.
• The Vcc (Pin 40) provides supply voltage to the chip. The voltage source is +5V and the GND (Pin 20) is
the ground. Pin 9 is the RESET pin and is normally low. Upon applying high pulse to this pin, the
microcontroller will reset and terminate all activities. This often referred to as power on reset. Once it
is activated the contents of all registers become zero except the content of SP which is 07H.
• The EA (External Access): this pin is connected to VCC for microcontrollers that have on-chip ROM
otherwise it is grounded (like in case of 8031 and 8032 because they have no on-chip ROM). If EA is
connected to Ground in an 8051, it indicates that its program is stored on an external ROM.

• The PSEN (program store enable), is an output pin. If an external memory is used, it is connected to
the OE pin of the external memory. For External Code Memory, PSEN=0; For External Data Memory,
PSEN=1.
• The ALE pin (Address Latch Enable) is an output pin used for demultiplexing the address and data.
When 8051is connected to external memory, both address and data are transferred through the port 0
pins. ALE signal is active high used to demultiplex address/data bus.

The I/O Ports


All the ports upon RESET are configured as output. To make a port become an input port, the hexadecimal
value FF is placed on the port (e.g., P1= 0xFF makes Port1 becomes input).
A code that gets data from Port0 and keeps sending it out through Port1 (in assembly language):
MOV A, #0FFH ;A = FF hexadecimal
MOV P0, A ;make P0 an input port by writing it all 1s
BACK: MOV A, P0 ;get data from P0
MOV P1, A ;send it to port 1
SJMP BACK ;keep doing it

1. Port0 is also designated as AD0 - AD7. When connecting an 8051 to an external memory, Port0
provides both address and data. The 8051 multiplexes address and data through Port0 to save pins.
The ALE pin indicates if P0 has address or data. When ALE=0, Port0 provides data D0-D7; When ALE=1,
Port0 provides address A0-A7 to the external memory. to use the port for input or output, each pin
must be connected externally to a 10K ohm pull-up resistor.
2. Port 1 and Port 2: In 8051 – based systems with no external memory connection, both P1and P2 are
used as simple I/O; the ports needs no pull-up resistors because they already have internal pull-up
resistors. In 8051 – based systems with external memory connections, Port2 must be used along with
P0 to provide the16 – bit address for the external memory (P0 provides the lower 8bits via A0–A7 and
P2 is used for the upper 8bits of the16 – bit address, A8–A15) and it cannot be used for I/O.
3. Port 3: Is a special port (also called Peripheral port). It can be used as input/output and does not need
any pull-up resistor. Upon reset, it is configured as an input port. However, as a special port, it has
other more significant functions listed below. To use each of the function, the corresponding SFR must
first be properly configured.

For serial communication

External interrupts

Timers/Counters

Read/write signal for external


memory

Programming 8051 in C
A. Data Types
There are various Data types in C:
1. unsigned char: The character data type is the most natural choice. 8051 is an 8-bit microcontroller and
unsigned char is also an 8-bit data type in the range of 0 –255 (00 –FFH). C compilers use the signed
char as the default data types if we do not put the keyword unsigned char. We always use unsigned
char in program until and unless we don’t need to represent signed numbers for example
Temperature.
2. signed char: The signed char is an 8-bit data type. signed char use the MSB D7 to represent –or +.
signed char giveus values from –128 to +127.
3. Unsigned int: The unsigned int is a 16-bit data type, it takes a value in the range of 0 to 65535 (0000 –
FFFFH). It is used to define 16-bit variables such as memory addresses or to set counter values of more
than 256. Since registers and memory accesses are in 8-bit chunks, the misuse of int variables will
result in a larger hex file.
4. Signed int: Signed int is a 16-bit data type. use the MSB D15 to represent – or +. We have 15 bits for
the magnitude of the number from –32768 to +32767.
5. Bit: The bit data type allows access to single bits of bit-addressable memory spaces 20–2FH.
6. sfr: To access the byte-size SFR registers, we use the sfr data type.

B. Examples (assuming students have prior knowledge of the C language):


1. Write a program to load three numbers into 3. Write an 8051 C program to send values 00
the Accumulator and send them to port 1 – FF to port P1.

# include <reg51.h> #include <reg51.h>


void main( ) void main( )
{ {
Acc = 0x25; unsigned char z;
P1 = Acc; for (z = 0; z <= 255; z++)
Acc = 0x46; P1=z;
P1 = Acc; }
Acc = 0x92;
P1 = Acc; 4. Write an 8051 C program to send hex values
} for ASCII characters of 0, 1, 2, 3, 4, 5, 6, 7, 8,
9, A, B, C, D and E to port P1.
2. Write an 8051 C program to toggle all the
#include <reg51.h>
bits of P1 continuously.
void main(void)
#include <reg51.h> {
void main( ) unsigned char mynum[ ] =
{ “0123456789ABCDE”;
for ( ; ; ) unsigned char z;
{ for (z = 0; z <= 15; z++)
P1=0x55; P1 = mynum[z];
P1=0xAA; }
}
}
5. Write an 8051 C program to toggle bits of {
P1 ports continuously with a 250ms delay. mybyte = P1; //get a byte from P1
#include <reg51.h> MSDelay(500); //wait for ½ second.
void MSDelay(unsigned int); P2 = mybyte; //send it to P2
}
//delay routine definition
}
void main( ) void MSDelay(unsigned int itime)
{ {
while (1) //repeat forever unsigned int i, j;
{ for (i = 0; i < itime; i++)
P1= 0x55; for (j = 0; j< 1275; j++);
MSDelay(250); }
P1= 0xAA;
MSDelay(250); 7. Write a C program for 8051 to transfer the
} letter “A” serially at 9600 baud rate
} continuously. Use 8-bit data and 1 stop bit.
//the delay routine implementation
void MSDelay(unsigned int itime) #include <reg51.h>
{ void main( )
unsigned int i, j; {
for (i = 0; i < itime; i++) TMOD = 0x20; //use Timer1, mode2
for (j = 0; j< 1275; j++); TH1= 0xFD; //9600 baud rate
} SCON = 0x50; //configure SCON
6. Write an 8051 C program to get a byte of TR1 = 1; // start the timer 1
data form P1, wait ½ second (i.e., 500 ms) while (1)
and then send it to P2. {
#include <reg51.h> SBUF = ‘A’; //place value in buffer
void MSDelay(unsigned int itime); while (TI == 0);
void main( ) TI = 0;
{ }
unsigned char mybyte; }
P1= 0x FF; //make P1 input port
while (1)

The signed char is an 8-bit data type. It uses the MSB D7 to represent – or + and gives us values from
– 128 to +127. Note: please stick with the unsigned char unless the data needs to be represented as signed
numbers, like in representing temperature values. Example: Write an 8051 C program to send values of –4 to
+4 to port P1.
#include <reg51.h>
void main(void)
{
char mynum[]={+1,-1,+2,-2,+3,-3,+4,-4};
unsigned char z;
for (z=0;z<=8;z++)
P1=mynum[z];
}
The unsigned int is a 16-bit data type and it takes a value in the range of 0 to 65535 (0000 – FFFFH). It
can be used to define 16-bit variables e.g., memory addresses or to set counter values of more than 256. Since
registers and memory accesses are in 8-bit chunks, the misuse of int variables will result in a larger hex file.
The Signed int is a 16-bit data type. The MSB D15 is used to represent the sign – or +, thus we have 15
bits left for the magnitude of the number from – 32768 to +32767
The bit data type allows access to single bits of bit-addressable memory spaces 20 – 2FH. To access the
byte-size SFR registers, we use the sfr data type.

Example: Write an 8051 C program to toggle bit D0 of the port P1 (P1.0) 50,000 times.
#include <reg51.h>
sbit MYBIT=P1^0; //sbit keyword allows access to single bits of SFR registers
void main(void)
{
unsigned int z;
for (z=0;z<=50000;z++)
{
MYBIT=0;
MYBIT=1;
}
}

Exercise: A door sensor is connected to the P1.1 pin, and a buzzer is connected to P1.7. Write a C program to
monitor the door sensor, and when it opens, sound the buzzer. You can sound the buzzer by sending a square
wave of a few hundred Hz.

#include <reg51.h>
void MSDelay(unsigned int);
sbit Dsensor=P1^1;
sbit Buzzer=P1^7;
void main(void)
{
Dsensor=1; //make P1.1 an input
while (1)
{
while (Dsensor==1)//while it opens
{
Buzzer=0;
MSDelay(200);
Buzzer=1;
MSDelay(200);
}
}
}
How to configure Timers
The 8051 has two timers/counters, they can be used either as Timers to generate a time delay or as Event
counters to count events happening outside the microcontroller. Both Timer 0 and Timer 1 are 16 bits wide.
Since 8051 has an 8-bit architecture, each 16-bits timer is accessed as two separate registers of low byte and
high byte.

Timer 0 Timer 1
They are accessed directly like any other register. E.g., instructions MOV TL0, #4FH moves the value 47 into
the TL0 register while MOV R5, TH0 moves the content of TH0 into the register R0.
Both timers 0 and 1 use the same register, called TMOD (timer mode), to set the various timer operation
modes. TMOD is an 8-bit register, its lower 4 bits are for Timer 0 while its upper 4 bits are for Timer 1.

The TMOD register


In each case, the lower 2 bits are used to set the timer mode while the upper 2 bits are used to specify the
operation:
• Gate = Gating control. When set, Timer/counter is enable only while the INTx pin is high and the TRx
control pin is set. When cleared, the timer is enabled whenever the TRx control bit is set.
• C/T = Timer or counter selected. It is cleared for timer operation (input from internal system clock). It is
set for counter operation (input from Tx input pin).
• The Operating modes:

Example:
MOV TMOD, #01H TMOD = 00000001, mode 1 of timer 0 is selected.
MOV TMOD, #20H TMOD = 00100000, mode 2 of timer 1 is selected.
MOV TMOD, #12H TMOD = 00010010, mode 2 of timer 0, and mode 1 of timer 1 are selected.

The TCON register (it is bit-addressable)


Timers of 8051 do starting and stopping by either software or hardware control. In using software to start and
stop the timer, if GATE=0 (in the TMOD register), then the start and stop of the timer are controlled by way of
software by the TR (timer start) bits TR0 and TR1 (writing a SETB instruction starts it, and it is stopped by the
CLR instruction) of the TCON register.
The hardware way of starting and stopping the timer by an external source is achieved by making GATE=1 in
the TMOD register.

The timer’s clock frequency and its period for various 8051-based system, with the crystal frequency
11.0592MHz when C/T bit of and TMOD is 0 is computed as:

1 11.0529 𝑀𝐻𝑧
The timer works with a clock of 12 of the XTAL frequency; therefore, we have = 921.6 𝑀𝐻𝑧 as the
12
timer frequency. As a result, each clock has a period of 𝑇 = 1/921.6𝑘𝐻𝑧 = 1.085 𝜇𝑠. In other words, the
Timer counts up each 1.085𝜇𝑠 us resulting in delay = number of counts × 1.085 𝜇𝑠.
Therefore, to generate a time delay:
1. Load the TMOD value register indicating which timer (timer 0 or timer 1) is to be used and which timer
mode (0 or 1) is selected
2. Load registers TL and TH with initial count value
3. Start the timer
4. Keep monitoring the timer flag (TF) with the JNB TFx, target instruction to see if it is raised
5. Get out of the loop when TF becomes high.
6. Stop the timer.
7. Clear the TF flag for the next round.
8. Go back to Step 2 to load TH and TL again

You might also like