cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_GPIO_ReadPin() reads float values

Ala
Senior
 
9 REPLIES 9
Ekim.1
Associate II

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;

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 II

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

Ozone
Lead

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.

can I use GPIO_Analog in CubeMX?

KnarfB
Principal III

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.

you are right dear KnarfB

KnarfB
Principal III

> 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.

> it cannot be used for reading digital input value.

In other words, GPIO functionality and analog functionality for a pin are mutually exclusive.

And the schmitt trigger configuration is ok for the purpose with a well-defined situation (covered/non-covered threshold). No need for analog input.