5. 8051 INTERNAL PERIPHERALS AND RELATED PROGRAMS
The 8051 microcontroller comes with a variety of internal peripherals to interact with external systems and devices. These peripherals are essential for embedded systems and can be controlled via assembly programming. The most significant peripherals in the 8051 include I/O Ports, Timers/Counters, and Serial Communication.
5.1. I/O Ports
The 8051 microcontroller has 4 I/O ports (Port 0, Port 1, Port 2, and Port 3), each of which is 8-bits wide. These ports can be used for reading and writing data to/from external devices like sensors, actuators, LEDs, and more.
List of I/O Ports:
Port 0 (P0):
- Pins: P0.0 to P0.7.
- Function: Bidirectional, open-drain output (needs external pull-up resistors).
- Primary Use: Communication with external devices via data transfer.
Port 1 (P1):
- Pins: P1.0 to P1.7.
- Function: Standard bidirectional I/O (no need for external pull-ups).
- Primary Use: General-purpose I/O.
Port 2 (P2):
- Pins: P2.0 to P2.7.
- Function: Bidirectional I/O with 5V TTL levels.
- Primary Use: Typically used for interfacing with memory or external devices in a multiplexed address-data bus system.
Port 3 (P3):
- Pins: P3.0 to P3.7.
- Function: Bidirectional I/O with special alternate functions (interrupts, serial communication, etc.).
- Primary Use: Used for advanced features like interrupts, serial communication, and external memory interfacing.
Diagram of 8051 I/O Ports:
Read/Write Operations for I/O Ports:
Write Operation: To write data to a port, simply load the data into the port register. For example:
Read Operation: To read data from a port, move the data from the port register into the accumulator:
I/O Port Special Function Registers (SFRs):
Each port has associated SFRs that control the direction (input/output), state, and configuration of the ports. The relevant SFRs for the ports are as follows:
SFR | Description |
---|---|
P0 | Port 0 Data Register |
P1 | Port 1 Data Register |
P2 | Port 2 Data Register |
P3 | Port 3 Data Register |
IE | Interrupt Enable Register |
IP | Interrupt Priority Register |
5.2. Timers/Counters
The 8051 microcontroller has 2 timers/counters, Timer 0 and Timer 1, each of which can be used either as a timer or a counter, depending on the application. These timers are very useful for creating delays, measuring time intervals, or generating pulses.
List of Timers/Counters:
Timer 0:
- Mode: Timer/Counter Mode.
- Control SFR: TCON (Timer Control Register), TMOD (Timer Mode Register).
- Related SFRs: TH0 (Timer 0 High), TL0 (Timer 0 Low), TCON, TMOD.
Timer 1:
- Mode: Timer/Counter Mode.
- Control SFR: TCON, TMOD.
- Related SFRs: TH1, TL1, TCON, TMOD.
Timer Modes:
Timers in the 8051 can operate in different modes, controlled by the TMOD register.
Mode 0 (13-bit Timer Mode):
- Timer counts from 0 to 8191.
- Uses both TH and TL registers.
Mode 1 (16-bit Timer Mode):
- Timer counts from 0 to 65535.
- Uses both TH and TL registers.
Mode 2 (8-bit Auto-reload Mode):
- Timer counts from 0 to 255.
- After overflow, it reloads from the TH register.
Mode 3 (Split Timer Mode):
- Timer is split into two separate 8-bit timers (Timer 0 and Timer 1).
Programming Timers Example (Timer 0):
To set Timer 0 in Mode 1 (16-bit mode):
To stop Timer 0:
Timer Operation Diagram:
5.3. Serial Communication
The 8051 microcontroller supports serial communication using UART (Universal Asynchronous Receiver-Transmitter). It allows the 8051 to communicate with other devices over a serial link (like a PC, other microcontrollers, or serial devices).
Basics of Serial Communication:
Serial communication involves the transmission of data bit by bit over a single communication line. The 8051 can send and receive data in 8-bit format. The data is transmitted asynchronously, meaning there is no need for a clock signal.
Baud Rate:
The baud rate is the rate at which data is transmitted or received. The 8051 can set the baud rate using the Timer 1 in Mode 2. The formula for calculating the baud rate is:
For example, to set a baud rate of 9600, the 8051 uses Timer 1 in Mode 2 and adjusts the TH1 and TL1 registers accordingly.
Serial Communication Control Registers:
- SBUF: Serial Data Buffer (used for both receiving and transmitting data).
- SCON: Serial Control Register.
- TI (Transmit Interrupt): A bit in TCON indicating when the transmission is complete.
- RI (Receive Interrupt): A bit in TCON indicating when data is received.
Serial Communication Example (Transmit and Receive):
- Transmitting Data:
- Set up the SCON register to enable the serial mode.
- Write data to the SBUF register.
- Receiving Data:
- Wait for the data to be received by checking the RI flag.
- Once data is received, read from the SBUF register.
Serial Communication Diagram:
1 Comments
Bahut badhiya 👍
ReplyDelete