历史上的今天
今天是:2024年09月25日(星期三)
2019年09月25日 | stm32 gpio口的库函数配置
2019-09-25 来源:eefocus
库函数的配置方法
1.例程
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
2.GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;是一个结构体变量的声明。
结构体是一种集合,它里面包含了多个变量或数组,它们的类型可以相同,也可以不同,每个这样的变量或数组都称为结构体的成员(Member)。
定义一般形式如下:
struct 结构类型名
{
数据类型 成员名 1;
数据类型 成员名 2;
…
数据类型 成员名 n;
};
结构变量声明的一般形式如下:
struct 结构类型名称 结构变量名; (表明这个变量名的结构类型是上面这个类型。)
也可以在定义结构体的同时定义结构体变量
struct 结构类型名
{
数据类型 成员名 1;
数据类型 成员名 2;
…
数据类型 成员名 n;
}stu1,stu2;
跳转到GPIO_InitTypeDef 的定义上,发现
typedef struct
{
uint16_t GPIO_Pin; /*!< Specifies the GPIO pins to be configured.
This parameter can be any value of @ref GPIO_pins_define */
GPIOSpeed_TypeDef GPIO_Speed; /*!< Specifies the speed for the selected pins.
This parameter can be a value of @ref GPIOSpeed_TypeDef */
GPIOMode_TypeDef GPIO_Mode; /*!< Specifies the operating mode for the selected pins.
This parameter can be a value of @ref GPIOMode_TypeDef */
}GPIO_InitTypeDef;
这个结构体定义了GPIO_Pin,GPIO_Speed和 GPIO_Speed( GPIO_Speed和GPIO_Speed 也是结构体)
在GPIO_InitTypeDef GPIO_InitStructure;
这句话的理解是我们定义一个结构体GPIO_InitStructure,他的成员有GPIO_Pin,GPIO_Speed和 GPIO_Speed。
GPIO_InitStructure成员的赋值;
结构体成员的引用有
变量名.成员名
例:GPIO_InitStructure.GPIO_Mode
变量名->成员名
例:GPIO_InitStructure->GPIO_Mode
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
它的意思是将GPIO_Mode_Out_PP(跳转到GPIO_Mode_Out_PP发现GPIO_Mode_Out_PP=0x10)赋值给GPIO_InitStructure.GPIO_Mode;将GPIO_Pin_5赋值给GPIO_InitStructure.GPIO_Pin;将GPIO_Speed_50MHz赋值给GPIO_InitStructure.GPIO_Speed。
GPIO_Init(GPIOB,&GPIO_InitStructure);
这句话的意思是按照GPIO_Init()这个函数的规则来初始化GPIO口。跳转到GPIO_Init这个函数.这里面写了是如何对寄存器进行操作,然后设置各个GPIO口。
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->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);
}
}
}
}
GPIOx->CRL = tmpreg;
}
/*---------------------------- GPIO CRH Configuration ------------------------*/
/* Configure the eight high port pins */
if (GPIO_InitStruct->GPIO_Pin > 0x00FF)
{
tmpreg = GPIOx->CRH;
for (pinpos = 0x00; pinpos < 0x08; pinpos++)
{
pos = (((uint32_t)0x01) << (pinpos + 0x08));
/* Get the port pins position */
currentpin = ((GPIO_InitStruct->GPIO_Pin) & pos);
if (currentpin == pos)
{
pos = pinpos << 2;
/* Clear the corresponding high 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->GPIO_Mode == GPIO_Mode_IPD)
{
GPIOx->BRR = (((uint32_t)0x01) << (pinpos + 0x08));
}
/* Set the corresponding ODR bit */
if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
{
GPIOx->BSRR = (((uint32_t)0x01) << (pinpos + 0x08));
}
}
}
GPIOx->CRH = tmpreg;
}
}
/**
* @brief Fills each GPIO_InitStruct member with its default value.
* @param GPIO_InitStruct : pointer to a GPIO_InitTypeDef structure which will
* be initialized.
* @retval None
*/
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));的作用就是检测传递给IS_GPIO_ALL_PERIPH函数的参数是否是有效的参数。IS_GPIO_ALL_PERIPH函数的定义是:
#define IS_GPIO_ALL_PERIPH(PERIPH) (((PERIPH) == GPIOA) ||
((PERIPH) == GPIOB) ||
((PERIPH) == GPIOC) ||
((PERIPH) == GPIOD) ||
((PERIPH) == GPIOE) ||
((PERIPH) == GPIOF) ||
((PERIPH) == GPIOG))
这表明IS_GPIO_ALL_PERIPH的函数只能是GPIOABCDEFG的一个。如果不是其中的一个,函数无效。
史海拾趣
|
对于像一个电阻,常有103KΩ这样的写法用来表示电阻的阻值.有一些电子电路书中介绍说103K=10 x 103Ω,即104Ω也有的书上介绍说是1.0 x 103Ω,即103Ω.我也觉得应该是后者,我认为前者违反了科学计数法的规则.您觉得哪个对呢?说说您的理由. … 查看全部问答> |
|
我搭建了一个最简单的正交LC oscillator. (quadrature). 要求phase noise -120dbc/hz.我的只能调到-111dbc/hz。求助大家如何设计才能达到要求?一般什么东西导致phase noise?如何才能减小phase noise? 谢谢。… 查看全部问答> |
|
本帖最后由 paulhyde 于 2014-9-15 09:18 编辑 电路的功能 本电路适用于电阻电桥电路(应变仪、半导体压力传感器等),其电压放大倍数取决于外接电阻RO,当RO=∞时,A=1,RO=0时可获得201倍的放大倍数。即使电桥电路失去平衡,如从零端注入电 ...… 查看全部问答> |
|
【FPGA问题讨论】用XILINX自带的XST做综合遇到问题 之前是用synplify的,现在改用XST,但是发现UCF文件中的一些语句不知道怎么写,比如用synplify是可以这样写,用XST呢,该怎么改? INST genblk0.FC_GTX_[0].FC2_DUAL LOC = GTX_DUAL_X1Y11;其中FC_GTX是用generate和for产生的blocks。有没有大虾知 ...… 查看全部问答> |
|
请问各位大侠,什么问题会造成PC机端的VC串口通讯程序死掉了。 这个程序是用VC编的,同时和多个单片机通讯,波特率是57600。 出现的问题是VC程序无规律地死掉,死后用任务管理器都无法关闭。 详细说明请看我在VC板块发的帖子:http: ...… 查看全部问答> |
|
编译内核NK时报如下错误 BUILD: [01:0000000131:ERRORE] NMAKE : U1073: don\'t know how to make \'D:\\WINCE500\\public\\COMMON\\OAK\\lib\\x86\\retail\\ehcd.def\' BUILD: [01:0000000133:ERRORE] NMAKE.EXE -i -c ...… 查看全部问答> |




