Skip to main content
Ala
Senior
March 10, 2020
Question

HAL_GPIO_ReadPin() reads float values

  • March 10, 2020
  • 9 replies
  • 2243 views

..

This topic has been closed for replies.

9 replies

Ekim.1
Associate III
March 10, 2020

This is because the return value of the HAL_GPIO_ReadPin() function is GPIO_PinState; the GPIO_PinState contains only the RESET(0) or SET(1) value.

typedef enum

{

  GPIO_PIN_RESET = 0U,

  GPIO_PIN_SET

} GPIO_PinState;

Ala
AlaAuthor
Senior
March 10, 2020

Dear Ekim.1

I tried GPIO_PinState, but still the value changes from SET to RESET and vice-versa...

do you think I should use a different function for reading PS Status?

Ekim.1
Associate III
March 10, 2020

If you want to read the sensor value of an optical sensor (e.g., a PS pin), you will need to use ADC.

Ozone
Principal
March 10, 2020

The STM32 GPIO peripheral has a IDR register that contains the input values.

Not sure how to access it with Cube functionality, never used that.

> If you want to read the sensor value of an optical sensor (e.g., a PS pin), you will need to use ADC.

Using a Schmitt trigger is a legit option.

KnarfB
Super User
March 10, 2020

You should measure the voltage levels when the the sensor is covered/uncovered. There are application notes with detailes about the GPIO input voltage conditions. Some STM32 have comparator inputs with adjustable voltage levels.

Ala
AlaAuthor
Senior
March 10, 2020

you are right dear KnarfB

KnarfB
Super User
March 10, 2020

> comparator input

Some chip families have a COMP peripheral which can compare input voltage to some reference value. Its easier than ADC where you need software for evaluating the measurments. What MCU are you using?

> BTW, can I use GPIO_Analog

GPIO_Analog is meant for two purposes:

  • connect an internal ADC peripheral for analog voltage measurement.
  • do not connect to anything internally (saves some power compared to digital input)

it cannot be used for reading digital input value.