STM32学习笔记——GPIO之从库函数到寄存器

        例子为单片机的“Hello World”级的 流水灯实验——虽然只有一个,其中并不是将完整的代码给出,只是给出关键部分来说明“如何调用ST公司的的库来完成对硬件的控制,以及对库文件代码进行跟踪和分析至 寄存器级”。所以从第一段代码往下看就可以了,要用到的函数和变量大部分会说明,至于寄存器级的,那就只能翻手册了。
GPIO (General Purpose Input/Output) - 通用输入/输出
 main.c :此函数为主函数,控制LED,亮1s,灭1s
1
2
3
4
5
6
7
8
9
10
11
12
int main( void)
{
     // LED初始化 
     LED_Configuration();
     while( 1)
    {
         GPIO_SetBits( GPIOB, GPIO_Pin_5);      //置为1  
        Systick_DelayMs( 1000);               //延时1s,自己实现的,暂不说明  
         GPIO_ResetBits( GPIOB, GPIO_Pin_5);    //置为0  
        Systick_DelayMs( 1000);               //延时1s  
    }
}


 stm32f10x_gpio.c GPIO_SetBits和GPIO_ResetBits
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
  * @brief  Sets the selected data port bits.
  * @param  GPIOx: where x can be (A..G) to select the GPIO peripheral.
  * @param  GPIO_Pin: specifies the port bits to be written.
  *   This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  * @retval None
  */

void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
   /* Check the parameters */
  assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  assert_param(IS_GPIO_PIN(GPIO_Pin));
  
  GPIOx->BSRR = GPIO_Pin;
}

/**
  * @brief  Clears the selected data port bits.
  * @param  GPIOx: where x can be (A..G) to select the GPIO peripheral.
  * @param  GPIO_Pin: specifies the port bits to be written.
  *   This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  * @retval None
  */

void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
   /* Check the parameters */
  assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  assert_param(IS_GPIO_PIN(GPIO_Pin));
  
  GPIOx->BRR = GPIO_Pin;
}
 
 led.c  需要用到以下库文件:stm32f10x_rcc.c,stm32f10x_gpio.c-->STM32的I/O口初始化函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void LED_Configuration( void)
{
     /*设置PB.5为输出模式,用于LED*/
     //定义一个GPIO数据结构,存放设置的参数
     GPIO_InitTypeDef  GPIO_InitStructure;
     //要使用一个I/O口时,需要先打开相应I/O口所在口的时钟,如使能PB端口时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
     //先设置要配置的引脚,LED0-->PB.5 端口配置
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
     //配置为推挽输出模式
    GPIO_InitStructure.GPIO_Mode = GPIO_
  • 22
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值