The GPIO_Analog
module provides functionality for handling analog input and Analog-to-Digital Conversion (ADC) on Microchip PIC16F microcontrollers. It allows configuration of analog channels, setting GPIO pins as analog inputs, and reading analog values from those channels.
File Information #
- Files:
gpio_analog.h
,gpio_analog.c
- Version: 1.0.1
Supported Processors: #
CORE GPIO_Analog Interface #
The GPIO_Analog
interface is a constant object that provides access to analog initialization, channel selection, and reading functions.
Functions in the GPIO_Analog Interface #
void Initialize(AnalogChannelSelectEnum_t Channel)
- Initializes the Analog-to-Digital Converter (ADC) for a specific analog channel, setting the necessary ADC clock and enabling the ADC for operation.
void SelectChannel(AnalogChannelSelectEnum_t Channel)
- Selects the analog channel to be used for ADC conversions. This is required before performing an ADC conversion on a new channel.
void PinSet(GPIO_Ports_t PortPin, AnalogChannelSelectEnum_t Channel)
- Configures the given GPIO pin to be used as an analog input. It associates the pin with a specific analog channel and sets it up for ADC operations.
uint16_t ReadChannel(void)
- Performs an ADC conversion on the selected analog channel and returns the 10-bit result. This function waits for the conversion to complete before returning the value.
Example 1: Configuring and Reading Analog Input #
This example shows how to configure a pin as an analog input and read the analog value using the GPIO_Analog
interface.
int main(void) {
// Initialize system
CORE.Initialize();
// Configure PORTA pin 0 as an analog input, associated with analog channel ANA0
GPIO_Analog.PinSet(PORTA_0, ANA0);
// Initialize the analog channel ANA0
GPIO_Analog.Initialize(ANA0);
// Variable to store the analog value
uint16_t adcValue;
while (1) {
// Read the analog value from channel ANA0
adcValue = GPIO_Analog.ReadChannel();
// Process the analog value (e.g., print or use in control logic)
__delay_ms(500); // Delay between readings
}
return 0;
}
Example 2: Switching Between Analog Channels #
In this example, we initialize multiple analog channels and switch between them to read values.
int main(void) {
// Initialize system
CORE.Initialize();
// Set up PORTA pin 0 as analog input for channel ANA0
GPIO_Analog.PinSet(PORTA_0, ANA0);
// Set up PORTA pin 1 as analog input for channel ANA1
GPIO_Analog.PinSet(PORTA_1, ANA1);
// Initialize both channels
GPIO_Analog.Initialize(ANA0);
GPIO_Analog.Initialize(ANA1);
uint16_t adcValue0, adcValue1;
while (1) {
// Select channel ANA0 and read its value
GPIO_Analog.SelectChannel(ANA0);
adcValue0 = GPIO_Analog.ReadChannel();
// Select channel ANA1 and read its value
GPIO_Analog.SelectChannel(ANA1);
adcValue1 = GPIO_Analog.ReadChannel();
// Process the values (e.g., display, log, or use for control logic)
__delay_ms(500); // Delay between readings
}
return 0;
}