STM32按键输入,上拉还是下拉
前言
书接上回,咱们在之前提到,要想配置GPIO的工作模式需要配置其对应端口的CRL或者CRH寄存器,但是对于输入而言有一种工作模式叫做上拉/下拉输入
,那它到底是上拉还是下拉呢?本文将会继续讨论GPIO的输出配置问题,分析一下GPIO_Init()
这个函数的内容。
寄存器
对于stm32F103系列的单片机,GPIO口的相关寄存器并不止之前提到的那两个,之前提到的高位寄存器CRH
和低位寄存器CRL
有一个统一的名字叫做配置寄存器
。
每个GPIO端口有两个32位配置寄存器(GPIOx_CRL,GPIOx_CRH)
,两个32位数据寄存器(GPIOx_IDR和GPIOx_ODR)
,一个32位置位/复位寄存器(GPIOx_BSRR)
,一个16位复位寄存器(GPIOx_BRR)
和一个32位锁定寄存器(GPIOx_LCKR)
。
GPIO_Init()
GPIO_Init()
这个函数大家经常会使用,但是你知道它究竟在干什么事情么?
它的内容如下(内容来自stm32f10x_gpio.c这个文件):
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
{
uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;
uint32_t tmpreg = 0x00, pinmask = 0x00;
/* Check the parameters */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
/*---------------------------- GPIO Mode Configuration -----------------------*/
currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F);
if ((((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00)
{
/* Check the parameters */
assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
/* Output mode */
currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed;
}
/*---------------------------- GPIO CRL Configuration ------------------------*/
/* Configure the eight low port pins */
if (((uint32_t)GPIO_InitStruct->GPIO_Pin & ((uint32_t)0x00FF)) != 0x00)
{
tmpreg = GPIOx->CRL;
for (pinpos = 0x00; pinpos < 0x08; pinpos++)
{
pos = ((uint32_t)0x01) << pinpos;
/* Get the port pins position */
currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
if (currentpin == pos)
{
pos = pinpos << 2;
/* Clear the corresponding low control register bits */
pinmask = ((uint32_t)0x0F) << pos;
tmpreg &= ~pinmask;
/* Write the mode configuration in the corresponding bits */
tmpreg |= (currentmode << pos);
/* Reset the corresponding ODR bit */
if (GPIO_InitStruct-&