Reading from two different GPIO pins involves setting up GPIO in digital input mode and then reading their state through your main loop or function call, depending on when you want to read those states. In the context of STM32 microcontrollers using HAL library functions in STM32CubeIDE, here's how you can approach it:
Step 1: Configure GPIO Pins as Input
First, you need to initialize two GPIO pins to work as inputs. You do this in your GPIO initialization function, where you specify each pin number and set its mode to input. Usually, this configuration is done in the MX_GPIO_Init(void) function.
Step 2: Read GPIO Pin State
After initializing the GPIO pins, you can read their state (HIGH or LOW) using HAL_GPIO_ReadPin() function at any point in your code where you need to check the GPIO state.
Here is a more specific example:
1. GPIO Initialization:
Assuming you're using GPIO pins PC13 and PB12 (just as examples, adapt to your actual needs), the initialization might look something like this:
#include "stm32f0xx_hal.h"
// Initialization function for GPIO
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
// Enable clock for the GPIO Ports
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
// GPIO Pin Configuration for PC13
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL; // or GPIO_PULLUP/DOWN based on your needs
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
// GPIO Pin Configuration for PB12
GPIO_InitStruct.Pin = GPIO_PIN_12;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
// Repeat the above structure for any additional GPIO pins you need
}
2. Reading GPIO Pin State:
You can now read the state of these pins anywhere in your main program loop or in a function, for example:
int main(void)
{
HAL_Init();
// Other initializations (Clock, ADC, etc.)
MX_GPIO_Init();
while (1)
{
// Read state of GPIOC Pin 13
uint8_t state_PC13 = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13);
// Read state of GPIOB Pin 12
uint8_t state_PB12 = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_12);
// Use states as needed
if(state_PC13 == GPIO_PIN_SET) {
// PC13 is HIGH
} else {
// PC13 is LOW
}
if(state_PB12 == GPIO_PIN_SET) {
// PB12 is HIGH
} else {
// PB12 is LOW
}
// Your application logic here
HAL_Delay(100); // Just an example delay
}
}
This example demonstrates a basic setup for reading two GPIO pins. You will need to adapt GPIO port and pin numbers to fit your specific STM32 development board and context. Also, consider debouncing the input if these pins are connected to mechanical switches or buttons.
Remember, the input pull mode (Pull-up/Pull-down/No Pull) in the GPIO_InitTypeDef should be configured considering your circuit. For instance, if you have an external pull-up resistor on a button, you can set .Pull = GPIO_NOPULL;, but if you rely on internal pull-up/down resistors, you should configure it accordingly.