The GPIO
module provides an abstraction layer for handling General-Purpose Input/Output (GPIO) operations on the Microchip PIC16F series microcontrollers. It includes functions for configuring, reading, and writing to GPIO pins, and handles additional features like pull-up resistors and open-drain configurations.
File Information #
- Filename:
gpio.h
,gpio.c
- Version: 1.0.1
Supported Processors: #
GPIO Interface #
Functions in the GPIO Interface #
void ModeSet(GPIO_Ports_t PortPin, PinDirectionEnum_t PinDirection)
- Sets the direction and mode of the specified GPIO pin (e.g.,
INPUT
,OUTPUT
,ANALOG
,INPUT_W_PULLUP
,OPEN_DRAIN
).
- Sets the direction and mode of the specified GPIO pin (e.g.,
void PinWrite(GPIO_Ports_t PortPin, LogicEnum_t PinLevel)
- Writes a logic level (
HIGH
orLOW
) to the specified GPIO pin.
- Writes a logic level (
void PinToggle(GPIO_Ports_t PortPin)
- Toggles the logic level of the specified GPIO pin (if it’s
HIGH
, it will be set toLOW
, and vice versa).
- Toggles the logic level of the specified GPIO pin (if it’s
LogicEnum_t PinRead(GPIO_Ports_t PortPin)
- Reads the current logic level (
HIGH
orLOW
) from the specified GPIO pin.
- Reads the current logic level (
void Configure(const GPIO_Config_t* Config)
(Optional, depends on_CORE16F_HAL_GPIO_CONFIG_ENABLE
)- Configures multiple GPIO pins based on a configuration table provided during system initialization.
Example 1: Basic Digital Pin Configuration #
This example demonstrates how to configure GPIO pins as input, output, and input with pull-up resistors using the GPIO
interface.
int main(void) {
// Initialize system (if required by your system setup)
CORE.Initialize();
// Set PORTA pin 0 as an input
GPIO.ModeSet(PORTA_0, INPUT);
// Set PORTA pin 1 as an output
GPIO.ModeSet(PORTA_1, OUTPUT);
// Set PORTA pin 2 as an input with pull-up resistor
GPIO.ModeSet(PORTA_2, INPUT_W_PULLUP);
// Rest of the code
while (1) {
// Main loop
}
return 0;
}
Example 2: Writing and Toggling Pin States #
This example shows how to write logic levels (HIGH/LOW) to output pins and how to toggle a pin’s state.
int main(void) {
// Initialize system
CORE.Initialize();
// Set PORTA pin 0 as an output
GPIO.ModeSet(PORTA_0, OUTPUT);
// Set PORTA pin 1 as an output
GPIO.ModeSet(PORTA_1, OUTPUT);
// Turn on PORTA pin 0 (set HIGH)
GPIO.PinWrite(PORTA_0, HIGH);
// Turn off PORTA pin 1 (set LOW)
GPIO.PinWrite(PORTA_1, LOW);
// Toggle PORTA pin 0 state (from HIGH to LOW)
GPIO.PinToggle(PORTA_0);
while (1) {
// Toggle PORTA pin 1 every loop iteration
GPIO.PinToggle(PORTA_1);
__delay_ms(500); // Delay 500 ms
}
return 0;
}
Example 3: Reading Pin States #
This example demonstrates how to read the logic level of a GPIO pin configured as an input.
int main(void) {
// Initialize system
CORE.Initialize();
// Set PORTA pin 0 as an input
GPIO.ModeSet(PORTA_0, INPUT);
// Variable to store pin state
LogicEnum_t pinState;
while (1) {
// Read the state of PORTA pin 0
pinState = GPIO.PinRead(PORTA_0);
if (pinState == HIGH) {
// Pin is HIGH (do something)
} else {
// Pin is LOW (do something else)
}
__delay_ms(100); // Delay for stability
}
return 0;
}