STM32: GPIO operations (HAL library)

       The steps are:

1) Enable the IO port clock and call the function __HAL_RCC_GPIOX_CLK_ENABLE().

2) Initialize IO parameters. Call function HAL_GPIO_Init();

3) Operate IO input and output.

       For the configuration of peripheral multiplexing functions, except for ADC and DAC, which configure IO as analog channels, all other peripheral functions must be configured in multiplexing function mode. This configuration is configured in the GPIOx_MODER register corresponding to the IO port.

       Library Functions:

1) Initialize GPIO:

     GPIO_InitTypeDef GPIO_Init;

     GPIO_Initure.Pin=GPIO_PIN_9;          //PA9

     GPIO_Initure.Mode=GPIO_MODE_AF_PP; //Multiplex push-pull output

     GPIO_Initure.Pull=GPIO_PULLUP; //Pull-up

     ​ ​ GPIO_Initure.Speed=GPIO_SPEED_FREQ_HIGH;//High speed

     GPIO_Initure.Alternate=GPIO_AF7_USART1;//Connect AF7 to multiplex as the sending pin of serial port 1

      HAL_GPIO_Init(GPIOA,&GPIO_Initure); //Initialize PA9

2) HAL_GPIO_WritePin(GPIOB,GPIO_PIN_5,GPIO_PIN_SET); //GPIOB.5 output high

      HAL_GPIO_WritePin(GPIOB,GPIO_PIN_5, GPIO_PIN_RESET); //GPIOB.5 output low

3) HAL_GPIO_ReadPin(GPIOF, GPIO_PIN_5);//Read the input level of PF5

     The return value of this function is the level status of the IO port.

                                     

Guess you like

Origin blog.csdn.net/chn_zx/article/details/131686916