stm32 GPIO配置以及什么时候用 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

本文详细介绍了配置GPIO用于控制LED的基本步骤,包括初始化结构体、使能相应时钟及设置GPIO模式等,并解释了当需要使用GPIO作为外部中断触发信号时如何配置。
AI助手已提取文章相关产品:

问题一:配置GPIO的步骤

 

现在做一个最简单的GPIO控制LED的 GPIO初始化

 

(1)初始化结构体

        GPIO_InitTypeDef GPIO_InitStructure;

 

(2)使能相应的时钟(程序最初应该有#define RCC_GPIO_LED  GPIOB 或其他组端口)
        RCC_APB2PeriphClockCmd(RCC_GPIO_LED  , ENABLE);  

 

(3)对GPIO结构体初始化。

        GPIO结构体:

      typedef struct
     {
       uint16_t GPIO_Pin;    //选择管脚,是你想用到的管脚       

       GPIOSpeed_TypeDef GPIO_Speed;  //选择速度 可选2M  10M  50M

       GPIOMode_TypeDef GPIO_Mode;  //输入输出的8种模式,这要根据外电路和作用选择 

     }GPIO_InitTypeDef;


      /* LEDs pins configuration */
         GPIO_InitStructure.GPIO_Pin = GPIO_LED_ALL;
         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  //LED 一般就选推挽输出了
         GPIO_Init(GPIO_LED, &GPIO_InitStructure);

 

 

 

问题二: 什么时候用 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

 

        

            GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IPU;IPU是指IO口的工作模式是带上拉输入,这个和具体的电路有关,总之如果选择某个IO口作为外部中断的触发信号,就必须配置这个IO口为输入模式,不然无法触发中断

您可能感兴趣的与本文相关内容

void usart_init(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE); // PA9 - USART1_TX_ GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); // PA10 - USART1_RX GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOA, &GPIO_InitStructure); USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_Init(USART1, &USART_InitStructure); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3); NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_Init(&NVIC_InitStructure); USART1->SR; USART1->DR; USART_ClearITPendingBit(USART1, USART_IT_RXNE); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); USART_Cmd(USART1, ENABLE); } void Usart_Init(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); // TX-PA2 GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStructure); // RX-PA3 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStructure); USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate=9600; USART_InitStructure.USART_HardwareFlowControl= USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; USART_InitStructure.USART_Parity=USART_Parity_No; USART_InitStructure.USART_StopBits=USART_StopBits_1; USART_InitStructure.USART_WordLength=USART_WordLength_8b; USART_Init(USART2,&USART_InitStructure); USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1; NVIC_InitStructure.NVIC_IRQChannelSubPriority=1; NVIC_Init(&NVIC_InitStructure); USART_Cmd(USART2,ENABLE); }
07-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值