cancel
Showing results for 
Search instead for 
Did you mean: 

How to switch GPIO PORT's individual pin into analog input mode without disabling whole GPIO port before going into Stop mode, how to do it?

Shilpashree Madhu
Associate III
 
1 ACCEPTED SOLUTION

Accepted Solutions

For example, to set PA5 to analog mode:

GPIOA_MODER |= (3 << (5 * 2));

where 3 is 0b11 for Analog Mode as given by GPIOx_MODER description in the GPIO chapter of RM; 5 is for PA5; 2 is because there are 2 bits per pin in GPIOx_MODER.

JW

View solution in original post

8 REPLIES 8
TDK
Guru

Please include your chip number in posts.

Set the appropriate bits within GPIOx->MODER to analog mode.

If you feel a post has answered your question, please click "Accept as Solution".

The chip I'm using is STM32L4R5ZI-P.

My question is what if I want to switch only a particular bit ​to Analog mode without switching whole GPIO PORT into Analog mode before entering to Stop2mode..

> My question is what if I want to switch only a particular bit ​to Analog mode without switching whole GPIO PORT into Analog mode before entering to Stop2mode..

Yes, that is the question I answered.

Set the bits associated with that pin to analog mode and leave the bits associated with other pins unmodified.

You can also use HAL_GPIO_Init to do this.

If you feel a post has answered your question, please click "Accept as Solution".

Thanks for your response @TDK​ 

Bit set is quite confusing,

Can you please help me with that?​

For example, to set PA5 to analog mode:

GPIOA_MODER |= (3 << (5 * 2));

where 3 is 0b11 for Analog Mode as given by GPIOx_MODER description in the GPIO chapter of RM; 5 is for PA5; 2 is because there are 2 bits per pin in GPIOx_MODER.

JW

TDK
Guru

If you're not comfortable with register-level manipulation, I would suggest sticking with HAL.

To change PA5 to analog mode:

    GPIO_InitTypeDef GPIO_InitStruct = {0};
    GPIO_InitStruct.Pin = GPIO_PIN_5;
    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

If you feel a post has answered your question, please click "Accept as Solution".

Thank you soo much @TDK​ (ST Employee)​ 

Your explanation was very informative.

Much appreciated for the explanation, Thank you very much@waclawek.jan