8051 MC BT-1-LAPTOP-LJ7D0M7S
8051 MC BT-1-LAPTOP-LJ7D0M7S
8051 MC BT-1-LAPTOP-LJ7D0M7S
Introduction:
The Intel 8051 is a 8-bit microcontroller introduced in 1980, renowned for its
versatility and reliability with 4KB of on-chip program memory and 128 bytes of data
memory, it's suitable for various applications. The 8051 features 32 I/O lines, two 16-
bit timers, and a serial communication port. Power-saving modes reduce energy
consumption. The 8051 has been widely adopted in embedded systems, robotics,
industrial control, and consumer electronics. Its low cost, ease of use, and flexibility
have made it a popular choice. The 8051 remains a fundamental component in many
applications. Its legacy continues, with modern variants offering enhanced features.
The 8051's impact on the microcontroller industry is still significant today. The 8051
is microcontrollerold microcontroller but still the 8051 plays an important role in
market.
What is microcontroller:
It is a CPU on a single chip. It is execute only single task at a time. It can be used fir
small applications. Processing speed is low and consumes les power(3.3v,5v).It is less
expensive and all are with limited size.
3. 128 bytes RAM: On-chip data memory for storing data temporarily.
5. Two 16-bit timers: Timer 0 and Timer 1 for timing and counting applications.
10. Stack: 8-level stack for storing return addresses and data.
11. Arithmetic and logical operations: Supports basic arithmetic and logical
operations.
13. Programmable counter array: PCA (Programmable Counter Array) for custom
counter applications.
18. Priority interrupts: Supports priority interrupts for critical task handling.
Architecture of 8051:
Input Pins
1. RST (Pin 40): Active low reset input. When this pin is low, the microcontroller is reset.
2. EA (Pin 37): Input pin for enabling/disabling external memory access.
3. INT0 (Pin 3): External interrupt 0 input.
4. INT1 (Pin 5): External interrupt 1 input.
5. T0 (Pin 4): Timer 0 input.
6. T1 (Pin 3): Timer 1 input.
7. RXD (Pin 8): Serial input.
Output Pins:
1. P0.0 - P0.7 (Pins 32-25): 8-bit bidirectional I/O port.
2. P1.0 - P1.7 (Pins 24-17): 8-bit bidirectional I/O port.
3. P2.0 - P2.7 (Pins 16-9): 8-bit bidirectional I/O port.
4. P3.0 - P3.7 (Pins 8-1): 8-bit bidirectional I/O port.
5. ALE (Pin 39): Output pulse for latching address.
6. PSEN (Pin 38): Output pulse for enabling external program memory.
7. TXD (Pin 7): Serial output.
8. WR (Pin 2): Write signal output.
9. RD (Pin 1): Read signal output.
Bidirectional Pins:
1. P0.0 - P0.7 (Pins 32-25): Can be used as input or output.
2. P1.0 - P1.7 (Pins 24-17): Can be used as input or output.
3. P2.0 - P2.7 (Pins 16-9): Can be used as input or output.
4. P3.0 - P3.7 (Pins 8-1): Can be used as input or output.
#include <reg51.h>
void main(void) {
// Set initial state
LED = 0; // Turn off LED (assuming active high)
Button = 1; // Set button pin as input (high state)
while (1) {
// Check if the button is pressed
if (Button == 0) { // Active low, so 0 means pressed
LED = 1; // Turn on LED
delay(500); // Wait for some time
LED = 0; // Turn off LED
delay(500); // Debounce delay
}
}
}
Explanation:
Header File (reg51.h): This includes the standard definitions for the 8051
microcontroller.
sbit: This keyword defines single-bit access to specific pins. sbit LED = P1^0; defines
LED as the first pin of Port 1, and sbit Button = P1^1; defines Button as the second pin of
Port 1.
delay: A simple delay function to create a delay between operations. The actual time
delay depends on the microcontroller's clock speed.
Timer is a specialized type of clock which is used to measure time intervals. A timer that
counts from zero upwards for measuring time elapsed is often called a stopwatch. It is a
device that counts down from a specified time interval and used to generate a time delay,
for example, an hourglass is a timer.
TIMER 0:
The 16-bit register of Timer 0 is accessed as low and high-byte. The low-byte register is
called TL0 (Timer 0 low byte) and the high-byte register is called TH0 (Timer 0 high byte).
These registers can be accessed like any other register. For example, the instruction MOV
TL0, #4H moves the value into the low-byte of Timer #0.
Timer 0 Overview:
16-bit timer/counter
Can be used as a timer or counter
Can generate interrupts on overflow
Can be used in various modes (see below)
Timer 0 Modes:
o Mode 0: 13-bit Timer (TH0 = 0)
Timer runs for 13 bits (0-8191)
Then resets to 0
o Mode 1: 16-bit Timer (TH0 = 1)
Timer runs for 16 bits (0-65535)
Then resets to 0
o Mode 2: 8-bit Auto-Reload (TH0 = 2)
Timer runs for 8 bits (0-255)
Then reloads with value in TH0
o Mode 3: Split-Timer (TH0 = 3)
Timer 0 is split into two 8-bit timers (TL0 and TH0)
Timer 0 Registers
TL0 (Timer 0 Low): Lower 8 bits of Timer 0
TH0 (Timer 0 High): Upper 8 bits of Timer 0
Timer 0 Interrupt
TF0 (Timer 0 Overflow Interrupt): Generated when Timer 0 overflows
Timer 0 Clock
Fosc/12: Timer 0 clock frequency (default)
Fosc/4: Timer 0 clock frequency (when T0M bit is set)
TIMER 1:
The 16-bit register of Timer 1 is accessed as low- and high-byte. The low-byte register is
called TL1 (Timer 1 low byte) and the high-byte register is called TH1 (Timer 1 high
byte). These registers can be accessed like any other register. For example, the
instruction MOV TL1, #4H moves the value into the low-byte of Timer 1.
Timer 1 Overview:
16-bit timer/counter
Can be used as a timer or counter
Can generate interrupts on overflow
Can be used in various modes (see below)
TMOD Registers:
Both Timer 0 and Timer 1 use the same register to set the various timer operation modes.
It is an 8-bit register in which the lower 4 bits are set aside for Timer 0 and the upper four
bits for Timers. In each case, the lower 2 bits are used to set the timer mode in advance
and the upper 2 bits are used to specify the location.
Gate − When set, the timer only runs while INT(0,1) is high.
C/T − Counter/Timer select bit.
M1 − Mode bit 1.
M0 − Mode bit 0.
GATE
Every timer has a means of starting and stopping. Some timers do this by software, some by
hardware, and some have both software and hardware controls. 8051 timers have both
software and hardware controls. The start and stop of a timer is controlled by software using
the instruction SETB TR1 and CLR TR1 for timer 1, and SETB TR0 and CLR TR0 for
timer 0.
The SETB instruction is used to start it and it is stopped by the CLR instruction. These
instructions start and stop the timers as long as GATE = 0 in the TMOD register. Timers can
be started and stopped by an external source by making GATE = 1 in the TMOD register.
C/T (CLOCK / TIMER)
This bit in the TMOD register is used to decide whether a timer is used as a delay
generator or an event manager. If C/T = 0, it is used as a timer for timer delay generation.
The clock source to create the time delay is the crystal frequency of the 8051. If C/T = 0, the
crystal frequency attached to the 8051 also decides the speed at which the 8051 timer ticks at
a regular interval.
Timer frequency is always 1/12th of the frequency of the crystal attached to the 8051.
Although various 8051 based systems have an XTAL frequency of 10 MHz to 40 MHz, we
normally work with the XTAL frequency of 11.0592 MHz. It is because the baud rate for
serial communication of the 8051.XTAL = 11.0592 allows the 8051 system to communicate
with the PC with no errors.
Modes of operation:
#include <reg51.h>
void main() {
// Initialize Timer 0
TMOD = 0x01; // Mode 1 (16-bit timer) for Timer 0
TH0 = 0x3C; // Load high byte for Timer 0
TL0 = 0xFF; // Load low byte for Timer 0
TR0 = 1; // Start Timer 0
// Initialize Timer 1
TMOD |= 0x10; // Mode 1 (16-bit timer) for Timer 1
TH1 = 0x3C; // Load high byte for Timer 1
TL1 = 0xFF; // Load low byte for Timer 1
TR1 = 1; // Start Timer 1
while(1) {
// Wait for Timer 0 overflow
while(!TF0);
TF0 = 0; // Clear overflow flag
P1 ^= 0x01; // Toggle LED connected to P1.0
Timer 0 Initialization:
Timer 1 Initialization:
Main Loop:
Explanation:
The program uses both Timer 0 and Timer 1 to generate periodic interrupts, which
toggle LEDs connected to Port 1 and Port 2. The timers are set to Mode 1 (16-bit timer)
and loaded with the same values (0x3C and 0xFF). The program waits for each timer to
overflow, clears the overflow flag, and toggles the corresponding LED.
Counters:
A counter is a device that stores (and sometimes displays) the number of times aparticular
event or process occurred, with respect to a clock signal. It is used to count the
eventhappening outside the microcontroller. In electronics, counters can be implemented
quite easily using register-type circuits such as a flip-flop.
PROGRAM:
void main(void) {
// Initialize Timer 0 in 16-bit counter mode
TMOD = 0x06; // Set Timer 0 as a 16-bit counter (mode 2)
TR0 = 1; // Start Timer 0
while (1) {
unsigned char lowByte = TL0; // Read lower byte of the counter
unsigned char highByte = TH0; // Read higher byte of the counter
// This example will just store the value for further processing
unsigned int counterValue = (highByte << 8) | lowByte;
Serial port:
5. RS-232:
Function: A standard for serial communication transmission of data.
Application: Common in computer serial ports and older communication equipment.
Voltage Levels: ±12V signaling levels.
6. RS-485:
Function: An enhanced version of RS-232 for multi-point systems.
Application: Industrial automation and networking.
Differential Signaling: Provides better noise immunity and longer distance.
1. Baud Rate:
Definition: Number of signal changes per second.
Impact: Higher baud rates increase data transfer speed but may reduce reliability over
long distances.
2. Data Bits:
Common Values: 7 or 8 bits.
Usage: Determines the size of the data payload in each frame.
3. Parity Bit:
Types: None, Even, Odd.
Purpose: Used for error detection.
4. Stop Bits:
Common Values: 1 or 2 bits.
Function: Indicates the end of a data packet, ensuring correct synchronization.
5. Flow Control:
Types: Hardware (RTS/CTS), Software (XON/XOFF).
Usage: Prevents data overflow by managing data transmission speed.
1. Initialization:
Configuration: Set baud rate, data bits, parity, and stop bits.
Enabling: Turn on UART/USART modules in the microcontroller.
2. Transmission:
Data Register: Load data into the transmit data register.
Transmission Complete Flag: Check for completion before sending the next byte.
3. Reception:
Data Register: Read data from the receive data register.
Buffer Overrun: Ensure the receive buffer is read before new data arrives to avoid
overflow.
PROGRAM:
void UART_Init(void) {
// Set up the serial port for 8-bit UART mode
SCON = 0x50; // 0101 0000b: 8-bit UART, mode 1, REN enabled
// Start Timer 1
TR1 = 1;
void UART_SendChar(char c) {
SBUF = c; // Load character into the buffer
while (TI == 0); // Wait until transmission is complete
TI = 0; // Clear the transmit interrupt flag
}
char UART_ReceiveChar(void) {
while (RI == 0); // Wait until data is received
RI = 0; // Clear the receive interrupt flag
return SBUF; // Return received character
}
void main(void) {
UART_Init(); // Initialize UART
while (1) {
char received_char = UART_ReceiveChar(); // Receive a character
UART_SendChar(received_char); // Echo the received character
}
}
Short Jump:
Syntax of short jump is SJMP Addr8.
1.
2. One byte is apcode SJMP.
3. Another one is operand addr8.
The range of address in short jump is -128 to +127.
Long Jump:-
Syntax of long jump is LJMP addr16.
The size of long jump is 3byte.
1. One byte is apcode LJMP.
2. Another two bytes are operand addr16.
The range of address in long jump is 0000 to FFFF.
Absolute Jump:-
Syntax of Absolute jump is AJMP addr11.
The size of Absolute jump is 2byte.
1. One byte is apcode AJMP.
2. Another byte is operand addr11.
JZ Instruction Target:
JZ means jump if zero.
The content in the accumulator is the zero then it jumps to the target address is known
as JZ instruction. If A = 0.
JC Instruction Target:
JC means jump if carry.
If carry flag is high then it jumps to target address is known as JC instruction. If CY =
1.
JB Instruction:
JB means jump if bit.
If bit is high then jump to target address is known as JB instruction. If B = 1.
JNB Instruction:
JNB means jump if not bit.
If bit is not high then it jump to target address is known as JNB instruction. If B = 0.
DJNZ Instruction:
Decrement jump if not zero.
After doing decrement the result will be zero then it jumps to target is known as
DJNZ instruction.
CJNE Instruction:
CJNE means compare jump if not equal.
After both source byte and destination byte are comparing if there are not equal then
it jumps to target is known as CJNE instruction.
I/O Ports:
Input Ports: Receive data from external devices (e.g., sensors, switches).
- Output Ports: Send data to external devices (e.g., LED 's, displays).
Programming Techniques:
1. Bit Manipulation: Directly access and manipulate individual bits or groups of bits within a
port.
2. Port Addressing: Use specific addresses to access and control I/O ports.
3. Register-Level Programming: Use registers to control and monitor I/O ports.
Programming Languages:
1. Assembly Language: Low-level, symbolic representation of machine code.
2. C/C++: High-level languages with libraries and functions for I/O port access.
3. Microcontroller-Specific Languages: Languages like Arduino's IDE, PIC BASIC, or AVR
Studio.