STM32F103C8T6 上拉输入模式配置 - PE2 引脚示例

本文将介绍如何在 STM32F103C8T6 开发板上配置 PE2 引脚为上拉输入模式,并提供相应的代码示例。

1. 开启 PE 口时钟

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);

2. 设置 PE2 为上拉输入模式

GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOE, &GPIO_InitStructure);

完整代码

#include "stm32f10x.h"

int main(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);

    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(GPIOE, &GPIO_InitStructure);

    while (1)
    {
        if (GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_2) == Bit_SET)
        {
            // PE2 is high
        }
        else
        {
            // PE2 is low
        }
    }
}

以上代码将 PE2 设置为上拉输入模式,并在主循环中读取其状态。您可以根据需要修改代码以实现不同的功能。

相关资源:

  • STM32F103C8T6 参考手册
  • STM32CubeMX 工具
  • STM32F10x 库函数参考手册

原文地址: https://www.cveoy.top/t/topic/nl44 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录