AT89C51 has four 8-bit I/O ports (P0, P1, P2, and P3), each containing 8 pins that can be configured as inputs or outputs. Hence, these ports are called General Purpose Input/Output (GPIO) ports.
Port 0 (P0)
This is an 8-bit bidirectional I/O port with internal pull-ups. As an output port, it can sink eight TTL inputs. When interfacing with external memory, it acts as both address and data bus.
Port 1 (P1)
Port 1 is also an 8-bit bidirectional I/O port with internal pull-ups. Unlike Port 0, this port doesn't serve any other function. It's purely a general-purpose I/O port.
Port 2 (P2)
Port 2 is similar to P1, an 8-bit bidirectional I/O port with internal pull-ups. However, when used for external memory interfacing, it acts as an address bus.
Port 3 (P3)
Port 3 is an 8-bit bidirectional I/O port with internal pull-ups like P1 and P2. But this port also serves as functions for various special features of the AT89C51 like interrupts, timer input, control signals for external memory interfacing, serial communication, etc.
These GPIO ports allow the microcontroller to interact with other devices like LEDs, switches, displays, other microcontrollers, and microprocessors.
To use these ports, you need to configure the data direction registers, which determine whether each port or pin acts as an input or an output.
In the AT89C51, the GPIO ports are automatically configured as inputs on reset. To use a pin as an output, you must explicitly write a logic '1' to the pin. To read from an input pin, you can simply read the pin's value.
Here's a simple example to demonstrate GPIO functionality on AT89C51. Assume we connected a button to Port 1, pin 0 (P1.0), and an LED to Port 2, pin 0 (P2.0). We want the LED to light up when the button is pressed:
#include // include this header for AT89C51
#define button P1_0 // Assume we connected button to Port 1, pin 0
#define LED P2_0 // Assume we connected LED to Port 2, pin 0
void main() {
while(1) { // infinite loop
if(button == 0) { // if button is pressed
LED = 1; // Turn ON the LED
} else {
LED = 0; // Turn OFF the LED
}
}
}
In this code, we're constantly checking the state of the button in the infinite while loop.
If the button is pressed (logic '0'), we turn on the LED by setting P2.0 to '1'.
If the button is not pressed (logic '1'), we turn off the LED by setting P2.0 to '0'.
GPIOs are a critical part of any microcontroller, and knowing how to use them is crucial for building many digital devices and embedded systems.
AT89C51 GPIO Registers
The AT89C51 microcontroller does not have specific GPIO direction registers like some other modern microcontrollers.
Instead, it simplifies GPIO operations: all GPIO pins are by default inputs. When you want to use a pin as an output, you can write a logic value to it directly.
Reading a value from a pin will give you its current state, regardless of whether it is an output or an input.
This microcontroller has four 8-bit ports P0, P1, P2, and P3, each containing 8 GPIO pins.
Each port is associated with a single Special Function Register (SFR) in the microcontroller's memory, which holds the current logic levels of the 8 GPIO pins in that port.
These registers are directly accessible for both read and write operations.
Port P1 Register
Here's an example to illustrate how to access these registers and perform read and write operations:
#include // include this header for AT89C51
void main() {
P1 = 0xFF; // Set all pins on Port 1 as high (output)
P2 = P1; // Copy the state of Port 1 to Port 2
if (P3_0 == 1) { // If Pin 0 on Port 3 is high
P0_0 = 0; // Set Pin 0 on Port 0 as low
} else {
P0_0 = 1; // Otherwise set it as high
}
}
In this example, `P1 = 0xFF;` is writing to the GPIO pins in Port 1 to make them all output high voltage.
`P2 = P1;` is reading the state of all GPIO pins in Port 1 and then writing that state to all GPIO pins in Port 2.
`if (P3_0 == 1)` is reading the state of the GPIO pin 0 in Port 3.
Even without dedicated direction registers, the AT89C51 provides flexible GPIO functionality through its port registers. Understanding these concepts and knowing how to use these registers can greatly enhance your projects with the AT89C51 microcontroller.