PB6/PB7是I2C1 的SCL和SDA端,如果作为普通的I/O口,就一般的配置就可以。GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; //设置速率
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//设置你要的模式
GPIO_Init(GPIOB, &GPIO_InitStructure);
我不得不给你看看GPIO的结构图,如下:
看到没有,如果你把IO配置为输入的话,输出回路的那个开关就断开了,输出寄存器的值不会影响到输入状态的。注意看,输入回路有上拉开关和下拉开关,而这个上下拉开关就是由输出寄存器来控制的。你说你配置的高低电平能生效,如果你的硬件电路没问题,而你又是配置的输入的话,只有一种情况,那就是你把IO配置为上下拉输入了。比如像下面的代码:
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
或者:
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
在GPIO_Init这个函数中,会判断你配置的GPIO_Mode,如果是GPIO_Mode_IPU
或GPIO_Mode_IPD的话,它会有下面的代码来设置输出寄存器为高或为低:
/* Reset the corresponding ODR bit */
if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
{
GPIOx->BRR = (((uint32_t)0x01) << pinpos);
}
else
{
/* Set the corresponding ODR bit */
if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
{
GPIOx->BSRR = (((uint32_t)0x01) << pinpos);
}
}
所以,你配置为浮空输入就好了,像下面这样:
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);